mpc8610_hpcd.c 17.2 KB
Newer Older
1
/**
2
 * Freescale MPC8610HPCD ALSA SoC Machine driver
3 4 5
 *
 * Author: Timur Tabi <timur@freescale.com>
 *
6 7 8 9 10
 * Copyright 2007-2010 Freescale Semiconductor, Inc.
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
11 12 13 14 15
 */

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/of_device.h>
16
#include <linux/slab.h>
17
#include <sound/soc.h>
18
#include <asm/fsl_guts.h>
19 20 21 22

#include "fsl_dma.h"
#include "fsl_ssi.h"

23 24 25 26 27
/* There's only one global utilities register */
static phys_addr_t guts_phys;

#define DAI_NAME_SIZE	32

28
/**
29
 * mpc8610_hpcd_data: machine-specific ASoC device data
30 31 32 33 34
 *
 * This structure contains data for a single sound platform device on an
 * MPC8610 HPCD.  Some of the data is taken from the device tree.
 */
struct mpc8610_hpcd_data {
35 36
	struct snd_soc_dai_link dai[2];
	struct snd_soc_card card;
37 38 39 40
	unsigned int dai_format;
	unsigned int codec_clk_direction;
	unsigned int cpu_clk_direction;
	unsigned int clk_frequency;
41 42
	unsigned int ssi_id;		/* 0 = SSI1, 1 = SSI2, etc */
	unsigned int dma_id[2];		/* 0 = DMA1, 1 = DMA2, etc */
43
	unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
44 45 46
	char codec_dai_name[DAI_NAME_SIZE];
	char codec_name[DAI_NAME_SIZE];
	char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
47 48 49
};

/**
50
 * mpc8610_hpcd_machine_probe: initialize the board
51
 *
52
 * This function is used to initialize the board-specific hardware.
53 54 55
 *
 * Here we program the DMACR and PMUXCR registers.
 */
56
static int mpc8610_hpcd_machine_probe(struct snd_soc_card *card)
57 58
{
	struct mpc8610_hpcd_data *machine_data =
59
		container_of(card, struct mpc8610_hpcd_data, card);
60
	struct ccsr_guts_86xx __iomem *guts;
61

62
	guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
63 64 65 66
	if (!guts) {
		dev_err(card->dev, "could not map global utilities\n");
		return -ENOMEM;
	}
67

68 69 70 71 72 73 74 75 76 77 78 79
	/* Program the signal routing between the SSI and the DMA */
	guts_set_dmacr(guts, machine_data->dma_id[0],
		       machine_data->dma_channel_id[0],
		       CCSR_GUTS_DMACR_DEV_SSI);
	guts_set_dmacr(guts, machine_data->dma_id[1],
		       machine_data->dma_channel_id[1],
		       CCSR_GUTS_DMACR_DEV_SSI);

	guts_set_pmuxcr_dma(guts, machine_data->dma_id[0],
			    machine_data->dma_channel_id[0], 0);
	guts_set_pmuxcr_dma(guts, machine_data->dma_id[1],
			    machine_data->dma_channel_id[1], 0);
80 81 82

	switch (machine_data->ssi_id) {
	case 0:
83
		clrsetbits_be32(&guts->pmuxcr,
84 85 86
			CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_SSI);
		break;
	case 1:
87
		clrsetbits_be32(&guts->pmuxcr,
88 89 90 91
			CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_SSI);
		break;
	}

92 93
	iounmap(guts);

94 95 96 97 98 99 100 101 102 103 104 105 106 107
	return 0;
}

/**
 * mpc8610_hpcd_startup: program the board with various hardware parameters
 *
 * This function takes board-specific information, like clock frequencies
 * and serial data formats, and passes that information to the codec and
 * transport drivers.
 */
static int mpc8610_hpcd_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct mpc8610_hpcd_data *machine_data =
108 109
		container_of(rtd->card, struct mpc8610_hpcd_data, card);
	struct device *dev = rtd->card->dev;
110 111 112
	int ret = 0;

	/* Tell the codec driver what the serial protocol is. */
113
	ret = snd_soc_dai_set_fmt(rtd->codec_dai, machine_data->dai_format);
