cregit-Linux how code gets into the kernel

Release 4.8 sound/spi/at73c213.c

Directory: sound/spi
/*
 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
 *
 * Copyright (C) 2006-2007 Atmel Norway
 *
 * 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.
 */

/*#define DEBUG*/

#include <linux/clk.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/io.h>

#include <sound/initval.h>
#include <sound/control.h>
#include <sound/core.h>
#include <sound/pcm.h>

#include <linux/atmel-ssc.h>

#include <linux/spi/spi.h>
#include <linux/spi/at73c213.h>

#include "at73c213.h"


#define BITRATE_MIN	 8000 
/* Hardware limit? */

#define BITRATE_TARGET	CONFIG_SND_AT73C213_TARGET_BITRATE

#define BITRATE_MAX	50000 
/* Hardware limit. */

/* Initial (hardware reset) AT73C213 register values. */

static u8 snd_at73c213_original_image[18] =
{
	0x00,	/* 00 - CTRL    */
	0x05,	/* 01 - LLIG    */
	0x05,	/* 02 - RLIG    */
	0x08,	/* 03 - LPMG    */
	0x08,	/* 04 - RPMG    */
	0x00,	/* 05 - LLOG    */
	0x00,	/* 06 - RLOG    */
	0x22,	/* 07 - OLC     */
	0x09,	/* 08 - MC      */
	0x00,	/* 09 - CSFC    */
	0x00,	/* 0A - MISC    */
	0x00,	/* 0B -         */
	0x00,	/* 0C - PRECH   */
	0x05,	/* 0D - AUXG    */
	0x00,	/* 0E -         */
	0x00,	/* 0F -         */
	0x00,	/* 10 - RST     */
	0x00,	/* 11 - PA_CTRL */
};


struct snd_at73c213 {
	
struct snd_card			*card;
	
struct snd_pcm			*pcm;
	
struct snd_pcm_substream	*substream;
	
struct at73c213_board_info	*board;
	
int				irq;
	
int				period;
	
unsigned long			bitrate;
	
struct ssc_device		*ssc;
	
struct spi_device		*spi;
	
u8				spi_wbuffer[2];
	
u8				spi_rbuffer[2];
	/* Image of the SPI registers in AT73C213. */
	
u8				reg_image[18];
	/* Protect SSC registers against concurrent access. */
	
spinlock_t			lock;
	/* Protect mixer registers against concurrent access. */
	
struct mutex			mixer_lock;
};


#define get_chip(card) ((struct snd_at73c213 *)card->private_data)


static int snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val) { struct spi_message msg; struct spi_transfer msg_xfer = { .len = 2, .cs_change = 0, }; int retval; spi_message_init(&msg); chip->spi_wbuffer[0] = reg; chip->spi_wbuffer[1] = val; msg_xfer.tx_buf = chip->spi_wbuffer; msg_xfer.rx_buf = chip->spi_rbuffer; spi_message_add_tail(&msg_xfer, &msg); retval = spi_sync(chip->spi, &msg); if (!retval) chip->reg_image[reg] = val; return retval; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt118100.00%1100.00%
Total118100.00%1100.00%

static struct snd_pcm_hardware snd_at73c213_playback_hw = { .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER, .formats = SNDRV_PCM_FMTBIT_S16_BE, .rates = SNDRV_PCM_RATE_CONTINUOUS, .rate_min = 8000, /* Replaced by chip->bitrate later. */ .rate_max = 50000, /* Replaced by chip->bitrate later. */ .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 64 * 1024 - 1, .period_bytes_min = 512, .period_bytes_max = 64 * 1024 - 1, .periods_min = 4, .periods_max = 1024, }; /* * Calculate and set bitrate and divisions. */
static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip) { unsigned long ssc_rate = clk_get_rate(chip->ssc->clk); unsigned long dac_rate_new, ssc_div; int status; unsigned long ssc_div_max, ssc_div_min; int max_tries; /* * We connect two clocks here, picking divisors so the I2S clocks * out data at the same rate the DAC clocks it in ... and as close * as practical to the desired target rate. * * The DAC master clock (MCLK) is programmable, and is either 256 * or (not here) 384 times the I2S output clock (BCLK). */ /* SSC clock / (bitrate * stereo * 16-bit). */ ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16); ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16); ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16); max_tries = (ssc_div_max - ssc_div_min) / 2; if (max_tries < 1) max_tries = 1; /* ssc_div must be even. */ ssc_div = (ssc_div + 1) & ~1UL; if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) { ssc_div -= 2; if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX) return -ENXIO; } /* Search for a possible bitrate. */ do { /* SSC clock / (ssc divider * 16-bit * stereo). */ if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) return -ENXIO; /* 256 / (2 * 16) = 8 */ dac_rate_new = 8 * (ssc_rate / ssc_div); status = clk_round_rate(chip->board->dac_clk, dac_rate_new); if (status <= 0) return status; /* Ignore difference smaller than 256 Hz. */ if ((status/256) == (dac_rate_new/256)) goto set_rate; ssc_div += 2; } while (--max_tries); /* Not able to find a valid bitrate. */ return -ENXIO; set_rate: status = clk_set_rate(chip->board->dac_clk, status); if (status < 0) return status; /* Set divider in SSC device. */ ssc_writel(chip->ssc->regs, CMR, ssc_div/2); /* SSC clock / (ssc divider * 16-bit * stereo). */ chip->bitrate = ssc_rate / (ssc_div * 16 * 2); dev_info(&chip->spi->dev, "at73c213: supported bitrate is %lu (%lu divider)\n", chip->bitrate, ssc_div); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt31798.75%125.00%
atsushi nemotoatsushi nemoto20.62%125.00%
paul walmsleypaul walmsley10.31%125.00%
peter rosinpeter rosin10.31%125.00%
Total321100.00%4100.00%


