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
19struct imx_rpmsg {
20	struct snd_soc_dai_link dai;
21	struct snd_soc_card card;
22	unsigned long sysclk;
23	bool lpa;
24};
25
26static struct dev_pm_ops lpa_pm;
27
28static const struct snd_soc_dapm_widget imx_rpmsg_dapm_widgets[] = {
29	SND_SOC_DAPM_HP("Headphone Jack", NULL),
30	SND_SOC_DAPM_SPK("Ext Spk", NULL),
31	SND_SOC_DAPM_MIC("Mic Jack", NULL),
32	SND_SOC_DAPM_MIC("Main MIC", NULL),
33};
34
35static int imx_rpmsg_late_probe(struct snd_soc_card *card)
36{
37	struct imx_rpmsg *data = snd_soc_card_get_drvdata(card);
38	struct snd_soc_pcm_runtime *rtd = list_first_entry(&card->rtd_list,
39							   struct snd_soc_pcm_runtime, list);
40	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
41	struct device *dev = card->dev;
42	int ret;
43
44	if (data->lpa) {
45		struct snd_soc_component *codec_comp;
46		struct device_node *codec_np;
47		struct device_driver *codec_drv;
48		struct device *codec_dev = NULL;
49
50		codec_np = data->dai.codecs->of_node;
51		if (codec_np) {
52			struct platform_device *codec_pdev;
53			struct i2c_client *codec_i2c;
54
55			codec_i2c = of_find_i2c_device_by_node(codec_np);
56			if (codec_i2c)
57				codec_dev = &codec_i2c->dev;
58			if (!codec_dev) {
59				codec_pdev = of_find_device_by_node(codec_np);
60				if (codec_pdev)
61					codec_dev = &codec_pdev->dev;
62			}
63		}
64		if (codec_dev) {
65			codec_comp = snd_soc_lookup_component_nolocked(codec_dev, NULL);
66			if (codec_comp) {
67				int i, num_widgets;
68				const char *widgets;
69				struct snd_soc_dapm_context *dapm;
70
71				num_widgets = of_property_count_strings(data->card.dev->of_node,
72									"ignore-suspend-widgets");
73				for (i = 0; i < num_widgets; i++) {
74					of_property_read_string_index(data->card.dev->of_node,
75								      "ignore-suspend-widgets",
76								      i, &widgets);
77					dapm = snd_soc_component_get_dapm(codec_comp);
78					snd_soc_dapm_ignore_suspend(dapm, widgets);
79				}
80			}
81			codec_drv = codec_dev->driver;
82			if (codec_drv->pm) {
83				memcpy(&lpa_pm, codec_drv->pm, sizeof(lpa_pm));
84				lpa_pm.suspend = NULL;
85				lpa_pm.resume = NULL;
86				lpa_pm.freeze = NULL;
87				lpa_pm.thaw = NULL;
88				lpa_pm.poweroff = NULL;
89				lpa_pm.restore = NULL;
90				codec_drv->pm = &lpa_pm;
91			}
92			put_device(codec_dev);
93		}
94	}
95
96	if (!data->sysclk)
97		return 0;
98
99	ret = snd_soc_dai_set_sysclk(codec_dai, 0, data->sysclk, SND_SOC_CLOCK_IN);
100	if (ret && ret != -ENOTSUPP) {
101		dev_err(dev, "failed to set sysclk in %s\n", __func__);
102		return ret;
103	}
104
105	return 0;
106}
107
108static int imx_rpmsg_probe(struct platform_device *pdev)
109{
110	struct snd_soc_dai_link_component *dlc;
111	struct device *dev = pdev->dev.parent;
112	/* rpmsg_pdev is the platform device for the rpmsg node that probed us */
113	struct platform_device *rpmsg_pdev = to_platform_device(dev);
114	struct device_node *np = rpmsg_pdev->dev.of_node;
115	struct of_phandle_args args;
116	const char *platform_name;
117	struct imx_rpmsg *data;
118	int ret = 0;
119
120	dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
121	if (!dlc)
122		return -ENOMEM;
123
124	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
125	if (!data) {
126		ret = -ENOMEM;
127		goto fail;
128	}
129
130	ret = of_reserved_mem_device_init_by_idx(&pdev->dev, np, 0);
131	if (ret)
132		dev_warn(&pdev->dev, "no reserved DMA memory\n");
133
134	data->dai.cpus = &dlc[0];
135	data->dai.num_cpus = 1;
136	data->dai.platforms = &dlc[1];
137	data->dai.num_platforms = 1;
138	data->dai.codecs = &dlc[2];
139	data->dai.num_codecs = 1;
140
141	data->dai.name = "rpmsg hifi";
142	data->dai.stream_name = "rpmsg hifi";
143	data->dai.dai_fmt = SND_SOC_DAIFMT_I2S |
144			    SND_SOC_DAIFMT_NB_NF |
145			    SND_SOC_DAIFMT_CBC_CFC;
146
147	/*
148	 * i.MX rpmsg sound cards work on codec slave mode. MCLK will be
149	 * disabled by CPU DAI driver in hw_free(). Some codec requires MCLK
150	 * present at power up/down sequence. So need to set ignore_pmdown_time
151	 * to power down codec immediately before MCLK is turned off.
152	 */
153	data->dai.ignore_pmdown_time = 1;
154
155	/* Optional codec node */
156	ret = of_parse_phandle_with_fixed_args(np, "audio-codec", 0, 0, &args);
157	if (ret) {
158		*data->dai.codecs = snd_soc_dummy_dlc;
159	} else {
160		struct clk *clk;
161
162		ret = snd_soc_get_dlc(&args, data->dai.codecs);
163		if (ret) {
164			dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
165			goto fail;
166		}
167
168		clk = devm_get_clk_from_child(&pdev->dev, args.np, NULL);
169		if (!IS_ERR(clk))
170			data->sysclk = clk_get_rate(clk);
171	}
172
173	data->dai.cpus->dai_name = dev_name(&rpmsg_pdev->dev);
174	if (!of_property_read_string(np, "fsl,rpmsg-channel-name", &platform_name))
175		data->dai.platforms->name = platform_name;
176	else
177		data->dai.platforms->name = "rpmsg-audio-channel";
178	data->dai.playback_only = true;
179	data->dai.capture_only = true;
180	data->card.num_links = 1;
181	data->card.dai_link = &data->dai;
182
183	if (of_property_read_bool(np, "fsl,rpmsg-out"))
184		data->dai.capture_only = false;
185
186	if (of_property_read_bool(np, "fsl,rpmsg-in"))
187		data->dai.playback_only = false;
188
189	if (data->dai.playback_only && data->dai.capture_only) {
190		dev_err(&pdev->dev, "no enabled rpmsg DAI link\n");
191		ret = -EINVAL;
192		goto fail;
193	}
194
195	if (of_property_read_bool(np, "fsl,enable-lpa"))
196		data->lpa = true;
197
198	data->card.dev = &pdev->dev;
199	data->card.owner = THIS_MODULE;
200	data->card.dapm_widgets = imx_rpmsg_dapm_widgets;
201	data->card.num_dapm_widgets = ARRAY_SIZE(imx_rpmsg_dapm_widgets);
202	data->card.late_probe = imx_rpmsg_late_probe;
203	/*
204	 * Inoder to use common api to get card name and audio routing.
205	 * Use parent of_node for this device, revert it after finishing using
206	 */
207	data->card.dev->of_node = np;
208
209	ret = snd_soc_of_parse_card_name(&data->card, "model");
210	if (ret)
211		goto fail;
212
213	if (of_property_read_bool(np, "audio-routing")) {
214		ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
215		if (ret) {
216			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
217			goto fail;
218		}
219	}
220
221	platform_set_drvdata(pdev, &data->card);
222	snd_soc_card_set_drvdata(&data->card, data);
223	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
224	if (ret) {
225		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
226		goto fail;
227	}
228
229fail:
230	pdev->dev.of_node = NULL;
231	return ret;
232}
233
234static struct platform_driver imx_rpmsg_driver = {
235	.driver = {
236		.name = "imx-audio-rpmsg",
237		.pm = &snd_soc_pm_ops,
238	},
239	.probe = imx_rpmsg_probe,
240};
241module_platform_driver(imx_rpmsg_driver);
242
243MODULE_DESCRIPTION("Freescale SoC Audio RPMSG Machine Driver");
244MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
245MODULE_ALIAS("platform:imx-audio-rpmsg");
246MODULE_LICENSE("GPL v2");
247