114
	if (ret < 0) {
115
		dev_err(dev, "could not set codec driver audio format\n");
116
		return ret;
117 118 119 120 121 122
	}

	/*
	 * Tell the codec driver what the MCLK frequency is, and whether it's
	 * a slave or master.
	 */
123 124 125
	ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0,
				     machine_data->clk_frequency,
				     machine_data->codec_clk_direction);
126
	if (ret < 0) {
127
		dev_err(dev, "could not set codec driver clock params\n");
128
		return ret;
129 130 131 132 133 134 135 136 137 138 139
	}

	return 0;
}

/**
 * mpc8610_hpcd_machine_remove: Remove the sound device
 *
 * This function is called to remove the sound device for one SSI.  We
 * de-program the DMACR and PMUXCR register.
 */
140
static int mpc8610_hpcd_machine_remove(struct snd_soc_card *card)
141 142
{
	struct mpc8610_hpcd_data *machine_data =
143
		container_of(card, struct mpc8610_hpcd_data, card);
144
	struct ccsr_guts_86xx __iomem *guts;
145

146
	guts = ioremap(guts_phys, sizeof(struct ccsr_guts_86xx));
147 148 149 150
	if (!guts) {
		dev_err(card->dev, "could not map global utilities\n");
		return -ENOMEM;
	}
151 152 153

	/* Restore the signal routing */

154 155 156 157
	guts_set_dmacr(guts, machine_data->dma_id[0],
		       machine_data->dma_channel_id[0], 0);
	guts_set_dmacr(guts, machine_data->dma_id[1],
		       machine_data->dma_channel_id[1], 0);
158 159 160

	switch (machine_data->ssi_id) {
	case 0:
161
		clrsetbits_be32(&guts->pmuxcr,
162 163 164
			CCSR_GUTS_PMUXCR_SSI1_MASK, CCSR_GUTS_PMUXCR_SSI1_LA);
		break;
	case 1:
165
		clrsetbits_be32(&guts->pmuxcr,
166
			CCSR_GUTS_PMUXCR_SSI2_MASK, CCSR_GUTS_PMUXCR_SSI2_LA);
167 168 169
		break;
	}

170 171
	iounmap(guts);

172 173 174 175
	return 0;
}

/**
176
 * mpc8610_hpcd_ops: ASoC machine driver operations
177 178 179 180 181 182
 */
static struct snd_soc_ops mpc8610_hpcd_ops = {
	.startup = mpc8610_hpcd_startup,
};

/**
183
 * get_node_by_phandle_name - get a node by its phandle name
184
 *
185 186 187
 * This function takes a node, the name of a property in that node, and a
 * compatible string.  Assuming the property is a phandle to another node,
 * it returns that node, (optionally) if that node is compatible.
188
 *
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
 * If the property is not a phandle, or the node it points to is not compatible
 * with the specific string, then NULL is returned.
 */
static struct device_node *get_node_by_phandle_name(struct device_node *np,
					       const char *name,
					       const char *compatible)
{
	const phandle *ph;
	int len;

	ph = of_get_property(np, name, &len);
	if (!ph || (len != sizeof(phandle)))
		return NULL;

	np = of_find_node_by_phandle(*ph);
	if (!np)
		return NULL;

	if (compatible && !of_device_is_compatible(np, compatible)) {
		of_node_put(np);
		return NULL;
	}

	return np;
}

/**
 * get_parent_cell_index -- return the cell-index of the parent of a node
217
 *
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
 * Return the value of the cell-index property of the parent of the given
 * node.  This is used for DMA channel nodes that need to know the DMA ID
 * of the controller they are on.
 */
static int get_parent_cell_index(struct device_node *np)
{
	struct device_node *parent = of_get_parent(np);
	const u32 *iprop;

	if (!parent)
		return -1;

	iprop = of_get_property(parent, "cell-index", NULL);
	of_node_put(parent);

	if (!iprop)
		return -1;

236
	return be32_to_cpup(iprop);
237 238 239 240
}

/**
 * codec_node_dev_name - determine the dev_name for a codec node
241
 *
242 243 244 245 246 247
 * This function determines the dev_name for an I2C node.  This is the name
 * that would be returned by dev_name() if this device_node were part of a
 * 'struct device'  It's ugly and hackish, but it works.
 *
 * The dev_name for such devices include the bus number and I2C address. For
 * example, "cs4270-codec.0-004f".
248
 */
