mpc5200_psc_ac97.c 8.7 KB
Newer Older
J
Jon Smirl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.
 *
 * Copyright (C) 2009 Jon Smirl, Digispeaker
 * Author: Jon Smirl <jonsmirl@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
15
#include <linux/delay.h>
J
Jon Smirl 已提交
16 17 18 19 20 21 22

#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>

#include <asm/time.h>
#include <asm/delay.h>
23
#include <asm/mpc52xx.h>
J
Jon Smirl 已提交
24 25 26 27 28 29 30 31 32 33 34 35
#include <asm/mpc52xx_psc.h>

#include "mpc5200_dma.h"
#include "mpc5200_psc_ac97.h"

#define DRV_NAME "mpc5200-psc-ac97"

/* ALSA only supports a single AC97 device so static is recommend here */
static struct psc_dma *psc_dma;

static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
{
36
	int status;
J
Jon Smirl 已提交
37 38
	unsigned int val;

39 40
	mutex_lock(&psc_dma->mutex);

J
Jon Smirl 已提交
41
	/* Wait for command send status zero = ready */
42 43 44
	status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
				MPC52xx_PSC_SR_CMDSEND), 100, 0);
	if (status == 0) {
J
Jon Smirl 已提交
45
		pr_err("timeout on ac97 bus (rdy)\n");
46
		mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
47 48
		return -ENODEV;
	}
49 50 51 52

	/* Force clear the data valid bit */
	in_be32(&psc_dma->psc_regs->ac97_data);

J
Jon Smirl 已提交
53 54 55 56
	/* Send the read */
	out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));

	/* Wait for the answer */
57 58 59
	status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &
				MPC52xx_PSC_SR_DATA_VAL), 100, 0);
	if (status == 0) {
J
Jon Smirl 已提交
60 61
		pr_err("timeout on ac97 read (val) %x\n",
				in_be16(&psc_dma->psc_regs->sr_csr.status));
62
		mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
63 64 65 66 67 68
		return -ENODEV;
	}
	/* Get the data */
	val = in_be32(&psc_dma->psc_regs->ac97_data);
	if (((val >> 24) & 0x7f) != reg) {
		pr_err("reg echo error on ac97 read\n");
69
		mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
70 71 72 73
		return -ENODEV;
	}
	val = (val >> 8) & 0xffff;

74
	mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
75 76 77 78 79 80
	return (unsigned short) val;
}

static void psc_ac97_write(struct snd_ac97 *ac97,
				unsigned short reg, unsigned short val)
{
81
	int status;
J
Jon Smirl 已提交
82

83 84
	mutex_lock(&psc_dma->mutex);

J
Jon Smirl 已提交
85
	/* Wait for command status zero = ready */
86 87 88
	status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &
				MPC52xx_PSC_SR_CMDSEND), 100, 0);
	if (status == 0) {
J
Jon Smirl 已提交
89
		pr_err("timeout on ac97 bus (write)\n");
90
		goto out;
J
Jon Smirl 已提交
91 92 93 94
	}
	/* Write data */
	out_be32(&psc_dma->psc_regs->ac97_cmd,
			((reg & 0x7f) << 24) | (val << 8));
95 96 97

 out:
	mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
98 99 100 101 102 103
}

static void psc_ac97_warm_reset(struct snd_ac97 *ac97)
{
	struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;

104 105
	mutex_lock(&psc_dma->mutex);

J
Jon Smirl 已提交
106
	out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);
107
	udelay(3);
J
Jon Smirl 已提交
108
	out_be32(&regs->sicr, psc_dma->sicr);
109 110

	mutex_unlock(&psc_dma->mutex);
J
Jon Smirl 已提交
111 112 113 114 115 116
}

static void psc_ac97_cold_reset(struct snd_ac97 *ac97)
{
	struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;

117 118 119 120 121 122 123 124 125 126 127 128 129
	mutex_lock(&psc_dma->mutex);
	dev_dbg(psc_dma->dev, "cold reset\n");

	mpc5200_psc_ac97_gpio_reset(psc_dma->id);

	/* Notify the PSC that a reset has occurred */
	out_be32(&regs->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_ACRB);

	/* Re-enable RX and TX */
	out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);

	mutex_unlock(&psc_dma->mutex);

130
	msleep(1);
J
Jon Smirl 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	psc_ac97_warm_reset(ac97);
}

struct snd_ac97_bus_ops soc_ac97_ops = {
	.read		= psc_ac97_read,
	.write		= psc_ac97_write,
	.reset		= psc_ac97_cold_reset,
	.warm_reset	= psc_ac97_warm_reset,
};
EXPORT_SYMBOL_GPL(soc_ac97_ops);

static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *cpu_dai)
{
146
	struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
147
	struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
J
Jon Smirl 已提交
148 149 150 151 152 153 154 155 156 157

	dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"
		" periods=%i buffer_size=%i  buffer_bytes=%i channels=%i"
		" rate=%i format=%i\n",
		__func__, substream, params_period_size(params),
		params_period_bytes(params), params_periods(params),
		params_buffer_size(params), params_buffer_bytes(params),
		params_channels(params), params_rate(params),
		params_format(params));

158 159 160 161
	/* Determine the set of enable bits to turn on */
	s->ac97_slot_bits = (params_channels(params) == 1) ? 0x100 : 0x300;
	if (substream->pstr->stream != SNDRV_PCM_STREAM_CAPTURE)
		s->ac97_slot_bits <<= 16;
J
Jon Smirl 已提交
162 163 164 165 166 167 168
	return 0;
}