static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int err; /* ensure buffer_size is a multiple of period_size */ err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); if (err < 0) return err; snd_at73c213_playback_hw.rate_min = chip->bitrate; snd_at73c213_playback_hw.rate_max = chip->bitrate; runtime->hw = snd_at73c213_playback_hw; chip->substream = substream; clk_enable(chip->ssc->clk); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt6166.30%133.33%
atsushi nemotoatsushi nemoto2223.91%133.33%
mans rullgardmans rullgard99.78%133.33%
Total92100.00%3100.00%


static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); chip->substream = NULL; clk_disable(chip->ssc->clk); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt3076.92%150.00%
mans rullgardmans rullgard923.08%150.00%
Total39100.00%2100.00%


static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); int channels = params_channels(hw_params); int val; val = ssc_readl(chip->ssc->regs, TFMR); val = SSC_BFINS(TFMR_DATNB, channels - 1, val); ssc_writel(chip->ssc->regs, TFMR, val); return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); }

Contributors

PersonTokensPropCommitsCommitProp
atsushi nemotoatsushi nemoto6068.97%150.00%
hans-christian egtvedthans-christian egtvedt2731.03%150.00%
Total87100.00%2100.00%


static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream) { return snd_pcm_lib_free_pages(substream); }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt17100.00%1100.00%
Total17100.00%1100.00%


static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int block_size; block_size = frames_to_bytes(runtime, runtime->period_size); chip->period = 0; ssc_writel(chip->ssc->regs, PDC_TPR, (long)runtime->dma_addr); ssc_writel(chip->ssc->regs, PDC_TCR, runtime->period_size * runtime->channels); ssc_writel(chip->ssc->regs, PDC_TNPR, (long)runtime->dma_addr + block_size); ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * runtime->channels); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt12395.35%150.00%
atsushi nemotoatsushi nemoto64.65%150.00%
Total129100.00%2100.00%


static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); int retval = 0; spin_lock(&chip->lock); switch (cmd) { case SNDRV_PCM_TRIGGER_START: ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX)); ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN)); break; case SNDRV_PCM_TRIGGER_STOP: ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS)); ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX)); break; default: dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd); retval = -EINVAL; break; } spin_unlock(&chip->lock); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt147100.00%1100.00%
Total147100.00%1100.00%


