blob: 12a7917b57b0b2a57695109c604161e7835acbc4 [file] [log] [blame]
Grant Likelydc641372008-07-29 11:42:30 +01001/*
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
Jon Smirl89dd0842009-05-23 19:12:59 -040028#include "mpc5200_dma.h"
29
Grant Likelydc641372008-07-29 11:42:30 +010030MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
31MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
32MODULE_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
Grant Likelydc641372008-07-29 11:42:30 +010052static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +000053 struct snd_pcm_hw_params *params,
54 struct snd_soc_dai *dai)
Grant Likelydc641372008-07-29 11:42:30 +010055{
56 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jon Smirlcebe7762009-05-23 19:13:01 -040057 struct psc_dma *psc_dma = rtd->dai->cpu_dai->private_data;
Grant Likelydc641372008-07-29 11:42:30 +010058 u32 mode;
59
Jon Smirlcebe7762009-05-23 19:13:01 -040060 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
Grant Likelydc641372008-07-29 11:42:30 +010061 " 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:
Jon Smirlcebe7762009-05-23 19:13:01 -040080 dev_dbg(psc_dma->dev, "invalid format\n");
Grant Likelydc641372008-07-29 11:42:30 +010081 return -EINVAL;
82 }
Jon Smirlcebe7762009-05-23 19:13:01 -040083 out_be32(&psc_dma->psc_regs->sicr, psc_dma->sicr | mode);
Grant Likelydc641372008-07-29 11:42:30 +010084
85 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
86
87 return 0;
88}
89
Grant Likelydc641372008-07-29 11:42:30 +010090/**
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 */
104static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
105 int clk_id, unsigned int freq, int dir)
106{
Jon Smirlcebe7762009-05-23 19:13:01 -0400107 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",
Grant Likelydc641372008-07-29 11:42:30 +0100109 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 */
124static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
125{
Jon Smirlcebe7762009-05-23 19:13:01 -0400126 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",
Grant Likelydc641372008-07-29 11:42:30 +0100128 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 */
Eric Miao6335d052009-03-03 09:41:00 +0800142static struct snd_soc_dai_ops psc_i2s_dai_ops = {
Jon Smirlcebe7762009-05-23 19:13:01 -0400143 .startup = psc_dma_startup,
Eric Miao6335d052009-03-03 09:41:00 +0800144 .hw_params = psc_i2s_hw_params,
Jon Smirlcebe7762009-05-23 19:13:01 -0400145 .hw_free = psc_dma_hw_free,
146 .shutdown = psc_dma_shutdown,
147 .trigger = psc_dma_trigger,
Eric Miao6335d052009-03-03 09:41:00 +0800148 .set_sysclk = psc_i2s_set_sysclk,
149 .set_fmt = psc_i2s_set_fmt,
150};
151
Grant Likelydc641372008-07-29 11:42:30 +0100152static struct snd_soc_dai psc_i2s_dai_template = {
Grant Likelydc641372008-07-29 11:42:30 +0100153 .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 },
Eric Miao6335d052009-03-03 09:41:00 +0800165 .ops = &psc_i2s_dai_ops,
Grant Likelydc641372008-07-29 11:42:30 +0100166};
167
168/* ---------------------------------------------------------------------
Grant Likelydc641372008-07-29 11:42:30 +0100169 * Sysfs attributes for debugging
170 */
171
172static ssize_t psc_i2s_status_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
Jon Smirlcebe7762009-05-23 19:13:01 -0400175 struct psc_dma *psc_dma = dev_get_drvdata(dev);
Grant Likelydc641372008-07-29 11:42:30 +0100176
177 return sprintf(buf, "status=%.4x sicr=%.8x rfnum=%i rfstat=0x%.4x "
178 "tfnum=%i tfstat=0x%.4x\n",
Jon Smirlcebe7762009-05-23 19:13:01 -0400179 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));
Grant Likelydc641372008-07-29 11:42:30 +0100185}
186
Jon Smirlcebe7762009-05-23 19:13:01 -0400187static int *psc_i2s_get_stat_attr(struct psc_dma *psc_dma, const char *name)
Grant Likelydc641372008-07-29 11:42:30 +0100188{
189 if (strcmp(name, "playback_underrun") == 0)
Jon Smirlcebe7762009-05-23 19:13:01 -0400190 return &psc_dma->stats.underrun_count;
Grant Likelydc641372008-07-29 11:42:30 +0100191 if (strcmp(name, "capture_overrun") == 0)
Jon Smirlcebe7762009-05-23 19:13:01 -0400192 return &psc_dma->stats.overrun_count;
Grant Likelydc641372008-07-29 11:42:30 +0100193
194 return NULL;
195}
196
197static ssize_t psc_i2s_stat_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
Jon Smirlcebe7762009-05-23 19:13:01 -0400200 struct psc_dma *psc_dma = dev_get_drvdata(dev);
Grant Likelydc641372008-07-29 11:42:30 +0100201 int *attrib;
202
Jon Smirlcebe7762009-05-23 19:13:01 -0400203 attrib = psc_i2s_get_stat_attr(psc_dma, attr->attr.name);
Grant Likelydc641372008-07-29 11:42:30 +0100204 if (!attrib)
205 return 0;
206
207 return sprintf(buf, "%i\n", *attrib);
208}
209
210static ssize_t psc_i2s_stat_store(struct device *dev,
211 struct device_attribute *attr,
212 const char *buf,
213 size_t count)
214{
Jon Smirlcebe7762009-05-23 19:13:01 -0400215 struct psc_dma *psc_dma = dev_get_drvdata(dev);
Grant Likelydc641372008-07-29 11:42:30 +0100216 int *attrib;
217
Jon Smirlcebe7762009-05-23 19:13:01 -0400218 attrib = psc_i2s_get_stat_attr(psc_dma, attr->attr.name);
Grant Likelydc641372008-07-29 11:42:30 +0100219 if (!attrib)
220 return 0;
221
222 *attrib = simple_strtoul(buf, NULL, 0);
223 return count;
224}
225
Jon Smirlf06c8fd2008-10-30 12:37:09 +0000226static DEVICE_ATTR(status, 0644, psc_i2s_status_show, NULL);
227static DEVICE_ATTR(playback_underrun, 0644, psc_i2s_stat_show,
228 psc_i2s_stat_store);
229static DEVICE_ATTR(capture_overrun, 0644, psc_i2s_stat_show,
230 psc_i2s_stat_store);
Grant Likelydc641372008-07-29 11:42:30 +0100231
232/* ---------------------------------------------------------------------
233 * OF platform bus binding code:
234 * - Probe/remove operations
235 * - OF device match table
236 */
237static int __devinit psc_i2s_of_probe(struct of_device *op,
238 const struct of_device_id *match)
239{
240 phys_addr_t fifo;
Jon Smirlcebe7762009-05-23 19:13:01 -0400241 struct psc_dma *psc_dma;
Grant Likelydc641372008-07-29 11:42:30 +0100242 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 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400268 psc_dma = kzalloc(sizeof *psc_dma, GFP_KERNEL);
269 if (!psc_dma) {
Grant Likelydc641372008-07-29 11:42:30 +0100270 iounmap(regs);
271 return -ENOMEM;
272 }
Jon Smirlcebe7762009-05-23 19:13:01 -0400273 spin_lock_init(&psc_dma->lock);
274 psc_dma->irq = irq;
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);
Grant Likelydc641372008-07-29 11:42:30 +0100281
282 /* Fill out the CPU DAI structure */
Jon Smirlcebe7762009-05-23 19:13:01 -0400283 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;
Grant Likelydc641372008-07-29 11:42:30 +0100287
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);
Jon Smirlcebe7762009-05-23 19:13:01 -0400291 psc_dma->capture.bcom_task =
Grant Likelydc641372008-07-29 11:42:30 +0100292 bcom_psc_gen_bd_rx_init(psc_id, 10, fifo, 512);
Jon Smirlcebe7762009-05-23 19:13:01 -0400293 psc_dma->playback.bcom_task =
Grant Likelydc641372008-07-29 11:42:30 +0100294 bcom_psc_gen_bd_tx_init(psc_id, 10, fifo);
Jon Smirlcebe7762009-05-23 19:13:01 -0400295 if (!psc_dma->capture.bcom_task ||
296 !psc_dma->playback.bcom_task) {
Grant Likelydc641372008-07-29 11:42:30 +0100297 dev_err(&op->dev, "Could not allocate bestcomm tasks\n");
298 iounmap(regs);
Jon Smirlcebe7762009-05-23 19:13:01 -0400299 kfree(psc_dma);
Grant Likelydc641372008-07-29 11:42:30 +0100300 return -ENODEV;
301 }
302
303 /* Disable all interrupts and reset the PSC */
Jon Smirlcebe7762009-05-23 19:13:01 -0400304 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 */
Grant Likelydc641372008-07-29 11:42:30 +0100309
310 /* Configure the serial interface mode; defaulting to CODEC8 mode */
Jon Smirlcebe7762009-05-23 19:13:01 -0400311 psc_dma->sicr = MPC52xx_PSC_SICR_DTS1 | MPC52xx_PSC_SICR_I2S |
Grant Likelydc641372008-07-29 11:42:30 +0100312 MPC52xx_PSC_SICR_CLKPOL;
313 if (of_get_property(op->node, "fsl,cellslave", NULL))
Jon Smirlcebe7762009-05-23 19:13:01 -0400314 psc_dma->sicr |= MPC52xx_PSC_SICR_CELLSLAVE |
Grant Likelydc641372008-07-29 11:42:30 +0100315 MPC52xx_PSC_SICR_GENCLK;
Jon Smirlcebe7762009-05-23 19:13:01 -0400316 out_be32(&psc_dma->psc_regs->sicr,
317 psc_dma->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8);
Grant Likelydc641372008-07-29 11:42:30 +0100318
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 */
Jon Smirlcebe7762009-05-23 19:13:01 -0400328 out_8(&psc_dma->psc_regs->mode, 0);
329 out_8(&psc_dma->psc_regs->mode, 0);
Grant Likelydc641372008-07-29 11:42:30 +0100330
331 /* Set the TX and RX fifo alarm thresholds */
Jon Smirlcebe7762009-05-23 19:13:01 -0400332 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);
Grant Likelydc641372008-07-29 11:42:30 +0100336
337 /* Lookup the IRQ numbers */
Jon Smirlcebe7762009-05-23 19:13:01 -0400338 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);
Grant Likelydc641372008-07-29 11:42:30 +0100342
343 /* Save what we've done so it can be found again later */
Jon Smirlcebe7762009-05-23 19:13:01 -0400344 dev_set_drvdata(&op->dev, psc_dma);
Grant Likelydc641372008-07-29 11:42:30 +0100345
346 /* Register the SYSFS files */
Jon Smirlcebe7762009-05-23 19:13:01 -0400347 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);
Grant Likelydc641372008-07-29 11:42:30 +0100350 if (rc)
Jon Smirlcebe7762009-05-23 19:13:01 -0400351 dev_info(psc_dma->dev, "error creating sysfs files\n");
Grant Likelydc641372008-07-29 11:42:30 +0100352
Jon Smirlcebe7762009-05-23 19:13:01 -0400353 snd_soc_register_platform(&psc_dma_pcm_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000354
Grant Likelydc641372008-07-29 11:42:30 +0100355 /* Tell the ASoC OF helpers about it */
Jon Smirlcebe7762009-05-23 19:13:01 -0400356 of_snd_soc_register_platform(&psc_dma_pcm_soc_platform, op->node,
357 &psc_dma->dai);
Grant Likelydc641372008-07-29 11:42:30 +0100358
359 return 0;
360}
361
362static int __devexit psc_i2s_of_remove(struct of_device *op)
363{
Jon Smirlcebe7762009-05-23 19:13:01 -0400364 struct psc_dma *psc_dma = dev_get_drvdata(&op->dev);
Grant Likelydc641372008-07-29 11:42:30 +0100365
366 dev_dbg(&op->dev, "psc_i2s_remove()\n");
367
Jon Smirlcebe7762009-05-23 19:13:01 -0400368 snd_soc_unregister_platform(&psc_dma_pcm_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000369
Jon Smirlcebe7762009-05-23 19:13:01 -0400370 bcom_gen_bd_rx_release(psc_dma->capture.bcom_task);
371 bcom_gen_bd_tx_release(psc_dma->playback.bcom_task);
Grant Likelydc641372008-07-29 11:42:30 +0100372
Jon Smirlcebe7762009-05-23 19:13:01 -0400373 iounmap(psc_dma->psc_regs);
374 iounmap(psc_dma->fifo_regs);
375 kfree(psc_dma);
Grant Likelydc641372008-07-29 11:42:30 +0100376 dev_set_drvdata(&op->dev, NULL);
377
378 return 0;
379}
380
381/* Match table for of_platform binding */
382static struct of_device_id psc_i2s_match[] __devinitdata = {
383 { .compatible = "fsl,mpc5200-psc-i2s", },
384 {}
385};
386MODULE_DEVICE_TABLE(of, psc_i2s_match);
387
388static 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 */
402static int __init psc_i2s_init(void)
403{
404 return of_register_platform_driver(&psc_i2s_driver);
405}
406module_init(psc_i2s_init);
407
408static void __exit psc_i2s_exit(void)
409{
410 of_unregister_platform_driver(&psc_i2s_driver);
411}
412module_exit(psc_i2s_exit);
413
414