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