xref: /linux/sound/soc/ux500/mop500.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson SA 2012
4  *
5  * Author: Ola Lilja (ola.o.lilja@stericsson.com)
6  *         for ST-Ericsson.
7  */
8 
9 #include <asm/mach-types.h>
10 
11 #include <linux/module.h>
12 #include <linux/io.h>
13 #include <linux/spi/spi.h>
14 #include <linux/of.h>
15 
16 #include <sound/soc.h>
17 #include <sound/initval.h>
18 
19 #include "ux500_pcm.h"
20 #include "ux500_msp_dai.h"
21 
22 #include "mop500_ab8500.h"
23 
24 /* Define the whole MOP500 soundcard, linking platform to the codec-drivers  */
25 SND_SOC_DAILINK_DEFS(link1,
26 	DAILINK_COMP_ARRAY(COMP_CPU("ux500-msp-i2s.1")),
27 	DAILINK_COMP_ARRAY(COMP_CODEC("ab8500-codec.0", "ab8500-codec-dai.0")),
28 	DAILINK_COMP_ARRAY(COMP_PLATFORM("ux500-msp-i2s.1")));
29 
30 SND_SOC_DAILINK_DEFS(link2,
31 	DAILINK_COMP_ARRAY(COMP_CPU("ux500-msp-i2s.3")),
32 	DAILINK_COMP_ARRAY(COMP_CODEC("ab8500-codec.0", "ab8500-codec-dai.1")),
33 	DAILINK_COMP_ARRAY(COMP_PLATFORM("ux500-msp-i2s.3")));
34 
35 static struct snd_soc_dai_link mop500_dai_links[] = {
36 	{
37 		.name = "ab8500_0",
38 		.stream_name = "ab8500_0",
39 		.init = mop500_ab8500_machine_init,
40 		.ops = mop500_ab8500_ops,
41 		SND_SOC_DAILINK_REG(link1),
42 	},
43 	{
44 		.name = "ab8500_1",
45 		.stream_name = "ab8500_1",
46 		.init = NULL,
47 		.ops = mop500_ab8500_ops,
48 		SND_SOC_DAILINK_REG(link2),
49 	},
50 };
51 
52 static struct snd_soc_card mop500_card = {
53 	.name = "MOP500-card",
54 	.owner = THIS_MODULE,
55 	.probe = NULL,
56 	.dai_link = mop500_dai_links,
57 	.num_links = ARRAY_SIZE(mop500_dai_links),
58 };
59 
60 static void mop500_of_node_put(void)
61 {
62 	int i;
63 
64 	for (i = 0; i < 2; i++)
65 		of_node_put(mop500_dai_links[i].cpus->of_node);
66 
67 	/* Both links use the same codec, which is refcounted only once */
68 	of_node_put(mop500_dai_links[0].codecs->of_node);
69 }
70 
71 static int mop500_of_probe(struct platform_device *pdev,
72 			   struct device_node *np)
73 {
74 	struct device_node *codec_np, *msp_np[2];
75 	int i;
76 
77 	msp_np[0] = of_parse_phandle(np, "stericsson,cpu-dai", 0);
78 	msp_np[1] = of_parse_phandle(np, "stericsson,cpu-dai", 1);
79 	codec_np  = of_parse_phandle(np, "stericsson,audio-codec", 0);
80 
81 	if (!(msp_np[0] && msp_np[1] && codec_np)) {
82 		dev_err(&pdev->dev, "Phandle missing or invalid\n");
83 		for (i = 0; i < 2; i++)
84 			of_node_put(msp_np[i]);
85 		of_node_put(codec_np);
86 		return -EINVAL;
87 	}
88 
89 	for (i = 0; i < 2; i++) {
90 		mop500_dai_links[i].cpus->of_node = msp_np[i];
91 		mop500_dai_links[i].cpus->dai_name = NULL;
92 		mop500_dai_links[i].platforms->of_node = msp_np[i];
93 		mop500_dai_links[i].platforms->name = NULL;
94 		mop500_dai_links[i].codecs->of_node = codec_np;
95 		mop500_dai_links[i].codecs->name = NULL;
96 	}
97 
98 	snd_soc_of_parse_card_name(&mop500_card, "stericsson,card-name");
99 
100 	return 0;
101 }
102 
103 static int mop500_probe(struct platform_device *pdev)
104 {
105 	struct device_node *np = pdev->dev.of_node;
106 	int ret;
107 
108 	dev_dbg(&pdev->dev, "%s: Enter.\n", __func__);
109 
110 	mop500_card.dev = &pdev->dev;
111 
112 	if (np) {
113 		ret = mop500_of_probe(pdev, np);
114 		if (ret)
115 			return ret;
116 	}
117 
118 	dev_dbg(&pdev->dev, "%s: Card %s: Set platform drvdata.\n",
119 		__func__, mop500_card.name);
120 
121 	snd_soc_card_set_drvdata(&mop500_card, NULL);
122 
123 	dev_dbg(&pdev->dev, "%s: Card %s: num_links = %d\n",
124 		__func__, mop500_card.name, mop500_card.num_links);
125 	dev_dbg(&pdev->dev, "%s: Card %s: DAI-link 0: name = %s\n",
126 		__func__, mop500_card.name, mop500_card.dai_link[0].name);
127 	dev_dbg(&pdev->dev, "%s: Card %s: DAI-link 0: stream_name = %s\n",
128 		__func__, mop500_card.name,
129 		mop500_card.dai_link[0].stream_name);
130 
131 	ret = snd_soc_register_card(&mop500_card);
132 	if (ret)
133 		dev_err(&pdev->dev,
134 			"Error: snd_soc_register_card failed (%d)!\n", ret);
135 
136 	return ret;
137 }
138 
139 static int mop500_remove(struct platform_device *pdev)
140 {
141 	struct snd_soc_card *card = platform_get_drvdata(pdev);
142 
143 	pr_debug("%s: Enter.\n", __func__);
144 
145 	snd_soc_unregister_card(card);
146 	mop500_ab8500_remove(card);
147 	mop500_of_node_put();
148 
149 	return 0;
150 }
151 
152 static const struct of_device_id snd_soc_mop500_match[] = {
153 	{ .compatible = "stericsson,snd-soc-mop500", },
154 	{},
155 };
156 MODULE_DEVICE_TABLE(of, snd_soc_mop500_match);
157 
158 static struct platform_driver snd_soc_mop500_driver = {
159 	.driver = {
160 		.name = "snd-soc-mop500",
161 		.of_match_table = snd_soc_mop500_match,
162 	},
163 	.probe = mop500_probe,
164 	.remove = mop500_remove,
165 };
166 
167 module_platform_driver(snd_soc_mop500_driver);
168 
169 MODULE_LICENSE("GPL v2");
170 MODULE_DESCRIPTION("ASoC MOP500 board driver");
171 MODULE_AUTHOR("Ola Lilja");
172