ASoC: fsl: create fsl_utils to accommodate the common functions
[deliverable/linux.git] / sound / soc / fsl / p1022_ds.c
CommitLineData
27ef3744
TT
1/**
2 * Freescale P1022DS ALSA SoC Machine driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
6 * Copyright 2010 Freescale Semiconductor, Inc.
7 *
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
11 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/of_device.h>
16#include <linux/slab.h>
17#include <sound/soc.h>
18#include <asm/fsl_guts.h>
19
20#include "fsl_dma.h"
21#include "fsl_ssi.h"
60aae8da 22#include "fsl_utils.h"
27ef3744
TT
23
24/* P1022-specific PMUXCR and DMUXCR bit definitions */
25
26#define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
27#define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
28#define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
29
30#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
31#define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
32
33#define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
34#define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
35
36/*
37 * Set the DMACR register in the GUTS
38 *
39 * The DMACR register determines the source of initiated transfers for each
40 * channel on each DMA controller. Rather than have a bunch of repetitive
41 * macros for the bit patterns, we just have a function that calculates
42 * them.
43 *
44 * guts: Pointer to GUTS structure
45 * co: The DMA controller (0 or 1)
46 * ch: The channel on the DMA controller (0, 1, 2, or 3)
47 * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
48 */
49static inline void guts_set_dmuxcr(struct ccsr_guts_85xx __iomem *guts,
50 unsigned int co, unsigned int ch, unsigned int device)
51{
52 unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
53
54 clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
55}
56
57/* There's only one global utilities register */
58static phys_addr_t guts_phys;
59
27ef3744
TT
60/**
61 * machine_data: machine-specific ASoC device data
62 *
63 * This structure contains data for a single sound platform device on an
64 * P1022 DS. Some of the data is taken from the device tree.
65 */
66struct machine_data {
67 struct snd_soc_dai_link dai[2];
68 struct snd_soc_card card;
69 unsigned int dai_format;
70 unsigned int codec_clk_direction;
71 unsigned int cpu_clk_direction;
72 unsigned int clk_frequency;
73 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
74 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
75 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
76 char codec_name[DAI_NAME_SIZE];
77 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
78};
79
80/**
81 * p1022_ds_machine_probe: initialize the board
82 *
83 * This function is used to initialize the board-specific hardware.
84 *
85 * Here we program the DMACR and PMUXCR registers.
86 */
e7361ec4 87static int p1022_ds_machine_probe(struct snd_soc_card *card)
27ef3744 88{
27ef3744
TT
89 struct machine_data *mdata =
90 container_of(card, struct machine_data, card);
91 struct ccsr_guts_85xx __iomem *guts;
92
93 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_85xx));
94 if (!guts) {
95 dev_err(card->dev, "could not map global utilities\n");
96 return -ENOMEM;
97 }
98
99 /* Enable SSI Tx signal */
100 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
101 CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
102
103 /* Enable SSI Rx signal */
104 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
105 CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
106
107 /* Enable DMA Channel for SSI */
108 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
109 CCSR_GUTS_DMUXCR_SSI);
110
111 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
112 CCSR_GUTS_DMUXCR_SSI);
113
114 iounmap(guts);
115
116 return 0;
117}
118
119/**
120 * p1022_ds_startup: program the board with various hardware parameters
121 *
122 * This function takes board-specific information, like clock frequencies
123 * and serial data formats, and passes that information to the codec and
124 * transport drivers.
125 */
126static int p1022_ds_startup(struct snd_pcm_substream *substream)
127{
128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
129 struct machine_data *mdata =
130 container_of(rtd->card, struct machine_data, card);
131 struct device *dev = rtd->card->dev;
132 int ret = 0;
133
134 /* Tell the codec driver what the serial protocol is. */
135 ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
136 if (ret < 0) {
137 dev_err(dev, "could not set codec driver audio format\n");
138 return ret;
139 }
140
141 /*
142 * Tell the codec driver what the MCLK frequency is, and whether it's
143 * a slave or master.
144 */
145 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
146 mdata->codec_clk_direction);
147 if (ret < 0) {
148 dev_err(dev, "could not set codec driver clock params\n");
149 return ret;
150 }
151
152 return 0;
153}
154
155/**
156 * p1022_ds_machine_remove: Remove the sound device
157 *
158 * This function is called to remove the sound device for one SSI. We
159 * de-program the DMACR and PMUXCR register.
160 */
e7361ec4 161static int p1022_ds_machine_remove(struct snd_soc_card *card)
27ef3744 162{
27ef3744
TT
163 struct machine_data *mdata =
164 container_of(card, struct machine_data, card);
165 struct ccsr_guts_85xx __iomem *guts;
166
167 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_85xx));
168 if (!guts) {
169 dev_err(card->dev, "could not map global utilities\n");
170 return -ENOMEM;
171 }
172
173 /* Restore the signal routing */
174 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
175 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
176 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
177 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
178
179 iounmap(guts);
180
181 return 0;
182}
183
184/**
185 * p1022_ds_ops: ASoC machine driver operations
186 */
187static struct snd_soc_ops p1022_ds_ops = {
188 .startup = p1022_ds_startup,
189};
190
27ef3744
TT
191/**
192 * p1022_ds_probe: platform probe function for the machine driver
193 *
194 * Although this is a machine driver, the SSI node is the "master" node with
195 * respect to audio hardware connections. Therefore, we create a new ASoC
196 * device for each new SSI node that has a codec attached.
197 */
198static int p1022_ds_probe(struct platform_device *pdev)
199{
200 struct device *dev = pdev->dev.parent;
201 /* ssi_pdev is the platform device for the SSI node that probed us */
202 struct platform_device *ssi_pdev =
203 container_of(dev, struct platform_device, dev);
204 struct device_node *np = ssi_pdev->dev.of_node;
205 struct device_node *codec_np = NULL;
206 struct platform_device *sound_device = NULL;
207 struct machine_data *mdata;
208 int ret = -ENODEV;
209 const char *sprop;
210 const u32 *iprop;
211
212 /* Find the codec node for this SSI. */
213 codec_np = of_parse_phandle(np, "codec-handle", 0);
214 if (!codec_np) {
215 dev_err(dev, "could not find codec node\n");
216 return -EINVAL;
217 }
218
219 mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
880b8ffd
JL
220 if (!mdata) {
221 ret = -ENOMEM;
222 goto error_put;
223 }
27ef3744
TT
224
225 mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
226 mdata->dai[0].ops = &p1022_ds_ops;
227
228 /* Determine the codec name, it will be used as the codec DAI name */
60aae8da
SG
229 ret = fsl_asoc_get_codec_dev_name(codec_np, mdata->codec_name,
230 DAI_NAME_SIZE);
27ef3744
TT
231 if (ret) {
232 dev_err(&pdev->dev, "invalid codec node %s\n",
233 codec_np->full_name);
234 ret = -EINVAL;
235 goto error;
236 }
237 mdata->dai[0].codec_name = mdata->codec_name;
238
239 /* We register two DAIs per SSI, one for playback and the other for
240 * capture. We support codecs that have separate DAIs for both playback
241 * and capture.
242 */
243 memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
244
245 /* The DAI names from the codec (snd_soc_dai_driver.name) */
246 mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
247 mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
248
249 /* Get the device ID */
250 iprop = of_get_property(np, "cell-index", NULL);
251 if (!iprop) {
252 dev_err(&pdev->dev, "cell-index property not found\n");
253 ret = -EINVAL;
254 goto error;
255 }
147dfe90 256 mdata->ssi_id = be32_to_cpup(iprop);
27ef3744
TT
257
258 /* Get the serial format and clock direction. */
259 sprop = of_get_property(np, "fsl,mode", NULL);
260 if (!sprop) {
261 dev_err(&pdev->dev, "fsl,mode property not found\n");
262 ret = -EINVAL;
263 goto error;
264 }
265
266 if (strcasecmp(sprop, "i2s-slave") == 0) {
70ac07bb
TT
267 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
268 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
27ef3744
TT
269 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
270 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
271
272 /* In i2s-slave mode, the codec has its own clock source, so we
273 * need to get the frequency from the device tree and pass it to
274 * the codec driver.
275 */
276 iprop = of_get_property(codec_np, "clock-frequency", NULL);
277 if (!iprop || !*iprop) {
278 dev_err(&pdev->dev, "codec bus-frequency "
279 "property is missing or invalid\n");
280 ret = -EINVAL;
281 goto error;
282 }
147dfe90 283 mdata->clk_frequency = be32_to_cpup(iprop);
27ef3744 284 } else if (strcasecmp(sprop, "i2s-master") == 0) {
70ac07bb
TT
285 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
286 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
27ef3744
TT
287 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
288 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
289 } else if (strcasecmp(sprop, "lj-slave") == 0) {
70ac07bb
TT
290 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
291 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
27ef3744
TT
292 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
293 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
294 } else if (strcasecmp(sprop, "lj-master") == 0) {
70ac07bb
TT
295 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
296 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
27ef3744
TT
297 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
298 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
299 } else if (strcasecmp(sprop, "rj-slave") == 0) {
70ac07bb
TT
300 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
301 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
27ef3744
TT
302 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
303 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
304 } else if (strcasecmp(sprop, "rj-master") == 0) {
70ac07bb
TT
305 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
306 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
27ef3744
TT
307 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
308 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
309 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
70ac07bb
TT
310 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
311 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
27ef3744
TT
312 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
313 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
314 } else if (strcasecmp(sprop, "ac97-master") == 0) {
70ac07bb
TT
315 mdata->dai_format = SND_SOC_DAIFMT_NB_NF |
316 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
27ef3744
TT
317 mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
318 mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
319 } else {
320 dev_err(&pdev->dev,
321 "unrecognized fsl,mode property '%s'\n", sprop);
322 ret = -EINVAL;
323 goto error;
324 }
325
326 if (!mdata->clk_frequency) {
327 dev_err(&pdev->dev, "unknown clock frequency\n");
328 ret = -EINVAL;
329 goto error;
330 }
331
332 /* Find the playback DMA channel to use. */
333 mdata->dai[0].platform_name = mdata->platform_name[0];
60aae8da
SG
334 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
335 &mdata->dma_channel_id[0],
336 &mdata->dma_id[0]);
27ef3744
TT
337 if (ret) {
338 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
339 goto error;
340 }
341
342 /* Find the capture DMA channel to use. */
343 mdata->dai[1].platform_name = mdata->platform_name[1];
60aae8da
SG
344 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
345 &mdata->dma_channel_id[1],
346 &mdata->dma_id[1]);
27ef3744
TT
347 if (ret) {
348 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
349 goto error;
350 }
351
352 /* Initialize our DAI data structure. */
353 mdata->dai[0].stream_name = "playback";
354 mdata->dai[1].stream_name = "capture";
355 mdata->dai[0].name = mdata->dai[0].stream_name;
356 mdata->dai[1].name = mdata->dai[1].stream_name;
357
358 mdata->card.probe = p1022_ds_machine_probe;
359 mdata->card.remove = p1022_ds_machine_remove;
360 mdata->card.name = pdev->name; /* The platform driver name */
361 mdata->card.num_links = 2;
362 mdata->card.dai_link = mdata->dai;
363
364 /* Allocate a new audio platform device structure */
365 sound_device = platform_device_alloc("soc-audio", -1);
366 if (!sound_device) {
367 dev_err(&pdev->dev, "platform device alloc failed\n");
368 ret = -ENOMEM;
369 goto error;
370 }
371
372 /* Associate the card data with the sound device */
373 platform_set_drvdata(sound_device, &mdata->card);
374
375 /* Register with ASoC */
376 ret = platform_device_add(sound_device);
377 if (ret) {
378 dev_err(&pdev->dev, "platform device add failed\n");
379 goto error;
380 }
39a54555 381 dev_set_drvdata(&pdev->dev, sound_device);
27ef3744
TT
382
383 of_node_put(codec_np);
384
385 return 0;
386
387error:
27ef3744 388 if (sound_device)
d890a1a4 389 platform_device_put(sound_device);
27ef3744
TT
390
391 kfree(mdata);
880b8ffd
JL
392error_put:
393 of_node_put(codec_np);
27ef3744
TT
394 return ret;
395}
396
397/**
398 * p1022_ds_remove: remove the platform device
399 *
400 * This function is called when the platform device is removed.
401 */
402static int __devexit p1022_ds_remove(struct platform_device *pdev)
403{
404 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
405 struct snd_soc_card *card = platform_get_drvdata(sound_device);
406 struct machine_data *mdata =
407 container_of(card, struct machine_data, card);
408
409 platform_device_unregister(sound_device);
410
411 kfree(mdata);
412 sound_device->dev.platform_data = NULL;
413
414 dev_set_drvdata(&pdev->dev, NULL);
415
416 return 0;
417}
418
419static struct platform_driver p1022_ds_driver = {
420 .probe = p1022_ds_probe,
421 .remove = __devexit_p(p1022_ds_remove),
422 .driver = {
2b81ec69
SG
423 /*
424 * The name must match 'compatible' property in the device tree,
425 * in lowercase letters.
426 */
427 .name = "snd-soc-p1022ds",
27ef3744
TT
428 .owner = THIS_MODULE,
429 },
430};
431
432/**
433 * p1022_ds_init: machine driver initialization.
434 *
435 * This function is called when this module is loaded.
436 */
437static int __init p1022_ds_init(void)
438{
439 struct device_node *guts_np;
440 struct resource res;
27ef3744
TT
441
442 /* Get the physical address of the global utilities registers */
443 guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
444 if (of_address_to_resource(guts_np, 0, &res)) {
a806aa92
TT
445 pr_err("snd-soc-p1022ds: missing/invalid global utils node\n");
446 of_node_put(guts_np);
27ef3744
TT
447 return -EINVAL;
448 }
449 guts_phys = res.start;
450 of_node_put(guts_np);
451
452 return platform_driver_register(&p1022_ds_driver);
453}
454
455/**
456 * p1022_ds_exit: machine driver exit
457 *
458 * This function is called when this driver is unloaded.
459 */
460static void __exit p1022_ds_exit(void)
461{
462 platform_driver_unregister(&p1022_ds_driver);
463}
464
465module_init(p1022_ds_init);
466module_exit(p1022_ds_exit);
467
468MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
469MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
470MODULE_LICENSE("GPL v2");
This page took 0.093408 seconds and 5 git commands to generate.