249
static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
250 251
{
	const u32 *iprop;
252 253 254 255 256 257 258 259 260
	int bus, addr;
	char temp[DAI_NAME_SIZE];

	of_modalias_node(np, temp, DAI_NAME_SIZE);

	iprop = of_get_property(np, "reg", NULL);
	if (!iprop)
		return -EINVAL;

261
	addr = be32_to_cpup(iprop);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	bus = get_parent_cell_index(np);
	if (bus < 0)
		return bus;

	snprintf(buf, len, "%s-codec.%u-%04x", temp, bus, addr);

	return 0;
}

static int get_dma_channel(struct device_node *ssi_np,
			   const char *compatible,
			   struct snd_soc_dai_link *dai,
			   unsigned int *dma_channel_id,
			   unsigned int *dma_id)
{
278
	struct resource res;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	struct device_node *dma_channel_np;
	const u32 *iprop;
	int ret;

	dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
						  "fsl,ssi-dma-channel");
	if (!dma_channel_np)
		return -EINVAL;

	/* Determine the dev_name for the device_node.  This code mimics the
	 * behavior of of_device_make_bus_id(). We need this because ASoC uses
	 * the dev_name() of the device to match the platform (DMA) device with
	 * the CPU (SSI) device.  It's all ugly and hackish, but it works (for
	 * now).
	 *
	 * dai->platform name should already point to an allocated buffer.
	 */
	ret = of_address_to_resource(dma_channel_np, 0, &res);
	if (ret)
		return ret;
	snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
		 (unsigned long long) res.start, dma_channel_np->name);

	iprop = of_get_property(dma_channel_np, "cell-index", NULL);
	if (!iprop) {
		of_node_put(dma_channel_np);
		return -EINVAL;
	}

308
	*dma_channel_id = be32_to_cpup(iprop);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	*dma_id = get_parent_cell_index(dma_channel_np);
	of_node_put(dma_channel_np);

	return 0;
}

/**
 * mpc8610_hpcd_probe: platform probe function for the machine driver
 *
 * Although this is a machine driver, the SSI node is the "master" node with
 * respect to audio hardware connections.  Therefore, we create a new ASoC
 * device for each new SSI node that has a codec attached.
 */
