Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
# SPDX-License-Identifier: GPL-2.0-only
menu "Allwinner"
depends on ARCH_SUNXI || COMPILE_TEST
config SND_SUN4I_CODEC
tristate "Allwinner A10 Codec Support"
select SND_SOC_GENERIC_DMAENGINE_PCM
select REGMAP_MMIO
help
Select Y or M to add support for the Codec embedded in the Allwinner
A10 and affiliated SoCs.
config SND_SUN8I_CODEC
tristate "Allwinner SUN8I audio codec"
depends on OF
depends on MACH_SUN8I || (ARM64 && ARCH_SUNXI) || COMPILE_TEST
depends on COMMON_CLK
select REGMAP_MMIO
help
This option enables the digital part of the internal audio codec for
Allwinner sun8i SoC (and particularly A33).
Say Y or M if you want to add sun8i digital audio codec support.
config SND_SUN8I_CODEC_ANALOG
tristate "Allwinner sun8i Codec Analog Controls Support"
depends on MACH_SUN8I || (ARM64 && ARCH_SUNXI) || COMPILE_TEST
select SND_SUN8I_ADDA_PR_REGMAP
help
Say Y or M if you want to add support for the analog controls for
the codec embedded in newer Allwinner SoCs.
config SND_SUN50I_CODEC_ANALOG
tristate "Allwinner sun50i Codec Analog Controls Support"
depends on (ARM64 && ARCH_SUNXI) || COMPILE_TEST
select SND_SUN8I_ADDA_PR_REGMAP
help
Say Y or M if you want to add support for the analog controls for
the codec embedded in Allwinner A64 SoC.
config SND_SUN4I_I2S
tristate "Allwinner A10 I2S Support"
select SND_SOC_GENERIC_DMAENGINE_PCM
select REGMAP_MMIO
help
Say Y or M if you want to add support for codecs attached to
the Allwinner A10 I2S. You will also need to select the
individual machine drivers to support below.
config SND_SUN4I_SPDIF
tristate "Allwinner A10 SPDIF Support"
depends on OF
select SND_SOC_GENERIC_DMAENGINE_PCM
select REGMAP_MMIO
help
Say Y or M to add support for the S/PDIF audio block in the Allwinner
A10 and affiliated SoCs.
config SND_SUN50I_DMIC
tristate "Allwinner H6 DMIC Support"
select SND_SOC_GENERIC_DMAENGINE_PCM
help
Say Y or M to add support for the DMIC audio block in the Allwinner
H6 and affiliated SoCs.
config SND_SUN8I_ADDA_PR_REGMAP
tristate
select REGMAP
endmenu
+9
View File
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_SND_SUN4I_CODEC) += sun4i-codec.o
obj-$(CONFIG_SND_SUN4I_I2S) += sun4i-i2s.o
obj-$(CONFIG_SND_SUN4I_SPDIF) += sun4i-spdif.o
obj-$(CONFIG_SND_SUN8I_CODEC_ANALOG) += sun8i-codec-analog.o
obj-$(CONFIG_SND_SUN50I_CODEC_ANALOG) += sun50i-codec-analog.o
obj-$(CONFIG_SND_SUN8I_CODEC) += sun8i-codec.o
obj-$(CONFIG_SND_SUN8I_ADDA_PR_REGMAP) += sun8i-adda-pr-regmap.o
obj-$(CONFIG_SND_SUN50I_DMIC) += sun50i-dmic.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+773
View File
@@ -0,0 +1,773 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* ALSA SoC SPDIF Audio Layer
*
* Copyright 2015 Andrea Venturi <be17068@iperbole.bo.it>
* Copyright 2015 Marcus Cooper <codekipper@gmail.com>
*
* Based on the Allwinner SDK driver, released under the GPL.
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/regmap.h>
#include <linux/of.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/spinlock.h>
#include <sound/asoundef.h>
#include <sound/dmaengine_pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#define SUN4I_SPDIF_CTL (0x00)
#define SUN4I_SPDIF_CTL_MCLKDIV(v) ((v) << 4) /* v even */
#define SUN4I_SPDIF_CTL_MCLKOUTEN BIT(2)
#define SUN4I_SPDIF_CTL_GEN BIT(1)
#define SUN4I_SPDIF_CTL_RESET BIT(0)
#define SUN4I_SPDIF_TXCFG (0x04)
#define SUN4I_SPDIF_TXCFG_SINGLEMOD BIT(31)
#define SUN4I_SPDIF_TXCFG_ASS BIT(17)
#define SUN4I_SPDIF_TXCFG_NONAUDIO BIT(16)
#define SUN4I_SPDIF_TXCFG_TXRATIO(v) ((v) << 4)
#define SUN4I_SPDIF_TXCFG_TXRATIO_MASK GENMASK(8, 4)
#define SUN4I_SPDIF_TXCFG_FMTRVD GENMASK(3, 2)
#define SUN4I_SPDIF_TXCFG_FMT16BIT (0 << 2)
#define SUN4I_SPDIF_TXCFG_FMT20BIT (1 << 2)
#define SUN4I_SPDIF_TXCFG_FMT24BIT (2 << 2)
#define SUN4I_SPDIF_TXCFG_CHSTMODE BIT(1)
#define SUN4I_SPDIF_TXCFG_TXEN BIT(0)
#define SUN4I_SPDIF_RXCFG (0x08)
#define SUN4I_SPDIF_RXCFG_LOCKFLAG BIT(4)
#define SUN4I_SPDIF_RXCFG_CHSTSRC BIT(3)
#define SUN4I_SPDIF_RXCFG_CHSTCP BIT(1)
#define SUN4I_SPDIF_RXCFG_RXEN BIT(0)
#define SUN4I_SPDIF_TXFIFO (0x0C)
#define SUN4I_SPDIF_RXFIFO (0x10)
#define SUN4I_SPDIF_FCTL (0x14)
#define SUN4I_SPDIF_FCTL_FIFOSRC BIT(31)
#define SUN4I_SPDIF_FCTL_FTX BIT(17)
#define SUN4I_SPDIF_FCTL_FRX BIT(16)
#define SUN4I_SPDIF_FCTL_TXTL(v) ((v) << 8)
#define SUN4I_SPDIF_FCTL_TXTL_MASK GENMASK(12, 8)
#define SUN4I_SPDIF_FCTL_RXTL(v) ((v) << 3)
#define SUN4I_SPDIF_FCTL_RXTL_MASK GENMASK(7, 3)
#define SUN4I_SPDIF_FCTL_TXIM BIT(2)
#define SUN4I_SPDIF_FCTL_RXOM(v) ((v) << 0)
#define SUN4I_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
#define SUN50I_H6_SPDIF_FCTL (0x14)
#define SUN50I_H6_SPDIF_FCTL_HUB_EN BIT(31)
#define SUN50I_H6_SPDIF_FCTL_FTX BIT(30)
#define SUN50I_H6_SPDIF_FCTL_FRX BIT(29)
#define SUN50I_H6_SPDIF_FCTL_TXTL(v) ((v) << 12)
#define SUN50I_H6_SPDIF_FCTL_TXTL_MASK GENMASK(19, 12)
#define SUN50I_H6_SPDIF_FCTL_RXTL(v) ((v) << 4)
#define SUN50I_H6_SPDIF_FCTL_RXTL_MASK GENMASK(10, 4)
#define SUN50I_H6_SPDIF_FCTL_TXIM BIT(2)
#define SUN50I_H6_SPDIF_FCTL_RXOM(v) ((v) << 0)
#define SUN50I_H6_SPDIF_FCTL_RXOM_MASK GENMASK(1, 0)
#define SUN4I_SPDIF_FSTA (0x18)
#define SUN4I_SPDIF_FSTA_TXE BIT(14)
#define SUN4I_SPDIF_FSTA_TXECNTSHT (8)
#define SUN4I_SPDIF_FSTA_RXA BIT(6)
#define SUN4I_SPDIF_FSTA_RXACNTSHT (0)
#define SUN4I_SPDIF_INT (0x1C)
#define SUN4I_SPDIF_INT_RXLOCKEN BIT(18)
#define SUN4I_SPDIF_INT_RXUNLOCKEN BIT(17)
#define SUN4I_SPDIF_INT_RXPARERREN BIT(16)
#define SUN4I_SPDIF_INT_TXDRQEN BIT(7)
#define SUN4I_SPDIF_INT_TXUIEN BIT(6)
#define SUN4I_SPDIF_INT_TXOIEN BIT(5)
#define SUN4I_SPDIF_INT_TXEIEN BIT(4)
#define SUN4I_SPDIF_INT_RXDRQEN BIT(2)
#define SUN4I_SPDIF_INT_RXOIEN BIT(1)
#define SUN4I_SPDIF_INT_RXAIEN BIT(0)
#define SUN4I_SPDIF_ISTA (0x20)
#define SUN4I_SPDIF_ISTA_RXLOCKSTA BIT(18)
#define SUN4I_SPDIF_ISTA_RXUNLOCKSTA BIT(17)
#define SUN4I_SPDIF_ISTA_RXPARERRSTA BIT(16)
#define SUN4I_SPDIF_ISTA_TXUSTA BIT(6)
#define SUN4I_SPDIF_ISTA_TXOSTA BIT(5)
#define SUN4I_SPDIF_ISTA_TXESTA BIT(4)
#define SUN4I_SPDIF_ISTA_RXOSTA BIT(1)
#define SUN4I_SPDIF_ISTA_RXASTA BIT(0)
#define SUN8I_SPDIF_TXFIFO (0x20)
#define SUN4I_SPDIF_TXCNT (0x24)
#define SUN4I_SPDIF_RXCNT (0x28)
#define SUN4I_SPDIF_TXCHSTA0 (0x2C)
#define SUN4I_SPDIF_TXCHSTA0_CLK(v) ((v) << 28)
#define SUN4I_SPDIF_TXCHSTA0_SAMFREQ(v) ((v) << 24)
#define SUN4I_SPDIF_TXCHSTA0_SAMFREQ_MASK GENMASK(27, 24)
#define SUN4I_SPDIF_TXCHSTA0_CHNUM(v) ((v) << 20)
#define SUN4I_SPDIF_TXCHSTA0_CHNUM_MASK GENMASK(23, 20)
#define SUN4I_SPDIF_TXCHSTA0_SRCNUM(v) ((v) << 16)
#define SUN4I_SPDIF_TXCHSTA0_CATACOD(v) ((v) << 8)
#define SUN4I_SPDIF_TXCHSTA0_MODE(v) ((v) << 6)
#define SUN4I_SPDIF_TXCHSTA0_EMPHASIS(v) ((v) << 3)
#define SUN4I_SPDIF_TXCHSTA0_CP BIT(2)
#define SUN4I_SPDIF_TXCHSTA0_AUDIO BIT(1)
#define SUN4I_SPDIF_TXCHSTA0_PRO BIT(0)
#define SUN4I_SPDIF_TXCHSTA1 (0x30)
#define SUN4I_SPDIF_TXCHSTA1_CGMSA(v) ((v) << 8)
#define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ(v) ((v) << 4)
#define SUN4I_SPDIF_TXCHSTA1_ORISAMFREQ_MASK GENMASK(7, 4)
#define SUN4I_SPDIF_TXCHSTA1_SAMWORDLEN(v) ((v) << 1)
#define SUN4I_SPDIF_TXCHSTA1_MAXWORDLEN BIT(0)
#define SUN4I_SPDIF_RXCHSTA0 (0x34)
#define SUN4I_SPDIF_RXCHSTA0_CLK(v) ((v) << 28)
#define SUN4I_SPDIF_RXCHSTA0_SAMFREQ(v) ((v) << 24)
#define SUN4I_SPDIF_RXCHSTA0_CHNUM(v) ((v) << 20)
#define SUN4I_SPDIF_RXCHSTA0_SRCNUM(v) ((v) << 16)
#define SUN4I_SPDIF_RXCHSTA0_CATACOD(v) ((v) << 8)
#define SUN4I_SPDIF_RXCHSTA0_MODE(v) ((v) << 6)
#define SUN4I_SPDIF_RXCHSTA0_EMPHASIS(v) ((v) << 3)
#define SUN4I_SPDIF_RXCHSTA0_CP BIT(2)
#define SUN4I_SPDIF_RXCHSTA0_AUDIO BIT(1)
#define SUN4I_SPDIF_RXCHSTA0_PRO BIT(0)
#define SUN4I_SPDIF_RXCHSTA1 (0x38)
#define SUN4I_SPDIF_RXCHSTA1_CGMSA(v) ((v) << 8)
#define SUN4I_SPDIF_RXCHSTA1_ORISAMFREQ(v) ((v) << 4)
#define SUN4I_SPDIF_RXCHSTA1_SAMWORDLEN(v) ((v) << 1)
#define SUN4I_SPDIF_RXCHSTA1_MAXWORDLEN BIT(0)
/* Defines for Sampling Frequency */
#define SUN4I_SPDIF_SAMFREQ_44_1KHZ 0x0
#define SUN4I_SPDIF_SAMFREQ_NOT_INDICATED 0x1
#define SUN4I_SPDIF_SAMFREQ_48KHZ 0x2
#define SUN4I_SPDIF_SAMFREQ_32KHZ 0x3
#define SUN4I_SPDIF_SAMFREQ_22_05KHZ 0x4
#define SUN4I_SPDIF_SAMFREQ_24KHZ 0x6
#define SUN4I_SPDIF_SAMFREQ_88_2KHZ 0x8
#define SUN4I_SPDIF_SAMFREQ_76_8KHZ 0x9
#define SUN4I_SPDIF_SAMFREQ_96KHZ 0xa
#define SUN4I_SPDIF_SAMFREQ_176_4KHZ 0xc
#define SUN4I_SPDIF_SAMFREQ_192KHZ 0xe
/**
* struct sun4i_spdif_quirks - Differences between SoC variants.
*
* @reg_dac_txdata: TX FIFO offset for DMA config.
* @has_reset: SoC needs reset deasserted.
* @val_fctl_ftx: TX FIFO flush bitmask.
* @mclk_multiplier: ratio of internal MCLK divider
* @tx_clk_name: name of TX module clock if split clock design
*/
struct sun4i_spdif_quirks {
unsigned int reg_dac_txdata;
bool has_reset;
unsigned int val_fctl_ftx;
unsigned int mclk_multiplier;
const char *tx_clk_name;
};
struct sun4i_spdif_dev {
struct platform_device *pdev;
struct clk *spdif_clk;
struct clk *apb_clk;
struct reset_control *rst;
struct snd_soc_dai_driver cpu_dai_drv;
struct regmap *regmap;
struct snd_dmaengine_dai_dma_data dma_params_tx;
const struct sun4i_spdif_quirks *quirks;
spinlock_t lock;
};
static void sun4i_spdif_configure(struct sun4i_spdif_dev *host)
{
const struct sun4i_spdif_quirks *quirks = host->quirks;
/* soft reset SPDIF */
regmap_write(host->regmap, SUN4I_SPDIF_CTL, SUN4I_SPDIF_CTL_RESET);
/* flush TX FIFO */
regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
quirks->val_fctl_ftx, quirks->val_fctl_ftx);
/* Valid data at the MSB of TXFIFO Register */
regmap_update_bits(host->regmap, SUN4I_SPDIF_FCTL,
SUN4I_SPDIF_FCTL_TXIM, 0);
/* clear TX counter */
regmap_write(host->regmap, SUN4I_SPDIF_TXCNT, 0);
}
static void sun4i_snd_txctrl_on(struct snd_pcm_substream *substream,
struct sun4i_spdif_dev *host)
{
if (substream->runtime->channels == 1)
regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
SUN4I_SPDIF_TXCFG_SINGLEMOD,
SUN4I_SPDIF_TXCFG_SINGLEMOD);
/* SPDIF TX ENABLE */
regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
SUN4I_SPDIF_TXCFG_TXEN, SUN4I_SPDIF_TXCFG_TXEN);
/* DRQ ENABLE */
regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
SUN4I_SPDIF_INT_TXDRQEN, SUN4I_SPDIF_INT_TXDRQEN);
/* Global enable */
regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
SUN4I_SPDIF_CTL_GEN, SUN4I_SPDIF_CTL_GEN);
}
static void sun4i_snd_txctrl_off(struct snd_pcm_substream *substream,
struct sun4i_spdif_dev *host)
{
/* SPDIF TX DISABLE */
regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
SUN4I_SPDIF_TXCFG_TXEN, 0);
/* DRQ DISABLE */
regmap_update_bits(host->regmap, SUN4I_SPDIF_INT,
SUN4I_SPDIF_INT_TXDRQEN, 0);
/* Global disable */
regmap_update_bits(host->regmap, SUN4I_SPDIF_CTL,
SUN4I_SPDIF_CTL_GEN, 0);
}
static int sun4i_spdif_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *cpu_dai)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
return -EINVAL;
sun4i_spdif_configure(host);
return 0;
}
static int sun4i_spdif_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *cpu_dai)
{
int ret = 0;
int fmt;
unsigned long rate = params_rate(params);
u32 mclk_div = 0;
unsigned int mclk = 0;
u32 reg_val;
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
struct platform_device *pdev = host->pdev;
/* Add the PCM and raw data select interface */
switch (params_channels(params)) {
case 1: /* PCM mode */
case 2:
fmt = 0;
break;
case 4: /* raw data mode */
fmt = SUN4I_SPDIF_TXCFG_NONAUDIO;
break;
default:
return -EINVAL;
}
host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S16_LE:
fmt |= SUN4I_SPDIF_TXCFG_FMT16BIT;
host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
break;
case SNDRV_PCM_FORMAT_S20_3LE:
fmt |= SUN4I_SPDIF_TXCFG_FMT20BIT;
break;
case SNDRV_PCM_FORMAT_S24_LE:
case SNDRV_PCM_FORMAT_S32_LE:
fmt |= SUN4I_SPDIF_TXCFG_FMT24BIT;
break;
default:
return -EINVAL;
}
switch (rate) {
case 22050:
case 44100:
case 88200:
case 176400:
mclk = 22579200;
break;
case 24000:
case 32000:
case 48000:
case 96000:
case 192000:
mclk = 24576000;
break;
default:
return -EINVAL;
}
mclk *= host->quirks->mclk_multiplier;
ret = clk_set_rate(host->spdif_clk, mclk);
if (ret < 0) {
dev_err(&pdev->dev,
"Setting SPDIF clock rate for %d Hz failed!\n", mclk);
return ret;
}
switch (rate) {
case 22050:
case 24000:
mclk_div = 8;
break;
case 32000:
mclk_div = 6;
break;
case 44100:
case 48000:
mclk_div = 4;
break;
case 88200:
case 96000:
mclk_div = 2;
break;
case 176400:
case 192000:
mclk_div = 1;
break;
default:
return -EINVAL;
}
mclk_div *= host->quirks->mclk_multiplier;
reg_val = 0;
reg_val |= SUN4I_SPDIF_TXCFG_ASS;
reg_val |= fmt; /* set non audio and bit depth */
reg_val |= SUN4I_SPDIF_TXCFG_CHSTMODE;
reg_val |= SUN4I_SPDIF_TXCFG_TXRATIO(mclk_div - 1);
regmap_write(host->regmap, SUN4I_SPDIF_TXCFG, reg_val);
return 0;
}
static int sun4i_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
struct snd_soc_dai *dai)
{
int ret = 0;
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
return -EINVAL;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME:
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
sun4i_snd_txctrl_on(substream, host);
break;
case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
sun4i_snd_txctrl_off(substream, host);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int sun4i_spdif_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
uinfo->count = 1;
return 0;
}
static int sun4i_spdif_get_status_mask(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
u8 *status = ucontrol->value.iec958.status;
status[0] = 0xff;
status[1] = 0xff;
status[2] = 0xff;
status[3] = 0xff;
status[4] = 0xff;
status[5] = 0x03;
return 0;
}
static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
u8 *status = ucontrol->value.iec958.status;
unsigned long flags;
unsigned int reg;
spin_lock_irqsave(&host->lock, flags);
regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA0, &reg);
status[0] = reg & 0xff;
status[1] = (reg >> 8) & 0xff;
status[2] = (reg >> 16) & 0xff;
status[3] = (reg >> 24) & 0xff;
regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA1, &reg);
status[4] = reg & 0xff;
status[5] = (reg >> 8) & 0x3;
spin_unlock_irqrestore(&host->lock, flags);
return 0;
}
static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
u8 *status = ucontrol->value.iec958.status;
unsigned long flags;
unsigned int reg;
bool chg0, chg1;
spin_lock_irqsave(&host->lock, flags);
reg = (u32)status[3] << 24;
reg |= (u32)status[2] << 16;
reg |= (u32)status[1] << 8;
reg |= (u32)status[0];
regmap_update_bits_check(host->regmap, SUN4I_SPDIF_TXCHSTA0,
GENMASK(31,0), reg, &chg0);
reg = (u32)status[5] << 8;
reg |= (u32)status[4];
regmap_update_bits_check(host->regmap, SUN4I_SPDIF_TXCHSTA1,
GENMASK(9,0), reg, &chg1);
reg = SUN4I_SPDIF_TXCFG_CHSTMODE;
if (status[0] & IEC958_AES0_NONAUDIO)
reg |= SUN4I_SPDIF_TXCFG_NONAUDIO;
regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG,
SUN4I_SPDIF_TXCFG_CHSTMODE |
SUN4I_SPDIF_TXCFG_NONAUDIO, reg);
spin_unlock_irqrestore(&host->lock, flags);
return chg0 || chg1;
}
static struct snd_kcontrol_new sun4i_spdif_controls[] = {
{
.access = SNDRV_CTL_ELEM_ACCESS_READ,
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
.info = sun4i_spdif_info,
.get = sun4i_spdif_get_status_mask
},
{
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
.info = sun4i_spdif_info,
.get = sun4i_spdif_get_status,
.put = sun4i_spdif_set_status
}
};
static int sun4i_spdif_soc_dai_probe(struct snd_soc_dai *dai)
{
struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(dai);
snd_soc_dai_init_dma_data(dai, &host->dma_params_tx, NULL);
snd_soc_add_dai_controls(dai, sun4i_spdif_controls,
ARRAY_SIZE(sun4i_spdif_controls));
return 0;
}
static const struct snd_soc_dai_ops sun4i_spdif_dai_ops = {
.probe = sun4i_spdif_soc_dai_probe,
.startup = sun4i_spdif_startup,
.trigger = sun4i_spdif_trigger,
.hw_params = sun4i_spdif_hw_params,
};
static const struct regmap_config sun4i_spdif_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = SUN4I_SPDIF_RXCHSTA1,
};
#define SUN4I_RATES SNDRV_PCM_RATE_8000_192000
#define SUN4I_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
SNDRV_PCM_FMTBIT_S20_3LE | \
SNDRV_PCM_FMTBIT_S24_LE | \
SNDRV_PCM_FMTBIT_S32_LE)
static struct snd_soc_dai_driver sun4i_spdif_dai = {
.playback = {
.channels_min = 1,
.channels_max = 2,
.rates = SUN4I_RATES,
.formats = SUN4I_FORMATS,
},
.ops = &sun4i_spdif_dai_ops,
.name = "spdif",
};
static const struct sun4i_spdif_quirks sun4i_a10_spdif_quirks = {
.reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
.val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
.mclk_multiplier = 1,
};
static const struct sun4i_spdif_quirks sun6i_a31_spdif_quirks = {
.reg_dac_txdata = SUN4I_SPDIF_TXFIFO,
.val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
.has_reset = true,
.mclk_multiplier = 1,
};
static const struct sun4i_spdif_quirks sun8i_h3_spdif_quirks = {
.reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
.val_fctl_ftx = SUN4I_SPDIF_FCTL_FTX,
.has_reset = true,
.mclk_multiplier = 4,
};
static const struct sun4i_spdif_quirks sun50i_h6_spdif_quirks = {
.reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
.val_fctl_ftx = SUN50I_H6_SPDIF_FCTL_FTX,
.has_reset = true,
.mclk_multiplier = 1,
};
static const struct sun4i_spdif_quirks sun55i_a523_spdif_quirks = {
.reg_dac_txdata = SUN8I_SPDIF_TXFIFO,
.val_fctl_ftx = SUN50I_H6_SPDIF_FCTL_FTX,
.has_reset = true,
.mclk_multiplier = 1,
.tx_clk_name = "tx",
};
static const struct of_device_id sun4i_spdif_of_match[] = {
{
.compatible = "allwinner,sun4i-a10-spdif",
.data = &sun4i_a10_spdif_quirks,
},
{
.compatible = "allwinner,sun6i-a31-spdif",
.data = &sun6i_a31_spdif_quirks,
},
{
.compatible = "allwinner,sun8i-h3-spdif",
.data = &sun8i_h3_spdif_quirks,
},
{
.compatible = "allwinner,sun50i-h6-spdif",
.data = &sun50i_h6_spdif_quirks,
},
{
.compatible = "allwinner,sun50i-h616-spdif",
/* Essentially the same as the H6, but without RX */
.data = &sun50i_h6_spdif_quirks,
},
{
.compatible = "allwinner,sun55i-a523-spdif",
/*
* Almost the same as H6, but has split the TX and RX clocks,
* has a separate reset bit for the RX side, and has some
* expanded features for the RX side.
*/
.data = &sun55i_a523_spdif_quirks,
},
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, sun4i_spdif_of_match);
static const struct snd_soc_component_driver sun4i_spdif_component = {
.name = "sun4i-spdif",
.legacy_dai_naming = 1,
};
static int sun4i_spdif_runtime_suspend(struct device *dev)
{
struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
clk_disable_unprepare(host->spdif_clk);
clk_disable_unprepare(host->apb_clk);
return 0;
}
static int sun4i_spdif_runtime_resume(struct device *dev)
{
struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
int ret;
ret = clk_prepare_enable(host->spdif_clk);
if (ret)
return ret;
ret = clk_prepare_enable(host->apb_clk);
if (ret)
clk_disable_unprepare(host->spdif_clk);
return ret;
}
static int sun4i_spdif_probe(struct platform_device *pdev)
{
struct sun4i_spdif_dev *host;
struct resource *res;
const struct sun4i_spdif_quirks *quirks;
int ret;
void __iomem *base;
const char *tx_clk_name = "spdif";
dev_dbg(&pdev->dev, "Entered %s\n", __func__);
host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
if (!host)
return -ENOMEM;
host->pdev = pdev;
spin_lock_init(&host->lock);
/* Initialize this copy of the CPU DAI driver structure */
memcpy(&host->cpu_dai_drv, &sun4i_spdif_dai, sizeof(sun4i_spdif_dai));
host->cpu_dai_drv.name = dev_name(&pdev->dev);
/* Get the addresses */
base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
if (IS_ERR(base))
return PTR_ERR(base);
quirks = of_device_get_match_data(&pdev->dev);
if (quirks == NULL) {
dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
return -ENODEV;
}
host->quirks = quirks;
host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
&sun4i_spdif_regmap_config);
if (IS_ERR(host->regmap)) {
dev_err(&pdev->dev, "failed to initialise regmap.\n");
return PTR_ERR(host->regmap);
}
/* Clocks */
host->apb_clk = devm_clk_get(&pdev->dev, "apb");
if (IS_ERR(host->apb_clk)) {
dev_err(&pdev->dev, "failed to get a apb clock.\n");
return PTR_ERR(host->apb_clk);
}
if (quirks->tx_clk_name)
tx_clk_name = quirks->tx_clk_name;
host->spdif_clk = devm_clk_get(&pdev->dev, tx_clk_name);
if (IS_ERR(host->spdif_clk)) {
dev_err(&pdev->dev, "failed to get the \"%s\" clock.\n",
tx_clk_name);
return PTR_ERR(host->spdif_clk);
}
host->dma_params_tx.addr = res->start + quirks->reg_dac_txdata;
host->dma_params_tx.maxburst = 8;
host->dma_params_tx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
platform_set_drvdata(pdev, host);
if (quirks->has_reset) {
host->rst = devm_reset_control_get_exclusive_deasserted(&pdev->dev, NULL);
if (IS_ERR(host->rst))
return dev_err_probe(&pdev->dev, PTR_ERR(host->rst),
"Failed to get reset\n");
}
ret = devm_snd_soc_register_component(&pdev->dev,
&sun4i_spdif_component, &sun4i_spdif_dai, 1);
if (ret)
return ret;
pm_runtime_enable(&pdev->dev);
if (!pm_runtime_enabled(&pdev->dev)) {
ret = sun4i_spdif_runtime_resume(&pdev->dev);
if (ret)
goto err_unregister;
}
ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
if (ret)
goto err_suspend;
return 0;
err_suspend:
if (!pm_runtime_status_suspended(&pdev->dev))
sun4i_spdif_runtime_suspend(&pdev->dev);
err_unregister:
pm_runtime_disable(&pdev->dev);
return ret;
}
static void sun4i_spdif_remove(struct platform_device *pdev)
{
pm_runtime_disable(&pdev->dev);
if (!pm_runtime_status_suspended(&pdev->dev))
sun4i_spdif_runtime_suspend(&pdev->dev);
}
static const struct dev_pm_ops sun4i_spdif_pm = {
RUNTIME_PM_OPS(sun4i_spdif_runtime_suspend,
sun4i_spdif_runtime_resume, NULL)
};
static struct platform_driver sun4i_spdif_driver = {
.driver = {
.name = "sun4i-spdif",
.of_match_table = sun4i_spdif_of_match,
.pm = pm_ptr(&sun4i_spdif_pm),
},
.probe = sun4i_spdif_probe,
.remove = sun4i_spdif_remove,
};
module_platform_driver(sun4i_spdif_driver);
MODULE_AUTHOR("Marcus Cooper <codekipper@gmail.com>");
MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
MODULE_DESCRIPTION("Allwinner sun4i SPDIF SoC Interface");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sun4i-spdif");
@@ -0,0 +1,595 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* This driver supports the analog controls for the internal codec
* found in Allwinner's A64 SoC.
*
* Copyright (C) 2016 Chen-Yu Tsai <wens@csie.org>
* Copyright (C) 2017 Marcus Cooper <codekipper@gmail.com>
* Copyright (C) 2018 Vasily Khoruzhick <anarsoul@gmail.com>
*
* Based on sun8i-codec-analog.c
*
*/
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/tlv.h>
#include "sun8i-adda-pr-regmap.h"
/* Codec analog control register offsets and bit fields */
#define SUN50I_ADDA_HP_CTRL 0x00
#define SUN50I_ADDA_HP_CTRL_PA_CLK_GATE 7
#define SUN50I_ADDA_HP_CTRL_HPPA_EN 6
#define SUN50I_ADDA_HP_CTRL_HPVOL 0
#define SUN50I_ADDA_OL_MIX_CTRL 0x01
#define SUN50I_ADDA_OL_MIX_CTRL_MIC1 6
#define SUN50I_ADDA_OL_MIX_CTRL_MIC2 5
#define SUN50I_ADDA_OL_MIX_CTRL_PHONE 4
#define SUN50I_ADDA_OL_MIX_CTRL_PHONEN 3
#define SUN50I_ADDA_OL_MIX_CTRL_LINEINL 2
#define SUN50I_ADDA_OL_MIX_CTRL_DACL 1
#define SUN50I_ADDA_OL_MIX_CTRL_DACR 0
#define SUN50I_ADDA_OR_MIX_CTRL 0x02
#define SUN50I_ADDA_OR_MIX_CTRL_MIC1 6
#define SUN50I_ADDA_OR_MIX_CTRL_MIC2 5
#define SUN50I_ADDA_OR_MIX_CTRL_PHONE 4
#define SUN50I_ADDA_OR_MIX_CTRL_PHONEP 3
#define SUN50I_ADDA_OR_MIX_CTRL_LINEINR 2
#define SUN50I_ADDA_OR_MIX_CTRL_DACR 1
#define SUN50I_ADDA_OR_MIX_CTRL_DACL 0
#define SUN50I_ADDA_EARPIECE_CTRL0 0x03
#define SUN50I_ADDA_EARPIECE_CTRL0_EAR_RAMP_TIME 4
#define SUN50I_ADDA_EARPIECE_CTRL0_ESPSR 0
#define SUN50I_ADDA_EARPIECE_CTRL1 0x04
#define SUN50I_ADDA_EARPIECE_CTRL1_ESPPA_EN 7
#define SUN50I_ADDA_EARPIECE_CTRL1_ESPPA_MUTE 6
#define SUN50I_ADDA_EARPIECE_CTRL1_ESP_VOL 0
#define SUN50I_ADDA_LINEOUT_CTRL0 0x05
#define SUN50I_ADDA_LINEOUT_CTRL0_LEN 7
#define SUN50I_ADDA_LINEOUT_CTRL0_REN 6
#define SUN50I_ADDA_LINEOUT_CTRL0_LSRC_SEL 5
#define SUN50I_ADDA_LINEOUT_CTRL0_RSRC_SEL 4
#define SUN50I_ADDA_LINEOUT_CTRL1 0x06
#define SUN50I_ADDA_LINEOUT_CTRL1_VOL 0
#define SUN50I_ADDA_MIC1_CTRL 0x07
#define SUN50I_ADDA_MIC1_CTRL_MIC1G 4
#define SUN50I_ADDA_MIC1_CTRL_MIC1AMPEN 3
#define SUN50I_ADDA_MIC1_CTRL_MIC1BOOST 0
#define SUN50I_ADDA_MIC2_CTRL 0x08
#define SUN50I_ADDA_MIC2_CTRL_MIC2G 4
#define SUN50I_ADDA_MIC2_CTRL_MIC2AMPEN 3
#define SUN50I_ADDA_MIC2_CTRL_MIC2BOOST 0
#define SUN50I_ADDA_LINEIN_CTRL 0x09
#define SUN50I_ADDA_LINEIN_CTRL_LINEING 0
#define SUN50I_ADDA_MIX_DAC_CTRL 0x0a
#define SUN50I_ADDA_MIX_DAC_CTRL_DACAREN 7
#define SUN50I_ADDA_MIX_DAC_CTRL_DACALEN 6
#define SUN50I_ADDA_MIX_DAC_CTRL_RMIXEN 5
#define SUN50I_ADDA_MIX_DAC_CTRL_LMIXEN 4
#define SUN50I_ADDA_MIX_DAC_CTRL_RHPPAMUTE 3
#define SUN50I_ADDA_MIX_DAC_CTRL_LHPPAMUTE 2
#define SUN50I_ADDA_MIX_DAC_CTRL_RHPIS 1
#define SUN50I_ADDA_MIX_DAC_CTRL_LHPIS 0
#define SUN50I_ADDA_L_ADCMIX_SRC 0x0b
#define SUN50I_ADDA_L_ADCMIX_SRC_MIC1 6
#define SUN50I_ADDA_L_ADCMIX_SRC_MIC2 5
#define SUN50I_ADDA_L_ADCMIX_SRC_PHONE 4
#define SUN50I_ADDA_L_ADCMIX_SRC_PHONEN 3
#define SUN50I_ADDA_L_ADCMIX_SRC_LINEINL 2
#define SUN50I_ADDA_L_ADCMIX_SRC_OMIXRL 1
#define SUN50I_ADDA_L_ADCMIX_SRC_OMIXRR 0
#define SUN50I_ADDA_R_ADCMIX_SRC 0x0c
#define SUN50I_ADDA_R_ADCMIX_SRC_MIC1 6
#define SUN50I_ADDA_R_ADCMIX_SRC_MIC2 5
#define SUN50I_ADDA_R_ADCMIX_SRC_PHONE 4
#define SUN50I_ADDA_R_ADCMIX_SRC_PHONEP 3
#define SUN50I_ADDA_R_ADCMIX_SRC_LINEINR 2
#define SUN50I_ADDA_R_ADCMIX_SRC_OMIXR 1
#define SUN50I_ADDA_R_ADCMIX_SRC_OMIXL 0
#define SUN50I_ADDA_ADC_CTRL 0x0d
#define SUN50I_ADDA_ADC_CTRL_ADCREN 7
#define SUN50I_ADDA_ADC_CTRL_ADCLEN 6
#define SUN50I_ADDA_ADC_CTRL_ADCG 0
#define SUN50I_ADDA_HS_MBIAS_CTRL 0x0e
#define SUN50I_ADDA_HS_MBIAS_CTRL_MMICBIASEN 7
#define SUN50I_ADDA_MDET_CTRL 0x1c
#define SUN50I_ADDA_MDET_CTRL_SELDETADC_FS 4
#define SUN50I_ADDA_MDET_CTRL_SELDETADC_DB 2
#define SUN50I_ADDA_MDET_CTRL_SELDETADC_BF 0
#define SUN50I_ADDA_JACK_MIC_CTRL 0x1d
#define SUN50I_ADDA_JACK_MIC_CTRL_JACKDETEN 7
#define SUN50I_ADDA_JACK_MIC_CTRL_INNERRESEN 6
#define SUN50I_ADDA_JACK_MIC_CTRL_HMICBIASEN 5
#define SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN 4
/* mixer controls */
static const struct snd_kcontrol_new sun50i_a64_codec_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
SUN50I_ADDA_OL_MIX_CTRL,
SUN50I_ADDA_OR_MIX_CTRL,
SUN50I_ADDA_OL_MIX_CTRL_MIC1, 1, 0),
SOC_DAPM_DOUBLE_R("Mic2 Playback Switch",
SUN50I_ADDA_OL_MIX_CTRL,
SUN50I_ADDA_OR_MIX_CTRL,
SUN50I_ADDA_OL_MIX_CTRL_MIC2, 1, 0),
SOC_DAPM_DOUBLE_R("Line In Playback Switch",
SUN50I_ADDA_OL_MIX_CTRL,
SUN50I_ADDA_OR_MIX_CTRL,
SUN50I_ADDA_OL_MIX_CTRL_LINEINL, 1, 0),
SOC_DAPM_DOUBLE_R("DAC Playback Switch",
SUN50I_ADDA_OL_MIX_CTRL,
SUN50I_ADDA_OR_MIX_CTRL,
SUN50I_ADDA_OL_MIX_CTRL_DACL, 1, 0),
SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
SUN50I_ADDA_OL_MIX_CTRL,
SUN50I_ADDA_OR_MIX_CTRL,
SUN50I_ADDA_OL_MIX_CTRL_DACR, 1, 0),
};
/* ADC mixer controls */
static const struct snd_kcontrol_new sun50i_codec_adc_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
SUN50I_ADDA_L_ADCMIX_SRC,
SUN50I_ADDA_R_ADCMIX_SRC,
SUN50I_ADDA_L_ADCMIX_SRC_MIC1, 1, 0),
SOC_DAPM_DOUBLE_R("Mic2 Capture Switch",
SUN50I_ADDA_L_ADCMIX_SRC,
SUN50I_ADDA_R_ADCMIX_SRC,
SUN50I_ADDA_L_ADCMIX_SRC_MIC2, 1, 0),
SOC_DAPM_DOUBLE_R("Line In Capture Switch",
SUN50I_ADDA_L_ADCMIX_SRC,
SUN50I_ADDA_R_ADCMIX_SRC,
SUN50I_ADDA_L_ADCMIX_SRC_LINEINL, 1, 0),
SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
SUN50I_ADDA_L_ADCMIX_SRC,
SUN50I_ADDA_R_ADCMIX_SRC,
SUN50I_ADDA_L_ADCMIX_SRC_OMIXRL, 1, 0),
SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
SUN50I_ADDA_L_ADCMIX_SRC,
SUN50I_ADDA_R_ADCMIX_SRC,
SUN50I_ADDA_L_ADCMIX_SRC_OMIXRR, 1, 0),
};
static const DECLARE_TLV_DB_SCALE(sun50i_codec_out_mixer_pregain_scale,
-450, 150, 0);
static const DECLARE_TLV_DB_RANGE(sun50i_codec_mic_gain_scale,
0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
);
static const DECLARE_TLV_DB_SCALE(sun50i_codec_hp_vol_scale, -6300, 100, 1);
static const DECLARE_TLV_DB_RANGE(sun50i_codec_lineout_vol_scale,
0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
);
static const DECLARE_TLV_DB_RANGE(sun50i_codec_earpiece_vol_scale,
0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
);
/* volume / mute controls */
static const struct snd_kcontrol_new sun50i_a64_codec_controls[] = {
SOC_SINGLE_TLV("Headphone Playback Volume",
SUN50I_ADDA_HP_CTRL,
SUN50I_ADDA_HP_CTRL_HPVOL, 0x3f, 0,
sun50i_codec_hp_vol_scale),
/* Mixer pre-gain */
SOC_SINGLE_TLV("Mic1 Playback Volume", SUN50I_ADDA_MIC1_CTRL,
SUN50I_ADDA_MIC1_CTRL_MIC1G,
0x7, 0, sun50i_codec_out_mixer_pregain_scale),
/* Microphone Amp boost gain */
SOC_SINGLE_TLV("Mic1 Boost Volume", SUN50I_ADDA_MIC1_CTRL,
SUN50I_ADDA_MIC1_CTRL_MIC1BOOST, 0x7, 0,
sun50i_codec_mic_gain_scale),
/* Mixer pre-gain */
SOC_SINGLE_TLV("Mic2 Playback Volume",
SUN50I_ADDA_MIC2_CTRL, SUN50I_ADDA_MIC2_CTRL_MIC2G,
0x7, 0, sun50i_codec_out_mixer_pregain_scale),
/* Microphone Amp boost gain */
SOC_SINGLE_TLV("Mic2 Boost Volume", SUN50I_ADDA_MIC2_CTRL,
SUN50I_ADDA_MIC2_CTRL_MIC2BOOST, 0x7, 0,
sun50i_codec_mic_gain_scale),
/* ADC */
SOC_SINGLE_TLV("ADC Gain Capture Volume", SUN50I_ADDA_ADC_CTRL,
SUN50I_ADDA_ADC_CTRL_ADCG, 0x7, 0,
sun50i_codec_out_mixer_pregain_scale),
/* Mixer pre-gain */
SOC_SINGLE_TLV("Line In Playback Volume", SUN50I_ADDA_LINEIN_CTRL,
SUN50I_ADDA_LINEIN_CTRL_LINEING,
0x7, 0, sun50i_codec_out_mixer_pregain_scale),
SOC_SINGLE_TLV("Line Out Playback Volume",
SUN50I_ADDA_LINEOUT_CTRL1,
SUN50I_ADDA_LINEOUT_CTRL1_VOL, 0x1f, 0,
sun50i_codec_lineout_vol_scale),
SOC_SINGLE_TLV("Earpiece Playback Volume",
SUN50I_ADDA_EARPIECE_CTRL1,
SUN50I_ADDA_EARPIECE_CTRL1_ESP_VOL, 0x1f, 0,
sun50i_codec_earpiece_vol_scale),
};
static const char * const sun50i_codec_hp_src_enum_text[] = {
"DAC", "Mixer",
};
static SOC_ENUM_DOUBLE_DECL(sun50i_codec_hp_src_enum,
SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_LHPIS,
SUN50I_ADDA_MIX_DAC_CTRL_RHPIS,
sun50i_codec_hp_src_enum_text);
static const struct snd_kcontrol_new sun50i_codec_hp_src[] = {
SOC_DAPM_ENUM("Headphone Source Playback Route",
sun50i_codec_hp_src_enum),
};
static const struct snd_kcontrol_new sun50i_codec_hp_switch =
SOC_DAPM_DOUBLE("Headphone Playback Switch",
SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_LHPPAMUTE,
SUN50I_ADDA_MIX_DAC_CTRL_RHPPAMUTE, 1, 0);
static const char * const sun50i_codec_lineout_src_enum_text[] = {
"Stereo", "Mono Differential",
};
static SOC_ENUM_DOUBLE_DECL(sun50i_codec_lineout_src_enum,
SUN50I_ADDA_LINEOUT_CTRL0,
SUN50I_ADDA_LINEOUT_CTRL0_LSRC_SEL,
SUN50I_ADDA_LINEOUT_CTRL0_RSRC_SEL,
sun50i_codec_lineout_src_enum_text);
static const struct snd_kcontrol_new sun50i_codec_lineout_src[] = {
SOC_DAPM_ENUM("Line Out Source Playback Route",
sun50i_codec_lineout_src_enum),
};
static const struct snd_kcontrol_new sun50i_codec_lineout_switch =
SOC_DAPM_DOUBLE("Line Out Playback Switch",
SUN50I_ADDA_LINEOUT_CTRL0,
SUN50I_ADDA_LINEOUT_CTRL0_LEN,
SUN50I_ADDA_LINEOUT_CTRL0_REN, 1, 0);
static const char * const sun50i_codec_earpiece_src_enum_text[] = {
"DACR", "DACL", "Right Mixer", "Left Mixer",
};
static SOC_ENUM_SINGLE_DECL(sun50i_codec_earpiece_src_enum,
SUN50I_ADDA_EARPIECE_CTRL0,
SUN50I_ADDA_EARPIECE_CTRL0_ESPSR,
sun50i_codec_earpiece_src_enum_text);
static const struct snd_kcontrol_new sun50i_codec_earpiece_src[] = {
SOC_DAPM_ENUM("Earpiece Source Playback Route",
sun50i_codec_earpiece_src_enum),
};
static const struct snd_kcontrol_new sun50i_codec_earpiece_switch[] = {
SOC_DAPM_SINGLE("Earpiece Playback Switch",
SUN50I_ADDA_EARPIECE_CTRL1,
SUN50I_ADDA_EARPIECE_CTRL1_ESPPA_MUTE, 1, 0),
};
static int sun50i_codec_hbias_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
u32 value = !!SND_SOC_DAPM_EVENT_ON(event);
regmap_update_bits(component->regmap, SUN50I_ADDA_JACK_MIC_CTRL,
BIT(SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN),
value << SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN);
return 0;
}
static const struct snd_soc_dapm_widget sun50i_a64_codec_widgets[] = {
/* DAC */
SND_SOC_DAPM_DAC("Left DAC", NULL, SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_DACALEN, 0),
SND_SOC_DAPM_DAC("Right DAC", NULL, SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_DACAREN, 0),
/* ADC */
SND_SOC_DAPM_ADC("Left ADC", NULL, SUN50I_ADDA_ADC_CTRL,
SUN50I_ADDA_ADC_CTRL_ADCLEN, 0),
SND_SOC_DAPM_ADC("Right ADC", NULL, SUN50I_ADDA_ADC_CTRL,
SUN50I_ADDA_ADC_CTRL_ADCREN, 0),
/*
* Due to this component and the codec belonging to separate DAPM
* contexts, we need to manually link the above widgets to their
* stream widgets at the card level.
*/
SND_SOC_DAPM_REGULATOR_SUPPLY("cpvdd", 0, 0),
SND_SOC_DAPM_MUX("Left Headphone Source",
SND_SOC_NOPM, 0, 0, sun50i_codec_hp_src),
SND_SOC_DAPM_MUX("Right Headphone Source",
SND_SOC_NOPM, 0, 0, sun50i_codec_hp_src),
SND_SOC_DAPM_SWITCH("Left Headphone Switch",
SND_SOC_NOPM, 0, 0, &sun50i_codec_hp_switch),
SND_SOC_DAPM_SWITCH("Right Headphone Switch",
SND_SOC_NOPM, 0, 0, &sun50i_codec_hp_switch),
SND_SOC_DAPM_OUT_DRV("Left Headphone Amp",
SND_SOC_NOPM, 0, 0, NULL, 0),
SND_SOC_DAPM_OUT_DRV("Right Headphone Amp",
SND_SOC_NOPM, 0, 0, NULL, 0),
SND_SOC_DAPM_SUPPLY("Headphone Amp", SUN50I_ADDA_HP_CTRL,
SUN50I_ADDA_HP_CTRL_HPPA_EN, 0, NULL, 0),
SND_SOC_DAPM_OUTPUT("HP"),
SND_SOC_DAPM_MUX("Left Line Out Source",
SND_SOC_NOPM, 0, 0, sun50i_codec_lineout_src),
SND_SOC_DAPM_MUX("Right Line Out Source",
SND_SOC_NOPM, 0, 0, sun50i_codec_lineout_src),
SND_SOC_DAPM_SWITCH("Left Line Out Switch",
SND_SOC_NOPM, 0, 0, &sun50i_codec_lineout_switch),
SND_SOC_DAPM_SWITCH("Right Line Out Switch",
SND_SOC_NOPM, 0, 0, &sun50i_codec_lineout_switch),
SND_SOC_DAPM_OUTPUT("LINEOUT"),
SND_SOC_DAPM_MUX("Earpiece Source Playback Route",
SND_SOC_NOPM, 0, 0, sun50i_codec_earpiece_src),
SOC_MIXER_NAMED_CTL_ARRAY("Earpiece Switch",
SND_SOC_NOPM, 0, 0,
sun50i_codec_earpiece_switch),
SND_SOC_DAPM_OUT_DRV("Earpiece Amp", SUN50I_ADDA_EARPIECE_CTRL1,
SUN50I_ADDA_EARPIECE_CTRL1_ESPPA_EN, 0, NULL, 0),
SND_SOC_DAPM_OUTPUT("EARPIECE"),
/* Microphone inputs */
SND_SOC_DAPM_INPUT("MIC1"),
/* Microphone Bias */
SND_SOC_DAPM_SUPPLY("MBIAS", SUN50I_ADDA_HS_MBIAS_CTRL,
SUN50I_ADDA_HS_MBIAS_CTRL_MMICBIASEN,
0, NULL, 0),
/* Mic input path */
SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN50I_ADDA_MIC1_CTRL,
SUN50I_ADDA_MIC1_CTRL_MIC1AMPEN, 0, NULL, 0),
/* Microphone input */
SND_SOC_DAPM_INPUT("MIC2"),
/* Microphone Bias */
SND_SOC_DAPM_SUPPLY("HBIAS", SUN50I_ADDA_JACK_MIC_CTRL,
SUN50I_ADDA_JACK_MIC_CTRL_HMICBIASEN,
0, sun50i_codec_hbias_event,
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
/* Mic input path */
SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN50I_ADDA_MIC2_CTRL,
SUN50I_ADDA_MIC2_CTRL_MIC2AMPEN, 0, NULL, 0),
/* Line input */
SND_SOC_DAPM_INPUT("LINEIN"),
/* Mixers */
SND_SOC_DAPM_MIXER("Left Mixer", SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_LMIXEN, 0,
sun50i_a64_codec_mixer_controls,
ARRAY_SIZE(sun50i_a64_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Right Mixer", SUN50I_ADDA_MIX_DAC_CTRL,
SUN50I_ADDA_MIX_DAC_CTRL_RMIXEN, 0,
sun50i_a64_codec_mixer_controls,
ARRAY_SIZE(sun50i_a64_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Left ADC Mixer", SND_SOC_NOPM, 0, 0,
sun50i_codec_adc_mixer_controls,
ARRAY_SIZE(sun50i_codec_adc_mixer_controls)),
SND_SOC_DAPM_MIXER("Right ADC Mixer", SND_SOC_NOPM, 0, 0,
sun50i_codec_adc_mixer_controls,
ARRAY_SIZE(sun50i_codec_adc_mixer_controls)),
};
static const struct snd_soc_dapm_route sun50i_a64_codec_routes[] = {
/* Left Mixer Routes */
{ "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
{ "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
{ "Left Mixer", "Line In Playback Switch", "LINEIN" },
{ "Left Mixer", "DAC Playback Switch", "Left DAC" },
{ "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
/* Right Mixer Routes */
{ "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
{ "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
{ "Right Mixer", "Line In Playback Switch", "LINEIN" },
{ "Right Mixer", "DAC Playback Switch", "Right DAC" },
{ "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
/* Left ADC Mixer Routes */
{ "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
{ "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
{ "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
{ "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
{ "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
/* Right ADC Mixer Routes */
{ "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
{ "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
{ "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
{ "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
{ "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
/* ADC Routes */
{ "Left ADC", NULL, "Left ADC Mixer" },
{ "Right ADC", NULL, "Right ADC Mixer" },
/* Headphone Routes */
{ "Left Headphone Source", "DAC", "Left DAC" },
{ "Left Headphone Source", "Mixer", "Left Mixer" },
{ "Left Headphone Switch", "Headphone Playback Switch", "Left Headphone Source" },
{ "Left Headphone Amp", NULL, "Left Headphone Switch" },
{ "Left Headphone Amp", NULL, "Headphone Amp" },
{ "HP", NULL, "Left Headphone Amp" },
{ "Right Headphone Source", "DAC", "Right DAC" },
{ "Right Headphone Source", "Mixer", "Right Mixer" },
{ "Right Headphone Switch", "Headphone Playback Switch", "Right Headphone Source" },
{ "Right Headphone Amp", NULL, "Right Headphone Switch" },
{ "Right Headphone Amp", NULL, "Headphone Amp" },
{ "HP", NULL, "Right Headphone Amp" },
{ "Headphone Amp", NULL, "cpvdd" },
/* Microphone Routes */
{ "Mic1 Amplifier", NULL, "MIC1"},
/* Microphone Routes */
{ "Mic2 Amplifier", NULL, "MIC2"},
/* Line-out Routes */
{ "Left Line Out Source", "Stereo", "Left Mixer" },
{ "Left Line Out Source", "Mono Differential", "Left Mixer" },
{ "Left Line Out Source", "Mono Differential", "Right Mixer" },
{ "Left Line Out Switch", "Line Out Playback Switch", "Left Line Out Source" },
{ "LINEOUT", NULL, "Left Line Out Switch" },
{ "Right Line Out Switch", "Line Out Playback Switch", "Right Mixer" },
{ "Right Line Out Source", "Stereo", "Right Line Out Switch" },
{ "Right Line Out Source", "Mono Differential", "Left Line Out Switch" },
{ "LINEOUT", NULL, "Right Line Out Source" },
/* Earpiece Routes */
{ "Earpiece Source Playback Route", "DACL", "Left DAC" },
{ "Earpiece Source Playback Route", "DACR", "Right DAC" },
{ "Earpiece Source Playback Route", "Left Mixer", "Left Mixer" },
{ "Earpiece Source Playback Route", "Right Mixer", "Right Mixer" },
{ "Earpiece Switch", "Earpiece Playback Switch", "Earpiece Source Playback Route" },
{ "Earpiece Amp", NULL, "Earpiece Switch" },
{ "EARPIECE", NULL, "Earpiece Amp" },
};
static int sun50i_a64_codec_set_bias_level(struct snd_soc_component *component,
enum snd_soc_bias_level level)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
int hbias;
switch (level) {
case SND_SOC_BIAS_OFF:
regmap_clear_bits(component->regmap, SUN50I_ADDA_JACK_MIC_CTRL,
BIT(SUN50I_ADDA_JACK_MIC_CTRL_JACKDETEN) |
BIT(SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN));
regmap_set_bits(component->regmap, SUN50I_ADDA_HP_CTRL,
BIT(SUN50I_ADDA_HP_CTRL_PA_CLK_GATE));
break;
case SND_SOC_BIAS_STANDBY:
regmap_clear_bits(component->regmap, SUN50I_ADDA_HP_CTRL,
BIT(SUN50I_ADDA_HP_CTRL_PA_CLK_GATE));
hbias = snd_soc_dapm_get_pin_status(dapm, "HBIAS");
regmap_update_bits(component->regmap, SUN50I_ADDA_JACK_MIC_CTRL,
BIT(SUN50I_ADDA_JACK_MIC_CTRL_JACKDETEN) |
BIT(SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN),
BIT(SUN50I_ADDA_JACK_MIC_CTRL_JACKDETEN) |
hbias << SUN50I_ADDA_JACK_MIC_CTRL_MICADCEN);
break;
default:
break;
}
return 0;
}
static const struct snd_soc_component_driver sun50i_codec_analog_cmpnt_drv = {
.controls = sun50i_a64_codec_controls,
.num_controls = ARRAY_SIZE(sun50i_a64_codec_controls),
.dapm_widgets = sun50i_a64_codec_widgets,
.num_dapm_widgets = ARRAY_SIZE(sun50i_a64_codec_widgets),
.dapm_routes = sun50i_a64_codec_routes,
.num_dapm_routes = ARRAY_SIZE(sun50i_a64_codec_routes),
.set_bias_level = sun50i_a64_codec_set_bias_level,
.idle_bias_on = true,
.suspend_bias_off = true,
};
static const struct of_device_id sun50i_codec_analog_of_match[] = {
{
.compatible = "allwinner,sun50i-a64-codec-analog",
},
{}
};
MODULE_DEVICE_TABLE(of, sun50i_codec_analog_of_match);
static int sun50i_codec_analog_probe(struct platform_device *pdev)
{
struct regmap *regmap;
void __iomem *base;
bool enable;
base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(base)) {
dev_err(&pdev->dev, "Failed to map the registers\n");
return PTR_ERR(base);
}
regmap = sun8i_adda_pr_regmap_init(&pdev->dev, base);
if (IS_ERR(regmap)) {
dev_err(&pdev->dev, "Failed to create regmap\n");
return PTR_ERR(regmap);
}
enable = device_property_read_bool(&pdev->dev,
"allwinner,internal-bias-resistor");
regmap_update_bits(regmap, SUN50I_ADDA_JACK_MIC_CTRL,
BIT(SUN50I_ADDA_JACK_MIC_CTRL_INNERRESEN),
enable << SUN50I_ADDA_JACK_MIC_CTRL_INNERRESEN);
/* Select sample interval of the ADC sample to 16ms */
regmap_update_bits(regmap, SUN50I_ADDA_MDET_CTRL,
0x7 << SUN50I_ADDA_MDET_CTRL_SELDETADC_FS |
0x3 << SUN50I_ADDA_MDET_CTRL_SELDETADC_BF,
0x3 << SUN50I_ADDA_MDET_CTRL_SELDETADC_FS |
0x3 << SUN50I_ADDA_MDET_CTRL_SELDETADC_BF);
return devm_snd_soc_register_component(&pdev->dev,
&sun50i_codec_analog_cmpnt_drv,
NULL, 0);
}
static struct platform_driver sun50i_codec_analog_driver = {
.driver = {
.name = "sun50i-codec-analog",
.of_match_table = sun50i_codec_analog_of_match,
},
.probe = sun50i_codec_analog_probe,
};
module_platform_driver(sun50i_codec_analog_driver);
MODULE_DESCRIPTION("Allwinner internal codec analog controls driver for A64");
MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sun50i-codec-analog");
+439
View File
@@ -0,0 +1,439 @@
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This driver supports the DMIC in Allwinner's H6 SoCs.
//
// Copyright 2021 Ban Tao <fengzheng923@gmail.com>
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <sound/dmaengine_pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#define SUN50I_DMIC_EN_CTL (0x00)
#define SUN50I_DMIC_EN_CTL_GLOBE BIT(8)
#define SUN50I_DMIC_EN_CTL_CHAN(v) ((v) << 0)
#define SUN50I_DMIC_EN_CTL_CHAN_MASK GENMASK(7, 0)
#define SUN50I_DMIC_SR (0x04)
#define SUN50I_DMIC_SR_SAMPLE_RATE(v) ((v) << 0)
#define SUN50I_DMIC_SR_SAMPLE_RATE_MASK GENMASK(2, 0)
#define SUN50I_DMIC_CTL (0x08)
#define SUN50I_DMIC_CTL_OVERSAMPLE_RATE BIT(0)
#define SUN50I_DMIC_DATA (0x10)
#define SUN50I_DMIC_INTC (0x14)
#define SUN50I_DMIC_FIFO_DRQ_EN BIT(2)
#define SUN50I_DMIC_INT_STA (0x18)
#define SUN50I_DMIC_INT_STA_OVERRUN_IRQ_PENDING BIT(1)
#define SUN50I_DMIC_INT_STA_DATA_IRQ_PENDING BIT(0)
#define SUN50I_DMIC_RXFIFO_CTL (0x1c)
#define SUN50I_DMIC_RXFIFO_CTL_FLUSH BIT(31)
#define SUN50I_DMIC_RXFIFO_CTL_MODE_MASK BIT(9)
#define SUN50I_DMIC_RXFIFO_CTL_MODE_LSB (0 << 9)
#define SUN50I_DMIC_RXFIFO_CTL_MODE_MSB (1 << 9)
#define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK BIT(8)
#define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_16 (0 << 8)
#define SUN50I_DMIC_RXFIFO_CTL_SAMPLE_24 (1 << 8)
#define SUN50I_DMIC_CH_NUM (0x24)
#define SUN50I_DMIC_CH_NUM_N(v) ((v) << 0)
#define SUN50I_DMIC_CH_NUM_N_MASK GENMASK(2, 0)
#define SUN50I_DMIC_CNT (0x2c)
#define SUN50I_DMIC_CNT_N (1 << 0)
#define SUN50I_DMIC_D0D1_VOL_CTR (0x30)
#define SUN50I_DMIC_D0D1_VOL_CTR_0R (0)
#define SUN50I_DMIC_D0D1_VOL_CTR_0L (8)
#define SUN50I_DMIC_D0D1_VOL_CTR_1R (16)
#define SUN50I_DMIC_D0D1_VOL_CTR_1L (24)
#define SUN50I_DMIC_D2D3_VOL_CTR (0x34)
#define SUN50I_DMIC_D2D3_VOL_CTR_2R (0)
#define SUN50I_DMIC_D2D3_VOL_CTR_2L (8)
#define SUN50I_DMIC_D2D3_VOL_CTR_3R (16)
#define SUN50I_DMIC_D2D3_VOL_CTR_3L (24)
#define SUN50I_DMIC_HPF_CTRL (0x38)
#define SUN50I_DMIC_VERSION (0x50)
struct sun50i_dmic_dev {
struct clk *dmic_clk;
struct clk *bus_clk;
struct reset_control *rst;
struct regmap *regmap;
struct snd_dmaengine_dai_dma_data dma_params_rx;
};
struct dmic_rate {
unsigned int samplerate;
unsigned int rate_bit;
};
static const struct dmic_rate dmic_rate_s[] = {
{48000, 0x0},
{44100, 0x0},
{32000, 0x1},
{24000, 0x2},
{22050, 0x2},
{16000, 0x3},
{12000, 0x4},
{11025, 0x4},
{8000, 0x5},
};
static int sun50i_dmic_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *cpu_dai)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
/* only support capture */
if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
return -EINVAL;
regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
SUN50I_DMIC_RXFIFO_CTL_FLUSH,
SUN50I_DMIC_RXFIFO_CTL_FLUSH);
regmap_write(host->regmap, SUN50I_DMIC_CNT, SUN50I_DMIC_CNT_N);
return 0;
}
static int sun50i_dmic_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *cpu_dai)
{
int i = 0;
unsigned long rate = params_rate(params);
unsigned int mclk = 0;
unsigned int channels = params_channels(params);
unsigned int chan_en = (1 << channels) - 1;
struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
/* DMIC num is N+1 */
regmap_update_bits(host->regmap, SUN50I_DMIC_CH_NUM,
SUN50I_DMIC_CH_NUM_N_MASK,
SUN50I_DMIC_CH_NUM_N(channels - 1));
regmap_write(host->regmap, SUN50I_DMIC_HPF_CTRL, chan_en);
regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
SUN50I_DMIC_EN_CTL_CHAN_MASK,
SUN50I_DMIC_EN_CTL_CHAN(chan_en));
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S16_LE:
regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK,
SUN50I_DMIC_RXFIFO_CTL_SAMPLE_16);
break;
case SNDRV_PCM_FORMAT_S24_LE:
regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
SUN50I_DMIC_RXFIFO_CTL_SAMPLE_MASK,
SUN50I_DMIC_RXFIFO_CTL_SAMPLE_24);
break;
default:
dev_err(cpu_dai->dev, "Invalid format!\n");
return -EINVAL;
}
/* The hardware supports FIFO mode 1 for 24-bit samples */
regmap_update_bits(host->regmap, SUN50I_DMIC_RXFIFO_CTL,
SUN50I_DMIC_RXFIFO_CTL_MODE_MASK,
SUN50I_DMIC_RXFIFO_CTL_MODE_MSB);
switch (rate) {
case 11025:
case 22050:
case 44100:
mclk = 22579200;
break;
case 8000:
case 12000:
case 16000:
case 24000:
case 32000:
case 48000:
mclk = 24576000;
break;
default:
dev_err(cpu_dai->dev, "Invalid rate!\n");
return -EINVAL;
}
if (clk_set_rate(host->dmic_clk, mclk)) {
dev_err(cpu_dai->dev, "mclk : %u not support\n", mclk);
return -EINVAL;
}
for (i = 0; i < ARRAY_SIZE(dmic_rate_s); i++) {
if (dmic_rate_s[i].samplerate == rate) {
regmap_update_bits(host->regmap, SUN50I_DMIC_SR,
SUN50I_DMIC_SR_SAMPLE_RATE_MASK,
SUN50I_DMIC_SR_SAMPLE_RATE(dmic_rate_s[i].rate_bit));
break;
}
}
switch (params_physical_width(params)) {
case 16:
host->dma_params_rx.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
break;
case 32:
host->dma_params_rx.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
break;
default:
dev_err(cpu_dai->dev, "Unsupported physical sample width: %d\n",
params_physical_width(params));
return -EINVAL;
}
/* oversamplerate adjust */
if (params_rate(params) >= 24000)
regmap_update_bits(host->regmap, SUN50I_DMIC_CTL,
SUN50I_DMIC_CTL_OVERSAMPLE_RATE,
SUN50I_DMIC_CTL_OVERSAMPLE_RATE);
else
regmap_update_bits(host->regmap, SUN50I_DMIC_CTL,
SUN50I_DMIC_CTL_OVERSAMPLE_RATE, 0);
return 0;
}
static int sun50i_dmic_trigger(struct snd_pcm_substream *substream, int cmd,
struct snd_soc_dai *dai)
{
int ret = 0;
struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(dai);
if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
return -EINVAL;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME:
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
/* DRQ ENABLE */
regmap_update_bits(host->regmap, SUN50I_DMIC_INTC,
SUN50I_DMIC_FIFO_DRQ_EN,
SUN50I_DMIC_FIFO_DRQ_EN);
/* Global enable */
regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
SUN50I_DMIC_EN_CTL_GLOBE,
SUN50I_DMIC_EN_CTL_GLOBE);
break;
case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
/* DRQ DISABLE */
regmap_update_bits(host->regmap, SUN50I_DMIC_INTC,
SUN50I_DMIC_FIFO_DRQ_EN, 0);
/* Global disable */
regmap_update_bits(host->regmap, SUN50I_DMIC_EN_CTL,
SUN50I_DMIC_EN_CTL_GLOBE, 0);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int sun50i_dmic_soc_dai_probe(struct snd_soc_dai *dai)
{
struct sun50i_dmic_dev *host = snd_soc_dai_get_drvdata(dai);
snd_soc_dai_init_dma_data(dai, NULL, &host->dma_params_rx);
return 0;
}
static const struct snd_soc_dai_ops sun50i_dmic_dai_ops = {
.probe = sun50i_dmic_soc_dai_probe,
.startup = sun50i_dmic_startup,
.trigger = sun50i_dmic_trigger,
.hw_params = sun50i_dmic_hw_params,
};
static const struct regmap_config sun50i_dmic_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = SUN50I_DMIC_VERSION,
.cache_type = REGCACHE_NONE,
};
#define SUN50I_DMIC_RATES (SNDRV_PCM_RATE_8000_48000)
#define SUN50I_DMIC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
static struct snd_soc_dai_driver sun50i_dmic_dai = {
.capture = {
.channels_min = 1,
.channels_max = 8,
.rates = SUN50I_DMIC_RATES,
.formats = SUN50I_DMIC_FORMATS,
.sig_bits = 21,
},
.ops = &sun50i_dmic_dai_ops,
.name = "dmic",
};
static const struct of_device_id sun50i_dmic_of_match[] = {
{
.compatible = "allwinner,sun50i-h6-dmic",
},
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, sun50i_dmic_of_match);
static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(sun50i_dmic_vol_scale, -12000, 75, 1);
static const struct snd_kcontrol_new sun50i_dmic_controls[] = {
SOC_DOUBLE_TLV("DMIC Channel 0 Capture Volume", SUN50I_DMIC_D0D1_VOL_CTR,
SUN50I_DMIC_D0D1_VOL_CTR_0L, SUN50I_DMIC_D0D1_VOL_CTR_0R,
0xFF, 0, sun50i_dmic_vol_scale),
SOC_DOUBLE_TLV("DMIC Channel 1 Capture Volume", SUN50I_DMIC_D0D1_VOL_CTR,
SUN50I_DMIC_D0D1_VOL_CTR_1L, SUN50I_DMIC_D0D1_VOL_CTR_1R,
0xFF, 0, sun50i_dmic_vol_scale),
SOC_DOUBLE_TLV("DMIC Channel 2 Capture Volume", SUN50I_DMIC_D2D3_VOL_CTR,
SUN50I_DMIC_D2D3_VOL_CTR_2L, SUN50I_DMIC_D2D3_VOL_CTR_2R,
0xFF, 0, sun50i_dmic_vol_scale),
SOC_DOUBLE_TLV("DMIC Channel 3 Capture Volume", SUN50I_DMIC_D2D3_VOL_CTR,
SUN50I_DMIC_D2D3_VOL_CTR_3L, SUN50I_DMIC_D2D3_VOL_CTR_3R,
0xFF, 0, sun50i_dmic_vol_scale),
};
static const struct snd_soc_component_driver sun50i_dmic_component = {
.name = "sun50i-dmic",
.controls = sun50i_dmic_controls,
.num_controls = ARRAY_SIZE(sun50i_dmic_controls),
};
static int sun50i_dmic_runtime_suspend(struct device *dev)
{
struct sun50i_dmic_dev *host = dev_get_drvdata(dev);
clk_disable_unprepare(host->dmic_clk);
clk_disable_unprepare(host->bus_clk);
return 0;
}
static int sun50i_dmic_runtime_resume(struct device *dev)
{
struct sun50i_dmic_dev *host = dev_get_drvdata(dev);
int ret;
ret = clk_prepare_enable(host->dmic_clk);
if (ret)
return ret;
ret = clk_prepare_enable(host->bus_clk);
if (ret) {
clk_disable_unprepare(host->dmic_clk);
return ret;
}
return 0;
}
static int sun50i_dmic_probe(struct platform_device *pdev)
{
struct sun50i_dmic_dev *host;
struct resource *res;
int ret;
void __iomem *base;
host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
if (!host)
return -ENOMEM;
/* Get the addresses */
base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
if (IS_ERR(base))
return dev_err_probe(&pdev->dev, PTR_ERR(base),
"get resource failed.\n");
host->regmap = devm_regmap_init_mmio(&pdev->dev, base,
&sun50i_dmic_regmap_config);
if (IS_ERR(host->regmap))
return dev_err_probe(&pdev->dev, PTR_ERR(host->regmap),
"failed to initialise regmap\n");
/* Clocks */
host->bus_clk = devm_clk_get(&pdev->dev, "bus");
if (IS_ERR(host->bus_clk))
return dev_err_probe(&pdev->dev, PTR_ERR(host->bus_clk),
"failed to get bus clock.\n");
host->dmic_clk = devm_clk_get(&pdev->dev, "mod");
if (IS_ERR(host->dmic_clk))
return dev_err_probe(&pdev->dev, PTR_ERR(host->dmic_clk),
"failed to get dmic clock.\n");
host->dma_params_rx.addr = res->start + SUN50I_DMIC_DATA;
host->dma_params_rx.maxburst = 8;
platform_set_drvdata(pdev, host);
host->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
if (IS_ERR(host->rst))
return dev_err_probe(&pdev->dev, PTR_ERR(host->rst),
"Failed to get reset.\n");
reset_control_deassert(host->rst);
ret = devm_snd_soc_register_component(&pdev->dev, &sun50i_dmic_component,
&sun50i_dmic_dai, 1);
if (ret)
return dev_err_probe(&pdev->dev, ret,
"failed to register component.\n");
pm_runtime_enable(&pdev->dev);
if (!pm_runtime_enabled(&pdev->dev)) {
ret = sun50i_dmic_runtime_resume(&pdev->dev);
if (ret)
goto err_disable_runtime_pm;
}
ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
if (ret)
goto err_suspend;
return 0;
err_suspend:
if (!pm_runtime_status_suspended(&pdev->dev))
sun50i_dmic_runtime_suspend(&pdev->dev);
err_disable_runtime_pm:
pm_runtime_disable(&pdev->dev);
return ret;
}
static void sun50i_dmic_remove(struct platform_device *pdev)
{
pm_runtime_disable(&pdev->dev);
if (!pm_runtime_status_suspended(&pdev->dev))
sun50i_dmic_runtime_suspend(&pdev->dev);
}
static const struct dev_pm_ops sun50i_dmic_pm = {
RUNTIME_PM_OPS(sun50i_dmic_runtime_suspend,
sun50i_dmic_runtime_resume, NULL)
};
static struct platform_driver sun50i_dmic_driver = {
.driver = {
.name = "sun50i-dmic",
.of_match_table = sun50i_dmic_of_match,
.pm = pm_ptr(&sun50i_dmic_pm),
},
.probe = sun50i_dmic_probe,
.remove = sun50i_dmic_remove,
};
module_platform_driver(sun50i_dmic_driver);
MODULE_DESCRIPTION("Allwinner sun50i DMIC SoC Interface");
MODULE_AUTHOR("Ban Tao <fengzheng923@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sun50i-dmic");
@@ -0,0 +1,102 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* This driver provides regmap to access to analog part of audio codec
* found on Allwinner A23, A31s, A33, H3 and A64 Socs
*
* Copyright 2016 Chen-Yu Tsai <wens@csie.org>
* Copyright (C) 2018 Vasily Khoruzhick <anarsoul@gmail.com>
*/
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include "sun8i-adda-pr-regmap.h"
/* Analog control register access bits */
#define ADDA_PR 0x0 /* PRCM base + 0x1c0 */
#define ADDA_PR_RESET BIT(28)
#define ADDA_PR_WRITE BIT(24)
#define ADDA_PR_ADDR_SHIFT 16
#define ADDA_PR_ADDR_MASK GENMASK(4, 0)
#define ADDA_PR_DATA_IN_SHIFT 8
#define ADDA_PR_DATA_IN_MASK GENMASK(7, 0)
#define ADDA_PR_DATA_OUT_SHIFT 0
#define ADDA_PR_DATA_OUT_MASK GENMASK(7, 0)
/* regmap access bits */
static int adda_reg_read(void *context, unsigned int reg, unsigned int *val)
{
void __iomem *base = (void __iomem *)context;
u32 tmp;
/* De-assert reset */
writel(readl(base) | ADDA_PR_RESET, base);
/* Clear write bit */
writel(readl(base) & ~ADDA_PR_WRITE, base);
/* Set register address */
tmp = readl(base);
tmp &= ~(ADDA_PR_ADDR_MASK << ADDA_PR_ADDR_SHIFT);
tmp |= (reg & ADDA_PR_ADDR_MASK) << ADDA_PR_ADDR_SHIFT;
writel(tmp, base);
/* Read back value */
*val = readl(base) & ADDA_PR_DATA_OUT_MASK;
return 0;
}
static int adda_reg_write(void *context, unsigned int reg, unsigned int val)
{
void __iomem *base = (void __iomem *)context;
u32 tmp;
/* De-assert reset */
writel(readl(base) | ADDA_PR_RESET, base);
/* Set register address */
tmp = readl(base);
tmp &= ~(ADDA_PR_ADDR_MASK << ADDA_PR_ADDR_SHIFT);
tmp |= (reg & ADDA_PR_ADDR_MASK) << ADDA_PR_ADDR_SHIFT;
writel(tmp, base);
/* Set data to write */
tmp = readl(base);
tmp &= ~(ADDA_PR_DATA_IN_MASK << ADDA_PR_DATA_IN_SHIFT);
tmp |= (val & ADDA_PR_DATA_IN_MASK) << ADDA_PR_DATA_IN_SHIFT;
writel(tmp, base);
/* Set write bit to signal a write */
writel(readl(base) | ADDA_PR_WRITE, base);
/* Clear write bit */
writel(readl(base) & ~ADDA_PR_WRITE, base);
return 0;
}
static const struct regmap_config adda_pr_regmap_cfg = {
.name = "adda-pr",
.reg_bits = 5,
.reg_stride = 1,
.val_bits = 8,
.reg_read = adda_reg_read,
.reg_write = adda_reg_write,
.fast_io = true,
.max_register = 31,
};
struct regmap *sun8i_adda_pr_regmap_init(struct device *dev,
void __iomem *base)
{
return devm_regmap_init(dev, NULL, base, &adda_pr_regmap_cfg);
}
EXPORT_SYMBOL_GPL(sun8i_adda_pr_regmap_init);
MODULE_DESCRIPTION("Allwinner analog audio codec regmap driver");
MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sunxi-adda-pr");
@@ -0,0 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2018 Vasily Khoruzhick <anarsoul@gmail.com>
*/
struct regmap *sun8i_adda_pr_regmap_init(struct device *dev,
void __iomem *base);
@@ -0,0 +1,853 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* This driver supports the analog controls for the internal codec
* found in Allwinner's A31s, A23, A33 and H3 SoCs.
*
* Copyright 2016 Chen-Yu Tsai <wens@csie.org>
*/
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/tlv.h>
#include "sun8i-adda-pr-regmap.h"
/* Codec analog control register offsets and bit fields */
#define SUN8I_ADDA_HP_VOLC 0x00
#define SUN8I_ADDA_HP_VOLC_PA_CLK_GATE 7
#define SUN8I_ADDA_HP_VOLC_HP_VOL 0
#define SUN8I_ADDA_LOMIXSC 0x01
#define SUN8I_ADDA_LOMIXSC_MIC1 6
#define SUN8I_ADDA_LOMIXSC_MIC2 5
#define SUN8I_ADDA_LOMIXSC_PHONE 4
#define SUN8I_ADDA_LOMIXSC_PHONEN 3
#define SUN8I_ADDA_LOMIXSC_LINEINL 2
#define SUN8I_ADDA_LOMIXSC_DACL 1
#define SUN8I_ADDA_LOMIXSC_DACR 0
#define SUN8I_ADDA_ROMIXSC 0x02
#define SUN8I_ADDA_ROMIXSC_MIC1 6
#define SUN8I_ADDA_ROMIXSC_MIC2 5
#define SUN8I_ADDA_ROMIXSC_PHONE 4
#define SUN8I_ADDA_ROMIXSC_PHONEP 3
#define SUN8I_ADDA_ROMIXSC_LINEINR 2
#define SUN8I_ADDA_ROMIXSC_DACR 1
#define SUN8I_ADDA_ROMIXSC_DACL 0
#define SUN8I_ADDA_DAC_PA_SRC 0x03
#define SUN8I_ADDA_DAC_PA_SRC_DACAREN 7
#define SUN8I_ADDA_DAC_PA_SRC_DACALEN 6
#define SUN8I_ADDA_DAC_PA_SRC_RMIXEN 5
#define SUN8I_ADDA_DAC_PA_SRC_LMIXEN 4
#define SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE 3
#define SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE 2
#define SUN8I_ADDA_DAC_PA_SRC_RHPIS 1
#define SUN8I_ADDA_DAC_PA_SRC_LHPIS 0
#define SUN8I_ADDA_PHONEIN_GCTRL 0x04
#define SUN8I_ADDA_PHONEIN_GCTRL_PHONEPG 4
#define SUN8I_ADDA_PHONEIN_GCTRL_PHONENG 0
#define SUN8I_ADDA_LINEIN_GCTRL 0x05
#define SUN8I_ADDA_LINEIN_GCTRL_LINEING 4
#define SUN8I_ADDA_LINEIN_GCTRL_PHONEG 0
#define SUN8I_ADDA_MICIN_GCTRL 0x06
#define SUN8I_ADDA_MICIN_GCTRL_MIC1G 4
#define SUN8I_ADDA_MICIN_GCTRL_MIC2G 0
#define SUN8I_ADDA_PAEN_HP_CTRL 0x07
#define SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN 7
#define SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN 7 /* H3 specific */
#define SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC 5
#define SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN 4
#define SUN8I_ADDA_PAEN_HP_CTRL_PA_ANTI_POP_CTRL 2
#define SUN8I_ADDA_PAEN_HP_CTRL_LTRNMUTE 1
#define SUN8I_ADDA_PAEN_HP_CTRL_RTLNMUTE 0
#define SUN8I_ADDA_PHONEOUT_CTRL 0x08
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTG 5
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUTEN 4
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC1 3
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_MIC2 2
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_RMIX 1
#define SUN8I_ADDA_PHONEOUT_CTRL_PHONEOUT_LMIX 0
#define SUN8I_ADDA_PHONE_GAIN_CTRL 0x09
#define SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL 3
#define SUN8I_ADDA_PHONE_GAIN_CTRL_PHONEPREG 0
#define SUN8I_ADDA_MIC2G_CTRL 0x0a
#define SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN 7
#define SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST 4
#define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN 3
#define SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN 2
#define SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC 1
#define SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC 0
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL 0x0b
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN 7
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN 6
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIAS_MODE 5
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN 3
#define SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST 0
#define SUN8I_ADDA_LADCMIXSC 0x0c
#define SUN8I_ADDA_LADCMIXSC_MIC1 6
#define SUN8I_ADDA_LADCMIXSC_MIC2 5
#define SUN8I_ADDA_LADCMIXSC_PHONE 4
#define SUN8I_ADDA_LADCMIXSC_PHONEN 3
#define SUN8I_ADDA_LADCMIXSC_LINEINL 2
#define SUN8I_ADDA_LADCMIXSC_OMIXRL 1
#define SUN8I_ADDA_LADCMIXSC_OMIXRR 0
#define SUN8I_ADDA_RADCMIXSC 0x0d
#define SUN8I_ADDA_RADCMIXSC_MIC1 6
#define SUN8I_ADDA_RADCMIXSC_MIC2 5
#define SUN8I_ADDA_RADCMIXSC_PHONE 4
#define SUN8I_ADDA_RADCMIXSC_PHONEP 3
#define SUN8I_ADDA_RADCMIXSC_LINEINR 2
#define SUN8I_ADDA_RADCMIXSC_OMIXR 1
#define SUN8I_ADDA_RADCMIXSC_OMIXL 0
#define SUN8I_ADDA_RES 0x0e
#define SUN8I_ADDA_RES_MMICBIAS_SEL 4
#define SUN8I_ADDA_RES_PA_ANTI_POP_CTRL 0
#define SUN8I_ADDA_ADC_AP_EN 0x0f
#define SUN8I_ADDA_ADC_AP_EN_ADCREN 7
#define SUN8I_ADDA_ADC_AP_EN_ADCLEN 6
#define SUN8I_ADDA_ADC_AP_EN_ADCG 0
/* mixer controls */
static const struct snd_kcontrol_new sun8i_codec_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("DAC Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
SOC_DAPM_DOUBLE_R("Line In Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_LINEINL, 1, 0),
SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
SOC_DAPM_DOUBLE_R("Mic2 Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_MIC2, 1, 0),
};
/* mixer controls */
static const struct snd_kcontrol_new sun8i_v3s_codec_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("DAC Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_DACL, 1, 0),
SOC_DAPM_DOUBLE_R("DAC Reversed Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_DACR, 1, 0),
SOC_DAPM_DOUBLE_R("Mic1 Playback Switch",
SUN8I_ADDA_LOMIXSC,
SUN8I_ADDA_ROMIXSC,
SUN8I_ADDA_LOMIXSC_MIC1, 1, 0),
};
/* ADC mixer controls */
static const struct snd_kcontrol_new sun8i_codec_adc_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
SOC_DAPM_DOUBLE_R("Line In Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_LINEINL, 1, 0),
SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
SOC_DAPM_DOUBLE_R("Mic2 Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_MIC2, 1, 0),
};
/* ADC mixer controls */
static const struct snd_kcontrol_new sun8i_v3s_codec_adc_mixer_controls[] = {
SOC_DAPM_DOUBLE_R("Mixer Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_OMIXRL, 1, 0),
SOC_DAPM_DOUBLE_R("Mixer Reversed Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_OMIXRR, 1, 0),
SOC_DAPM_DOUBLE_R("Mic1 Capture Switch",
SUN8I_ADDA_LADCMIXSC,
SUN8I_ADDA_RADCMIXSC,
SUN8I_ADDA_LADCMIXSC_MIC1, 1, 0),
};
/* volume / mute controls */
static const DECLARE_TLV_DB_SCALE(sun8i_codec_out_mixer_pregain_scale,
-450, 150, 0);
static const DECLARE_TLV_DB_RANGE(sun8i_codec_mic_gain_scale,
0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1, 7, TLV_DB_SCALE_ITEM(2400, 300, 0),
);
static const struct snd_kcontrol_new sun8i_codec_common_controls[] = {
/* Mixer pre-gain */
SOC_SINGLE_TLV("Mic1 Playback Volume", SUN8I_ADDA_MICIN_GCTRL,
SUN8I_ADDA_MICIN_GCTRL_MIC1G,
0x7, 0, sun8i_codec_out_mixer_pregain_scale),
/* Microphone Amp boost gain */
SOC_SINGLE_TLV("Mic1 Boost Volume", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1BOOST, 0x7, 0,
sun8i_codec_mic_gain_scale),
/* ADC */
SOC_SINGLE_TLV("ADC Gain Capture Volume", SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCG, 0x7, 0,
sun8i_codec_out_mixer_pregain_scale),
};
static const struct snd_soc_dapm_widget sun8i_codec_common_widgets[] = {
/* ADC */
SND_SOC_DAPM_ADC("Left ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0),
SND_SOC_DAPM_ADC("Right ADC", NULL, SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCREN, 0),
/* DAC */
SND_SOC_DAPM_DAC("Left DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_DACALEN, 0),
SND_SOC_DAPM_DAC("Right DAC", NULL, SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_DACAREN, 0),
/*
* Due to this component and the codec belonging to separate DAPM
* contexts, we need to manually link the above widgets to their
* stream widgets at the card level.
*/
/* Microphone input */
SND_SOC_DAPM_INPUT("MIC1"),
/* Mic input path */
SND_SOC_DAPM_PGA("Mic1 Amplifier", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MIC1AMPEN, 0, NULL, 0),
};
static const struct snd_soc_dapm_widget sun8i_codec_mixer_widgets[] = {
SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
sun8i_codec_mixer_controls,
ARRAY_SIZE(sun8i_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
sun8i_codec_mixer_controls,
ARRAY_SIZE(sun8i_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
sun8i_codec_adc_mixer_controls,
ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
sun8i_codec_adc_mixer_controls,
ARRAY_SIZE(sun8i_codec_adc_mixer_controls)),
};
static const struct snd_soc_dapm_widget sun8i_v3s_codec_mixer_widgets[] = {
SND_SOC_DAPM_MIXER("Left Mixer", SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_LMIXEN, 0,
sun8i_v3s_codec_mixer_controls,
ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Right Mixer", SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_RMIXEN, 0,
sun8i_v3s_codec_mixer_controls,
ARRAY_SIZE(sun8i_v3s_codec_mixer_controls)),
SND_SOC_DAPM_MIXER("Left ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCLEN, 0,
sun8i_v3s_codec_adc_mixer_controls,
ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
SND_SOC_DAPM_MIXER("Right ADC Mixer", SUN8I_ADDA_ADC_AP_EN,
SUN8I_ADDA_ADC_AP_EN_ADCREN, 0,
sun8i_v3s_codec_adc_mixer_controls,
ARRAY_SIZE(sun8i_v3s_codec_adc_mixer_controls)),
};
static const struct snd_soc_dapm_route sun8i_codec_common_routes[] = {
/* Microphone Routes */
{ "Mic1 Amplifier", NULL, "MIC1"},
};
static const struct snd_soc_dapm_route sun8i_codec_mixer_routes[] = {
/* Left Mixer Routes */
{ "Left Mixer", "DAC Playback Switch", "Left DAC" },
{ "Left Mixer", "DAC Reversed Playback Switch", "Right DAC" },
{ "Left Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
/* Right Mixer Routes */
{ "Right Mixer", "DAC Playback Switch", "Right DAC" },
{ "Right Mixer", "DAC Reversed Playback Switch", "Left DAC" },
{ "Right Mixer", "Mic1 Playback Switch", "Mic1 Amplifier" },
/* Left ADC Mixer Routes */
{ "Left ADC Mixer", "Mixer Capture Switch", "Left Mixer" },
{ "Left ADC Mixer", "Mixer Reversed Capture Switch", "Right Mixer" },
{ "Left ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
/* Right ADC Mixer Routes */
{ "Right ADC Mixer", "Mixer Capture Switch", "Right Mixer" },
{ "Right ADC Mixer", "Mixer Reversed Capture Switch", "Left Mixer" },
{ "Right ADC Mixer", "Mic1 Capture Switch", "Mic1 Amplifier" },
/* ADC Routes */
{ "Left ADC", NULL, "Left ADC Mixer" },
{ "Right ADC", NULL, "Right ADC Mixer" },
};
/* headphone specific controls, widgets, and routes */
static const DECLARE_TLV_DB_SCALE(sun8i_codec_hp_vol_scale, -6300, 100, 1);
static const struct snd_kcontrol_new sun8i_codec_headphone_controls[] = {
SOC_SINGLE_TLV("Headphone Playback Volume",
SUN8I_ADDA_HP_VOLC,
SUN8I_ADDA_HP_VOLC_HP_VOL, 0x3f, 0,
sun8i_codec_hp_vol_scale),
SOC_DOUBLE("Headphone Playback Switch",
SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_LHPPAMUTE,
SUN8I_ADDA_DAC_PA_SRC_RHPPAMUTE, 1, 0),
};
static const char * const sun8i_codec_hp_src_enum_text[] = {
"DAC", "Mixer",
};
static SOC_ENUM_DOUBLE_DECL(sun8i_codec_hp_src_enum,
SUN8I_ADDA_DAC_PA_SRC,
SUN8I_ADDA_DAC_PA_SRC_LHPIS,
SUN8I_ADDA_DAC_PA_SRC_RHPIS,
sun8i_codec_hp_src_enum_text);
static const struct snd_kcontrol_new sun8i_codec_hp_src[] = {
SOC_DAPM_ENUM("Headphone Source Playback Route",
sun8i_codec_hp_src_enum),
};
static int sun8i_headphone_amp_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *k, int event)
{
struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
if (SND_SOC_DAPM_EVENT_ON(event)) {
snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN));
/*
* Need a delay to have the amplifier up. 700ms seems the best
* compromise between the time to let the amplifier up and the
* time not to feel this delay while playing a sound.
*/
msleep(700);
} else if (SND_SOC_DAPM_EVENT_OFF(event)) {
snd_soc_component_update_bits(component, SUN8I_ADDA_PAEN_HP_CTRL,
BIT(SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN),
0x0);
}
return 0;
}
static const struct snd_soc_dapm_widget sun8i_codec_headphone_widgets[] = {
SND_SOC_DAPM_MUX("Headphone Source Playback Route",
SND_SOC_NOPM, 0, 0, sun8i_codec_hp_src),
SND_SOC_DAPM_OUT_DRV_E("Headphone Amp", SUN8I_ADDA_PAEN_HP_CTRL,
SUN8I_ADDA_PAEN_HP_CTRL_HPPAEN, 0, NULL, 0,
sun8i_headphone_amp_event,
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_SUPPLY("HPCOM Protection", SUN8I_ADDA_PAEN_HP_CTRL,
SUN8I_ADDA_PAEN_HP_CTRL_COMPTEN, 0, NULL, 0),
SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPCOM", SUN8I_ADDA_PAEN_HP_CTRL,
SUN8I_ADDA_PAEN_HP_CTRL_HPCOM_FC, 0x3, 0x3, 0),
SND_SOC_DAPM_OUTPUT("HP"),
};
static const struct snd_soc_dapm_route sun8i_codec_headphone_routes[] = {
{ "Headphone Source Playback Route", "DAC", "Left DAC" },
{ "Headphone Source Playback Route", "DAC", "Right DAC" },
{ "Headphone Source Playback Route", "Mixer", "Left Mixer" },
{ "Headphone Source Playback Route", "Mixer", "Right Mixer" },
{ "Headphone Amp", NULL, "Headphone Source Playback Route" },
{ "HPCOM", NULL, "HPCOM Protection" },
{ "HP", NULL, "Headphone Amp" },
};
static int sun8i_codec_add_headphone(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_add_component_controls(cmpnt,
sun8i_codec_headphone_controls,
ARRAY_SIZE(sun8i_codec_headphone_controls));
if (ret) {
dev_err(dev, "Failed to add Headphone controls: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_headphone_widgets,
ARRAY_SIZE(sun8i_codec_headphone_widgets));
if (ret) {
dev_err(dev, "Failed to add Headphone DAPM widgets: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_headphone_routes,
ARRAY_SIZE(sun8i_codec_headphone_routes));
if (ret) {
dev_err(dev, "Failed to add Headphone DAPM routes: %d\n", ret);
return ret;
}
return 0;
}
/* mbias specific widget */
static const struct snd_soc_dapm_widget sun8i_codec_mbias_widgets[] = {
SND_SOC_DAPM_SUPPLY("MBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
SUN8I_ADDA_MIC1G_MICBIAS_CTRL_MMICBIASEN,
0, NULL, 0),
};
static int sun8i_codec_add_mbias(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mbias_widgets,
ARRAY_SIZE(sun8i_codec_mbias_widgets));
if (ret)
dev_err(dev, "Failed to add MBIAS DAPM widgets: %d\n", ret);
return ret;
}
/* hmic specific widget */
static const struct snd_soc_dapm_widget sun8i_codec_hmic_widgets[] = {
SND_SOC_DAPM_SUPPLY("HBIAS", SUN8I_ADDA_MIC1G_MICBIAS_CTRL,
SUN8I_ADDA_MIC1G_MICBIAS_CTRL_HMICBIASEN,
0, NULL, 0),
};
static int sun8i_codec_add_hmic(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_hmic_widgets,
ARRAY_SIZE(sun8i_codec_hmic_widgets));
if (ret)
dev_err(dev, "Failed to add Mic3 DAPM widgets: %d\n", ret);
return ret;
}
/* line in specific controls, widgets and rines */
static const struct snd_kcontrol_new sun8i_codec_linein_controls[] = {
/* Mixer pre-gain */
SOC_SINGLE_TLV("Line In Playback Volume", SUN8I_ADDA_LINEIN_GCTRL,
SUN8I_ADDA_LINEIN_GCTRL_LINEING,
0x7, 0, sun8i_codec_out_mixer_pregain_scale),
};
static const struct snd_soc_dapm_widget sun8i_codec_linein_widgets[] = {
/* Line input */
SND_SOC_DAPM_INPUT("LINEIN"),
};
static const struct snd_soc_dapm_route sun8i_codec_linein_routes[] = {
{ "Left Mixer", "Line In Playback Switch", "LINEIN" },
{ "Right Mixer", "Line In Playback Switch", "LINEIN" },
{ "Left ADC Mixer", "Line In Capture Switch", "LINEIN" },
{ "Right ADC Mixer", "Line In Capture Switch", "LINEIN" },
};
static int sun8i_codec_add_linein(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_add_component_controls(cmpnt,
sun8i_codec_linein_controls,
ARRAY_SIZE(sun8i_codec_linein_controls));
if (ret) {
dev_err(dev, "Failed to add Line In controls: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_linein_widgets,
ARRAY_SIZE(sun8i_codec_linein_widgets));
if (ret) {
dev_err(dev, "Failed to add Line In DAPM widgets: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_linein_routes,
ARRAY_SIZE(sun8i_codec_linein_routes));
if (ret) {
dev_err(dev, "Failed to add Line In DAPM routes: %d\n", ret);
return ret;
}
return 0;
}
/* line out specific controls, widgets and routes */
static const DECLARE_TLV_DB_RANGE(sun8i_codec_lineout_vol_scale,
0, 1, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
2, 31, TLV_DB_SCALE_ITEM(-4350, 150, 0),
);
static const struct snd_kcontrol_new sun8i_codec_lineout_controls[] = {
SOC_SINGLE_TLV("Line Out Playback Volume",
SUN8I_ADDA_PHONE_GAIN_CTRL,
SUN8I_ADDA_PHONE_GAIN_CTRL_LINEOUT_VOL, 0x1f, 0,
sun8i_codec_lineout_vol_scale),
SOC_DOUBLE("Line Out Playback Switch",
SUN8I_ADDA_MIC2G_CTRL,
SUN8I_ADDA_MIC2G_CTRL_LINEOUTLEN,
SUN8I_ADDA_MIC2G_CTRL_LINEOUTREN, 1, 0),
};
static const char * const sun8i_codec_lineout_src_enum_text[] = {
"Stereo", "Mono Differential",
};
static SOC_ENUM_DOUBLE_DECL(sun8i_codec_lineout_src_enum,
SUN8I_ADDA_MIC2G_CTRL,
SUN8I_ADDA_MIC2G_CTRL_LINEOUTLSRC,
SUN8I_ADDA_MIC2G_CTRL_LINEOUTRSRC,
sun8i_codec_lineout_src_enum_text);
static const struct snd_kcontrol_new sun8i_codec_lineout_src[] = {
SOC_DAPM_ENUM("Line Out Source Playback Route",
sun8i_codec_lineout_src_enum),
};
static const struct snd_soc_dapm_widget sun8i_codec_lineout_widgets[] = {
SND_SOC_DAPM_MUX("Line Out Source Playback Route",
SND_SOC_NOPM, 0, 0, sun8i_codec_lineout_src),
/* It is unclear if this is a buffer or gate, model it as a supply */
SND_SOC_DAPM_SUPPLY("Line Out Enable", SUN8I_ADDA_PAEN_HP_CTRL,
SUN8I_ADDA_PAEN_HP_CTRL_LINEOUTEN, 0, NULL, 0),
SND_SOC_DAPM_OUTPUT("LINEOUT"),
};
static const struct snd_soc_dapm_route sun8i_codec_lineout_routes[] = {
{ "Line Out Source Playback Route", "Stereo", "Left Mixer" },
{ "Line Out Source Playback Route", "Stereo", "Right Mixer" },
{ "Line Out Source Playback Route", "Mono Differential", "Left Mixer" },
{ "Line Out Source Playback Route", "Mono Differential", "Right Mixer" },
{ "LINEOUT", NULL, "Line Out Source Playback Route" },
{ "LINEOUT", NULL, "Line Out Enable", },
};
static int sun8i_codec_add_lineout(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_add_component_controls(cmpnt,
sun8i_codec_lineout_controls,
ARRAY_SIZE(sun8i_codec_lineout_controls));
if (ret) {
dev_err(dev, "Failed to add Line Out controls: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_lineout_widgets,
ARRAY_SIZE(sun8i_codec_lineout_widgets));
if (ret) {
dev_err(dev, "Failed to add Line Out DAPM widgets: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_lineout_routes,
ARRAY_SIZE(sun8i_codec_lineout_routes));
if (ret) {
dev_err(dev, "Failed to add Line Out DAPM routes: %d\n", ret);
return ret;
}
return 0;
}
/* mic2 specific controls, widgets and routes */
static const struct snd_kcontrol_new sun8i_codec_mic2_controls[] = {
/* Mixer pre-gain */
SOC_SINGLE_TLV("Mic2 Playback Volume",
SUN8I_ADDA_MICIN_GCTRL, SUN8I_ADDA_MICIN_GCTRL_MIC2G,
0x7, 0, sun8i_codec_out_mixer_pregain_scale),
/* Microphone Amp boost gain */
SOC_SINGLE_TLV("Mic2 Boost Volume", SUN8I_ADDA_MIC2G_CTRL,
SUN8I_ADDA_MIC2G_CTRL_MIC2BOOST, 0x7, 0,
sun8i_codec_mic_gain_scale),
};
static const struct snd_soc_dapm_widget sun8i_codec_mic2_widgets[] = {
/* Microphone input */
SND_SOC_DAPM_INPUT("MIC2"),
/* Mic input path */
SND_SOC_DAPM_PGA("Mic2 Amplifier", SUN8I_ADDA_MIC2G_CTRL,
SUN8I_ADDA_MIC2G_CTRL_MIC2AMPEN, 0, NULL, 0),
};
static const struct snd_soc_dapm_route sun8i_codec_mic2_routes[] = {
{ "Mic2 Amplifier", NULL, "MIC2"},
{ "Left Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
{ "Right Mixer", "Mic2 Playback Switch", "Mic2 Amplifier" },
{ "Left ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
{ "Right ADC Mixer", "Mic2 Capture Switch", "Mic2 Amplifier" },
};
static int sun8i_codec_add_mic2(struct snd_soc_component *cmpnt)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
ret = snd_soc_add_component_controls(cmpnt,
sun8i_codec_mic2_controls,
ARRAY_SIZE(sun8i_codec_mic2_controls));
if (ret) {
dev_err(dev, "Failed to add MIC2 controls: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_new_controls(dapm, sun8i_codec_mic2_widgets,
ARRAY_SIZE(sun8i_codec_mic2_widgets));
if (ret) {
dev_err(dev, "Failed to add MIC2 DAPM widgets: %d\n", ret);
return ret;
}
ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mic2_routes,
ARRAY_SIZE(sun8i_codec_mic2_routes));
if (ret) {
dev_err(dev, "Failed to add MIC2 DAPM routes: %d\n", ret);
return ret;
}
return 0;
}
struct sun8i_codec_analog_quirks {
bool has_headphone;
bool has_hmic;
bool has_linein;
bool has_lineout;
bool has_mbias;
bool has_mic2;
};
static const struct sun8i_codec_analog_quirks sun8i_a23_quirks = {
.has_headphone = true,
.has_hmic = true,
.has_linein = true,
.has_mbias = true,
.has_mic2 = true,
};
static const struct sun8i_codec_analog_quirks sun8i_h3_quirks = {
.has_linein = true,
.has_lineout = true,
.has_mbias = true,
.has_mic2 = true,
};
static int sun8i_codec_analog_add_mixer(struct snd_soc_component *cmpnt,
const struct sun8i_codec_analog_quirks *quirks)
{
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cmpnt);
struct device *dev = cmpnt->dev;
int ret;
if (!quirks->has_mic2 && !quirks->has_linein) {
/*
* Apply the special widget set which has uses a control
* without MIC2 and Line In, for SoCs without these.
* TODO: not all special cases are supported now, this case
* is present because it's the case of V3s.
*/
ret = snd_soc_dapm_new_controls(dapm,
sun8i_v3s_codec_mixer_widgets,
ARRAY_SIZE(sun8i_v3s_codec_mixer_widgets));
if (ret) {
dev_err(dev, "Failed to add V3s Mixer DAPM widgets: %d\n", ret);
return ret;
}
} else {
/* Apply the generic mixer widget set. */
ret = snd_soc_dapm_new_controls(dapm,
sun8i_codec_mixer_widgets,
ARRAY_SIZE(sun8i_codec_mixer_widgets));
if (ret) {
dev_err(dev, "Failed to add Mixer DAPM widgets: %d\n", ret);
return ret;
}
}
ret = snd_soc_dapm_add_routes(dapm, sun8i_codec_mixer_routes,
ARRAY_SIZE(sun8i_codec_mixer_routes));
if (ret) {
dev_err(dev, "Failed to add Mixer DAPM routes: %d\n", ret);
return ret;
}
return 0;
}
static const struct sun8i_codec_analog_quirks sun8i_v3s_quirks = {
.has_headphone = true,
.has_hmic = true,
};
static int sun8i_codec_analog_cmpnt_probe(struct snd_soc_component *cmpnt)
{
struct device *dev = cmpnt->dev;
const struct sun8i_codec_analog_quirks *quirks;
int ret;
/*
* This would never return NULL unless someone directly registers a
* platform device matching this driver's name, without specifying a
* device tree node.
*/
quirks = of_device_get_match_data(dev);
/* Add controls, widgets, and routes for individual features */
ret = sun8i_codec_analog_add_mixer(cmpnt, quirks);
if (ret)
return ret;
if (quirks->has_headphone) {
ret = sun8i_codec_add_headphone(cmpnt);
if (ret)
return ret;
}
if (quirks->has_hmic) {
ret = sun8i_codec_add_hmic(cmpnt);
if (ret)
return ret;
}
if (quirks->has_linein) {
ret = sun8i_codec_add_linein(cmpnt);
if (ret)
return ret;
}
if (quirks->has_lineout) {
ret = sun8i_codec_add_lineout(cmpnt);
if (ret)
return ret;
}
if (quirks->has_mbias) {
ret = sun8i_codec_add_mbias(cmpnt);
if (ret)
return ret;
}
if (quirks->has_mic2) {
ret = sun8i_codec_add_mic2(cmpnt);
if (ret)
return ret;
}
return 0;
}
static const struct snd_soc_component_driver sun8i_codec_analog_cmpnt_drv = {
.controls = sun8i_codec_common_controls,
.num_controls = ARRAY_SIZE(sun8i_codec_common_controls),
.dapm_widgets = sun8i_codec_common_widgets,
.num_dapm_widgets = ARRAY_SIZE(sun8i_codec_common_widgets),
.dapm_routes = sun8i_codec_common_routes,
.num_dapm_routes = ARRAY_SIZE(sun8i_codec_common_routes),
.probe = sun8i_codec_analog_cmpnt_probe,
};
static const struct of_device_id sun8i_codec_analog_of_match[] = {
{
.compatible = "allwinner,sun8i-a23-codec-analog",
.data = &sun8i_a23_quirks,
},
{
.compatible = "allwinner,sun8i-h3-codec-analog",
.data = &sun8i_h3_quirks,
},
{
.compatible = "allwinner,sun8i-v3s-codec-analog",
.data = &sun8i_v3s_quirks,
},
{}
};
MODULE_DEVICE_TABLE(of, sun8i_codec_analog_of_match);
static int sun8i_codec_analog_probe(struct platform_device *pdev)
{
struct regmap *regmap;
void __iomem *base;
base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(base)) {
dev_err(&pdev->dev, "Failed to map the registers\n");
return PTR_ERR(base);
}
regmap = sun8i_adda_pr_regmap_init(&pdev->dev, base);
if (IS_ERR(regmap)) {
dev_err(&pdev->dev, "Failed to create regmap\n");
return PTR_ERR(regmap);
}
return devm_snd_soc_register_component(&pdev->dev,
&sun8i_codec_analog_cmpnt_drv,
NULL, 0);
}
static struct platform_driver sun8i_codec_analog_driver = {
.driver = {
.name = "sun8i-codec-analog",
.of_match_table = sun8i_codec_analog_of_match,
},
.probe = sun8i_codec_analog_probe,
};
module_platform_driver(sun8i_codec_analog_driver);
MODULE_DESCRIPTION("Allwinner internal codec analog controls driver");
MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sun8i-codec-analog");
File diff suppressed because it is too large Load Diff