Path: blob/master/sound/pcmcia/pdaudiocf/pdaudiocf_pcm.c
10817 views
/*1* Driver for Sound Core PDAudioCF soundcards2*3* PCM part4*5* Copyright (c) 2003 by Jaroslav Kysela <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*/2122#include <linux/delay.h>23#include <sound/core.h>24#include <sound/asoundef.h>25#include "pdaudiocf.h"262728/*29* clear the SRAM contents30*/31static int pdacf_pcm_clear_sram(struct snd_pdacf *chip)32{33int max_loop = 64 * 1024;3435while (inw(chip->port + PDAUDIOCF_REG_RDP) != inw(chip->port + PDAUDIOCF_REG_WDP)) {36if (max_loop-- < 0)37return -EIO;38inw(chip->port + PDAUDIOCF_REG_MD);39}40return 0;41}4243/*44* pdacf_pcm_trigger - trigger callback for capture45*/46static int pdacf_pcm_trigger(struct snd_pcm_substream *subs, int cmd)47{48struct snd_pdacf *chip = snd_pcm_substream_chip(subs);49struct snd_pcm_runtime *runtime = subs->runtime;50int inc, ret = 0, rate;51unsigned short mask, val, tmp;5253if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)54return -EBUSY;5556switch (cmd) {57case SNDRV_PCM_TRIGGER_START:58chip->pcm_hwptr = 0;59chip->pcm_tdone = 0;60/* fall thru */61case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:62case SNDRV_PCM_TRIGGER_RESUME:63mask = 0;64val = PDAUDIOCF_RECORD;65inc = 1;66rate = snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_STAT|AK4117_CHECK_NO_RATE);67break;68case SNDRV_PCM_TRIGGER_STOP:69case SNDRV_PCM_TRIGGER_PAUSE_PUSH:70case SNDRV_PCM_TRIGGER_SUSPEND:71mask = PDAUDIOCF_RECORD;72val = 0;73inc = -1;74rate = 0;75break;76default:77return -EINVAL;78}79spin_lock(&chip->reg_lock);80chip->pcm_running += inc;81tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);82if (chip->pcm_running) {83if ((chip->ak4117->rcs0 & AK4117_UNLCK) || runtime->rate != rate) {84chip->pcm_running -= inc;85ret = -EIO;86goto __end;87}88}89tmp &= ~mask;90tmp |= val;91pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, tmp);92__end:93spin_unlock(&chip->reg_lock);94snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_RATE);95return ret;96}9798/*99* pdacf_pcm_hw_params - hw_params callback for playback and capture100*/101static int pdacf_pcm_hw_params(struct snd_pcm_substream *subs,102struct snd_pcm_hw_params *hw_params)103{104return snd_pcm_lib_alloc_vmalloc_32_buffer105(subs, params_buffer_bytes(hw_params));106}107108/*109* pdacf_pcm_hw_free - hw_free callback for playback and capture110*/111static int pdacf_pcm_hw_free(struct snd_pcm_substream *subs)112{113return snd_pcm_lib_free_vmalloc_buffer(subs);114}115116/*117* pdacf_pcm_prepare - prepare callback for playback and capture118*/119static int pdacf_pcm_prepare(struct snd_pcm_substream *subs)120{121struct snd_pdacf *chip = snd_pcm_substream_chip(subs);122struct snd_pcm_runtime *runtime = subs->runtime;123u16 val, nval, aval;124125if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)126return -EBUSY;127128chip->pcm_channels = runtime->channels;129130chip->pcm_little = snd_pcm_format_little_endian(runtime->format) > 0;131#ifdef SNDRV_LITTLE_ENDIAN132chip->pcm_swab = snd_pcm_format_big_endian(runtime->format) > 0;133#else134chip->pcm_swab = chip->pcm_little;135#endif136137if (snd_pcm_format_unsigned(runtime->format))138chip->pcm_xor = 0x80008000;139140if (pdacf_pcm_clear_sram(chip) < 0)141return -EIO;142143val = nval = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);144nval &= ~(PDAUDIOCF_DATAFMT0|PDAUDIOCF_DATAFMT1);145switch (runtime->format) {146case SNDRV_PCM_FORMAT_S16_LE:147case SNDRV_PCM_FORMAT_S16_BE:148break;149default: /* 24-bit */150nval |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1;151break;152}153aval = 0;154chip->pcm_sample = 4;155switch (runtime->format) {156case SNDRV_PCM_FORMAT_S16_LE:157case SNDRV_PCM_FORMAT_S16_BE:158aval = AK4117_DIF_16R;159chip->pcm_frame = 2;160chip->pcm_sample = 2;161break;162case SNDRV_PCM_FORMAT_S24_3LE:163case SNDRV_PCM_FORMAT_S24_3BE:164chip->pcm_sample = 3;165/* fall through */166default: /* 24-bit */167aval = AK4117_DIF_24R;168chip->pcm_frame = 3;169chip->pcm_xor &= 0xffff0000;170break;171}172173if (val != nval) {174snd_ak4117_reg_write(chip->ak4117, AK4117_REG_IO, AK4117_DIF2|AK4117_DIF1|AK4117_DIF0, aval);175pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, nval);176}177178val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);179val &= ~(PDAUDIOCF_IRQLVLEN1);180val |= PDAUDIOCF_IRQLVLEN0;181pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);182183chip->pcm_size = runtime->buffer_size;184chip->pcm_period = runtime->period_size;185chip->pcm_area = runtime->dma_area;186187return 0;188}189190191/*192* capture hw information193*/194195static struct snd_pcm_hardware pdacf_pcm_capture_hw = {196.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |197SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |198SNDRV_PCM_INFO_MMAP_VALID |199SNDRV_PCM_INFO_BATCH),200.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |201SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |202SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,203.rates = SNDRV_PCM_RATE_32000 |204SNDRV_PCM_RATE_44100 |205SNDRV_PCM_RATE_48000 |206SNDRV_PCM_RATE_88200 |207SNDRV_PCM_RATE_96000 |208SNDRV_PCM_RATE_176400 |209SNDRV_PCM_RATE_192000,210.rate_min = 32000,211.rate_max = 192000,212.channels_min = 1,213.channels_max = 2,214.buffer_bytes_max = (512*1024),215.period_bytes_min = 8*1024,216.period_bytes_max = (64*1024),217.periods_min = 2,218.periods_max = 128,219.fifo_size = 0,220};221222223/*224* pdacf_pcm_capture_open - open callback for capture225*/226static int pdacf_pcm_capture_open(struct snd_pcm_substream *subs)227{228struct snd_pcm_runtime *runtime = subs->runtime;229struct snd_pdacf *chip = snd_pcm_substream_chip(subs);230231if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)232return -EBUSY;233234runtime->hw = pdacf_pcm_capture_hw;235runtime->private_data = chip;236chip->pcm_substream = subs;237238return 0;239}240241/*242* pdacf_pcm_capture_close - close callback for capture243*/244static int pdacf_pcm_capture_close(struct snd_pcm_substream *subs)245{246struct snd_pdacf *chip = snd_pcm_substream_chip(subs);247248if (!chip)249return -EINVAL;250pdacf_reinit(chip, 0);251chip->pcm_substream = NULL;252return 0;253}254255256/*257* pdacf_pcm_capture_pointer - pointer callback for capture258*/259static snd_pcm_uframes_t pdacf_pcm_capture_pointer(struct snd_pcm_substream *subs)260{261struct snd_pdacf *chip = snd_pcm_substream_chip(subs);262return chip->pcm_hwptr;263}264265/*266* operators for PCM capture267*/268static struct snd_pcm_ops pdacf_pcm_capture_ops = {269.open = pdacf_pcm_capture_open,270.close = pdacf_pcm_capture_close,271.ioctl = snd_pcm_lib_ioctl,272.hw_params = pdacf_pcm_hw_params,273.hw_free = pdacf_pcm_hw_free,274.prepare = pdacf_pcm_prepare,275.trigger = pdacf_pcm_trigger,276.pointer = pdacf_pcm_capture_pointer,277.page = snd_pcm_lib_get_vmalloc_page,278.mmap = snd_pcm_lib_mmap_vmalloc,279};280281282/*283* snd_pdacf_pcm_new - create and initialize a pcm284*/285int snd_pdacf_pcm_new(struct snd_pdacf *chip)286{287struct snd_pcm *pcm;288int err;289290err = snd_pcm_new(chip->card, "PDAudioCF", 0, 0, 1, &pcm);291if (err < 0)292return err;293294snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pdacf_pcm_capture_ops);295296pcm->private_data = chip;297pcm->info_flags = 0;298strcpy(pcm->name, chip->card->shortname);299chip->pcm = pcm;300301err = snd_ak4117_build(chip->ak4117, pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);302if (err < 0)303return err;304305return 0;306}307308309