static int mpc8610_hpcd_probe(struct platform_device *pdev)
{
	struct device *dev = pdev->dev.parent;
325 326 327 328
	/* ssi_pdev is the platform device for the SSI node that probed us */
	struct platform_device *ssi_pdev =
		container_of(dev, struct platform_device, dev);
	struct device_node *np = ssi_pdev->dev.of_node;
329
	struct device_node *codec_np = NULL;
330 331 332
	struct platform_device *sound_device = NULL;
	struct mpc8610_hpcd_data *machine_data;
	int ret = -ENODEV;
333 334 335 336 337 338 339 340 341 342 343 344 345
	const char *sprop;
	const u32 *iprop;

	/* We are only interested in SSIs with a codec phandle in them,
	 * so let's make sure this SSI has one. The MPC8610 HPCD only
	 * knows about the CS4270 codec, so reject anything else.
	 */
	codec_np = get_node_by_phandle_name(np, "codec-handle",
					    "cirrus,cs4270");
	if (!codec_np) {
		dev_err(dev, "invalid codec node\n");
		return -EINVAL;
	}
346 347

	machine_data = kzalloc(sizeof(struct mpc8610_hpcd_data), GFP_KERNEL);
348 349 350 351
	if (!machine_data) {
		ret = -ENOMEM;
		goto error_alloc;
	}
352

353
	machine_data->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
354
	machine_data->dai[0].ops = &mpc8610_hpcd_ops;
355

356 357 358 359 360 361 362
	/* Determine the codec name, it will be used as the codec DAI name */
	ret = codec_node_dev_name(codec_np, machine_data->codec_name,
				  DAI_NAME_SIZE);
	if (ret) {
		dev_err(&pdev->dev, "invalid codec node %s\n",
			codec_np->full_name);
		ret = -EINVAL;
363
		goto error;
364 365
	}
	machine_data->dai[0].codec_name = machine_data->codec_name;
366

367 368
	/* The DAI name from the codec (snd_soc_dai_driver.name) */
	machine_data->dai[0].codec_dai_name = "cs4270-hifi";
369

370 371 372 373 374 375
	/* We register two DAIs per SSI, one for playback and the other for
	 * capture.  Currently, we only support codecs that have one DAI for
	 * both playback and capture.
	 */
	memcpy(&machine_data->dai[1], &machine_data->dai[0],
	       sizeof(struct snd_soc_dai_link));
376 377 378 379

	/* Get the device ID */
	iprop = of_get_property(np, "cell-index", NULL);
	if (!iprop) {
380
		dev_err(&pdev->dev, "cell-index property not found\n");
381 382 383
		ret = -EINVAL;
		goto error;
	}
384
	machine_data->ssi_id = be32_to_cpup(iprop);
385 386 387 388

	/* Get the serial format and clock direction. */
	sprop = of_get_property(np, "fsl,mode", NULL);
	if (!sprop) {
389
		dev_err(&pdev->dev, "fsl,mode property not found\n");
390 391 392 393 394
		ret = -EINVAL;
		goto error;
	}

	if (strcasecmp(sprop, "i2s-slave") == 0) {
395 396
		machine_data->dai_format =
			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM;
397 398 399
		machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;

400
		/* In i2s-slave mode, the codec has its own clock source, so we
401 402 403 404 405
		 * need to get the frequency from the device tree and pass it to
		 * the codec driver.
		 */
		iprop = of_get_property(codec_np, "clock-frequency", NULL);
		if (!iprop || !*iprop) {
406 407
			dev_err(&pdev->dev, "codec bus-frequency "
				"property is missing or invalid\n");
408 409 410
			ret = -EINVAL;
			goto error;
		}
411
		machine_data->clk_frequency = be32_to_cpup(iprop);
412
	} else if (strcasecmp(sprop, "i2s-master") == 0) {
413 414
		machine_data->dai_format =
			SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
415 416 417
		machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
	} else if (strcasecmp(sprop, "lj-slave") == 0) {
418 419
		machine_data->dai_format =
			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBM_CFM;
420 421 422
		machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
	} else if (strcasecmp(sprop, "lj-master") == 0) {
423 424
		machine_data->dai_format =
			SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_CBS_CFS;
425 426
		machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
427
	} else if (strcasecmp(sprop, "rj-slave") == 0) {
428 429
		machine_data->dai_format =
			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBM_CFM;
430 431 432
		machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
	} else if (strcasecmp(sprop, "rj-master") == 0) {
433 434
		machine_data->dai_format =
			SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_CBS_CFS;
435 436 437
		machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
	} else if (strcasecmp(sprop, "ac97-slave") == 0) {
438 439
		machine_data->dai_format =
			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBM_CFM;
440 441 442
		machine_data->codec_clk_direction = SND_SOC_CLOCK_OUT;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_IN;
	} else if (strcasecmp(sprop, "ac97-master") == 0) {
443 444
		machine_data->dai_format =
			SND_SOC_DAIFMT_AC97 | SND_SOC_DAIFMT_CBS_CFS;
445 446 447
		machine_data->codec_clk_direction = SND_SOC_CLOCK_IN;
		machine_data->cpu_clk_direction = SND_SOC_CLOCK_OUT;
	} else {
448 449
		dev_err(&pdev->dev,
			"unrecognized fsl,mode property '%s'\n", sprop);
450 451 452 453 454
		ret = -EINVAL;
		goto error;
	}

	if (!machine_data->clk_frequency) {
455
		dev_err(&pdev->dev, "unknown clock frequency\n");
456 457 458 459
		ret = -EINVAL;
		goto error;
	}

460 461 462 463 464
	/* Find the playback DMA channel to use. */
	machine_data->dai[0].platform_name = machine_data->platform_name[0];
	ret = get_dma_channel(np, "fsl,playback-dma", &machine_data->dai[0],
			      &machine_data->dma_channel_id[0],
			      &machine_data->dma_id[0]);
465
	if (ret) {
466
		dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
467 468 469
		goto error;
	}

470 471 472 473 474 475 476
	/* Find the capture DMA channel to use. */
	machine_data->dai[1].platform_name = machine_data->platform_name[1];
	ret = get_dma_channel(np, "fsl,capture-dma", &machine_data->dai[1],
			      &machine_data->dma_channel_id[1],
			      &machine_data->dma_id[1]);
	if (ret) {
		dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
477 478 479
		goto error;
	}

480 481 482 483 484
	/* Initialize our DAI data structure.  */
	machine_data->dai[0].stream_name = "playback";
	machine_data->dai[1].stream_name = "capture";
	machine_data->dai[0].name = machine_data->dai[0].stream_name;
	machine_data->dai[1].name = machine_data->dai[1].stream_name;
485

486 487 488 489 490
	machine_data->card.probe = mpc8610_hpcd_machine_probe;
	machine_data->card.remove = mpc8610_hpcd_machine_remove;
	machine_data->card.name = pdev->name; /* The platform driver name */
	machine_data->card.num_links = 2;
	machine_data->card.dai_link = machine_data->dai;
491 492 493 494

	/* Allocate a new audio platform device structure */
	sound_device = platform_device_alloc("soc-audio", -1);
	if (!sound_device) {
495
		dev_err(&pdev->dev, "platform device alloc failed\n");
496 497 498 499
		ret = -ENOMEM;
		goto error;
	}

500 501
	/* Associate the card data with the sound device */
	platform_set_drvdata(sound_device, &machine_data->card);
502

503
	/* Register with ASoC */
504 505
	ret = platform_device_add(sound_device);
	if (ret) {
506
		dev_err(&pdev->dev, "platform device add failed\n");
507
		goto error_sound;
508
	}
509
	dev_set_drvdata(&pdev->dev, sound_device);
510

511
	of_node_put(codec_np);
512 513 514

	return 0;

515
error_sound:
516
	platform_device_put(sound_device);
517 518
error:
	kfree(machine_data);
519 520
error_alloc:
	of_node_put(codec_np);
521 522 523 524
	return ret;
}

