Path: blob/master/sound/soc/fsl/mpc5200_psc_ac97.c
10817 views
/*1* linux/sound/mpc5200-ac97.c -- AC97 support for the Freescale MPC52xx chip.2*3* Copyright (C) 2009 Jon Smirl, Digispeaker4* Author: Jon Smirl <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License version 2 as8* published by the Free Software Foundation.9*/1011#include <linux/module.h>12#include <linux/of_device.h>13#include <linux/of_platform.h>14#include <linux/delay.h>1516#include <sound/pcm.h>17#include <sound/pcm_params.h>18#include <sound/soc.h>1920#include <asm/time.h>21#include <asm/delay.h>22#include <asm/mpc52xx.h>23#include <asm/mpc52xx_psc.h>2425#include "mpc5200_dma.h"26#include "mpc5200_psc_ac97.h"2728#define DRV_NAME "mpc5200-psc-ac97"2930/* ALSA only supports a single AC97 device so static is recommend here */31static struct psc_dma *psc_dma;3233static unsigned short psc_ac97_read(struct snd_ac97 *ac97, unsigned short reg)34{35int status;36unsigned int val;3738mutex_lock(&psc_dma->mutex);3940/* Wait for command send status zero = ready */41status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &42MPC52xx_PSC_SR_CMDSEND), 100, 0);43if (status == 0) {44pr_err("timeout on ac97 bus (rdy)\n");45mutex_unlock(&psc_dma->mutex);46return -ENODEV;47}4849/* Force clear the data valid bit */50in_be32(&psc_dma->psc_regs->ac97_data);5152/* Send the read */53out_be32(&psc_dma->psc_regs->ac97_cmd, (1<<31) | ((reg & 0x7f) << 24));5455/* Wait for the answer */56status = spin_event_timeout((in_be16(&psc_dma->psc_regs->sr_csr.status) &57MPC52xx_PSC_SR_DATA_VAL), 100, 0);58if (status == 0) {59pr_err("timeout on ac97 read (val) %x\n",60in_be16(&psc_dma->psc_regs->sr_csr.status));61mutex_unlock(&psc_dma->mutex);62return -ENODEV;63}64/* Get the data */65val = in_be32(&psc_dma->psc_regs->ac97_data);66if (((val >> 24) & 0x7f) != reg) {67pr_err("reg echo error on ac97 read\n");68mutex_unlock(&psc_dma->mutex);69return -ENODEV;70}71val = (val >> 8) & 0xffff;7273mutex_unlock(&psc_dma->mutex);74return (unsigned short) val;75}7677static void psc_ac97_write(struct snd_ac97 *ac97,78unsigned short reg, unsigned short val)79{80int status;8182mutex_lock(&psc_dma->mutex);8384/* Wait for command status zero = ready */85status = spin_event_timeout(!(in_be16(&psc_dma->psc_regs->sr_csr.status) &86MPC52xx_PSC_SR_CMDSEND), 100, 0);87if (status == 0) {88pr_err("timeout on ac97 bus (write)\n");89goto out;90}91/* Write data */92out_be32(&psc_dma->psc_regs->ac97_cmd,93((reg & 0x7f) << 24) | (val << 8));9495out:96mutex_unlock(&psc_dma->mutex);97}9899static void psc_ac97_warm_reset(struct snd_ac97 *ac97)100{101struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;102103mutex_lock(&psc_dma->mutex);104105out_be32(®s->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_AWR);106udelay(3);107out_be32(®s->sicr, psc_dma->sicr);108109mutex_unlock(&psc_dma->mutex);110}111112static void psc_ac97_cold_reset(struct snd_ac97 *ac97)113{114struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;115116mutex_lock(&psc_dma->mutex);117dev_dbg(psc_dma->dev, "cold reset\n");118119mpc5200_psc_ac97_gpio_reset(psc_dma->id);120121/* Notify the PSC that a reset has occurred */122out_be32(®s->sicr, psc_dma->sicr | MPC52xx_PSC_SICR_ACRB);123124/* Re-enable RX and TX */125out_8(®s->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);126127mutex_unlock(&psc_dma->mutex);128129msleep(1);130psc_ac97_warm_reset(ac97);131}132133struct snd_ac97_bus_ops soc_ac97_ops = {134.read = psc_ac97_read,135.write = psc_ac97_write,136.reset = psc_ac97_cold_reset,137.warm_reset = psc_ac97_warm_reset,138};139EXPORT_SYMBOL_GPL(soc_ac97_ops);140141static int psc_ac97_hw_analog_params(struct snd_pcm_substream *substream,142struct snd_pcm_hw_params *params,143struct snd_soc_dai *cpu_dai)144{145struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);146struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);147148dev_dbg(psc_dma->dev, "%s(substream=%p) p_size=%i p_bytes=%i"149" periods=%i buffer_size=%i buffer_bytes=%i channels=%i"150" rate=%i format=%i\n",151__func__, substream, params_period_size(params),152params_period_bytes(params), params_periods(params),153params_buffer_size(params), params_buffer_bytes(params),154params_channels(params), params_rate(params),155params_format(params));156157/* Determine the set of enable bits to turn on */158s->ac97_slot_bits = (params_channels(params) == 1) ? 0x100 : 0x300;159if (substream->pstr->stream != SNDRV_PCM_STREAM_CAPTURE)160s->ac97_slot_bits <<= 16;161return 0;162}163164static int psc_ac97_hw_digital_params(struct snd_pcm_substream *substream,165struct snd_pcm_hw_params *params,166struct snd_soc_dai *cpu_dai)167{168struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);169170dev_dbg(psc_dma->dev, "%s(substream=%p)\n", __func__, substream);171172if (params_channels(params) == 1)173out_be32(&psc_dma->psc_regs->ac97_slots, 0x01000000);174else175out_be32(&psc_dma->psc_regs->ac97_slots, 0x03000000);176177return 0;178}179180static int psc_ac97_trigger(struct snd_pcm_substream *substream, int cmd,181struct snd_soc_dai *dai)182{183struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(dai);184struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);185186switch (cmd) {187case SNDRV_PCM_TRIGGER_START:188dev_dbg(psc_dma->dev, "AC97 START: stream=%i\n",189substream->pstr->stream);190191/* Set the slot enable bits */192psc_dma->slots |= s->ac97_slot_bits;193out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);194break;195196case SNDRV_PCM_TRIGGER_STOP:197dev_dbg(psc_dma->dev, "AC97 STOP: stream=%i\n",198substream->pstr->stream);199200/* Clear the slot enable bits */201psc_dma->slots &= ~(s->ac97_slot_bits);202out_be32(&psc_dma->psc_regs->ac97_slots, psc_dma->slots);203break;204}205return 0;206}207208static int psc_ac97_probe(struct snd_soc_dai *cpu_dai)209{210struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(cpu_dai);211struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;212213/* Go */214out_8(®s->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);215return 0;216}217218/* ---------------------------------------------------------------------219* ALSA SoC Bindings220*221* - Digital Audio Interface (DAI) template222* - create/destroy dai hooks223*/224225/**226* psc_ac97_dai_template: template CPU Digital Audio Interface227*/228static struct snd_soc_dai_ops psc_ac97_analog_ops = {229.hw_params = psc_ac97_hw_analog_params,230.trigger = psc_ac97_trigger,231};232233static struct snd_soc_dai_ops psc_ac97_digital_ops = {234.hw_params = psc_ac97_hw_digital_params,235};236237static struct snd_soc_dai_driver psc_ac97_dai[] = {238{239.ac97_control = 1,240.probe = psc_ac97_probe,241.playback = {242.channels_min = 1,243.channels_max = 6,244.rates = SNDRV_PCM_RATE_8000_48000,245.formats = SNDRV_PCM_FMTBIT_S32_BE,246},247.capture = {248.channels_min = 1,249.channels_max = 2,250.rates = SNDRV_PCM_RATE_8000_48000,251.formats = SNDRV_PCM_FMTBIT_S32_BE,252},253.ops = &psc_ac97_analog_ops,254},255{256.ac97_control = 1,257.playback = {258.channels_min = 1,259.channels_max = 2,260.rates = SNDRV_PCM_RATE_32000 | \261SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,262.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,263},264.ops = &psc_ac97_digital_ops,265} };266267268269/* ---------------------------------------------------------------------270* OF platform bus binding code:271* - Probe/remove operations272* - OF device match table273*/274static int __devinit psc_ac97_of_probe(struct platform_device *op)275{276int rc;277struct snd_ac97 ac97;278struct mpc52xx_psc __iomem *regs;279280rc = snd_soc_register_dais(&op->dev, psc_ac97_dai, ARRAY_SIZE(psc_ac97_dai));281if (rc != 0) {282dev_err(&op->dev, "Failed to register DAI\n");283return rc;284}285286psc_dma = dev_get_drvdata(&op->dev);287regs = psc_dma->psc_regs;288ac97.private_data = psc_dma;289290psc_dma->imr = 0;291out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);292293/* Configure the serial interface mode to AC97 */294psc_dma->sicr = MPC52xx_PSC_SICR_SIM_AC97 | MPC52xx_PSC_SICR_ENAC97;295out_be32(®s->sicr, psc_dma->sicr);296297/* No slots active */298out_be32(®s->ac97_slots, 0x00000000);299300return 0;301}302303static int __devexit psc_ac97_of_remove(struct platform_device *op)304{305snd_soc_unregister_dais(&op->dev, ARRAY_SIZE(psc_ac97_dai));306return 0;307}308309/* Match table for of_platform binding */310static struct of_device_id psc_ac97_match[] __devinitdata = {311{ .compatible = "fsl,mpc5200-psc-ac97", },312{ .compatible = "fsl,mpc5200b-psc-ac97", },313{}314};315MODULE_DEVICE_TABLE(of, psc_ac97_match);316317static struct platform_driver psc_ac97_driver = {318.probe = psc_ac97_of_probe,319.remove = __devexit_p(psc_ac97_of_remove),320.driver = {321.name = "mpc5200-psc-ac97",322.owner = THIS_MODULE,323.of_match_table = psc_ac97_match,324},325};326327/* ---------------------------------------------------------------------328* Module setup and teardown; simply register the of_platform driver329* for the PSC in AC97 mode.330*/331static int __init psc_ac97_init(void)332{333return platform_driver_register(&psc_ac97_driver);334}335module_init(psc_ac97_init);336337static void __exit psc_ac97_exit(void)338{339platform_driver_unregister(&psc_ac97_driver);340}341module_exit(psc_ac97_exit);342343MODULE_AUTHOR("Jon Smirl <[email protected]>");344MODULE_DESCRIPTION("mpc5200 AC97 module");345MODULE_LICENSE("GPL");346347348349