1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/fsl/guts.h>
22#include <linux/interrupt.h>
23#include <linux/of_address.h>
24#include <linux/of_device.h>
25#include <linux/slab.h>
26#include <sound/soc.h>
27
28#include "fsl_dma.h"
29#include "fsl_ssi.h"
30#include "fsl_utils.h"
31
32
33
34#define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
35#define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
36#define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
37
38#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
39#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
40
41#define CCSR_GUTS_DMUXCR_PAD 1
42#define CCSR_GUTS_DMUXCR_SSI 2
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts,
58 unsigned int co, unsigned int ch, unsigned int device)
59{
60 unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
61
62 clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
63}
64
65
66static phys_addr_t guts_phys;
67
68
69
70
71
72
73
74struct machine_data {
75 struct snd_soc_dai_link dai[2];
76 struct snd_soc_card card;
77 unsigned int dai_format;
78 unsigned int codec_clk_direction;
79 unsigned int cpu_clk_direction;
80 unsigned int clk_frequency;
81 unsigned int dma_id[2];
82 unsigned int dma_channel_id[2];
83 char platform_name[2][DAI_NAME_SIZE];
84};
85
86
87
88
89
90
91
92
93static int p1022_rdk_machine_probe(struct snd_soc_card *card)
94{
95 struct machine_data *mdata =
96 container_of(card, struct machine_data, card);
97 struct ccsr_guts __iomem *guts;
98
99 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
100 if (!guts) {
101 dev_err(card->dev, "could not map global utilities\n");
102 return -ENOMEM;
103 }
104
105
106 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
107 CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
108
109
110 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
111 CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
112
113
114 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
115 CCSR_GUTS_DMUXCR_SSI);
116
117 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
118 CCSR_GUTS_DMUXCR_SSI);
119
120 iounmap(guts);
121
122 return 0;
123}
124
125
126
127
128
129
130
131
132static int p1022_rdk_startup(struct snd_pcm_substream *substream)
133{
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct machine_data *mdata =
136 container_of(rtd->card, struct machine_data, card);
137 struct device *dev = rtd->card->dev;
138 int ret = 0;
139
140
141 ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
142 if (ret < 0) {
143 dev_err(dev, "could not set codec driver audio format (ret=%i)\n",
144 ret);
145 return ret;
146 }
147
148 ret = snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, mdata->clk_frequency,
149 mdata->clk_frequency);
150 if (ret < 0) {
151 dev_err(dev, "could not set codec PLL frequency (ret=%i)\n",
152 ret);
153 return ret;
154 }
155
156 return 0;
157}
158
159
160
161
162
163
164
165static int p1022_rdk_machine_remove(struct snd_soc_card *card)
166{
167 struct machine_data *mdata =
168 container_of(card, struct machine_data, card);
169 struct ccsr_guts __iomem *guts;
170
171 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
172 if (!guts) {
173 dev_err(card->dev, "could not map global utilities\n");
174 return -ENOMEM;
175 }
176
177
178 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
179 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
180 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
181 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
182
183 iounmap(guts);
184
185 return 0;
186}
187
188
189
190
191static struct snd_soc_ops p1022_rdk_ops = {
192 .startup = p1022_rdk_startup,
193};
194
195
196
197
198
199
200
201
202static int p1022_rdk_probe(struct platform_device *pdev)
203{
204 struct device *dev = pdev->dev.parent;
205
206 struct platform_device *ssi_pdev = to_platform_device(dev);
207 struct device_node *np = ssi_pdev->dev.of_node;
208 struct device_node *codec_np = NULL;
209 struct machine_data *mdata;
210 const u32 *iprop;
211 int ret;
212
213
214 codec_np = of_parse_phandle(np, "codec-handle", 0);
215 if (!codec_np) {
216 dev_err(dev, "could not find codec node\n");
217 return -EINVAL;
218 }
219
220 mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
221 if (!mdata) {
222 ret = -ENOMEM;
223 goto error_put;
224 }
225
226 mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
227 mdata->dai[0].ops = &p1022_rdk_ops;
228
229
230 mdata->dai[0].codec_of_node = codec_np;
231
232
233
234
235
236
237 memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
238
239
240 mdata->dai[0].codec_dai_name = "wm8960-hifi";
241 mdata->dai[1].codec_dai_name = mdata->dai[0].codec_dai_name;
242
243
244
245
246
247
248 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
249 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
250 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
251 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
252
253
254
255
256
257
258 iprop = of_get_property(codec_np, "clock-frequency", NULL);
259 if (!iprop || !*iprop) {
260 dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n");
261 ret = -EINVAL;
262 goto error;
263 }
264 mdata->clk_frequency = be32_to_cpup(iprop);
265
266 if (!mdata->clk_frequency) {
267 dev_err(&pdev->dev, "unknown clock frequency\n");
268 ret = -EINVAL;
269 goto error;
270 }
271
272
273 mdata->dai[0].platform_name = mdata->platform_name[0];
274 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
275 &mdata->dma_channel_id[0],
276 &mdata->dma_id[0]);
277 if (ret) {
278 dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n",
279 ret);
280 goto error;
281 }
282
283
284 mdata->dai[1].platform_name = mdata->platform_name[1];
285 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
286 &mdata->dma_channel_id[1],
287 &mdata->dma_id[1]);
288 if (ret) {
289 dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n",
290 ret);
291 goto error;
292 }
293
294
295 mdata->dai[0].stream_name = "playback";
296 mdata->dai[1].stream_name = "capture";
297 mdata->dai[0].name = mdata->dai[0].stream_name;
298 mdata->dai[1].name = mdata->dai[1].stream_name;
299
300 mdata->card.probe = p1022_rdk_machine_probe;
301 mdata->card.remove = p1022_rdk_machine_remove;
302 mdata->card.name = pdev->name;
303 mdata->card.owner = THIS_MODULE;
304 mdata->card.dev = &pdev->dev;
305 mdata->card.num_links = 2;
306 mdata->card.dai_link = mdata->dai;
307
308
309 ret = snd_soc_register_card(&mdata->card);
310 if (ret) {
311 dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret);
312 goto error;
313 }
314
315 return 0;
316
317error:
318 kfree(mdata);
319error_put:
320 of_node_put(codec_np);
321 return ret;
322}
323
324
325
326
327
328
329static int p1022_rdk_remove(struct platform_device *pdev)
330{
331 struct snd_soc_card *card = platform_get_drvdata(pdev);
332 struct machine_data *mdata =
333 container_of(card, struct machine_data, card);
334
335 snd_soc_unregister_card(card);
336 kfree(mdata);
337
338 return 0;
339}
340
341static struct platform_driver p1022_rdk_driver = {
342 .probe = p1022_rdk_probe,
343 .remove = p1022_rdk_remove,
344 .driver = {
345
346
347
348
349 .name = "snd-soc-p1022rdk",
350 },
351};
352
353
354
355
356
357
358static int __init p1022_rdk_init(void)
359{
360 struct device_node *guts_np;
361 struct resource res;
362
363
364 guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
365 if (of_address_to_resource(guts_np, 0, &res)) {
366 pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n");
367 of_node_put(guts_np);
368 return -EINVAL;
369 }
370 guts_phys = res.start;
371 of_node_put(guts_np);
372
373 return platform_driver_register(&p1022_rdk_driver);
374}
375
376
377
378
379
380
381static void __exit p1022_rdk_exit(void)
382{
383 platform_driver_unregister(&p1022_rdk_driver);
384}
385
386late_initcall(p1022_rdk_init);
387module_exit(p1022_rdk_exit);
388
389MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
390MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver");
391MODULE_LICENSE("GPL v2");
392