static snd_pcm_uframes_t snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream) { struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_uframes_t pos; unsigned long bytes; bytes = ssc_readl(chip->ssc->regs, PDC_TPR) - (unsigned long)runtime->dma_addr; pos = bytes_to_frames(runtime, bytes); if (pos >= runtime->buffer_size) pos -= runtime->buffer_size; return pos; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt84100.00%1100.00%
Total84100.00%1100.00%

static struct snd_pcm_ops at73c213_playback_ops = { .open = snd_at73c213_pcm_open, .close = snd_at73c213_pcm_close, .ioctl = snd_pcm_lib_ioctl, .hw_params = snd_at73c213_pcm_hw_params, .hw_free = snd_at73c213_pcm_hw_free, .prepare = snd_at73c213_pcm_prepare, .trigger = snd_at73c213_pcm_trigger, .pointer = snd_at73c213_pcm_pointer, };
static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device) { struct snd_pcm *pcm; int retval; retval = snd_pcm_new(chip->card, chip->card->shortname, device, 1, 0, &pcm); if (retval < 0) goto out; pcm->private_data = chip; pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER; strcpy(pcm->name, "at73c213"); chip->pcm = pcm; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops); retval = snd_pcm_lib_preallocate_pages_for_all(chip->pcm, SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev, 64 * 1024, 64 * 1024); out: return retval; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt125100.00%1100.00%
Total125100.00%1100.00%


static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id) { struct snd_at73c213 *chip = dev_id; struct snd_pcm_runtime *runtime = chip->substream->runtime; u32 status; int offset; int block_size; int next_period; int retval = IRQ_NONE; spin_lock(&chip->lock); block_size = frames_to_bytes(runtime, runtime->period_size); status = ssc_readl(chip->ssc->regs, IMR); if (status & SSC_BIT(IMR_ENDTX)) { chip->period++; if (chip->period == runtime->periods) chip->period = 0; next_period = chip->period + 1; if (next_period == runtime->periods) next_period = 0; offset = block_size * next_period; ssc_writel(chip->ssc->regs, PDC_TNPR, (long)runtime->dma_addr + offset); ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * runtime->channels); retval = IRQ_HANDLED; } ssc_readl(chip->ssc->regs, IMR); spin_unlock(&chip->lock); if (status & SSC_BIT(IMR_ENDTX)) snd_pcm_period_elapsed(chip->substream); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt21698.63%150.00%
atsushi nemotoatsushi nemoto31.37%150.00%
Total219100.00%2100.00%

/* * Mixer functions. */
static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; mutex_lock(&chip->mixer_lock); ucontrol->value.integer.value[0] = (chip->reg_image[reg] >> shift) & mask; if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; mutex_unlock(&chip->mixer_lock); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt145100.00%2100.00%
Total145100.00%2100.00%


static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change, retval; unsigned short val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; mutex_lock(&chip->mixer_lock); val = (chip->reg_image[reg] & ~(mask << shift)) | val; change = val != chip->reg_image[reg]; retval = snd_at73c213_write_reg(chip, reg, val); mutex_unlock(&chip->mixer_lock); if (retval) return retval; return change; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt182100.00%2100.00%
Total182100.00%2100.00%


static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; if (mask == 1) uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; else uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt77100.00%1100.00%
Total77100.00%1100.00%


static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; mutex_lock(&chip->mixer_lock); ucontrol->value.integer.value[0] = (chip->reg_image[left_reg] >> shift_left) & mask; ucontrol->value.integer.value[1] = (chip->reg_image[right_reg] >> shift_right) & mask; if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } mutex_unlock(&chip->mixer_lock); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt221100.00%2100.00%
Total221100.00%2100.00%


static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change, retval; unsigned short val1, val2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; mutex_lock(&chip->mixer_lock); val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1; val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2; change = val1 != chip->reg_image[left_reg] || val2 != chip->reg_image[right_reg]; retval = snd_at73c213_write_reg(chip, left_reg, val1); if (retval) { mutex_unlock(&chip->mixer_lock); goto out; } retval = snd_at73c213_write_reg(chip, right_reg, val2); if (retval) { mutex_unlock(&chip->mixer_lock); goto out; } mutex_unlock(&chip->mixer_lock); return change; out: return retval; }

Contributors

PersonTokensPropCommitsCommitProp
hans-christian egtvedthans-christian egtvedt307100.00%2100.00%
Total307100.00%2100.00%

#define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >>