blob: d8232943ccb67c42471959d0cb6f4cd2e36114d9 [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.
Jon Smirldbcc3472009-05-26 08:34:08 -04006 * Copyright (C) 2009 Jon Smirl, Digispeaker
Grant Likelydc641372008-07-29 11:42:30 +01007 */
8
Grant Likelydc641372008-07-29 11:42:30 +01009#include <linux/module.h>
Grant Likelydc641372008-07-29 11:42:30 +010010#include <linux/of_device.h>
11#include <linux/of_platform.h>
Grant Likelydc641372008-07-29 11:42:30 +010012
Grant Likelydc641372008-07-29 11:42:30 +010013#include <sound/pcm.h>
14#include <sound/pcm_params.h>
Grant Likelydc641372008-07-29 11:42:30 +010015#include <sound/soc.h>
Grant Likelydc641372008-07-29 11:42:30 +010016
Grant Likelydc641372008-07-29 11:42:30 +010017#include <asm/mpc52xx_psc.h>
18
Jon Smirl89dd0842009-05-23 19:12:59 -040019#include "mpc5200_dma.h"
20
Grant Likelydc641372008-07-29 11:42:30 +010021/**
22 * PSC_I2S_RATES: sample rates supported by the I2S
23 *
24 * This driver currently only supports the PSC running in I2S slave mode,
25 * which means the codec determines the sample rate. Therefore, we tell
26 * ALSA that we support all rates and let the codec driver decide what rates
27 * are really supported.
28 */
Lars-Peter Clausen24710c92014-01-11 10:24:41 +010029#define PSC_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
Grant Likelydc641372008-07-29 11:42:30 +010030
31/**
32 * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode
33 */
34#define PSC_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
Jon Smirldbcc3472009-05-26 08:34:08 -040035 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE)
Grant Likelydc641372008-07-29 11:42:30 +010036
Grant Likelydc641372008-07-29 11:42:30 +010037static int psc_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +000038 struct snd_pcm_hw_params *params,
39 struct snd_soc_dai *dai)
Grant Likelydc641372008-07-29 11:42:30 +010040{
41 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Grant Likelydc641372008-07-29 11:42:30 +010043 u32 mode;
44
Jon Smirlcebe7762009-05-23 19:13:01 -040045 dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
Grant Likelydc641372008-07-29 11:42:30 +010046 " periods=%i buffer_size=%i buffer_bytes=%i\n",
47 __func__, substream, params_period_size(params),
48 params_period_bytes(params), params_periods(params),
49 params_buffer_size(params), params_buffer_bytes(params));
50
51 switch (params_format(params)) {
52 case SNDRV_PCM_FORMAT_S8:
53 mode = MPC52xx_PSC_SICR_SIM_CODEC_8;
54 break;
55 case SNDRV_PCM_FORMAT_S16_BE:
56 mode = MPC52xx_PSC_SICR_SIM_CODEC_16;
57 break;
58 case SNDRV_PCM_FORMAT_S24_BE:
59 mode = MPC52xx_PSC_SICR_SIM_CODEC_24;
60 break;
61 case SNDRV_PCM_FORMAT_S32_BE:
62 mode = MPC52xx_PSC_SICR_SIM_CODEC_32;
63 break;
64 default:
Jon Smirlcebe7762009-05-23 19:13:01 -040065 dev_dbg(psc_dma->dev, "invalid format\n");
Grant Likelydc641372008-07-29 11:42:30 +010066 return -EINVAL;
67 }
Jon Smirlcebe7762009-05-23 19:13:01 -040068 out_be32(&psc_dma->psc_regs->sicr, psc_dma->sicr | mode);
Grant Likelydc641372008-07-29 11:42:30 +010069
Grant Likelydc641372008-07-29 11:42:30 +010070 return 0;
71}
72
Grant Likelydc641372008-07-29 11:42:30 +010073/**
74 * psc_i2s_set_sysclk: set the clock frequency and direction
75 *
76 * This function is called by the machine driver to tell us what the clock
77 * frequency and direction are.
78 *
79 * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
80 * and we don't care about the frequency. Return an error if the direction
81 * is not SND_SOC_CLOCK_IN.
82 *
83 * @clk_id: reserved, should be zero
84 * @freq: the frequency of the given clock ID, currently ignored
85 * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
86 */
87static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
88 int clk_id, unsigned int freq, int dir)
89{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000090 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirlcebe7762009-05-23 19:13:01 -040091 dev_dbg(psc_dma->dev, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n",
Grant Likelydc641372008-07-29 11:42:30 +010092 cpu_dai, dir);
93 return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
94}
95
96/**
97 * psc_i2s_set_fmt: set the serial format.
98 *
99 * This function is called by the machine driver to tell us what serial
100 * format to use.
101 *
102 * This driver only supports I2S mode. Return an error if the format is
103 * not SND_SOC_DAIFMT_I2S.
104 *
105 * @format: one of SND_SOC_DAIFMT_xxx
106 */
107static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
108{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000109 struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
Jon Smirlcebe7762009-05-23 19:13:01 -0400110 dev_dbg(psc_dma->dev, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n",
Grant Likelydc641372008-07-29 11:42:30 +0100111 cpu_dai, format);
112 return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
113}
114
115/* ---------------------------------------------------------------------
116 * ALSA SoC Bindings
117 *
118 * - Digital Audio Interface (DAI) template
119 * - create/destroy dai hooks
120 */
121
122/**
123 * psc_i2s_dai_template: template CPU Digital Audio Interface
124 */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100125static const struct snd_soc_dai_ops psc_i2s_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800126 .hw_params = psc_i2s_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +0800127 .set_sysclk = psc_i2s_set_sysclk,
128 .set_fmt = psc_i2s_set_fmt,
129};
130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000131static struct snd_soc_dai_driver psc_i2s_dai[] = {{
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400132 .name = "mpc5200-psc-i2s.0",
Grant Likelydc641372008-07-29 11:42:30 +0100133 .playback = {
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400134 .stream_name = "I2S Playback",
Grant Likelydc641372008-07-29 11:42:30 +0100135 .channels_min = 2,
136 .channels_max = 2,
137 .rates = PSC_I2S_RATES,
138 .formats = PSC_I2S_FORMATS,
139 },
140 .capture = {
Eric Millbrandta4f7b702012-09-13 17:43:12 -0400141 .stream_name = "I2S Capture",
Grant Likelydc641372008-07-29 11:42:30 +0100142 .channels_min = 2,
143 .channels_max = 2,
144 .rates = PSC_I2S_RATES,
145 .formats = PSC_I2S_FORMATS,
146 },
Eric Miao6335d052009-03-03 09:41:00 +0800147 .ops = &psc_i2s_dai_ops,
Jon Smirldbcc3472009-05-26 08:34:08 -0400148} };
Grant Likelydc641372008-07-29 11:42:30 +0100149
Kuninori Morimotof200c022013-03-21 03:31:53 -0700150static const struct snd_soc_component_driver psc_i2s_component = {
151 .name = "mpc5200-i2s",
152};
153
Grant Likelydc641372008-07-29 11:42:30 +0100154/* ---------------------------------------------------------------------
155 * OF platform bus binding code:
156 * - Probe/remove operations
157 * - OF device match table
158 */
Bill Pembertona0a3d512012-12-07 09:26:16 -0500159static int psc_i2s_of_probe(struct platform_device *op)
Grant Likelydc641372008-07-29 11:42:30 +0100160{
Jon Smirldbcc3472009-05-26 08:34:08 -0400161 int rc;
Jon Smirlcebe7762009-05-23 19:13:01 -0400162 struct psc_dma *psc_dma;
Jon Smirldbcc3472009-05-26 08:34:08 -0400163 struct mpc52xx_psc __iomem *regs;
Grant Likelydc641372008-07-29 11:42:30 +0100164
Eric Millbrandtf515b672012-09-13 17:43:11 -0400165 rc = mpc5200_audio_dma_create(op);
166 if (rc != 0)
167 return rc;
168
Kuninori Morimotof200c022013-03-21 03:31:53 -0700169 rc = snd_soc_register_component(&op->dev, &psc_i2s_component,
170 psc_i2s_dai, ARRAY_SIZE(psc_i2s_dai));
Jon Smirldbcc3472009-05-26 08:34:08 -0400171 if (rc != 0) {
172 pr_err("Failed to register DAI\n");
Axel Lin1ebd0062010-11-08 13:24:58 +0800173 return rc;
Grant Likelydc641372008-07-29 11:42:30 +0100174 }
175
Jon Smirldbcc3472009-05-26 08:34:08 -0400176 psc_dma = dev_get_drvdata(&op->dev);
177 regs = psc_dma->psc_regs;
Grant Likelydc641372008-07-29 11:42:30 +0100178
179 /* Configure the serial interface mode; defaulting to CODEC8 mode */
Jon Smirlcebe7762009-05-23 19:13:01 -0400180 psc_dma->sicr = MPC52xx_PSC_SICR_DTS1 | MPC52xx_PSC_SICR_I2S |
Grant Likelydc641372008-07-29 11:42:30 +0100181 MPC52xx_PSC_SICR_CLKPOL;
Jon Smirlcebe7762009-05-23 19:13:01 -0400182 out_be32(&psc_dma->psc_regs->sicr,
183 psc_dma->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8);
Grant Likelydc641372008-07-29 11:42:30 +0100184
185 /* Check for the codec handle. If it is not present then we
186 * are done */
Grant Likely61c7a082010-04-13 16:12:29 -0700187 if (!of_get_property(op->dev.of_node, "codec-handle", NULL))
Grant Likelydc641372008-07-29 11:42:30 +0100188 return 0;
189
Jon Smirldbcc3472009-05-26 08:34:08 -0400190 /* Due to errata in the dma mode; need to line up enabling
191 * the transmitter with a transition on the frame sync
192 * line */
Grant Likelydc641372008-07-29 11:42:30 +0100193
Jon Smirldbcc3472009-05-26 08:34:08 -0400194 /* first make sure it is low */
195 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) != 0)
196 ;
197 /* then wait for the transition to high */
198 while ((in_8(&regs->ipcr_acr.ipcr) & 0x80) == 0)
199 ;
200 /* Finally, enable the PSC.
201 * Receiver must always be enabled; even when we only want
202 * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */
Grant Likelydc641372008-07-29 11:42:30 +0100203
Jon Smirldbcc3472009-05-26 08:34:08 -0400204 /* Go */
205 out_8(&psc_dma->psc_regs->command,
206 MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
Grant Likelydc641372008-07-29 11:42:30 +0100207
208 return 0;
Jon Smirldbcc3472009-05-26 08:34:08 -0400209
Grant Likelydc641372008-07-29 11:42:30 +0100210}
211
Bill Pembertona0a3d512012-12-07 09:26:16 -0500212static int psc_i2s_of_remove(struct platform_device *op)
Grant Likelydc641372008-07-29 11:42:30 +0100213{
Eric Millbrandtf515b672012-09-13 17:43:11 -0400214 mpc5200_audio_dma_destroy(op);
Kuninori Morimotof200c022013-03-21 03:31:53 -0700215 snd_soc_unregister_component(&op->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 return 0;
Grant Likelydc641372008-07-29 11:42:30 +0100217}
218
219/* Match table for of_platform binding */
Fabian Frederick6ffa84d2015-03-18 17:48:57 +0100220static const struct of_device_id psc_i2s_match[] = {
Grant Likelydc641372008-07-29 11:42:30 +0100221 { .compatible = "fsl,mpc5200-psc-i2s", },
Jon Smirldbcc3472009-05-26 08:34:08 -0400222 { .compatible = "fsl,mpc5200b-psc-i2s", },
Grant Likelydc641372008-07-29 11:42:30 +0100223 {}
224};
225MODULE_DEVICE_TABLE(of, psc_i2s_match);
226
Grant Likelyf07eb222011-02-22 21:05:04 -0700227static struct platform_driver psc_i2s_driver = {
Grant Likelydc641372008-07-29 11:42:30 +0100228 .probe = psc_i2s_of_probe,
Bill Pembertona0a3d512012-12-07 09:26:16 -0500229 .remove = psc_i2s_of_remove,
Grant Likelydc641372008-07-29 11:42:30 +0100230 .driver = {
231 .name = "mpc5200-psc-i2s",
Grant Likely40182942010-04-13 16:13:02 -0700232 .of_match_table = psc_i2s_match,
Grant Likelydc641372008-07-29 11:42:30 +0100233 },
234};
235
Axel Linba0a7e02011-11-25 10:10:55 +0800236module_platform_driver(psc_i2s_driver);
Grant Likelydc641372008-07-29 11:42:30 +0100237
Jon Smirldbcc3472009-05-26 08:34:08 -0400238MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
239MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
240MODULE_LICENSE("GPL");
Grant Likelydc641372008-07-29 11:42:30 +0100241