blob: 0d7dcf1e4863592c439a626b7fe449b56669f45d [file] [log] [blame]
Timur Tabi17467f22008-01-11 18:15:26 +01001/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002 * Freescale MPC8610HPCD ALSA SoC Machine driver
Timur Tabi17467f22008-01-11 18:15:26 +01003 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * 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.
Timur Tabi17467f22008-01-11 18:15:26 +010011 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/of_device.h>
Timur Tabi38fec722010-08-19 15:26:58 -050016#include <linux/slab.h>
Timur Tabi17467f22008-01-11 18:15:26 +010017#include <sound/soc.h>
Timur Tabi6e6f6622010-07-22 11:33:30 -050018#include <asm/fsl_guts.h>
Timur Tabi17467f22008-01-11 18:15:26 +010019
Timur Tabi17467f22008-01-11 18:15:26 +010020#include "fsl_dma.h"
21#include "fsl_ssi.h"
22
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000023/* There's only one global utilities register */
24static phys_addr_t guts_phys;
25
26#define DAI_NAME_SIZE 32
27
Timur Tabi17467f22008-01-11 18:15:26 +010028/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000029 * mpc8610_hpcd_data: machine-specific ASoC device data
Timur Tabi17467f22008-01-11 18:15:26 +010030 *
31 * This structure contains data for a single sound platform device on an
32 * MPC8610 HPCD. Some of the data is taken from the device tree.
33 */
34struct mpc8610_hpcd_data {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000035 struct snd_soc_dai_link dai[2];
36 struct snd_soc_card card;
Timur Tabi17467f22008-01-11 18:15:26 +010037 unsigned int dai_format;
38 unsigned int codec_clk_direction;
39 unsigned int cpu_clk_direction;
40 unsigned int clk_frequency;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000041 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
42 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
Timur Tabi17467f22008-01-11 18:15:26 +010043 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044 char codec_dai_name[DAI_NAME_SIZE];
45 char codec_name[DAI_NAME_SIZE];
46 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
Timur Tabi17467f22008-01-11 18:15:26 +010047};
48
49/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000050 * mpc8610_hpcd_machine_probe: initialize the board
Timur Tabi17467f22008-01-11 18:15:26 +010051 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000052 * This function is used to initialize the board-specific hardware.
Timur Tabi17467f22008-01-11 18:15:26 +010053 *
54 * Here we program the DMACR and PMUXCR registers.
55 */
56static int mpc8610_hpcd_machine_probe(struct platform_device *sound_device)
57{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000058 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +010059 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000060 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi6e6f6622010-07-22 11:33:30 -050061 struct ccsr_guts_86xx __iomem *guts;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000062
Timur Tabi6e6f6622010-07-22 11:33:30 -050063 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000064 if (!guts) {
65 dev_err(card->dev, "could not map global utilities\n");
66 return -ENOMEM;
67 }
Timur Tabi17467f22008-01-11 18:15:26 +010068
69 /* Program the signal routing between the SSI and the DMA */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000070 guts_set_dmacr(guts, machine_data->dma_id[0],
71 machine_data->dma_channel_id[0],
72 CCSR_GUTS_DMACR_DEV_SSI);
73 guts_set_dmacr(guts, machine_data->dma_id[1],
74 machine_data->dma_channel_id[1],
75 CCSR_GUTS_DMACR_DEV_SSI);
Timur Tabi17467f22008-01-11 18:15:26 +010076
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000077 guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
78 machine_data->dma_channel_id[0], 0);
79 guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
80 machine_data->dma_channel_id[1], 0);
Timur Tabi17467f22008-01-11 18:15:26 +010081
Timur Tabi17467f22008-01-11 18:15:26 +010082 switch (machine_data->ssi_id) {
83 case 0:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +010085 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
86 break;
87 case 1:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000088 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +010089 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
90 break;
91 }
92
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000093 iounmap(guts);
94
Timur Tabi17467f22008-01-11 18:15:26 +010095 return 0;
96}
97
98/**
99 * mpc8610_hpcd_startup: program the board with various hardware parameters
100 *
101 * This function takes board-specific information, like clock frequencies
102 * and serial data formats, and passes that information to the codec and
103 * transport drivers.
104 */
105static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
106{
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Timur Tabi17467f22008-01-11 18:15:26 +0100108 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000109 container_of(rtd->card, struct mpc8610_hpcd_data, card);
110 struct device *dev = rtd->card->dev;
Timur Tabi17467f22008-01-11 18:15:26 +0100111 int ret = 0;
112
Timur Tabi17467f22008-01-11 18:15:26 +0100113 /* Tell the codec driver what the serial protocol is. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000114 ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
Liam Girdwood64105cf2008-07-08 13:19:18 +0100115 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000116 dev_err(dev, "could not set codec driver audio format\n");
Liam Girdwood64105cf2008-07-08 13:19:18 +0100117 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100118 }
119
120 /*
121 * Tell the codec driver what the MCLK frequency is, and whether it's
122 * a slave or master.
123 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000124 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
125 machine_data->clk_frequency,
126 machine_data->codec_clk_direction);
Liam Girdwood64105cf2008-07-08 13:19:18 +0100127 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000128 dev_err(dev, "could not set codec driver clock params\n");
Liam Girdwood64105cf2008-07-08 13:19:18 +0100129 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100130 }
131
132 return 0;
133}
134
135/**
136 * mpc8610_hpcd_machine_remove: Remove the sound device
137 *
138 * This function is called to remove the sound device for one SSI. We
139 * de-program the DMACR and PMUXCR register.
140 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141static int mpc8610_hpcd_machine_remove(struct platform_device *sound_device)
Timur Tabi17467f22008-01-11 18:15:26 +0100142{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100144 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi6e6f6622010-07-22 11:33:30 -0500146 struct ccsr_guts_86xx __iomem *guts;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147
Timur Tabi6e6f6622010-07-22 11:33:30 -0500148 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 if (!guts) {
150 dev_err(card->dev, "could not map global utilities\n");
151 return -ENOMEM;
152 }
Timur Tabi17467f22008-01-11 18:15:26 +0100153
154 /* Restore the signal routing */
155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 guts_set_dmacr(guts, machine_data->dma_id[0],
157 machine_data->dma_channel_id[0], 0);
158 guts_set_dmacr(guts, machine_data->dma_id[1],
159 machine_data->dma_channel_id[1], 0);
Timur Tabi17467f22008-01-11 18:15:26 +0100160
161 switch (machine_data->ssi_id) {
162 case 0:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +0100164 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
165 break;
166 case 1:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000167 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi83544992008-06-13 14:02:31 -0500168 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
Timur Tabi17467f22008-01-11 18:15:26 +0100169 break;
170 }
171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172 iounmap(guts);
173
Timur Tabi17467f22008-01-11 18:15:26 +0100174 return 0;
175}
176
177/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000178 * mpc8610_hpcd_ops: ASoC machine driver operations
Timur Tabi17467f22008-01-11 18:15:26 +0100179 */
180static struct snd_soc_ops mpc8610_hpcd_ops = {
181 .startup = mpc8610_hpcd_startup,
182};
183
184/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 * get_node_by_phandle_name - get a node by its phandle name
Timur Tabi17467f22008-01-11 18:15:26 +0100186 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000187 * This function takes a node, the name of a property in that node, and a
188 * compatible string. Assuming the property is a phandle to another node,
189 * it returns that node, (optionally) if that node is compatible.
Timur Tabi17467f22008-01-11 18:15:26 +0100190 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000191 * If the property is not a phandle, or the node it points to is not compatible
192 * with the specific string, then NULL is returned.
193 */
194static struct device_node *get_node_by_phandle_name(struct device_node *np,
195 const char *name,
196 const char *compatible)
197{
198 const phandle *ph;
199 int len;
200
201 ph = of_get_property(np, name, &len);
202 if (!ph || (len != sizeof(phandle)))
203 return NULL;
204
205 np = of_find_node_by_phandle(*ph);
206 if (!np)
207 return NULL;
208
209 if (compatible && !of_device_is_compatible(np, compatible)) {
210 of_node_put(np);
211 return NULL;
212 }
213
214 return np;
215}
216
217/**
218 * get_parent_cell_index -- return the cell-index of the parent of a node
219 *
220 * Return the value of the cell-index property of the parent of the given
221 * node. This is used for DMA channel nodes that need to know the DMA ID
222 * of the controller they are on.
223 */
224static int get_parent_cell_index(struct device_node *np)
225{
226 struct device_node *parent = of_get_parent(np);
227 const u32 *iprop;
228
229 if (!parent)
230 return -1;
231
232 iprop = of_get_property(parent, "cell-index", NULL);
233 of_node_put(parent);
234
235 if (!iprop)
236 return -1;
237
238 return *iprop;
239}
240
241/**
242 * codec_node_dev_name - determine the dev_name for a codec node
243 *
244 * This function determines the dev_name for an I2C node. This is the name
245 * that would be returned by dev_name() if this device_node were part of a
246 * 'struct device' It's ugly and hackish, but it works.
247 *
248 * The dev_name for such devices include the bus number and I2C address. For
249 * example, "cs4270-codec.0-004f".
250 */
251static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
252{
253 const u32 *iprop;
254 int bus, addr;
255 char temp[DAI_NAME_SIZE];
256
257 of_modalias_node(np, temp, DAI_NAME_SIZE);
258
259 iprop = of_get_property(np, "reg", NULL);
260 if (!iprop)
261 return -EINVAL;
262
263 addr = *iprop;
264
265 bus = get_parent_cell_index(np);
266 if (bus < 0)
267 return bus;
268
269 snprintf(buf, len, "%s-codec.%u-%04x", temp, bus, addr);
270
271 return 0;
272}
273
274static int get_dma_channel(struct device_node *ssi_np,
275 const char *compatible,
276 struct snd_soc_dai_link *dai,
277 unsigned int *dma_channel_id,
278 unsigned int *dma_id)
279{
280 struct resource res;
281 struct device_node *dma_channel_np;
282 const u32 *iprop;
283 int ret;
284
285 dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
286 "fsl,ssi-dma-channel");
287 if (!dma_channel_np)
288 return -EINVAL;
289
290 /* Determine the dev_name for the device_node. This code mimics the
291 * behavior of of_device_make_bus_id(). We need this because ASoC uses
292 * the dev_name() of the device to match the platform (DMA) device with
293 * the CPU (SSI) device. It's all ugly and hackish, but it works (for
294 * now).
295 *
296 * dai->platform name should already point to an allocated buffer.
297 */
298 ret = of_address_to_resource(dma_channel_np, 0, &res);
299 if (ret)
300 return ret;
301 snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
302 (unsigned long long) res.start, dma_channel_np->name);
303
304 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
305 if (!iprop) {
306 of_node_put(dma_channel_np);
307 return -EINVAL;
308 }
309
310 *dma_channel_id = *iprop;
311 *dma_id = get_parent_cell_index(dma_channel_np);
312 of_node_put(dma_channel_np);
313
314 return 0;
315}
316
317/**
318 * mpc8610_hpcd_probe: platform probe function for the machine driver
319 *
320 * Although this is a machine driver, the SSI node is the "master" node with
Timur Tabi17467f22008-01-11 18:15:26 +0100321 * respect to audio hardware connections. Therefore, we create a new ASoC
322 * device for each new SSI node that has a codec attached.
Timur Tabi17467f22008-01-11 18:15:26 +0100323 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000324static int mpc8610_hpcd_probe(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100325{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000326 struct device *dev = pdev->dev.parent;
Timur Tabi38fec722010-08-19 15:26:58 -0500327 /* ssi_pdev is the platform device for the SSI node that probed us */
328 struct platform_device *ssi_pdev =
329 container_of(dev, struct platform_device, dev);
330 struct device_node *np = ssi_pdev->dev.of_node;
Timur Tabi17467f22008-01-11 18:15:26 +0100331 struct device_node *codec_np = NULL;
Timur Tabi17467f22008-01-11 18:15:26 +0100332 struct platform_device *sound_device = NULL;
333 struct mpc8610_hpcd_data *machine_data;
Timur Tabi17467f22008-01-11 18:15:26 +0100334 int ret = -ENODEV;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000335 const char *sprop;
336 const u32 *iprop;
337
338 /* We are only interested in SSIs with a codec phandle in them,
339 * so let's make sure this SSI has one. The MPC8610 HPCD only
340 * knows about the CS4270 codec, so reject anything else.
341 */
342 codec_np = get_node_by_phandle_name(np, "codec-handle",
343 "cirrus,cs4270");
344 if (!codec_np) {
345 dev_err(dev, "invalid codec node\n");
346 return -EINVAL;
347 }
Timur Tabi17467f22008-01-11 18:15:26 +0100348
349 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
350 if (!machine_data)
351 return -ENOMEM;
352
Timur Tabi38fec722010-08-19 15:26:58 -0500353 machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000354 machine_data->dai[0].ops = &mpc8610_hpcd_ops;
Timur Tabi17467f22008-01-11 18:15:26 +0100355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356 /* Determine the codec name, it will be used as the codec DAI name */
357 ret = codec_node_dev_name(codec_np, machine_data->codec_name,
358 DAI_NAME_SIZE);
359 if (ret) {
360 dev_err(&pdev->dev, "invalid codec node %s\n",
361 codec_np->full_name);
362 ret = -EINVAL;
363 goto error;
364 }
365 machine_data->dai[0].codec_name = machine_data->codec_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100366
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000367 /* The DAI name from the codec (snd_soc_dai_driver.name) */
368 machine_data->dai[0].codec_dai_name = "cs4270-hifi";
369
370 /* We register two DAIs per SSI, one for playback and the other for
371 * capture. Currently, we only support codecs that have one DAI for
372 * both playback and capture.
Timur Tabi17467f22008-01-11 18:15:26 +0100373 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000374 memcpy(&machine_data->dai[1], &machine_data->dai[0],
375 sizeof(struct snd_soc_dai_link));
Timur Tabi17467f22008-01-11 18:15:26 +0100376
377 /* Get the device ID */
378 iprop = of_get_property(np, "cell-index", NULL);
379 if (!iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000380 dev_err(&pdev->dev, "cell-index property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100381 ret = -EINVAL;
382 goto error;
383 }
384 machine_data->ssi_id = *iprop;
Timur Tabi17467f22008-01-11 18:15:26 +0100385
386 /* Get the serial format and clock direction. */
387 sprop = of_get_property(np, "fsl,mode", NULL);
388 if (!sprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000389 dev_err(&pdev->dev, "fsl,mode property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100390 ret = -EINVAL;
391 goto error;
392 }
393
394 if (strcasecmp(sprop, "i2s-slave") == 0) {
395 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
396 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
397 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
398
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000399 /* In i2s-slave mode, the codec has its own clock source, so we
Timur Tabi17467f22008-01-11 18:15:26 +0100400 * need to get the frequency from the device tree and pass it to
401 * the codec driver.
402 */
403 iprop = of_get_property(codec_np, "clock-frequency", NULL);
404 if (!iprop || !*iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 dev_err(&pdev->dev, "codec bus-frequency "
406 "property is missing or invalid\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100407 ret = -EINVAL;
408 goto error;
409 }
410 machine_data->clk_frequency = *iprop;
411 } else if (strcasecmp(sprop, "i2s-master") == 0) {
412 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
413 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
414 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
415 } else if (strcasecmp(sprop, "lj-slave") == 0) {
416 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
417 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
418 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
419 } else if (strcasecmp(sprop, "lj-master") == 0) {
420 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
421 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
422 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
Roel Kluine5c21572008-02-22 18:41:41 +0100423 } else if (strcasecmp(sprop, "rj-slave") == 0) {
Timur Tabi17467f22008-01-11 18:15:26 +0100424 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
425 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
426 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
427 } else if (strcasecmp(sprop, "rj-master") == 0) {
428 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
429 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
430 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
431 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
432 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
433 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
434 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
435 } else if (strcasecmp(sprop, "ac97-master") == 0) {
436 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
437 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
438 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
439 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 dev_err(&pdev->dev,
441 "unrecognized fsl,mode property '%s'\n", sprop);
Timur Tabi17467f22008-01-11 18:15:26 +0100442 ret = -EINVAL;
443 goto error;
444 }
445
446 if (!machine_data->clk_frequency) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 dev_err(&pdev->dev, "unknown clock frequency\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100448 ret = -EINVAL;
449 goto error;
450 }
451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 /* Find the playback DMA channel to use. */
453 machine_data->dai[0].platform_name = machine_data->platform_name[0];
454 ret = get_dma_channel(np, "fsl,playback-dma", &machine_data->dai[0],
455 &machine_data->dma_channel_id[0],
456 &machine_data->dma_id[0]);
Timur Tabi17467f22008-01-11 18:15:26 +0100457 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100459 goto error;
460 }
461
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000462 /* Find the capture DMA channel to use. */
463 machine_data->dai[1].platform_name = machine_data->platform_name[1];
464 ret = get_dma_channel(np, "fsl,capture-dma", &machine_data->dai[1],
465 &machine_data->dma_channel_id[1],
466 &machine_data->dma_id[1]);
467 if (ret) {
468 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100469 goto error;
470 }
471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 /* Initialize our DAI data structure. */
473 machine_data->dai[0].stream_name = "playback";
474 machine_data->dai[1].stream_name = "capture";
475 machine_data->dai[0].name = machine_data->dai[0].stream_name;
476 machine_data->dai[1].name = machine_data->dai[1].stream_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100477
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 machine_data->card.probe = mpc8610_hpcd_machine_probe;
479 machine_data->card.remove = mpc8610_hpcd_machine_remove;
480 machine_data->card.name = pdev->name; /* The platform driver name */
481 machine_data->card.num_links = 2;
482 machine_data->card.dai_link = machine_data->dai;
Timur Tabi17467f22008-01-11 18:15:26 +0100483
484 /* Allocate a new audio platform device structure */
485 sound_device = platform_device_alloc("soc-audio", -1);
486 if (!sound_device) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 dev_err(&pdev->dev, "platform device alloc failed\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100488 ret = -ENOMEM;
489 goto error;
490 }
491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 /* Associate the card data with the sound device */
493 platform_set_drvdata(sound_device, &machine_data->card);
Timur Tabi17467f22008-01-11 18:15:26 +0100494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 /* Register with ASoC */
Timur Tabi17467f22008-01-11 18:15:26 +0100496 ret = platform_device_add(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100497 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 dev_err(&pdev->dev, "platform device add failed\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100499 goto error;
500 }
501
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100503
504 return 0;
505
506error:
507 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100508
509 if (sound_device)
510 platform_device_unregister(sound_device);
511
Timur Tabi17467f22008-01-11 18:15:26 +0100512 kfree(machine_data);
513
514 return ret;
515}
516
517/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 * mpc8610_hpcd_remove: remove the platform device
Timur Tabi17467f22008-01-11 18:15:26 +0100519 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 * This function is called when the platform device is removed.
Timur Tabi17467f22008-01-11 18:15:26 +0100521 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100523{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
525 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100526 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi17467f22008-01-11 18:15:26 +0100528
529 platform_device_unregister(sound_device);
530
Timur Tabi17467f22008-01-11 18:15:26 +0100531 kfree(machine_data);
532 sound_device->dev.platform_data = NULL;
533
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 dev_set_drvdata(&pdev->dev, NULL);
Timur Tabi17467f22008-01-11 18:15:26 +0100535
536 return 0;
537}
538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539static struct platform_driver mpc8610_hpcd_driver = {
540 .probe = mpc8610_hpcd_probe,
541 .remove = __devexit_p(mpc8610_hpcd_remove),
Grant Likely40182942010-04-13 16:13:02 -0700542 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 /* The name must match the 'model' property in the device tree,
544 * in lowercase letters.
545 */
546 .name = "snd-soc-mpc8610hpcd",
Grant Likely40182942010-04-13 16:13:02 -0700547 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700548 },
Timur Tabi17467f22008-01-11 18:15:26 +0100549};
550
551/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 * mpc8610_hpcd_init: machine driver initialization.
Timur Tabi17467f22008-01-11 18:15:26 +0100553 *
554 * This function is called when this module is loaded.
555 */
556static int __init mpc8610_hpcd_init(void)
557{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 struct device_node *guts_np;
559 struct resource res;
Timur Tabi17467f22008-01-11 18:15:26 +0100560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 /* Get the physical address of the global utilities registers */
564 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
565 if (of_address_to_resource(guts_np, 0, &res)) {
566 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
567 return -EINVAL;
568 }
569 guts_phys = res.start;
Timur Tabi17467f22008-01-11 18:15:26 +0100570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 return platform_driver_register(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100572}
573
574/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 * mpc8610_hpcd_exit: machine driver exit
Timur Tabi17467f22008-01-11 18:15:26 +0100576 *
577 * This function is called when this driver is unloaded.
578 */
579static void __exit mpc8610_hpcd_exit(void)
580{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 platform_driver_unregister(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100582}
583
584module_init(mpc8610_hpcd_init);
585module_exit(mpc8610_hpcd_exit);
586
587MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
589MODULE_LICENSE("GPL v2");