/**
525
 * mpc8610_hpcd_remove: remove the platform device
526
 *
527
 * This function is called when the platform device is removed.
528
 */
529
static int __devexit mpc8610_hpcd_remove(struct platform_device *pdev)
530
{
531 532
	struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
	struct snd_soc_card *card = platform_get_drvdata(sound_device);
533
	struct mpc8610_hpcd_data *machine_data =
534
		container_of(card, struct mpc8610_hpcd_data, card);
535 536 537 538 539 540

	platform_device_unregister(sound_device);

	kfree(machine_data);
	sound_device->dev.platform_data = NULL;

541
	dev_set_drvdata(&pdev->dev, NULL);
542 543 544 545

	return 0;
}

546 547 548
static struct platform_driver mpc8610_hpcd_driver = {
	.probe = mpc8610_hpcd_probe,
	.remove = __devexit_p(mpc8610_hpcd_remove),
549
	.driver = {
550 551 552 553
		/* The name must match the 'model' property in the device tree,
		 * in lowercase letters.
		 */
		.name = "snd-soc-mpc8610hpcd",
554 555
		.owner = THIS_MODULE,
	},
556 557 558
};

/**
559
 * mpc8610_hpcd_init: machine driver initialization.
560 561 562 563 564
 *
 * This function is called when this module is loaded.
 */
static int __init mpc8610_hpcd_init(void)
{
565 566
	struct device_node *guts_np;
	struct resource res;
567

568
	pr_info("Freescale MPC8610 HPCD ALSA SoC machine driver\n");
569

570 571 572 573 574 575 576
	/* Get the physical address of the global utilities registers */
	guts_np = of_find_compatible_node(NULL, NULL, "fsl,mpc8610-guts");
	if (of_address_to_resource(guts_np, 0, &res)) {
		pr_err("mpc8610-hpcd: missing/invalid global utilities node\n");
		return -EINVAL;
	}
	guts_phys = res.start;
577

578
	return platform_driver_register(&mpc8610_hpcd_driver);
579 580 581
}

/**
582
 * mpc8610_hpcd_exit: machine driver exit
583 584 585 586 587
 *
 * This function is called when this driver is unloaded.
 */
static void __exit mpc8610_hpcd_exit(void)
{
588
	platform_driver_unregister(&mpc8610_hpcd_driver);
589 590 591 592 593 594
}

module_init(mpc8610_hpcd_init);
module_exit(mpc8610_hpcd_exit);

MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
595 596
MODULE_DESCRIPTION("Freescale MPC8610 HPCD ALSA SoC machine driver");
MODULE_LICENSE("GPL v2");