2 * Freescale MPC5200 PSC in I2S mode
3 * ALSA SoC Digital Audio Interface (DAI) driver
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/delay.h>
13 #include <linux/of_device.h>
14 #include <linux/of_platform.h>
15 #include <linux/dma-mapping.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/initval.h>
21 #include <sound/soc.h>
22 #include <sound/soc-of-simple.h>
24 #include <sysdev/bestcomm/bestcomm.h>
25 #include <sysdev/bestcomm/gen_bd.h>
26 #include <asm/mpc52xx_psc.h>
28 #include "mpc5200_dma.h"
30 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
31 MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
32 MODULE_LICENSE("GPL");
35 * PSC_I2S_RATES: sample rates supported by the I2S
37 * This driver currently only supports the PSC running in I2S slave mode,
38 * which means the codec determines the sample rate. Therefore, we tell
39 * ALSA that we support all rates and let the codec driver decide what rates
40 * are really supported.
42 #define PSC_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
43 SNDRV_PCM_RATE_CONTINUOUS)
46 * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode
48 #define PSC_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
49 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE | \
50 SNDRV_PCM_FMTBIT_S32_BE)
52 static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params,
54 struct snd_soc_dai *dai)
56 struct snd_soc_pcm_runtime *rtd = substream->private_data;
57 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
60 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
61 " periods=%i buffer_size=%i buffer_bytes=%i\n",
62 __func__, substream, params_period_size(params),
63 params_period_bytes(params), params_periods(params),
64 params_buffer_size(params), params_buffer_bytes(params));
66 switch (params_format(params)) {
67 case SNDRV_PCM_FORMAT_S8:
68 mode = MPC52xx_PSC_SICR_SIM_CODEC_8;
70 case SNDRV_PCM_FORMAT_S16_BE:
71 mode = MPC52xx_PSC_SICR_SIM_CODEC_16;
73 case SNDRV_PCM_FORMAT_S24_BE:
74 mode = MPC52xx_PSC_SICR_SIM_CODEC_24;
76 case SNDRV_PCM_FORMAT_S32_BE:
77 mode = MPC52xx_PSC_SICR_SIM_CODEC_32;
80 dev_dbg(psc_dma->dev, "invalid format\n");
83 out_be32(&psc_dma->psc_regs->sicr, psc_dma->sicr | mode);
85 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
91 * psc_i2s_set_sysclk: set the clock frequency and direction
93 * This function is called by the machine driver to tell us what the clock
94 * frequency and direction are.
96 * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
97 * and we don't care about the frequency. Return an error if the direction
98 * is not SND_SOC_CLOCK_IN.
100 * @clk_id: reserved, should be zero
101 * @freq: the frequency of the given clock ID, currently ignored
102 * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
104 static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
105 int clk_id, unsigned int freq, int dir)
107 struct psc_dma *psc_dma = cpu_dai->private_data;
108 dev_dbg(psc_dma->dev, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n",
110 return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
114 * psc_i2s_set_fmt: set the serial format.
116 * This function is called by the machine driver to tell us what serial
119 * This driver only supports I2S mode. Return an error if the format is
120 * not SND_SOC_DAIFMT_I2S.
122 * @format: one of SND_SOC_DAIFMT_xxx
124 static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
126 struct psc_dma *psc_dma = cpu_dai->private_data;
127 dev_dbg(psc_dma->dev, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n",
129 return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
132 /* ---------------------------------------------------------------------
135 * - Digital Audio Interface (DAI) template
136 * - create/destroy dai hooks
140 * psc_i2s_dai_template: template CPU Digital Audio Interface
142 static struct snd_soc_dai_ops psc_i2s_dai_ops = {
143 .startup = psc_dma_startup,
144 .hw_params = psc_i2s_hw_params,
145 .hw_free = psc_dma_hw_free,
146 .shutdown = psc_dma_shutdown,
147 .trigger = psc_dma_trigger,
148 .set_sysclk = psc_i2s_set_sysclk,
149 .set_fmt = psc_i2s_set_fmt,
152 static struct snd_soc_dai psc_i2s_dai_template = {
156 .rates = PSC_I2S_RATES,
157 .formats = PSC_I2S_FORMATS,
162 .rates = PSC_I2S_RATES,
163 .formats = PSC_I2S_FORMATS,
165 .ops = &psc_i2s_dai_ops,
168 /* ---------------------------------------------------------------------
169 * Sysfs attributes for debugging
172 static ssize_t psc_i2s_status_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct psc_dma *psc_dma = dev_get_drvdata(dev);
177 return sprintf(buf, "status=%.4x sicr=%.8x rfnum=%i rfstat=0x%.4x "
178 "tfnum=%i tfstat=0x%.4x\n",
179 in_be16(&psc_dma->psc_regs->sr_csr.status),
180 in_be32(&psc_dma->psc_regs->sicr),
181 in_be16(&psc_dma->fifo_regs->rfnum) & 0x1ff,
182 in_be16(&psc_dma->fifo_regs->rfstat),
183 in_be16(&psc_dma->fifo_regs->tfnum) & 0x1ff,
184 in_be16(&psc_dma->fifo_regs->tfstat));
187 static int *psc_i2s_get_stat_attr(struct psc_dma *psc_dma, const char *name)
189 if (strcmp(name, "playback_underrun") == 0)
190 return &psc_dma->stats.underrun_count;
191 if (strcmp(name, "capture_overrun") == 0)
192 return &psc_dma->stats.overrun_count;
197 static ssize_t psc_i2s_stat_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 struct psc_dma *psc_dma = dev_get_drvdata(dev);
203 attrib = psc_i2s_get_stat_attr(psc_dma, attr->attr.name);
207 return sprintf(buf, "%i\n", *attrib);
210 static ssize_t psc_i2s_stat_store(struct device *dev,
211 struct device_attribute *attr,
215 struct psc_dma *psc_dma = dev_get_drvdata(dev);
218 attrib = psc_i2s_get_stat_attr(psc_dma, attr->attr.name);
222 *attrib = simple_strtoul(buf, NULL, 0);
226 static DEVICE_ATTR(status, 0644, psc_i2s_status_show, NULL);
227 static DEVICE_ATTR(playback_underrun, 0644, psc_i2s_stat_show,
229 static DEVICE_ATTR(capture_overrun, 0644, psc_i2s_stat_show,
232 /* ---------------------------------------------------------------------
233 * OF platform bus binding code:
234 * - Probe/remove operations
235 * - OF device match table
237 static int __devinit psc_i2s_of_probe(struct of_device *op,
238 const struct of_device_id *match)
241 struct psc_dma *psc_dma;
243 int size, psc_id, irq, rc;
247 dev_dbg(&op->dev, "probing psc i2s device\n");
250 prop = of_get_property(op->node, "cell-index", &size);
251 if (!prop || size < sizeof *prop)
253 psc_id = be32_to_cpu(*prop);
255 /* Fetch the registers and IRQ of the PSC */
256 irq = irq_of_parse_and_map(op->node, 0);
257 if (of_address_to_resource(op->node, 0, &res)) {
258 dev_err(&op->dev, "Missing reg property\n");
261 regs = ioremap(res.start, 1 + res.end - res.start);
263 dev_err(&op->dev, "Could not map registers\n");
267 /* Allocate and initialize the driver private data */
268 psc_dma = kzalloc(sizeof *psc_dma, GFP_KERNEL);
273 spin_lock_init(&psc_dma->lock);
275 psc_dma->psc_regs = regs;
276 psc_dma->fifo_regs = regs + sizeof *psc_dma->psc_regs;
277 psc_dma->dev = &op->dev;
278 psc_dma->playback.psc_dma = psc_dma;
279 psc_dma->capture.psc_dma = psc_dma;
280 snprintf(psc_dma->name, sizeof psc_dma->name, "PSC%u", psc_id+1);
282 /* Fill out the CPU DAI structure */
283 memcpy(&psc_dma->dai, &psc_i2s_dai_template, sizeof psc_dma->dai);
284 psc_dma->dai.private_data = psc_dma;
285 psc_dma->dai.name = psc_dma->name;
286 psc_dma->dai.id = psc_id;
288 /* Find the address of the fifo data registers and setup the
290 fifo = res.start + offsetof(struct mpc52xx_psc, buffer.buffer_32);
291 psc_dma->capture.bcom_task =
292 bcom_psc_gen_bd_rx_init(psc_id, 10, fifo, 512);
293 psc_dma->playback.bcom_task =
294 bcom_psc_gen_bd_tx_init(psc_id, 10, fifo);
295 if (!psc_dma->capture.bcom_task ||
296 !psc_dma->playback.bcom_task) {
297 dev_err(&op->dev, "Could not allocate bestcomm tasks\n");
303 /* Disable all interrupts and reset the PSC */
304 out_be16(&psc_dma->psc_regs->isr_imr.imr, 0);
305 out_8(&psc_dma->psc_regs->command, 3 << 4); /* reset transmitter */
306 out_8(&psc_dma->psc_regs->command, 2 << 4); /* reset receiver */
307 out_8(&psc_dma->psc_regs->command, 1 << 4); /* reset mode */
308 out_8(&psc_dma->psc_regs->command, 4 << 4); /* reset error */
310 /* Configure the serial interface mode; defaulting to CODEC8 mode */
311 psc_dma->sicr = MPC52xx_PSC_SICR_DTS1 | MPC52xx_PSC_SICR_I2S |
312 MPC52xx_PSC_SICR_CLKPOL;
313 if (of_get_property(op->node, "fsl,cellslave", NULL))
314 psc_dma->sicr |= MPC52xx_PSC_SICR_CELLSLAVE |
315 MPC52xx_PSC_SICR_GENCLK;
316 out_be32(&psc_dma->psc_regs->sicr,
317 psc_dma->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8);
319 /* Check for the codec handle. If it is not present then we
321 if (!of_get_property(op->node, "codec-handle", NULL))
324 /* Set up mode register;
325 * First write: RxRdy (FIFO Alarm) generates rx FIFO irq
326 * Second write: register Normal mode for non loopback
328 out_8(&psc_dma->psc_regs->mode, 0);
329 out_8(&psc_dma->psc_regs->mode, 0);
331 /* Set the TX and RX fifo alarm thresholds */
332 out_be16(&psc_dma->fifo_regs->rfalarm, 0x100);
333 out_8(&psc_dma->fifo_regs->rfcntl, 0x4);
334 out_be16(&psc_dma->fifo_regs->tfalarm, 0x100);
335 out_8(&psc_dma->fifo_regs->tfcntl, 0x7);
337 /* Lookup the IRQ numbers */
338 psc_dma->playback.irq =
339 bcom_get_task_irq(psc_dma->playback.bcom_task);
340 psc_dma->capture.irq =
341 bcom_get_task_irq(psc_dma->capture.bcom_task);
343 /* Save what we've done so it can be found again later */
344 dev_set_drvdata(&op->dev, psc_dma);
346 /* Register the SYSFS files */
347 rc = device_create_file(psc_dma->dev, &dev_attr_status);
348 rc |= device_create_file(psc_dma->dev, &dev_attr_capture_overrun);
349 rc |= device_create_file(psc_dma->dev, &dev_attr_playback_underrun);
351 dev_info(psc_dma->dev, "error creating sysfs files\n");
353 snd_soc_register_platform(&psc_dma_pcm_soc_platform);
355 /* Tell the ASoC OF helpers about it */
356 of_snd_soc_register_platform(&psc_dma_pcm_soc_platform, op->node,
362 static int __devexit psc_i2s_of_remove(struct of_device *op)
364 struct psc_dma *psc_dma = dev_get_drvdata(&op->dev);
366 dev_dbg(&op->dev, "psc_i2s_remove()\n");
368 snd_soc_unregister_platform(&psc_dma_pcm_soc_platform);
370 bcom_gen_bd_rx_release(psc_dma->capture.bcom_task);
371 bcom_gen_bd_tx_release(psc_dma->playback.bcom_task);
373 iounmap(psc_dma->psc_regs);
374 iounmap(psc_dma->fifo_regs);
376 dev_set_drvdata(&op->dev, NULL);
381 /* Match table for of_platform binding */
382 static struct of_device_id psc_i2s_match[] __devinitdata = {
383 { .compatible = "fsl,mpc5200-psc-i2s", },
386 MODULE_DEVICE_TABLE(of, psc_i2s_match);
388 static struct of_platform_driver psc_i2s_driver = {
389 .match_table = psc_i2s_match,
390 .probe = psc_i2s_of_probe,
391 .remove = __devexit_p(psc_i2s_of_remove),
393 .name = "mpc5200-psc-i2s",
394 .owner = THIS_MODULE,
398 /* ---------------------------------------------------------------------
399 * Module setup and teardown; simply register the of_platform driver
400 * for the PSC in I2S mode.
402 static int __init psc_i2s_init(void)
404 return of_register_platform_driver(&psc_i2s_driver);
406 module_init(psc_i2s_init);
408 static void __exit psc_i2s_exit(void)
410 of_unregister_platform_driver(&psc_i2s_driver);
412 module_exit(psc_i2s_exit);