Path: blob/master/sound/pcmcia/pdaudiocf/pdaudiocf_pcm.c
29266 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Driver for Sound Core PDAudioCF soundcards3*4* PCM part5*6* Copyright (c) 2003 by Jaroslav Kysela <[email protected]>7*/89#include <linux/delay.h>10#include <sound/core.h>11#include <sound/asoundef.h>12#include "pdaudiocf.h"131415/*16* clear the SRAM contents17*/18static int pdacf_pcm_clear_sram(struct snd_pdacf *chip)19{20int max_loop = 64 * 1024;2122while (inw(chip->port + PDAUDIOCF_REG_RDP) != inw(chip->port + PDAUDIOCF_REG_WDP)) {23if (max_loop-- < 0)24return -EIO;25inw(chip->port + PDAUDIOCF_REG_MD);26}27return 0;28}2930/*31* pdacf_pcm_trigger - trigger callback for capture32*/33static int pdacf_pcm_trigger(struct snd_pcm_substream *subs, int cmd)34{35struct snd_pdacf *chip = snd_pcm_substream_chip(subs);36struct snd_pcm_runtime *runtime = subs->runtime;37int inc, ret = 0, rate;38unsigned short mask, val, tmp;3940if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)41return -EBUSY;4243switch (cmd) {44case SNDRV_PCM_TRIGGER_START:45chip->pcm_hwptr = 0;46chip->pcm_tdone = 0;47fallthrough;48case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:49case SNDRV_PCM_TRIGGER_RESUME:50mask = 0;51val = PDAUDIOCF_RECORD;52inc = 1;53rate = snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_STAT|AK4117_CHECK_NO_RATE);54break;55case SNDRV_PCM_TRIGGER_STOP:56case SNDRV_PCM_TRIGGER_PAUSE_PUSH:57case SNDRV_PCM_TRIGGER_SUSPEND:58mask = PDAUDIOCF_RECORD;59val = 0;60inc = -1;61rate = 0;62break;63default:64return -EINVAL;65}66scoped_guard(mutex, &chip->reg_lock) {67chip->pcm_running += inc;68tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);69if (chip->pcm_running) {70if ((chip->ak4117->rcs0 & AK4117_UNLCK) || runtime->rate != rate) {71chip->pcm_running -= inc;72ret = -EIO;73break;74}75}76tmp &= ~mask;77tmp |= val;78pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, tmp);79}80snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_RATE);81return ret;82}8384/*85* pdacf_pcm_prepare - prepare callback for playback and capture86*/87static int pdacf_pcm_prepare(struct snd_pcm_substream *subs)88{89struct snd_pdacf *chip = snd_pcm_substream_chip(subs);90struct snd_pcm_runtime *runtime = subs->runtime;91u16 val, nval, aval;9293if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)94return -EBUSY;9596chip->pcm_channels = runtime->channels;9798chip->pcm_little = snd_pcm_format_little_endian(runtime->format) > 0;99#ifdef SNDRV_LITTLE_ENDIAN100chip->pcm_swab = snd_pcm_format_big_endian(runtime->format) > 0;101#else102chip->pcm_swab = chip->pcm_little;103#endif104105if (snd_pcm_format_unsigned(runtime->format))106chip->pcm_xor = 0x80008000;107108if (pdacf_pcm_clear_sram(chip) < 0)109return -EIO;110111val = nval = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);112nval &= ~(PDAUDIOCF_DATAFMT0|PDAUDIOCF_DATAFMT1);113switch (runtime->format) {114case SNDRV_PCM_FORMAT_S16_LE:115case SNDRV_PCM_FORMAT_S16_BE:116break;117default: /* 24-bit */118nval |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1;119break;120}121aval = 0;122chip->pcm_sample = 4;123switch (runtime->format) {124case SNDRV_PCM_FORMAT_S16_LE:125case SNDRV_PCM_FORMAT_S16_BE:126aval = AK4117_DIF_16R;127chip->pcm_frame = 2;128chip->pcm_sample = 2;129break;130case SNDRV_PCM_FORMAT_S24_3LE:131case SNDRV_PCM_FORMAT_S24_3BE:132chip->pcm_sample = 3;133fallthrough;134default: /* 24-bit */135aval = AK4117_DIF_24R;136chip->pcm_frame = 3;137chip->pcm_xor &= 0xffff0000;138break;139}140141if (val != nval) {142snd_ak4117_reg_write(chip->ak4117, AK4117_REG_IO, AK4117_DIF2|AK4117_DIF1|AK4117_DIF0, aval);143pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, nval);144}145146val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);147val &= ~(PDAUDIOCF_IRQLVLEN1);148val |= PDAUDIOCF_IRQLVLEN0;149pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);150151chip->pcm_size = runtime->buffer_size;152chip->pcm_period = runtime->period_size;153chip->pcm_area = runtime->dma_area;154155return 0;156}157158159/*160* capture hw information161*/162163static const struct snd_pcm_hardware pdacf_pcm_capture_hw = {164.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |165SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |166SNDRV_PCM_INFO_MMAP_VALID |167SNDRV_PCM_INFO_BATCH),168.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |169SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |170SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,171.rates = SNDRV_PCM_RATE_32000 |172SNDRV_PCM_RATE_44100 |173SNDRV_PCM_RATE_48000 |174SNDRV_PCM_RATE_88200 |175SNDRV_PCM_RATE_96000 |176SNDRV_PCM_RATE_176400 |177SNDRV_PCM_RATE_192000,178.rate_min = 32000,179.rate_max = 192000,180.channels_min = 1,181.channels_max = 2,182.buffer_bytes_max = (512*1024),183.period_bytes_min = 8*1024,184.period_bytes_max = (64*1024),185.periods_min = 2,186.periods_max = 128,187.fifo_size = 0,188};189190191/*192* pdacf_pcm_capture_open - open callback for capture193*/194static int pdacf_pcm_capture_open(struct snd_pcm_substream *subs)195{196struct snd_pcm_runtime *runtime = subs->runtime;197struct snd_pdacf *chip = snd_pcm_substream_chip(subs);198199if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)200return -EBUSY;201202runtime->hw = pdacf_pcm_capture_hw;203runtime->private_data = chip;204chip->pcm_substream = subs;205206return 0;207}208209/*210* pdacf_pcm_capture_close - close callback for capture211*/212static int pdacf_pcm_capture_close(struct snd_pcm_substream *subs)213{214struct snd_pdacf *chip = snd_pcm_substream_chip(subs);215216if (!chip)217return -EINVAL;218pdacf_reinit(chip, 0);219chip->pcm_substream = NULL;220return 0;221}222223224/*225* pdacf_pcm_capture_pointer - pointer callback for capture226*/227static snd_pcm_uframes_t pdacf_pcm_capture_pointer(struct snd_pcm_substream *subs)228{229struct snd_pdacf *chip = snd_pcm_substream_chip(subs);230return chip->pcm_hwptr;231}232233/*234* operators for PCM capture235*/236static const struct snd_pcm_ops pdacf_pcm_capture_ops = {237.open = pdacf_pcm_capture_open,238.close = pdacf_pcm_capture_close,239.prepare = pdacf_pcm_prepare,240.trigger = pdacf_pcm_trigger,241.pointer = pdacf_pcm_capture_pointer,242};243244245/*246* snd_pdacf_pcm_new - create and initialize a pcm247*/248int snd_pdacf_pcm_new(struct snd_pdacf *chip)249{250struct snd_pcm *pcm;251int err;252253err = snd_pcm_new(chip->card, "PDAudioCF", 0, 0, 1, &pcm);254if (err < 0)255return err;256257snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pdacf_pcm_capture_ops);258snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL,2590, 0);260261pcm->private_data = chip;262pcm->info_flags = 0;263pcm->nonatomic = true;264strscpy(pcm->name, chip->card->shortname);265chip->pcm = pcm;266267err = snd_ak4117_build(chip->ak4117, pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);268if (err < 0)269return err;270271return 0;272}273274275