blob: 0ea4a5a96e06533e53ae95e5bebd60cdc16e7832 [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 Tabi61327252011-12-01 19:52:47 -060017#include <linux/of_i2c.h>
Timur Tabi17467f22008-01-11 18:15:26 +010018#include <sound/soc.h>
Timur Tabi6e6f6622010-07-22 11:33:30 -050019#include <asm/fsl_guts.h>
Timur Tabi17467f22008-01-11 18:15:26 +010020
Timur Tabi17467f22008-01-11 18:15:26 +010021#include "fsl_dma.h"
22#include "fsl_ssi.h"
23
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000024/* There's only one global utilities register */
25static phys_addr_t guts_phys;
26
27#define DAI_NAME_SIZE 32
28
Timur Tabi17467f22008-01-11 18:15:26 +010029/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000030 * mpc8610_hpcd_data: machine-specific ASoC device data
Timur Tabi17467f22008-01-11 18:15:26 +010031 *
32 * This structure contains data for a single sound platform device on an
33 * MPC8610 HPCD. Some of the data is taken from the device tree.
34 */
35struct mpc8610_hpcd_data {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000036 struct snd_soc_dai_link dai[2];
37 struct snd_soc_card card;
Timur Tabi17467f22008-01-11 18:15:26 +010038 unsigned int dai_format;
39 unsigned int codec_clk_direction;
40 unsigned int cpu_clk_direction;
41 unsigned int clk_frequency;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042 unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
43 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
Timur Tabi17467f22008-01-11 18:15:26 +010044 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045 char codec_dai_name[DAI_NAME_SIZE];
46 char codec_name[DAI_NAME_SIZE];
47 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
Timur Tabi17467f22008-01-11 18:15:26 +010048};
49
50/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051 * mpc8610_hpcd_machine_probe: initialize the board
Timur Tabi17467f22008-01-11 18:15:26 +010052 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000053 * This function is used to initialize the board-specific hardware.
Timur Tabi17467f22008-01-11 18:15:26 +010054 *
55 * Here we program the DMACR and PMUXCR registers.
56 */
Mark Browne7361ec2011-01-26 14:17:20 +000057static int mpc8610_hpcd_machine_probe(struct snd_soc_card *card)
Timur Tabi17467f22008-01-11 18:15:26 +010058{
59 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 */
Mark Browne7361ec2011-01-26 14:17:20 +0000141static int mpc8610_hpcd_machine_remove(struct snd_soc_card *card)
Timur Tabi17467f22008-01-11 18:15:26 +0100142{
143 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
Timur Tabi147dfe92011-06-08 15:02:55 -0500237 return be32_to_cpup(iprop);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000238}
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;
Timur Tabi61327252011-12-01 19:52:47 -0600253 int addr;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000254 char temp[DAI_NAME_SIZE];
Timur Tabi61327252011-12-01 19:52:47 -0600255 struct i2c_client *i2c;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000256
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
Timur Tabi147dfe92011-06-08 15:02:55 -0500263 addr = be32_to_cpup(iprop);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264
Timur Tabi61327252011-12-01 19:52:47 -0600265 /* We need the adapter number */
266 i2c = of_find_i2c_device_by_node(np);
267 if (!i2c)
268 return -ENODEV;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000269
Timur Tabi61327252011-12-01 19:52:47 -0600270 snprintf(buf, len, "%s-codec.%u-%04x", temp, i2c->adapter->nr, addr);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000271
272 return 0;
273}
274
275static int get_dma_channel(struct device_node *ssi_np,
276 const char *compatible,
277 struct snd_soc_dai_link *dai,
278 unsigned int *dma_channel_id,
279 unsigned int *dma_id)
280{
281 struct resource res;
282 struct device_node *dma_channel_np;
283 const u32 *iprop;
284 int ret;
285
286 dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
287 "fsl,ssi-dma-channel");
288 if (!dma_channel_np)
289 return -EINVAL;
290
291 /* Determine the dev_name for the device_node. This code mimics the
292 * behavior of of_device_make_bus_id(). We need this because ASoC uses
293 * the dev_name() of the device to match the platform (DMA) device with
294 * the CPU (SSI) device. It's all ugly and hackish, but it works (for
295 * now).
296 *
297 * dai->platform name should already point to an allocated buffer.
298 */
299 ret = of_address_to_resource(dma_channel_np, 0, &res);
300 if (ret)
301 return ret;
302 snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
303 (unsigned long long) res.start, dma_channel_np->name);
304
305 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
306 if (!iprop) {
307 of_node_put(dma_channel_np);
308 return -EINVAL;
309 }
310
Timur Tabi147dfe92011-06-08 15:02:55 -0500311 *dma_channel_id = be32_to_cpup(iprop);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000312 *dma_id = get_parent_cell_index(dma_channel_np);
313 of_node_put(dma_channel_np);
314
315 return 0;
316}
317
318/**
319 * mpc8610_hpcd_probe: platform probe function for the machine driver
320 *
321 * Although this is a machine driver, the SSI node is the "master" node with
Timur Tabi17467f22008-01-11 18:15:26 +0100322 * respect to audio hardware connections. Therefore, we create a new ASoC
323 * device for each new SSI node that has a codec attached.
Timur Tabi17467f22008-01-11 18:15:26 +0100324 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000325static int mpc8610_hpcd_probe(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100326{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000327 struct device *dev = pdev->dev.parent;
Timur Tabi38fec722010-08-19 15:26:58 -0500328 /* ssi_pdev is the platform device for the SSI node that probed us */
329 struct platform_device *ssi_pdev =
330 container_of(dev, struct platform_device, dev);
331 struct device_node *np = ssi_pdev->dev.of_node;
Timur Tabi17467f22008-01-11 18:15:26 +0100332 struct device_node *codec_np = NULL;
Timur Tabi17467f22008-01-11 18:15:26 +0100333 struct platform_device *sound_device = NULL;
334 struct mpc8610_hpcd_data *machine_data;
Timur Tabi17467f22008-01-11 18:15:26 +0100335 int ret = -ENODEV;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336 const char *sprop;
337 const u32 *iprop;
338
339 /* We are only interested in SSIs with a codec phandle in them,
340 * so let's make sure this SSI has one. The MPC8610 HPCD only
341 * knows about the CS4270 codec, so reject anything else.
342 */
343 codec_np = get_node_by_phandle_name(np, "codec-handle",
344 "cirrus,cs4270");
345 if (!codec_np) {
346 dev_err(dev, "invalid codec node\n");
347 return -EINVAL;
348 }
Timur Tabi17467f22008-01-11 18:15:26 +0100349
350 machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
Julia Lawallc09f5ca2011-08-20 09:02:01 +0200351 if (!machine_data) {
352 ret = -ENOMEM;
353 goto error_alloc;
354 }
Timur Tabi17467f22008-01-11 18:15:26 +0100355
Timur Tabi38fec722010-08-19 15:26:58 -0500356 machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000357 machine_data->dai[0].ops = &mpc8610_hpcd_ops;
Timur Tabi17467f22008-01-11 18:15:26 +0100358
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000359 /* Determine the codec name, it will be used as the codec DAI name */
360 ret = codec_node_dev_name(codec_np, machine_data->codec_name,
361 DAI_NAME_SIZE);
362 if (ret) {
363 dev_err(&pdev->dev, "invalid codec node %s\n",
364 codec_np->full_name);
365 ret = -EINVAL;
366 goto error;
367 }
368 machine_data->dai[0].codec_name = machine_data->codec_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100369
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000370 /* The DAI name from the codec (snd_soc_dai_driver.name) */
371 machine_data->dai[0].codec_dai_name = "cs4270-hifi";
372
373 /* We register two DAIs per SSI, one for playback and the other for
374 * capture. Currently, we only support codecs that have one DAI for
375 * both playback and capture.
Timur Tabi17467f22008-01-11 18:15:26 +0100376 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 memcpy(&machine_data->dai[1], &machine_data->dai[0],
378 sizeof(struct snd_soc_dai_link));
Timur Tabi17467f22008-01-11 18:15:26 +0100379
380 /* Get the device ID */
381 iprop = of_get_property(np, "cell-index", NULL);
382 if (!iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383 dev_err(&pdev->dev, "cell-index property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100384 ret = -EINVAL;
385 goto error;
386 }
Timur Tabi147dfe92011-06-08 15:02:55 -0500387 machine_data->ssi_id = be32_to_cpup(iprop);
Timur Tabi17467f22008-01-11 18:15:26 +0100388
389 /* Get the serial format and clock direction. */
390 sprop = of_get_property(np, "fsl,mode", NULL);
391 if (!sprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000392 dev_err(&pdev->dev, "fsl,mode property not found\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100393 ret = -EINVAL;
394 goto error;
395 }
396
397 if (strcasecmp(sprop, "i2s-slave") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600398 machine_data->dai_format =
399 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
Timur Tabi17467f22008-01-11 18:15:26 +0100400 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
401 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
402
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000403 /* In i2s-slave mode, the codec has its own clock source, so we
Timur Tabi17467f22008-01-11 18:15:26 +0100404 * need to get the frequency from the device tree and pass it to
405 * the codec driver.
406 */
407 iprop = of_get_property(codec_np, "clock-frequency", NULL);
408 if (!iprop || !*iprop) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409 dev_err(&pdev->dev, "codec bus-frequency "
410 "property is missing or invalid\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100411 ret = -EINVAL;
412 goto error;
413 }
Timur Tabi147dfe92011-06-08 15:02:55 -0500414 machine_data->clk_frequency = be32_to_cpup(iprop);
Timur Tabi17467f22008-01-11 18:15:26 +0100415 } else if (strcasecmp(sprop, "i2s-master") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600416 machine_data->dai_format =
417 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
Timur Tabi17467f22008-01-11 18:15:26 +0100418 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
419 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
420 } else if (strcasecmp(sprop, "lj-slave") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600421 machine_data->dai_format =
422 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
Timur Tabi17467f22008-01-11 18:15:26 +0100423 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
424 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
425 } else if (strcasecmp(sprop, "lj-master") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600426 machine_data->dai_format =
427 SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
Timur Tabi17467f22008-01-11 18:15:26 +0100428 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
429 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
Roel Kluine5c21572008-02-22 18:41:41 +0100430 } else if (strcasecmp(sprop, "rj-slave") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600431 machine_data->dai_format =
432 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
Timur Tabi17467f22008-01-11 18:15:26 +0100433 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
434 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
435 } else if (strcasecmp(sprop, "rj-master") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600436 machine_data->dai_format =
437 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
Timur Tabi17467f22008-01-11 18:15:26 +0100438 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
439 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
440 } else if (strcasecmp(sprop, "ac97-slave") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600441 machine_data->dai_format =
442 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
Timur Tabi17467f22008-01-11 18:15:26 +0100443 machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
444 machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
445 } else if (strcasecmp(sprop, "ac97-master") == 0) {
Timur Tabi380c8832011-11-22 14:38:59 -0600446 machine_data->dai_format =
447 SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
Timur Tabi17467f22008-01-11 18:15:26 +0100448 machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
449 machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
450 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 dev_err(&pdev->dev,
452 "unrecognized fsl,mode property '%s'\n", sprop);
Timur Tabi17467f22008-01-11 18:15:26 +0100453 ret = -EINVAL;
454 goto error;
455 }
456
457 if (!machine_data->clk_frequency) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458 dev_err(&pdev->dev, "unknown clock frequency\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100459 ret = -EINVAL;
460 goto error;
461 }
462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 /* Find the playback DMA channel to use. */
464 machine_data->dai[0].platform_name = machine_data->platform_name[0];
465 ret = get_dma_channel(np, "fsl,playback-dma", &machine_data->dai[0],
466 &machine_data->dma_channel_id[0],
467 &machine_data->dma_id[0]);
Timur Tabi17467f22008-01-11 18:15:26 +0100468 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100470 goto error;
471 }
472
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473 /* Find the capture DMA channel to use. */
474 machine_data->dai[1].platform_name = machine_data->platform_name[1];
475 ret = get_dma_channel(np, "fsl,capture-dma", &machine_data->dai[1],
476 &machine_data->dma_channel_id[1],
477 &machine_data->dma_id[1]);
478 if (ret) {
479 dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100480 goto error;
481 }
482
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483 /* Initialize our DAI data structure. */
484 machine_data->dai[0].stream_name = "playback";
485 machine_data->dai[1].stream_name = "capture";
486 machine_data->dai[0].name = machine_data->dai[0].stream_name;
487 machine_data->dai[1].name = machine_data->dai[1].stream_name;
Timur Tabi17467f22008-01-11 18:15:26 +0100488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 machine_data->card.probe = mpc8610_hpcd_machine_probe;
490 machine_data->card.remove = mpc8610_hpcd_machine_remove;
491 machine_data->card.name = pdev->name; /* The platform driver name */
492 machine_data->card.num_links = 2;
493 machine_data->card.dai_link = machine_data->dai;
Timur Tabi17467f22008-01-11 18:15:26 +0100494
495 /* Allocate a new audio platform device structure */
496 sound_device = platform_device_alloc("soc-audio", -1);
497 if (!sound_device) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 dev_err(&pdev->dev, "platform device alloc failed\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100499 ret = -ENOMEM;
500 goto error;
501 }
502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 /* Associate the card data with the sound device */
504 platform_set_drvdata(sound_device, &machine_data->card);
Timur Tabi17467f22008-01-11 18:15:26 +0100505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 /* Register with ASoC */
Timur Tabi17467f22008-01-11 18:15:26 +0100507 ret = platform_device_add(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100508 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 dev_err(&pdev->dev, "platform device add failed\n");
Julia Lawallc09f5ca2011-08-20 09:02:01 +0200510 goto error_sound;
Timur Tabi17467f22008-01-11 18:15:26 +0100511 }
Axel Lin67bd4892010-11-29 14:54:58 +0800512 dev_set_drvdata(&pdev->dev, sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100515
516 return 0;
517
Julia Lawallc09f5ca2011-08-20 09:02:01 +0200518error_sound:
Axel Lind890a1a2011-09-20 15:09:00 +0800519 platform_device_put(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100520error:
Timur Tabi17467f22008-01-11 18:15:26 +0100521 kfree(machine_data);
Julia Lawallc09f5ca2011-08-20 09:02:01 +0200522error_alloc:
523 of_node_put(codec_np);
Timur Tabi17467f22008-01-11 18:15:26 +0100524 return ret;
525}
526
527/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 * mpc8610_hpcd_remove: remove the platform device
Timur Tabi17467f22008-01-11 18:15:26 +0100529 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 * This function is called when the platform device is removed.
Timur Tabi17467f22008-01-11 18:15:26 +0100531 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +0100533{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
535 struct snd_soc_card *card = platform_get_drvdata(sound_device);
Timur Tabi17467f22008-01-11 18:15:26 +0100536 struct mpc8610_hpcd_data *machine_data =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 container_of(card, struct mpc8610_hpcd_data, card);
Timur Tabi17467f22008-01-11 18:15:26 +0100538
539 platform_device_unregister(sound_device);
540
Timur Tabi17467f22008-01-11 18:15:26 +0100541 kfree(machine_data);
542 sound_device->dev.platform_data = NULL;
543
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 dev_set_drvdata(&pdev->dev, NULL);
Timur Tabi17467f22008-01-11 18:15:26 +0100545
546 return 0;
547}
548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549static struct platform_driver mpc8610_hpcd_driver = {
550 .probe = mpc8610_hpcd_probe,
551 .remove = __devexit_p(mpc8610_hpcd_remove),
Grant Likely40182942010-04-13 16:13:02 -0700552 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 /* The name must match the 'model' property in the device tree,
554 * in lowercase letters.
555 */
556 .name = "snd-soc-mpc8610hpcd",
Grant Likely40182942010-04-13 16:13:02 -0700557 .owner = THIS_MODULE,
Grant Likely40182942010-04-13 16:13:02 -0700558 },
Timur Tabi17467f22008-01-11 18:15:26 +0100559};
560
561/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 * mpc8610_hpcd_init: machine driver initialization.
Timur Tabi17467f22008-01-11 18:15:26 +0100563 *
564 * This function is called when this module is loaded.
565 */
566static int __init mpc8610_hpcd_init(void)
567{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 struct device_node *guts_np;
569 struct resource res;
Timur Tabi17467f22008-01-11 18:15:26 +0100570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
Timur Tabi17467f22008-01-11 18:15:26 +0100572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 /* Get the physical address of the global utilities registers */
574 guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
575 if (of_address_to_resource(guts_np, 0, &res)) {
576 pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
577 return -EINVAL;
578 }
579 guts_phys = res.start;
Timur Tabi17467f22008-01-11 18:15:26 +0100580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 return platform_driver_register(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100582}
583
584/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 * mpc8610_hpcd_exit: machine driver exit
Timur Tabi17467f22008-01-11 18:15:26 +0100586 *
587 * This function is called when this driver is unloaded.
588 */
589static void __exit mpc8610_hpcd_exit(void)
590{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 platform_driver_unregister(&mpc8610_hpcd_driver);
Timur Tabi17467f22008-01-11 18:15:26 +0100592}
593
594module_init(mpc8610_hpcd_init);
595module_exit(mpc8610_hpcd_exit);
596
597MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
599MODULE_LICENSE("GPL v2");