ASoC: Basic split of mpc5200 DMA code out of mpc5200_psc_i2s
[pandora-kernel.git] / sound / soc / fsl / mpc5200_psc_i2s.c
1 /*
2  * Freescale MPC5200 PSC in I2S mode
3  * ALSA SoC Digital Audio Interface (DAI) driver
4  *
5  * Copyright (C) 2008 Secret Lab Technologies Ltd.
6  */
7
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>
16
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>
23
24 #include <sysdev/bestcomm/bestcomm.h>
25 #include <sysdev/bestcomm/gen_bd.h>
26 #include <asm/mpc52xx_psc.h>
27
28 #include "mpc5200_dma.h"
29
30 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
31 MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
32 MODULE_LICENSE("GPL");
33
34 /**
35  * PSC_I2S_RATES: sample rates supported by the I2S
36  *
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.
41  */
42 #define PSC_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
43                         SNDRV_PCM_RATE_CONTINUOUS)
44
45 /**
46  * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode
47  */
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)
51
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)
55 {
56         struct snd_soc_pcm_runtime *rtd = substream->private_data;
57         struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data;
58         u32 mode;
59
60         dev_dbg(psc_i2s->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));
65
66         switch (params_format(params)) {
67         case SNDRV_PCM_FORMAT_S8:
68                 mode = MPC52xx_PSC_SICR_SIM_CODEC_8;
69                 break;
70         case SNDRV_PCM_FORMAT_S16_BE:
71                 mode = MPC52xx_PSC_SICR_SIM_CODEC_16;
72                 break;
73         case SNDRV_PCM_FORMAT_S24_BE:
74                 mode = MPC52xx_PSC_SICR_SIM_CODEC_24;
75                 break;
76         case SNDRV_PCM_FORMAT_S32_BE:
77                 mode = MPC52xx_PSC_SICR_SIM_CODEC_32;
78                 break;
79         default:
80                 dev_dbg(psc_i2s->dev, "invalid format\n");
81                 return -EINVAL;
82         }
83         out_be32(&psc_i2s->psc_regs->sicr, psc_i2s->sicr | mode);
84
85         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
86
87         return 0;
88 }
89
90 /**
91  * psc_i2s_set_sysclk: set the clock frequency and direction
92  *
93  * This function is called by the machine driver to tell us what the clock
94  * frequency and direction are.
95  *
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.
99  *
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)
103  */
104 static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
105                               int clk_id, unsigned int freq, int dir)
106 {
107         struct psc_i2s *psc_i2s = cpu_dai->private_data;
108         dev_dbg(psc_i2s->dev, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n",
109                                 cpu_dai, dir);
110         return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
111 }
112
113 /**
114  * psc_i2s_set_fmt: set the serial format.
115  *
116  * This function is called by the machine driver to tell us what serial
117  * format to use.
118  *
119  * This driver only supports I2S mode.  Return an error if the format is
120  * not SND_SOC_DAIFMT_I2S.
121  *
122  * @format: one of SND_SOC_DAIFMT_xxx
123  */
124 static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
125 {
126         struct psc_i2s *psc_i2s = cpu_dai->private_data;
127         dev_dbg(psc_i2s->dev, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n",
128                                 cpu_dai, format);
129         return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
130 }
131
132 /* ---------------------------------------------------------------------
133  * ALSA SoC Bindings
134  *
135  * - Digital Audio Interface (DAI) template
136  * - create/destroy dai hooks
137  */
138
139 /**
140  * psc_i2s_dai_template: template CPU Digital Audio Interface
141  */
142 static struct snd_soc_dai_ops psc_i2s_dai_ops = {
143         .startup        = psc_i2s_startup,
144         .hw_params      = psc_i2s_hw_params,
145         .hw_free        = psc_i2s_hw_free,
146         .shutdown       = psc_i2s_shutdown,
147         .trigger        = psc_i2s_trigger,
148         .set_sysclk     = psc_i2s_set_sysclk,
149         .set_fmt        = psc_i2s_set_fmt,
150 };
151
152 static struct snd_soc_dai psc_i2s_dai_template = {
153         .playback = {
154                 .channels_min = 2,
155                 .channels_max = 2,
156                 .rates = PSC_I2S_RATES,
157                 .formats = PSC_I2S_FORMATS,
158         },
159         .capture = {
160                 .channels_min = 2,
161                 .channels_max = 2,
162                 .rates = PSC_I2S_RATES,
163                 .formats = PSC_I2S_FORMATS,
164         },
165         .ops = &psc_i2s_dai_ops,
166 };
167
168 /* ---------------------------------------------------------------------
169  * Sysfs attributes for debugging
170  */
171
172 static ssize_t psc_i2s_status_show(struct device *dev,
173                            struct device_attribute *attr, char *buf)
174 {
175         struct psc_i2s *psc_i2s = dev_get_drvdata(dev);
176
177         return sprintf(buf, "status=%.4x sicr=%.8x rfnum=%i rfstat=0x%.4x "
178                         "tfnum=%i tfstat=0x%.4x\n",
179                         in_be16(&psc_i2s->psc_regs->sr_csr.status),
180                         in_be32(&psc_i2s->psc_regs->sicr),
181                         in_be16(&psc_i2s->fifo_regs->rfnum) & 0x1ff,
182                         in_be16(&psc_i2s->fifo_regs->rfstat),
183                         in_be16(&psc_i2s->fifo_regs->tfnum) & 0x1ff,
184                         in_be16(&psc_i2s->fifo_regs->tfstat));
185 }
186
187 static int *psc_i2s_get_stat_attr(struct psc_i2s *psc_i2s, const char *name)
188 {
189         if (strcmp(name, "playback_underrun") == 0)
190                 return &psc_i2s->stats.underrun_count;
191         if (strcmp(name, "capture_overrun") == 0)
192                 return &psc_i2s->stats.overrun_count;
193
194         return NULL;
195 }
196
197 static ssize_t psc_i2s_stat_show(struct device *dev,
198                                  struct device_attribute *attr, char *buf)
199 {
200         struct psc_i2s *psc_i2s = dev_get_drvdata(dev);
201         int *attrib;
202
203         attrib = psc_i2s_get_stat_attr(psc_i2s, attr->attr.name);
204         if (!attrib)
205                 return 0;
206
207         return sprintf(buf, "%i\n", *attrib);
208 }
209
210 static ssize_t psc_i2s_stat_store(struct device *dev,
211                                   struct device_attribute *attr,
212                                   const char *buf,
213                                   size_t count)
214 {
215         struct psc_i2s *psc_i2s = dev_get_drvdata(dev);
216         int *attrib;
217
218         attrib = psc_i2s_get_stat_attr(psc_i2s, attr->attr.name);
219         if (!attrib)
220                 return 0;
221
222         *attrib = simple_strtoul(buf, NULL, 0);
223         return count;
224 }
225
226 static DEVICE_ATTR(status, 0644, psc_i2s_status_show, NULL);
227 static DEVICE_ATTR(playback_underrun, 0644, psc_i2s_stat_show,
228                         psc_i2s_stat_store);
229 static DEVICE_ATTR(capture_overrun, 0644, psc_i2s_stat_show,
230                         psc_i2s_stat_store);
231
232 /* ---------------------------------------------------------------------
233  * OF platform bus binding code:
234  * - Probe/remove operations
235  * - OF device match table
236  */
237 static int __devinit psc_i2s_of_probe(struct of_device *op,
238                                       const struct of_device_id *match)
239 {
240         phys_addr_t fifo;
241         struct psc_i2s *psc_i2s;
242         struct resource res;
243         int size, psc_id, irq, rc;
244         const __be32 *prop;
245         void __iomem *regs;
246
247         dev_dbg(&op->dev, "probing psc i2s device\n");
248
249         /* Get the PSC ID */
250         prop = of_get_property(op->node, "cell-index", &size);
251         if (!prop || size < sizeof *prop)
252                 return -ENODEV;
253         psc_id = be32_to_cpu(*prop);
254
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");
259                 return -ENODEV;
260         }
261         regs = ioremap(res.start, 1 + res.end - res.start);
262         if (!regs) {
263                 dev_err(&op->dev, "Could not map registers\n");
264                 return -ENODEV;
265         }
266
267         /* Allocate and initialize the driver private data */
268         psc_i2s = kzalloc(sizeof *psc_i2s, GFP_KERNEL);
269         if (!psc_i2s) {
270                 iounmap(regs);
271                 return -ENOMEM;
272         }
273         spin_lock_init(&psc_i2s->lock);
274         psc_i2s->irq = irq;
275         psc_i2s->psc_regs = regs;
276         psc_i2s->fifo_regs = regs + sizeof *psc_i2s->psc_regs;
277         psc_i2s->dev = &op->dev;
278         psc_i2s->playback.psc_i2s = psc_i2s;
279         psc_i2s->capture.psc_i2s = psc_i2s;
280         snprintf(psc_i2s->name, sizeof psc_i2s->name, "PSC%u", psc_id+1);
281
282         /* Fill out the CPU DAI structure */
283         memcpy(&psc_i2s->dai, &psc_i2s_dai_template, sizeof psc_i2s->dai);
284         psc_i2s->dai.private_data = psc_i2s;
285         psc_i2s->dai.name = psc_i2s->name;
286         psc_i2s->dai.id = psc_id;
287
288         /* Find the address of the fifo data registers and setup the
289          * DMA tasks */
290         fifo = res.start + offsetof(struct mpc52xx_psc, buffer.buffer_32);
291         psc_i2s->capture.bcom_task =
292                 bcom_psc_gen_bd_rx_init(psc_id, 10, fifo, 512);
293         psc_i2s->playback.bcom_task =
294                 bcom_psc_gen_bd_tx_init(psc_id, 10, fifo);
295         if (!psc_i2s->capture.bcom_task ||
296             !psc_i2s->playback.bcom_task) {
297                 dev_err(&op->dev, "Could not allocate bestcomm tasks\n");
298                 iounmap(regs);
299                 kfree(psc_i2s);
300                 return -ENODEV;
301         }
302
303         /* Disable all interrupts and reset the PSC */
304         out_be16(&psc_i2s->psc_regs->isr_imr.imr, 0);
305         out_8(&psc_i2s->psc_regs->command, 3 << 4); /* reset transmitter */
306         out_8(&psc_i2s->psc_regs->command, 2 << 4); /* reset receiver */
307         out_8(&psc_i2s->psc_regs->command, 1 << 4); /* reset mode */
308         out_8(&psc_i2s->psc_regs->command, 4 << 4); /* reset error */
309
310         /* Configure the serial interface mode; defaulting to CODEC8 mode */
311         psc_i2s->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_i2s->sicr |= MPC52xx_PSC_SICR_CELLSLAVE |
315                                  MPC52xx_PSC_SICR_GENCLK;
316         out_be32(&psc_i2s->psc_regs->sicr,
317                  psc_i2s->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8);
318
319         /* Check for the codec handle.  If it is not present then we
320          * are done */
321         if (!of_get_property(op->node, "codec-handle", NULL))
322                 return 0;
323
324         /* Set up mode register;
325          * First write: RxRdy (FIFO Alarm) generates rx FIFO irq
326          * Second write: register Normal mode for non loopback
327          */
328         out_8(&psc_i2s->psc_regs->mode, 0);
329         out_8(&psc_i2s->psc_regs->mode, 0);
330
331         /* Set the TX and RX fifo alarm thresholds */
332         out_be16(&psc_i2s->fifo_regs->rfalarm, 0x100);
333         out_8(&psc_i2s->fifo_regs->rfcntl, 0x4);
334         out_be16(&psc_i2s->fifo_regs->tfalarm, 0x100);
335         out_8(&psc_i2s->fifo_regs->tfcntl, 0x7);
336
337         /* Lookup the IRQ numbers */
338         psc_i2s->playback.irq =
339                 bcom_get_task_irq(psc_i2s->playback.bcom_task);
340         psc_i2s->capture.irq =
341                 bcom_get_task_irq(psc_i2s->capture.bcom_task);
342
343         /* Save what we've done so it can be found again later */
344         dev_set_drvdata(&op->dev, psc_i2s);
345
346         /* Register the SYSFS files */
347         rc = device_create_file(psc_i2s->dev, &dev_attr_status);
348         rc |= device_create_file(psc_i2s->dev, &dev_attr_capture_overrun);
349         rc |= device_create_file(psc_i2s->dev, &dev_attr_playback_underrun);
350         if (rc)
351                 dev_info(psc_i2s->dev, "error creating sysfs files\n");
352
353         snd_soc_register_platform(&psc_i2s_pcm_soc_platform);
354
355         /* Tell the ASoC OF helpers about it */
356         of_snd_soc_register_platform(&psc_i2s_pcm_soc_platform, op->node,
357                                      &psc_i2s->dai);
358
359         return 0;
360 }
361
362 static int __devexit psc_i2s_of_remove(struct of_device *op)
363 {
364         struct psc_i2s *psc_i2s = dev_get_drvdata(&op->dev);
365
366         dev_dbg(&op->dev, "psc_i2s_remove()\n");
367
368         snd_soc_unregister_platform(&psc_i2s_pcm_soc_platform);
369
370         bcom_gen_bd_rx_release(psc_i2s->capture.bcom_task);
371         bcom_gen_bd_tx_release(psc_i2s->playback.bcom_task);
372
373         iounmap(psc_i2s->psc_regs);
374         iounmap(psc_i2s->fifo_regs);
375         kfree(psc_i2s);
376         dev_set_drvdata(&op->dev, NULL);
377
378         return 0;
379 }
380
381 /* Match table for of_platform binding */
382 static struct of_device_id psc_i2s_match[] __devinitdata = {
383         { .compatible = "fsl,mpc5200-psc-i2s", },
384         {}
385 };
386 MODULE_DEVICE_TABLE(of, psc_i2s_match);
387
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),
392         .driver = {
393                 .name = "mpc5200-psc-i2s",
394                 .owner = THIS_MODULE,
395         },
396 };
397
398 /* ---------------------------------------------------------------------
399  * Module setup and teardown; simply register the of_platform driver
400  * for the PSC in I2S mode.
401  */
402 static int __init psc_i2s_init(void)
403 {
404         return of_register_platform_driver(&psc_i2s_driver);
405 }
406 module_init(psc_i2s_init);
407
408 static void __exit psc_i2s_exit(void)
409 {
410         of_unregister_platform_driver(&psc_i2s_driver);
411 }
412 module_exit(psc_i2s_exit);
413
414