]> Git Repo - linux.git/blame - sound/soc/fsl/mpc8610_hpcd.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[linux.git] / sound / soc / fsl / mpc8610_hpcd.c
CommitLineData
17467f23 1/**
f0fba2ad 2 * Freescale MPC8610HPCD ALSA SoC Machine driver
17467f23
TT
3 *
4 * Author: Timur Tabi <[email protected]>
5 *
f0fba2ad
LG
6 * Copyright 2007-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.
17467f23
TT
11 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/of_device.h>
38fec727 16#include <linux/slab.h>
17467f23 17#include <sound/soc.h>
6e6f6622 18#include <asm/fsl_guts.h>
17467f23 19
17467f23
TT
20#include "fsl_dma.h"
21#include "fsl_ssi.h"
60aae8da 22#include "fsl_utils.h"
17467f23 23
f0fba2ad
LG
24/* There's only one global utilities register */
25static phys_addr_t guts_phys;
26
17467f23 27/**
f0fba2ad 28 * mpc8610_hpcd_data: machine-specific ASoC device data
17467f23
TT
29 *
30 * This structure contains data for a single sound platform device on an
31 * MPC8610 HPCD. Some of the data is taken from the device tree.
32 */
33struct mpc8610_hpcd_data {
f0fba2ad
LG
34 struct snd_soc_dai_link dai[2];
35 struct snd_soc_card card;
17467f23
TT
36 unsigned int dai_format;
37 unsigned int codec_clk_direction;
38 unsigned int cpu_clk_direction;
39 unsigned int clk_frequency;
f0fba2ad
LG
40 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
41 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
17467f23 42 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
f0fba2ad 43 char codec_dai_name[DAI_NAME_SIZE];
f0fba2ad 44 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
17467f23
TT
45};
46
47/**
f0fba2ad 48 * mpc8610_hpcd_machine_probe: initialize the board
17467f23 49 *
f0fba2ad 50 * This function is used to initialize the board-specific hardware.
17467f23
TT
51 *
52 * Here we program the DMACR and PMUXCR registers.
53 */
e7361ec4 54static int mpc8610_hpcd_machine_probe(struct snd_soc_card *card)
17467f23
TT
55{
56 struct mpc8610_hpcd_data *machine_data =
f0fba2ad 57 container_of(card, struct mpc8610_hpcd_data, card);
9cb6abcb 58 struct ccsr_guts __iomem *guts;
17467f23 59
9cb6abcb 60 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
f0fba2ad
LG
61 if (!guts) {
62 dev_err(card->dev, "could not map global utilities\n");
63 return -ENOMEM;
64 }
17467f23 65
f0fba2ad
LG
66 /* Program the signal routing between the SSI and the DMA */
67 guts_set_dmacr(guts, machine_data->dma_id[0],
68 machine_data->dma_channel_id[0],
69 CCSR_GUTS_DMACR_DEV_SSI);
70 guts_set_dmacr(guts, machine_data->dma_id[1],
71 machine_data->dma_channel_id[1],
72 CCSR_GUTS_DMACR_DEV_SSI);
73
74 guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
75 machine_data->dma_channel_id[0], 0);
76 guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
77 machine_data->dma_channel_id[1], 0);
17467f23 78
17467f23
TT
79 switch (machine_data->ssi_id) {
80 case 0:
f0fba2ad 81 clrsetbits_be32(&guts->pmuxcr,
17467f23
TT
82 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
83 break;
84 case 1:
f0fba2ad 85 clrsetbits_be32(&guts->pmuxcr,
17467f23
TT
86 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
87 break;
88 }
89
f0fba2ad
LG
90 iounmap(guts);
91
17467f23
TT
92 return 0;
93}
94
95/**
96 * mpc8610_hpcd_startup: program the board with various hardware parameters
97 *
98 * This function takes board-specific information, like clock frequencies
99 * and serial data formats, and passes that information to the codec and
100 * transport drivers.
101 */
102static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
103{
104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
17467f23 105 struct mpc8610_hpcd_data *machine_data =
f0fba2ad
LG
106 container_of(rtd->card, struct mpc8610_hpcd_data, card);
107 struct device *dev = rtd->card->dev;
17467f23
TT
108 int ret = 0;
109
17467f23 110 /* Tell the codec driver what the serial protocol is. */
f0fba2ad 111 ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
64105cfd 112 if (ret < 0) {
f0fba2ad 113 dev_err(dev, "could not set codec driver audio format\n");
64105cfd 114 return ret;
17467f23
TT
115 }
116
117 /*
118 * Tell the codec driver what the MCLK frequency is, and whether it's
119 * a slave or master.
120 */
f0fba2ad
LG
121 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
122 machine_data->clk_frequency,
123 machine_data->codec_clk_direction);
64105cfd 124 if (ret < 0) {
f0fba2ad 125 dev_err(dev, "could not set codec driver clock params\n");
64105cfd 126 return ret;
17467f23
TT
127 }
128
129 return 0;
130}
131
132/**
133 * mpc8610_hpcd_machine_remove: Remove the sound device
134 *
135 * This function is called to remove the sound device for one SSI. We
136 * de-program the DMACR and PMUXCR register.
137 */
e7361ec4 138static int mpc8610_hpcd_machine_remove(struct snd_soc_card *card)
17467f23
TT
139{
140 struct mpc8610_hpcd_data *machine_data =
f0fba2ad 141 container_of(card, struct mpc8610_hpcd_data, card);
9cb6abcb 142 struct ccsr_guts __iomem *guts;
f0fba2ad 143
9cb6abcb 144 guts = ioremap(guts_phys, sizeof(struct ccsr_guts));
f0fba2ad
LG
145 if (!guts) {
146 dev_err(card->dev, "could not map global utilities\n");
147 return -ENOMEM;
148 }
17467f23
TT
149
150 /* Restore the signal routing */
151
f0fba2ad
LG
152 guts_set_dmacr(guts, machine_data->dma_id[0],
153 machine_data->dma_channel_id[0], 0);
154 guts_set_dmacr(guts, machine_data->dma_id[1],
155 machine_data->dma_channel_id[1], 0);
17467f23
TT
156
157 switch (machine_data->ssi_id) {
158 case 0:
f0fba2ad 159 clrsetbits_be32(&guts->pmuxcr,
17467f23
TT
160 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
161 break;
162 case 1:
f0fba2ad 163 clrsetbits_be32(&guts->pmuxcr,
83544994 164 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
17467f23
TT
165 break;
166 }
167
f0fba2ad
LG
168 iounmap(guts);
169
17467f23
TT
170 return 0;
171}
172
173/**
f0fba2ad 174 * mpc8610_hpcd_ops: ASoC machine driver operations
17467f23
TT
175 */
176static struct snd_soc_ops mpc8610_hpcd_ops = {
177 .startup = mpc8610_hpcd_startup,
178};
179
f0fba2ad
LG
180/**
181 * mpc8610_hpcd_probe: platform probe function for the machine driver
182 *
183 * Although this is a machine driver, the SSI node is the "master" node with
184 * respect to audio hardware connections. Therefore, we create a new ASoC
185 * device for each new SSI node that has a codec attached.
186 */
187static int mpc8610_hpcd_probe(struct platform_device *pdev)
188{
189 struct device *dev = pdev->dev.parent;
38fec727
TT
190 /* ssi_pdev is the platform device for the SSI node that probed us */
191 struct platform_device *ssi_pdev =
192 container_of(dev, struct platform_device, dev);
193 struct device_node *np = ssi_pdev->dev.of_node;
f0fba2ad 194 struct device_node *codec_np = NULL;
17467f23
TT
195 struct platform_device *sound_device = NULL;
196 struct mpc8610_hpcd_data *machine_data;
17467f23 197 int ret = -ENODEV;
f0fba2ad
LG
198 const char *sprop;
199 const u32 *iprop;
200
7c59bc55
SG
201 /* Find the codec node for this SSI. */
202 codec_np = of_parse_phandle(np, "codec-handle", 0);
f0fba2ad
LG
203 if (!codec_np) {
204 dev_err(dev, "invalid codec node\n");
205 return -EINVAL;
206 }
17467f23
TT
207
208 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
c09f5ca7
JL
209 if (!machine_data) {
210 ret = -ENOMEM;
211 goto error_alloc;
212 }
17467f23 213
38fec727 214 machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
f0fba2ad 215 machine_data->dai[0].ops = &mpc8610_hpcd_ops;
17467f23 216
8f549d7e
SG
217 /* ASoC core can match codec with device node */
218 machine_data->dai[0].codec_of_node = codec_np;
17467f23 219
f0fba2ad
LG
220 /* The DAI name from the codec (snd_soc_dai_driver.name) */
221 machine_data->dai[0].codec_dai_name = "cs4270-hifi";
17467f23 222
f0fba2ad
LG
223 /* We register two DAIs per SSI, one for playback and the other for
224 * capture. Currently, we only support codecs that have one DAI for
225 * both playback and capture.
226 */
227 memcpy(&machine_data->dai[1], &machine_data->dai[0],
228 sizeof(struct snd_soc_dai_link));
17467f23
TT
229
230 /* Get the device ID */
231 iprop = of_get_property(np, "cell-index", NULL);
232 if (!iprop) {
f0fba2ad 233 dev_err(&pdev->dev, "cell-index property not found\n");
17467f23
TT
234 ret = -EINVAL;
235 goto error;
236 }
147dfe90 237 machine_data->ssi_id = be32_to_cpup(iprop);
17467f23
TT
238
239 /* Get the serial format and clock direction. */
240 sprop = of_get_property(np, "fsl,mode", NULL);
241 if (!sprop) {
f0fba2ad 242 dev_err(&pdev->dev, "fsl,mode property not found\n");
17467f23
TT
243 ret = -EINVAL;
244 goto error;
245 }
246
247 if (strcasecmp(sprop, "i2s-slave") == 0) {
380c8830
TT
248 machine_data->dai_format =
249 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
17467f23
TT
250 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
251 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
252
f0fba2ad 253 /* In i2s-slave mode, the codec has its own clock source, so we
17467f23
TT
254 * need to get the frequency from the device tree and pass it to
255 * the codec driver.
256 */
257 iprop = of_get_property(codec_np, "clock-frequency", NULL);
258 if (!iprop || !*iprop) {
f0fba2ad
LG
259 dev_err(&pdev->dev, "codec bus-frequency "
260 "property is missing or invalid\n");
17467f23
TT
261 ret = -EINVAL;
262 goto error;
263 }
147dfe90 264 machine_data->clk_frequency = be32_to_cpup(iprop);
17467f23 265 } else if (strcasecmp(sprop, "i2s-master") == 0) {
380c8830
TT
266 machine_data->dai_format =
267 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
17467f23
TT
268 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
269 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
270 } else if (strcasecmp(sprop, "lj-slave") == 0) {
380c8830
TT
271 machine_data->dai_format =
272 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
17467f23
TT
273 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
274 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
275 } else if (strcasecmp(sprop, "lj-master") == 0) {
380c8830
TT
276 machine_data->dai_format =
277 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
17467f23
TT
278 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
279 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
e5c21571 280 } else if (strcasecmp(sprop, "rj-slave") == 0) {
380c8830
TT
281 machine_data->dai_format =
282 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
17467f23
TT
283 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
284 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
285 } else if (strcasecmp(sprop, "rj-master") == 0) {
380c8830
TT
286 machine_data->dai_format =
287 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
17467f23
TT
288 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
289 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
290 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
380c8830
TT
291 machine_data->dai_format =
292 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
17467f23
TT
293 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
294 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
295 } else if (strcasecmp(sprop, "ac97-master") == 0) {
380c8830
TT
296 machine_data->dai_format =
297 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
17467f23
TT
298 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
299 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
300 } else {
f0fba2ad
LG
301 dev_err(&pdev->dev,
302 "unrecognized fsl,mode property '%s'\n", sprop);
17467f23
TT
303 ret = -EINVAL;
304 goto error;
305 }
306
307 if (!machine_data->clk_frequency) {
f0fba2ad 308 dev_err(&pdev->dev, "unknown clock frequency\n");
17467f23
TT
309 ret = -EINVAL;
310 goto error;
311 }
312
f0fba2ad
LG
313 /* Find the playback DMA channel to use. */
314 machine_data->dai[0].platform_name = machine_data->platform_name[0];
60aae8da
SG
315 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma",
316 &machine_data->dai[0],
317 &machine_data->dma_channel_id[0],
318 &machine_data->dma_id[0]);
17467f23 319 if (ret) {
f0fba2ad 320 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
17467f23
TT
321 goto error;
322 }
323
f0fba2ad
LG
324 /* Find the capture DMA channel to use. */
325 machine_data->dai[1].platform_name = machine_data->platform_name[1];
60aae8da
SG
326 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma",
327 &machine_data->dai[1],
328 &machine_data->dma_channel_id[1],
329 &machine_data->dma_id[1]);
f0fba2ad
LG
330 if (ret) {
331 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
17467f23
TT
332 goto error;
333 }
334
f0fba2ad
LG
335 /* Initialize our DAI data structure. */
336 machine_data->dai[0].stream_name = "playback";
337 machine_data->dai[1].stream_name = "capture";
338 machine_data->dai[0].name = machine_data->dai[0].stream_name;
339 machine_data->dai[1].name = machine_data->dai[1].stream_name;
17467f23 340
f0fba2ad
LG
341 machine_data->card.probe = mpc8610_hpcd_machine_probe;
342 machine_data->card.remove = mpc8610_hpcd_machine_remove;
343 machine_data->card.name = pdev->name; /* The platform driver name */
344 machine_data->card.num_links = 2;
345 machine_data->card.dai_link = machine_data->dai;
17467f23
TT
346
347 /* Allocate a new audio platform device structure */
348 sound_device = platform_device_alloc("soc-audio", -1);
349 if (!sound_device) {
f0fba2ad 350 dev_err(&pdev->dev, "platform device alloc failed\n");
17467f23
TT
351 ret = -ENOMEM;
352 goto error;
353 }
354
f0fba2ad
LG
355 /* Associate the card data with the sound device */
356 platform_set_drvdata(sound_device, &machine_data->card);
17467f23 357
f0fba2ad 358 /* Register with ASoC */
17467f23 359 ret = platform_device_add(sound_device);
17467f23 360 if (ret) {
f0fba2ad 361 dev_err(&pdev->dev, "platform device add failed\n");
c09f5ca7 362 goto error_sound;
17467f23 363 }
67bd489a 364 dev_set_drvdata(&pdev->dev, sound_device);
17467f23 365
f0fba2ad 366 of_node_put(codec_np);
17467f23
TT
367
368 return 0;
369
c09f5ca7 370error_sound:
d890a1a4 371 platform_device_put(sound_device);
17467f23 372error:
17467f23 373 kfree(machine_data);
c09f5ca7
JL
374error_alloc:
375 of_node_put(codec_np);
17467f23
TT
376 return ret;
377}
378
379/**
f0fba2ad 380 * mpc8610_hpcd_remove: remove the platform device
17467f23 381 *
f0fba2ad 382 * This function is called when the platform device is removed.
17467f23 383 */
f0fba2ad 384static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
17467f23 385{
f0fba2ad
LG
386 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
387 struct snd_soc_card *card = platform_get_drvdata(sound_device);
17467f23 388 struct mpc8610_hpcd_data *machine_data =
f0fba2ad 389 container_of(card, struct mpc8610_hpcd_data, card);
17467f23
TT
390
391 platform_device_unregister(sound_device);
392
17467f23
TT
393 kfree(machine_data);
394 sound_device->dev.platform_data = NULL;
395
f0fba2ad 396 dev_set_drvdata(&pdev->dev, NULL);
17467f23
TT
397
398 return 0;
399}
400
f0fba2ad
LG
401static struct platform_driver mpc8610_hpcd_driver = {
402 .probe = mpc8610_hpcd_probe,
403 .remove = __devexit_p(mpc8610_hpcd_remove),
4018294b 404 .driver = {
2b81ec69 405 /* The name must match 'compatible' property in the device tree,
f0fba2ad
LG
406 * in lowercase letters.
407 */
408 .name = "snd-soc-mpc8610hpcd",
4018294b 409 .owner = THIS_MODULE,
4018294b 410 },
17467f23
TT
411};
412
413/**
f0fba2ad 414 * mpc8610_hpcd_init: machine driver initialization.
17467f23
TT
415 *
416 * This function is called when this module is loaded.
417 */
418static int __init mpc8610_hpcd_init(void)
419{
f0fba2ad
LG
420 struct device_node *guts_np;
421 struct resource res;
17467f23 422
f0fba2ad 423 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
17467f23 424
f0fba2ad
LG
425 /* Get the physical address of the global utilities registers */
426 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
427 if (of_address_to_resource(guts_np, 0, &res)) {
428 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
429 return -EINVAL;
430 }
431 guts_phys = res.start;
17467f23 432
f0fba2ad 433 return platform_driver_register(&mpc8610_hpcd_driver);
17467f23
TT
434}
435
436/**
f0fba2ad 437 * mpc8610_hpcd_exit: machine driver exit
17467f23
TT
438 *
439 * This function is called when this driver is unloaded.
440 */
441static void __exit mpc8610_hpcd_exit(void)
442{
f0fba2ad 443 platform_driver_unregister(&mpc8610_hpcd_driver);
17467f23
TT
444}
445
446module_init(mpc8610_hpcd_init);
447module_exit(mpc8610_hpcd_exit);
448
449MODULE_AUTHOR("Timur Tabi <[email protected]>");
f0fba2ad
LG
450MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
451MODULE_LICENSE("GPL v2");
This page took 0.391218 seconds and 4 git commands to generate.