blob: 38339c158ed94678377f08e2ae1a9e6dea59c9c5 [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 Tabi17467f22008-01-11 18:15:26 +010016#include <sound/soc.h>
Timur Tabi6e6f6622010-07-22 11:33:30 -050017#include <asm/fsl_guts.h>
Timur Tabi17467f22008-01-11 18:15:26 +010018
Timur Tabi17467f22008-01-11 18:15:26 +010019#include "fsl_dma.h"
20#include "fsl_ssi.h"
21
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000022/* There's only one global utilities register */
23static phys_addr_t guts_phys;
24
25#define DAI_NAME_SIZE 32
26
Timur Tabi17467f22008-01-11 18:15:26 +010027/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000028 * mpc8610_hpcd_data: machine-specific ASoC device data
Timur Tabi17467f22008-01-11 18:15:26 +010029 *
30 * This structure contains data for a single sound platform device on an
31 * MPC8610 HPCD. Some of the data is taken from the device tree.
32 */
33struct mpc8610_hpcd_data {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000034 struct snd_soc_dai_link dai[2];
35 struct snd_soc_card card;
Timur Tabi17467f22008-01-11 18:15:26 +010036 unsigned int dai_format;
37 unsigned int codec_clk_direction;
38 unsigned int cpu_clk_direction;
39 unsigned int clk_frequency;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000040 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
41 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
Timur Tabi17467f22008-01-11 18:15:26 +010042 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000043 char codec_dai_name[DAI_NAME_SIZE];
44 char codec_name[DAI_NAME_SIZE];
45 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
Timur Tabi17467f22008-01-11 18:15:26 +010046};
47
48/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000049 * mpc8610_hpcd_machine_probe: initialize the board
Timur Tabi17467f22008-01-11 18:15:26 +010050 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051 * This function is used to initialize the board-specific hardware.
Timur Tabi17467f22008-01-11 18:15:26 +010052 *
53 * Here we program the DMACR and PMUXCR registers.
54 */
55static int mpc8610_hpcd_machine_probe(struct platform_device *sound_device)
56{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000057 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +010058 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000059 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi6e6f6622010-07-22 11:33:30 -050060 struct ccsr_guts_86xx __iomem *guts;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000061
Timur Tabi6e6f6622010-07-22 11:33:30 -050062 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000063 if (!guts) {
64 dev_err(card->dev, "could not map global utilities\n");
65 return -ENOMEM;
66 }
Timur Tabi17467f22008-01-11 18:15:26 +010067
68 /* Program the signal routing between the SSI and the DMA */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000069 guts_set_dmacr(guts, machine_data->dma_id[0],
70 machine_data->dma_channel_id[0],
71 CCSR_GUTS_DMACR_DEV_SSI);
72 guts_set_dmacr(guts, machine_data->dma_id[1],
73 machine_data->dma_channel_id[1],
74 CCSR_GUTS_DMACR_DEV_SSI);
Timur Tabi17467f22008-01-11 18:15:26 +010075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000076 guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
77 machine_data->dma_channel_id[0], 0);
78 guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
79 machine_data->dma_channel_id[1], 0);
Timur Tabi17467f22008-01-11 18:15:26 +010080
Timur Tabi17467f22008-01-11 18:15:26 +010081 switch (machine_data->ssi_id) {
82 case 0:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000083 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +010084 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
85 break;
86 case 1:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000087 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +010088 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
89 break;
90 }
91
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000092 iounmap(guts);
93
Timur Tabi17467f22008-01-11 18:15:26 +010094 return 0;
95}
96
97/**
98 * mpc8610_hpcd_startup: program the board with various hardware parameters
99 *
100 * This function takes board-specific information, like clock frequencies
101 * and serial data formats, and passes that information to the codec and
102 * transport drivers.
103 */
104static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
105{
106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Timur Tabi17467f22008-01-11 18:15:26 +0100107 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000108 container_of(rtd->card, struct mpc8610_hpcd_data, card);
109 struct device *dev = rtd->card->dev;
Timur Tabi17467f22008-01-11 18:15:26 +0100110 int ret = 0;
111
Timur Tabi17467f22008-01-11 18:15:26 +0100112 /* Tell the codec driver what the serial protocol is. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000113 ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
Liam Girdwood64105cf2008-07-08 13:19:18 +0100114 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000115 dev_err(dev, "could not set codec driver audio format\n");
Liam Girdwood64105cf2008-07-08 13:19:18 +0100116 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100117 }
118
119 /*
120 * Tell the codec driver what the MCLK frequency is, and whether it's
121 * a slave or master.
122 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000123 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
124 machine_data->clk_frequency,
125 machine_data->codec_clk_direction);
Liam Girdwood64105cf2008-07-08 13:19:18 +0100126 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000127 dev_err(dev, "could not set codec driver clock params\n");
Liam Girdwood64105cf2008-07-08 13:19:18 +0100128 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100129 }
130
131 return 0;
132}
133
134/**
135 * mpc8610_hpcd_machine_remove: Remove the sound device
136 *
137 * This function is called to remove the sound device for one SSI. We
138 * de-program the DMACR and PMUXCR register.
139 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140static int mpc8610_hpcd_machine_remove(struct platform_device *sound_device)
Timur Tabi17467f22008-01-11 18:15:26 +0100141{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100143 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi6e6f6622010-07-22 11:33:30 -0500145 struct ccsr_guts_86xx __iomem *guts;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146
Timur Tabi6e6f6622010-07-22 11:33:30 -0500147 guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (!guts) {
149 dev_err(card->dev, "could not map global utilities\n");
150 return -ENOMEM;
151 }
Timur Tabi17467f22008-01-11 18:15:26 +0100152
153 /* Restore the signal routing */
154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000155 guts_set_dmacr(guts, machine_data->dma_id[0],
156 machine_data->dma_channel_id[0], 0);
157 guts_set_dmacr(guts, machine_data->dma_id[1],
158 machine_data->dma_channel_id[1], 0);
Timur Tabi17467f22008-01-11 18:15:26 +0100159
160 switch (machine_data->ssi_id) {
161 case 0:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi17467f22008-01-11 18:15:26 +0100163 CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
164 break;
165 case 1:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 clrsetbits_be32(&guts->pmuxcr,
Timur Tabi83544992008-06-13 14:02:31 -0500167 CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
Timur Tabi17467f22008-01-11 18:15:26 +0100168 break;
169 }
170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000171 iounmap(guts);
172
Timur Tabi17467f22008-01-11 18:15:26 +0100173 return 0;
174}
175
176/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177 * mpc8610_hpcd_ops: ASoC machine driver operations
Timur Tabi17467f22008-01-11 18:15:26 +0100178 */
179static struct snd_soc_ops mpc8610_hpcd_ops = {
180 .startup = mpc8610_hpcd_startup,
181};
182
183/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 * get_node_by_phandle_name - get a node by its phandle name
Timur Tabi17467f22008-01-11 18:15:26 +0100185 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000186 * This function takes a node, the name of a property in that node, and a
187 * compatible string. Assuming the property is a phandle to another node,
188 * it returns that node, (optionally) if that node is compatible.
Timur Tabi17467f22008-01-11 18:15:26 +0100189 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000190 * If the property is not a phandle, or the node it points to is not compatible
191 * with the specific string, then NULL is returned.
192 */
193static struct device_node *get_node_by_phandle_name(struct device_node *np,
194 const char *name,
195 const char *compatible)
196{
197 const phandle *ph;
198 int len;
199
200 ph = of_get_property(np, name, &len);
201 if (!ph || (len != sizeof(phandle)))
202 return NULL;
203
204 np = of_find_node_by_phandle(*ph);
205 if (!np)
206 return NULL;
207
208 if (compatible && !of_device_is_compatible(np, compatible)) {
209 of_node_put(np);
210 return NULL;
211 }
212
213 return np;
214}
215
216/**
217 * get_parent_cell_index -- return the cell-index of the parent of a node
218 *
219 * Return the value of the cell-index property of the parent of the given
220 * node. This is used for DMA channel nodes that need to know the DMA ID
221 * of the controller they are on.
222 */
223static int get_parent_cell_index(struct device_node *np)
224{
225 struct device_node *parent = of_get_parent(np);
226 const u32 *iprop;
227
228 if (!parent)
229 return -1;
230
231 iprop = of_get_property(parent, "cell-index", NULL);
232 of_node_put(parent);
233
234 if (!iprop)
235 return -1;
236
237 return *iprop;
238}
239
240/**
241 * codec_node_dev_name - determine the dev_name for a codec node
242 *
243 * This function determines the dev_name for an I2C node. This is the name
244 * that would be returned by dev_name() if this device_node were part of a
245 * 'struct device' It's ugly and hackish, but it works.
246 *
247 * The dev_name for such devices include the bus number and I2C address. For
248 * example, "cs4270-codec.0-004f".
249 */
250static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
251{
252 const u32 *iprop;
253 int bus, addr;
254 char temp[DAI_NAME_SIZE];
255
256 of_modalias_node(np, temp, DAI_NAME_SIZE);
257
258 iprop = of_get_property(np, "reg", NULL);
259 if (!iprop)
260 return -EINVAL;
261
262 addr = *iprop;
263
264 bus = get_parent_cell_index(np);
265 if (bus < 0)
266 return bus;
267
268 snprintf(buf, len, "%s-codec.%u-%04x", temp, bus, addr);
269
270 return 0;
271}
272
273static int get_dma_channel(struct device_node *ssi_np,
274 const char *compatible,
275 struct snd_soc_dai_link *dai,
276 unsigned int *dma_channel_id,
277 unsigned int *dma_id)
278{
279 struct resource res;
280 struct device_node *dma_channel_np;
281 const u32 *iprop;
282 int ret;
283
284 dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
285 "fsl,ssi-dma-channel");
286 if (!dma_channel_np)
287 return -EINVAL;
288
289 /* Determine the dev_name for the device_node. This code mimics the
290 * behavior of of_device_make_bus_id(). We need this because ASoC uses
291 * the dev_name() of the device to match the platform (DMA) device with
292 * the CPU (SSI) device. It's all ugly and hackish, but it works (for
293 * now).
294 *
295 * dai->platform name should already point to an allocated buffer.
296 */
297 ret = of_address_to_resource(dma_channel_np, 0, &res);
298 if (ret)
299 return ret;
300 snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
301 (unsigned long long) res.start, dma_channel_np->name);
302
303 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
304 if (!iprop) {
305 of_node_put(dma_channel_np);
306 return -EINVAL;
307 }
308
309 *dma_channel_id = *iprop;
310 *dma_id = get_parent_cell_index(dma_channel_np);
311 of_node_put(dma_channel_np);
312
313 return 0;
314}
315
316/**
317 * mpc8610_hpcd_probe: platform probe function for the machine driver
318 *
319 * Although this is a machine driver, the SSI node is the "master" node with
Timur Tabi17467f22008-01-11 18:15:26 +0100320 * respect to audio hardware connections. Therefore, we create a new ASoC
321 * device for each new SSI node that has a codec attached.
Timur Tabi17467f22008-01-11 18:15:26 +0100322 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000323static int mpc8610_hpcd_probe(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000325 struct device *dev = pdev->dev.parent;
326 /* of_dev is the OF device for the SSI node that probed us */
327 struct of_device *of_dev = container_of(dev, struct of_device, dev);
328 struct device_node *np = of_dev->dev.of_node;
Timur Tabi17467f22008-01-11 18:15:26 +0100329 struct device_node *codec_np = NULL;
Timur Tabi17467f22008-01-11 18:15:26 +0100330 struct platform_device *sound_device = NULL;
331 struct mpc8610_hpcd_data *machine_data;
Timur Tabi17467f22008-01-11 18:15:26 +0100332 int ret = -ENODEV;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 const char *sprop;
334 const u32 *iprop;
335
336 /* We are only interested in SSIs with a codec phandle in them,
337 * so let's make sure this SSI has one. The MPC8610 HPCD only
338 * knows about the CS4270 codec, so reject anything else.
339 */
340 codec_np = get_node_by_phandle_name(np, "codec-handle",
341 "cirrus,cs4270");
342 if (!codec_np) {
343 dev_err(dev, "invalid codec node\n");
344 return -EINVAL;
345 }
Timur Tabi17467f22008-01-11 18:15:26 +0100346
347 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
348 if (!machine_data)
349 return -ENOMEM;
350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000351 machine_data->dai[0].cpu_dai_name = dev_name(&of_dev->dev);
352 machine_data->dai[0].ops = &mpc8610_hpcd_ops;
Timur Tabi17467f22008-01-11 18:15:26 +0100353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000354 /* Determine the codec name, it will be used as the codec DAI name */
355 ret = codec_node_dev_name(codec_np, machine_data->codec_name,
356 DAI_NAME_SIZE);
357 if (ret) {
358 dev_err(&pdev->dev, "invalid codec node %s\n",
359 codec_np->full_name);
360 ret = -EINVAL;
361 goto error;
362 }
363 machine_data->dai[0].codec_name = machine_data->codec_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000365 /* The DAI name from the codec (snd_soc_dai_driver.name) */
366 machine_data->dai[0].codec_dai_name = "cs4270-hifi";
367
368 /* We register two DAIs per SSI, one for playback and the other for
369 * capture. Currently, we only support codecs that have one DAI for
370 * both playback and capture.
Timur Tabi17467f22008-01-11 18:15:26 +0100371 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000372 memcpy(&machine_data->dai[1], &machine_data->dai[0],
373 sizeof(struct snd_soc_dai_link));
Timur Tabi17467f22008-01-11 18:15:26 +0100374
375 /* Get the device ID */
376 iprop = of_get_property(np, "cell-index", NULL);
377 if (!iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000378 dev_err(&pdev->dev, "cell-index property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100379 ret = -EINVAL;
380 goto error;
381 }
382 machine_data->ssi_id = *iprop;
Timur Tabi17467f22008-01-11 18:15:26 +0100383
384 /* Get the serial format and clock direction. */
385 sprop = of_get_property(np, "fsl,mode", NULL);
386 if (!sprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 dev_err(&pdev->dev, "fsl,mode property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100388 ret = -EINVAL;
389 goto error;
390 }
391
392 if (strcasecmp(sprop, "i2s-slave") == 0) {
393 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
394 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
395 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
396
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000397 /* In i2s-slave mode, the codec has its own clock source, so we
Timur Tabi17467f22008-01-11 18:15:26 +0100398 * need to get the frequency from the device tree and pass it to
399 * the codec driver.
400 */
401 iprop = of_get_property(codec_np, "clock-frequency", NULL);
402 if (!iprop || !*iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000403 dev_err(&pdev->dev, "codec bus-frequency "
404 "property is missing or invalid\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100405 ret = -EINVAL;
406 goto error;
407 }
408 machine_data->clk_frequency = *iprop;
409 } else if (strcasecmp(sprop, "i2s-master") == 0) {
410 machine_data->dai_format = SND_SOC_DAIFMT_I2S;
411 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
412 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
413 } else if (strcasecmp(sprop, "lj-slave") == 0) {
414 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
415 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
416 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
417 } else if (strcasecmp(sprop, "lj-master") == 0) {
418 machine_data->dai_format = SND_SOC_DAIFMT_LEFT_J;
419 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
420 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
Roel Kluine5c21572008-02-22 18:41:41 +0100421 } else if (strcasecmp(sprop, "rj-slave") == 0) {
Timur Tabi17467f22008-01-11 18:15:26 +0100422 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
423 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
424 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
425 } else if (strcasecmp(sprop, "rj-master") == 0) {
426 machine_data->dai_format = SND_SOC_DAIFMT_RIGHT_J;
427 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
428 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
429 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
430 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
431 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
432 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
433 } else if (strcasecmp(sprop, "ac97-master") == 0) {
434 machine_data->dai_format = SND_SOC_DAIFMT_AC97;
435 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
436 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
437 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 dev_err(&pdev->dev,
439 "unrecognized fsl,mode property '%s'\n", sprop);
Timur Tabi17467f22008-01-11 18:15:26 +0100440 ret = -EINVAL;
441 goto error;
442 }
443
444 if (!machine_data->clk_frequency) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000445 dev_err(&pdev->dev, "unknown clock frequency\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100446 ret = -EINVAL;
447 goto error;
448 }
449
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000450 /* Find the playback DMA channel to use. */
451 machine_data->dai[0].platform_name = machine_data->platform_name[0];
452 ret = get_dma_channel(np, "fsl,playback-dma", &machine_data->dai[0],
453 &machine_data->dma_channel_id[0],
454 &machine_data->dma_id[0]);
Timur Tabi17467f22008-01-11 18:15:26 +0100455 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100457 goto error;
458 }
459
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000460 /* Find the capture DMA channel to use. */
461 machine_data->dai[1].platform_name = machine_data->platform_name[1];
462 ret = get_dma_channel(np, "fsl,capture-dma", &machine_data->dai[1],
463 &machine_data->dma_channel_id[1],
464 &machine_data->dma_id[1]);
465 if (ret) {
466 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100467 goto error;
468 }
469
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470 /* Initialize our DAI data structure. */
471 machine_data->dai[0].stream_name = "playback";
472 machine_data->dai[1].stream_name = "capture";
473 machine_data->dai[0].name = machine_data->dai[0].stream_name;
474 machine_data->dai[1].name = machine_data->dai[1].stream_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 machine_data->card.probe = mpc8610_hpcd_machine_probe;
477 machine_data->card.remove = mpc8610_hpcd_machine_remove;
478 machine_data->card.name = pdev->name; /* The platform driver name */
479 machine_data->card.num_links = 2;
480 machine_data->card.dai_link = machine_data->dai;
Timur Tabi17467f22008-01-11 18:15:26 +0100481
482 /* Allocate a new audio platform device structure */
483 sound_device = platform_device_alloc("soc-audio", -1);
484 if (!sound_device) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000485 dev_err(&pdev->dev, "platform device alloc failed\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100486 ret = -ENOMEM;
487 goto error;
488 }
489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 /* Associate the card data with the sound device */
491 platform_set_drvdata(sound_device, &machine_data->card);
Timur Tabi17467f22008-01-11 18:15:26 +0100492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 /* Register with ASoC */
Timur Tabi17467f22008-01-11 18:15:26 +0100494 ret = platform_device_add(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100495 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 dev_err(&pdev->dev, "platform device add failed\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100497 goto error;
498 }
499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100501
502 return 0;
503
504error:
505 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100506
507 if (sound_device)
508 platform_device_unregister(sound_device);
509
Timur Tabi17467f22008-01-11 18:15:26 +0100510 kfree(machine_data);
511
512 return ret;
513}
514
515/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 * mpc8610_hpcd_remove: remove the platform device
Timur Tabi17467f22008-01-11 18:15:26 +0100517 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 * This function is called when the platform device is removed.
Timur Tabi17467f22008-01-11 18:15:26 +0100519 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100521{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
523 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100524 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi17467f22008-01-11 18:15:26 +0100526
527 platform_device_unregister(sound_device);
528
Timur Tabi17467f22008-01-11 18:15:26 +0100529 kfree(machine_data);
530 sound_device->dev.platform_data = NULL;
531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 dev_set_drvdata(&pdev->dev, NULL);
Timur Tabi17467f22008-01-11 18:15:26 +0100533
534 return 0;
535}
536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537static struct platform_driver mpc8610_hpcd_driver = {
538 .probe = mpc8610_hpcd_probe,
539 .remove = __devexit_p(mpc8610_hpcd_remove),
Grant Likely40182942010-04-13 16:13:02 -0700540 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 /* The name must match the 'model' property in the device tree,
542 * in lowercase letters.
543 */
544 .name = "snd-soc-mpc8610hpcd",
Grant Likely40182942010-04-13 16:13:02 -0700545 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700546 },
Timur Tabi17467f22008-01-11 18:15:26 +0100547};
548
549/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 * mpc8610_hpcd_init: machine driver initialization.
Timur Tabi17467f22008-01-11 18:15:26 +0100551 *
552 * This function is called when this module is loaded.
553 */
554static int __init mpc8610_hpcd_init(void)
555{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 struct device_node *guts_np;
557 struct resource res;
Timur Tabi17467f22008-01-11 18:15:26 +0100558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 /* Get the physical address of the global utilities registers */
562 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
563 if (of_address_to_resource(guts_np, 0, &res)) {
564 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
565 return -EINVAL;
566 }
567 guts_phys = res.start;
Timur Tabi17467f22008-01-11 18:15:26 +0100568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 return platform_driver_register(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100570}
571
572/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 * mpc8610_hpcd_exit: machine driver exit
Timur Tabi17467f22008-01-11 18:15:26 +0100574 *
575 * This function is called when this driver is unloaded.
576 */
577static void __exit mpc8610_hpcd_exit(void)
578{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 platform_driver_unregister(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100580}
581
582module_init(mpc8610_hpcd_init);
583module_exit(mpc8610_hpcd_exit);
584
585MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
587MODULE_LICENSE("GPL v2");