static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *cpu_dai)
{
169
	struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
J
Jon Smirl 已提交
170

171 172
	dev_dbg(psc_dma->dev, "%s(substream=%p)\n", __func__, substream);

J
Jon Smirl 已提交
173 174 175 176 177 178 179 180 181 182 183
	if (params_channels(params) == 1)
		out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);
	else
		out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);

	return 0;
}

static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
							struct snd_soc_dai *dai)
{
184
	struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(dai);
185
	struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
J
Jon Smirl 已提交
186 187

	switch (cmd) {
188 189 190 191 192 193 194 195 196
	case SNDRV_PCM_TRIGGER_START:
		dev_dbg(psc_dma->dev, "AC97 START: stream=%i\n",
			substream->pstr->stream);

		/* Set the slot enable bits */
		psc_dma->slots |= s->ac97_slot_bits;
		out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
		break;

J
Jon Smirl 已提交
197
	case SNDRV_PCM_TRIGGER_STOP:
198 199
		dev_dbg(psc_dma->dev, "AC97 STOP: stream=%i\n",
			substream->pstr->stream);
J
Jon Smirl 已提交
200

201 202
		/* Clear the slot enable bits */
		psc_dma->slots &= ~(s->ac97_slot_bits);
J
Jon Smirl 已提交
203 204 205 206 207 208
		out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);
		break;
	}
	return 0;
}

209
static int psc_ac97_probe(struct snd_soc_dai *cpu_dai)
J
Jon Smirl 已提交
210
{
211
	struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);
J
Jon Smirl 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;

	/* Go */
	out_8(&regs->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
	return 0;
}

/* ---------------------------------------------------------------------
 * ALSA SoC Bindings
 *
 * - Digital Audio Interface (DAI) template
 * - create/destroy dai hooks
 */

/**
 * psc_ac97_dai_template: template CPU Digital Audio Interface
 */
229
static const struct snd_soc_dai_ops psc_ac97_analog_ops = {
J
Jon Smirl 已提交
230 231 232 233
	.hw_params	= psc_ac97_hw_analog_params,
	.trigger	= psc_ac97_trigger,
};

234
static const struct snd_soc_dai_ops psc_ac97_digital_ops = {
J
Jon Smirl 已提交
235 236 237
	.hw_params	= psc_ac97_hw_digital_params,
};

238
static struct snd_soc_dai_driver psc_ac97_dai[] = {
J
Jon Smirl 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
{
	.ac97_control = 1,
	.probe	= psc_ac97_probe,
	.playback = {
		.channels_min   = 1,
		.channels_max   = 6,
		.rates          = SNDRV_PCM_RATE_8000_48000,
		.formats = SNDRV_PCM_FMTBIT_S32_BE,
	},
	.capture = {
		.channels_min   = 1,
		.channels_max   = 2,
		.rates          = SNDRV_PCM_RATE_8000_48000,
		.formats = SNDRV_PCM_FMTBIT_S32_BE,
	},
	.ops = &psc_ac97_analog_ops,
},
{
	.ac97_control = 1,
	.playback = {
		.channels_min   = 1,
		.channels_max   = 2,
		.rates          = SNDRV_PCM_RATE_32000 | \
			SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
		.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
	},
	.ops = &psc_ac97_digital_ops,
} };



/* ---------------------------------------------------------------------
 * OF platform bus binding code:
 * - Probe/remove operations
 * - OF device match table
 */
275
static int __devinit psc_ac97_of_probe(struct platform_device *op)
J
Jon Smirl 已提交
276
{
277
	int rc;
J
Jon Smirl 已提交
278 279 280
	struct snd_ac97 ac97;
	struct mpc52xx_psc __iomem *regs;

281 282 283 284
	rc = mpc5200_audio_dma_create(op);
	if (rc != 0)
		return rc;

285
	rc = snd_soc_register_dais(&op->dev, psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));
J
Jon Smirl 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	if (rc != 0) {
		dev_err(&op->dev, "Failed to register DAI\n");
		return rc;
	}

	psc_dma = dev_get_drvdata(&op->dev);
	regs = psc_dma->psc_regs;
	ac97.private_data = psc_dma;

	psc_dma->imr = 0;
	out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);

	/* Configure the serial interface mode to AC97 */
	psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;
	out_be32(&regs->sicr, psc_dma->sicr);

	/* No slots active */
	out_be32(&regs->ac97_slots, 0x00000000);

	return 0;
}

308
static int __devexit psc_ac97_of_remove(struct platform_device *op)
J
Jon Smirl 已提交
309
{
310
	mpc5200_audio_dma_destroy(op);
311 312
	snd_soc_unregister_dais(&op->dev, ARRAY_SIZE(psc_ac97_dai));
	return 0;
J
Jon Smirl 已提交
313 314 315 316 317 318 319 320 321 322
}

/* Match table for of_platform binding */
static struct of_device_id psc_ac97_match[] __devinitdata = {
	{ .compatible = "fsl,mpc5200-psc-ac97", },
	{ .compatible = "fsl,mpc5200b-psc-ac97", },
	{}
};
MODULE_DEVICE_TABLE(of, psc_ac97_match);

323
static struct platform_driver psc_ac97_driver = {
J
Jon Smirl 已提交
324 325 326 327 328
	.probe = psc_ac97_of_probe,
	.remove = __devexit_p(psc_ac97_of_remove),
	.driver = {
		.name = "mpc5200-psc-ac97",
		.owner = THIS_MODULE,
329
		.of_match_table = psc_ac97_match,
J
Jon Smirl 已提交
330 331 332
	},
};

333
module_platform_driver(psc_ac97_driver);
J
Jon Smirl 已提交
334 335 336 337 338

MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
MODULE_DESCRIPTION("mpc5200 AC97 module");
MODULE_LICENSE("GPL");