1// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (C) 2013 Freescale Semiconductor, Inc.
4
5#include <linux/module.h>
6#include <linux/of_platform.h>
7#include <sound/soc.h>
8
9struct imx_spdif_data {
10	struct snd_soc_dai_link dai;
11	struct snd_soc_card card;
12};
13
14static int imx_spdif_audio_probe(struct platform_device *pdev)
15{
16	struct device_node *spdif_np, *np = pdev->dev.of_node;
17	struct imx_spdif_data *data;
18	struct snd_soc_dai_link_component *comp;
19	int ret = 0;
20
21	spdif_np = of_parse_phandle(np, "spdif-controller", 0);
22	if (!spdif_np) {
23		dev_err(&pdev->dev, "failed to find spdif-controller\n");
24		ret = -EINVAL;
25		goto end;
26	}
27
28	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
29	comp = devm_kzalloc(&pdev->dev, 3 * sizeof(*comp), GFP_KERNEL);
30	if (!data || !comp) {
31		ret = -ENOMEM;
32		goto end;
33	}
34
35	data->dai.cpus		= &comp[0];
36	data->dai.codecs	= &comp[1];
37	data->dai.platforms	= &comp[2];
38
39	data->dai.num_cpus	= 1;
40	data->dai.num_codecs	= 1;
41	data->dai.num_platforms	= 1;
42
43	data->dai.name = "S/PDIF PCM";
44	data->dai.stream_name = "S/PDIF PCM";
45	data->dai.codecs->dai_name = "snd-soc-dummy-dai";
46	data->dai.codecs->name = "snd-soc-dummy";
47	data->dai.cpus->of_node = spdif_np;
48	data->dai.platforms->of_node = spdif_np;
49	data->dai.playback_only = true;
50	data->dai.capture_only = true;
51
52	if (of_property_read_bool(np, "spdif-out"))
53		data->dai.capture_only = false;
54
55	if (of_property_read_bool(np, "spdif-in"))
56		data->dai.playback_only = false;
57
58	if (data->dai.playback_only && data->dai.capture_only) {
59		dev_err(&pdev->dev, "no enabled S/PDIF DAI link\n");
60		goto end;
61	}
62
63	data->card.dev = &pdev->dev;
64	data->card.dai_link = &data->dai;
65	data->card.num_links = 1;
66	data->card.owner = THIS_MODULE;
67
68	ret = snd_soc_of_parse_card_name(&data->card, "model");
69	if (ret)
70		goto end;
71
72	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
73	if (ret && ret != -EPROBE_DEFER)
74		dev_err(&pdev->dev, "snd_soc_register_card failed: %d\n", ret);
75
76end:
77	of_node_put(spdif_np);
78
79	return ret;
80}
81
82static const struct of_device_id imx_spdif_dt_ids[] = {
83	{ .compatible = "fsl,imx-audio-spdif", },
84	{ /* sentinel */ }
85};
86MODULE_DEVICE_TABLE(of, imx_spdif_dt_ids);
87
88static struct platform_driver imx_spdif_driver = {
89	.driver = {
90		.name = "imx-spdif",
91		.pm = &snd_soc_pm_ops,
92		.of_match_table = imx_spdif_dt_ids,
93	},
94	.probe = imx_spdif_audio_probe,
95};
96
97module_platform_driver(imx_spdif_driver);
98
99MODULE_AUTHOR("Freescale Semiconductor, Inc.");
100MODULE_DESCRIPTION("Freescale i.MX S/PDIF machine driver");
101MODULE_LICENSE("GPL v2");
102MODULE_ALIAS("platform:imx-spdif");
103