153eb4b7aSJerome Brunet // SPDX-License-Identifier: (GPL-2.0 OR MIT) 253eb4b7aSJerome Brunet // 353eb4b7aSJerome Brunet // Copyright (c) 2018 BayLibre, SAS. 453eb4b7aSJerome Brunet // Author: Jerome Brunet <jbrunet@baylibre.com> 553eb4b7aSJerome Brunet 653eb4b7aSJerome Brunet #include <linux/clk.h> 753eb4b7aSJerome Brunet #include <linux/module.h> 853eb4b7aSJerome Brunet #include <linux/of_platform.h> 953eb4b7aSJerome Brunet #include <linux/regmap.h> 1053eb4b7aSJerome Brunet #include <sound/soc.h> 1153eb4b7aSJerome Brunet #include <sound/soc-dai.h> 1253eb4b7aSJerome Brunet #include <sound/pcm_params.h> 1353eb4b7aSJerome Brunet #include <sound/pcm_iec958.h> 1453eb4b7aSJerome Brunet 1553eb4b7aSJerome Brunet /* 1653eb4b7aSJerome Brunet * NOTE: 1753eb4b7aSJerome Brunet * The meaning of bits SPDIFOUT_CTRL0_XXX_SEL is actually the opposite 1853eb4b7aSJerome Brunet * of what the documentation says. Manual control on V, U and C bits is 1953eb4b7aSJerome Brunet * applied when the related sel bits are cleared 2053eb4b7aSJerome Brunet */ 2153eb4b7aSJerome Brunet 2253eb4b7aSJerome Brunet #define SPDIFOUT_STAT 0x00 2353eb4b7aSJerome Brunet #define SPDIFOUT_GAIN0 0x04 2453eb4b7aSJerome Brunet #define SPDIFOUT_GAIN1 0x08 2553eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0 0x0c 2653eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_EN BIT(31) 2753eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_RST_OUT BIT(29) 2853eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_RST_IN BIT(28) 2953eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_USEL BIT(26) 3053eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_USET BIT(25) 3153eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_CHSTS_SEL BIT(24) 3253eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_DATA_SEL BIT(20) 3353eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_MSB_FIRST BIT(19) 3453eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_VSEL BIT(18) 3553eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_VSET BIT(17) 3653eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_MASK_MASK GENMASK(11, 4) 3753eb4b7aSJerome Brunet #define SPDIFOUT_CTRL0_MASK(x) ((x) << 4) 3853eb4b7aSJerome Brunet #define SPDIFOUT_CTRL1 0x10 3953eb4b7aSJerome Brunet #define SPDIFOUT_CTRL1_MSB_POS_MASK GENMASK(12, 8) 4053eb4b7aSJerome Brunet #define SPDIFOUT_CTRL1_MSB_POS(x) ((x) << 8) 4153eb4b7aSJerome Brunet #define SPDIFOUT_CTRL1_TYPE_MASK GENMASK(6, 4) 4253eb4b7aSJerome Brunet #define SPDIFOUT_CTRL1_TYPE(x) ((x) << 4) 4353eb4b7aSJerome Brunet #define SPDIFOUT_PREAMB 0x14 4453eb4b7aSJerome Brunet #define SPDIFOUT_SWAP 0x18 4553eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS0 0x1c 4653eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS1 0x20 4753eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS2 0x24 4853eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS3 0x28 4953eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS4 0x2c 5053eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS5 0x30 5153eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS6 0x34 5253eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS7 0x38 5353eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS8 0x3c 5453eb4b7aSJerome Brunet #define SPDIFOUT_CHSTS9 0x40 5553eb4b7aSJerome Brunet #define SPDIFOUT_CHSTSA 0x44 5653eb4b7aSJerome Brunet #define SPDIFOUT_CHSTSB 0x48 5753eb4b7aSJerome Brunet #define SPDIFOUT_MUTE_VAL 0x4c 5853eb4b7aSJerome Brunet 5953eb4b7aSJerome Brunet struct axg_spdifout { 6053eb4b7aSJerome Brunet struct regmap *map; 6153eb4b7aSJerome Brunet struct clk *mclk; 6253eb4b7aSJerome Brunet struct clk *pclk; 6353eb4b7aSJerome Brunet }; 6453eb4b7aSJerome Brunet 6553eb4b7aSJerome Brunet static void axg_spdifout_enable(struct regmap *map) 6653eb4b7aSJerome Brunet { 6753eb4b7aSJerome Brunet /* Apply both reset */ 6853eb4b7aSJerome Brunet regmap_update_bits(map, SPDIFOUT_CTRL0, 6953eb4b7aSJerome Brunet SPDIFOUT_CTRL0_RST_OUT | SPDIFOUT_CTRL0_RST_IN, 7053eb4b7aSJerome Brunet 0); 7153eb4b7aSJerome Brunet 7253eb4b7aSJerome Brunet /* Clear out reset before in reset */ 7353eb4b7aSJerome Brunet regmap_update_bits(map, SPDIFOUT_CTRL0, 7453eb4b7aSJerome Brunet SPDIFOUT_CTRL0_RST_OUT, SPDIFOUT_CTRL0_RST_OUT); 7553eb4b7aSJerome Brunet regmap_update_bits(map, SPDIFOUT_CTRL0, 7653eb4b7aSJerome Brunet SPDIFOUT_CTRL0_RST_IN, SPDIFOUT_CTRL0_RST_IN); 7753eb4b7aSJerome Brunet 7853eb4b7aSJerome Brunet /* Enable spdifout */ 7953eb4b7aSJerome Brunet regmap_update_bits(map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_EN, 8053eb4b7aSJerome Brunet SPDIFOUT_CTRL0_EN); 8153eb4b7aSJerome Brunet } 8253eb4b7aSJerome Brunet 8353eb4b7aSJerome Brunet static void axg_spdifout_disable(struct regmap *map) 8453eb4b7aSJerome Brunet { 8553eb4b7aSJerome Brunet regmap_update_bits(map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_EN, 0); 8653eb4b7aSJerome Brunet } 8753eb4b7aSJerome Brunet 8853eb4b7aSJerome Brunet static int axg_spdifout_trigger(struct snd_pcm_substream *substream, int cmd, 8953eb4b7aSJerome Brunet struct snd_soc_dai *dai) 9053eb4b7aSJerome Brunet { 9153eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 9253eb4b7aSJerome Brunet 9353eb4b7aSJerome Brunet switch (cmd) { 9453eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_START: 9553eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_RESUME: 9653eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 9753eb4b7aSJerome Brunet axg_spdifout_enable(priv->map); 9853eb4b7aSJerome Brunet return 0; 9953eb4b7aSJerome Brunet 10053eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_STOP: 10153eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_SUSPEND: 10253eb4b7aSJerome Brunet case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 10353eb4b7aSJerome Brunet axg_spdifout_disable(priv->map); 10453eb4b7aSJerome Brunet return 0; 10553eb4b7aSJerome Brunet 10653eb4b7aSJerome Brunet default: 10753eb4b7aSJerome Brunet return -EINVAL; 10853eb4b7aSJerome Brunet } 10953eb4b7aSJerome Brunet } 11053eb4b7aSJerome Brunet 111*f795ff78SKuninori Morimoto static int axg_spdifout_mute(struct snd_soc_dai *dai, int mute, int direction) 11253eb4b7aSJerome Brunet { 11353eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 11453eb4b7aSJerome Brunet 11553eb4b7aSJerome Brunet /* Use spdif valid bit to perform digital mute */ 11653eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_VSET, 11753eb4b7aSJerome Brunet mute ? SPDIFOUT_CTRL0_VSET : 0); 11853eb4b7aSJerome Brunet 11953eb4b7aSJerome Brunet return 0; 12053eb4b7aSJerome Brunet } 12153eb4b7aSJerome Brunet 12253eb4b7aSJerome Brunet static int axg_spdifout_sample_fmt(struct snd_pcm_hw_params *params, 12353eb4b7aSJerome Brunet struct snd_soc_dai *dai) 12453eb4b7aSJerome Brunet { 12553eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 12653eb4b7aSJerome Brunet unsigned int val; 12753eb4b7aSJerome Brunet 12853eb4b7aSJerome Brunet /* Set the samples spdifout will pull from the FIFO */ 12953eb4b7aSJerome Brunet switch (params_channels(params)) { 13053eb4b7aSJerome Brunet case 1: 13153eb4b7aSJerome Brunet val = SPDIFOUT_CTRL0_MASK(0x1); 13253eb4b7aSJerome Brunet break; 13353eb4b7aSJerome Brunet case 2: 13453eb4b7aSJerome Brunet val = SPDIFOUT_CTRL0_MASK(0x3); 13553eb4b7aSJerome Brunet break; 13653eb4b7aSJerome Brunet default: 13753eb4b7aSJerome Brunet dev_err(dai->dev, "too many channels for spdif dai: %u\n", 13853eb4b7aSJerome Brunet params_channels(params)); 13953eb4b7aSJerome Brunet return -EINVAL; 14053eb4b7aSJerome Brunet } 14153eb4b7aSJerome Brunet 14253eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL0, 14353eb4b7aSJerome Brunet SPDIFOUT_CTRL0_MASK_MASK, val); 14453eb4b7aSJerome Brunet 14553eb4b7aSJerome Brunet /* FIFO data are arranged in chunks of 64bits */ 14653eb4b7aSJerome Brunet switch (params_physical_width(params)) { 14753eb4b7aSJerome Brunet case 8: 14853eb4b7aSJerome Brunet /* 8 samples of 8 bits */ 14953eb4b7aSJerome Brunet val = SPDIFOUT_CTRL1_TYPE(0); 15053eb4b7aSJerome Brunet break; 15153eb4b7aSJerome Brunet case 16: 15253eb4b7aSJerome Brunet /* 4 samples of 16 bits - right justified */ 15353eb4b7aSJerome Brunet val = SPDIFOUT_CTRL1_TYPE(2); 15453eb4b7aSJerome Brunet break; 15553eb4b7aSJerome Brunet case 32: 15653eb4b7aSJerome Brunet /* 2 samples of 32 bits - right justified */ 15753eb4b7aSJerome Brunet val = SPDIFOUT_CTRL1_TYPE(4); 15853eb4b7aSJerome Brunet break; 15953eb4b7aSJerome Brunet default: 16053eb4b7aSJerome Brunet dev_err(dai->dev, "Unsupported physical width: %u\n", 16153eb4b7aSJerome Brunet params_physical_width(params)); 16253eb4b7aSJerome Brunet return -EINVAL; 16353eb4b7aSJerome Brunet } 16453eb4b7aSJerome Brunet 16553eb4b7aSJerome Brunet /* Position of the MSB in FIFO samples */ 16653eb4b7aSJerome Brunet val |= SPDIFOUT_CTRL1_MSB_POS(params_width(params) - 1); 16753eb4b7aSJerome Brunet 16853eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL1, 16953eb4b7aSJerome Brunet SPDIFOUT_CTRL1_MSB_POS_MASK | 17053eb4b7aSJerome Brunet SPDIFOUT_CTRL1_TYPE_MASK, val); 17153eb4b7aSJerome Brunet 17253eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL0, 17353eb4b7aSJerome Brunet SPDIFOUT_CTRL0_MSB_FIRST | SPDIFOUT_CTRL0_DATA_SEL, 17453eb4b7aSJerome Brunet 0); 17553eb4b7aSJerome Brunet 17653eb4b7aSJerome Brunet return 0; 17753eb4b7aSJerome Brunet } 17853eb4b7aSJerome Brunet 17953eb4b7aSJerome Brunet static int axg_spdifout_set_chsts(struct snd_pcm_hw_params *params, 18053eb4b7aSJerome Brunet struct snd_soc_dai *dai) 18153eb4b7aSJerome Brunet { 18253eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 18353eb4b7aSJerome Brunet unsigned int offset; 18453eb4b7aSJerome Brunet int ret; 18553eb4b7aSJerome Brunet u8 cs[4]; 18653eb4b7aSJerome Brunet u32 val; 18753eb4b7aSJerome Brunet 18853eb4b7aSJerome Brunet ret = snd_pcm_create_iec958_consumer_hw_params(params, cs, 4); 18953eb4b7aSJerome Brunet if (ret < 0) { 19053eb4b7aSJerome Brunet dev_err(dai->dev, "Creating IEC958 channel status failed %d\n", 19153eb4b7aSJerome Brunet ret); 19253eb4b7aSJerome Brunet return ret; 19353eb4b7aSJerome Brunet } 19453eb4b7aSJerome Brunet val = cs[0] | cs[1] << 8 | cs[2] << 16 | cs[3] << 24; 19553eb4b7aSJerome Brunet 19653eb4b7aSJerome Brunet /* Setup channel status A bits [31 - 0]*/ 19753eb4b7aSJerome Brunet regmap_write(priv->map, SPDIFOUT_CHSTS0, val); 19853eb4b7aSJerome Brunet 19953eb4b7aSJerome Brunet /* Clear channel status A bits [191 - 32] */ 20053eb4b7aSJerome Brunet for (offset = SPDIFOUT_CHSTS1; offset <= SPDIFOUT_CHSTS5; 20153eb4b7aSJerome Brunet offset += regmap_get_reg_stride(priv->map)) 20253eb4b7aSJerome Brunet regmap_write(priv->map, offset, 0); 20353eb4b7aSJerome Brunet 20453eb4b7aSJerome Brunet /* Setup channel status B bits [31 - 0]*/ 20553eb4b7aSJerome Brunet regmap_write(priv->map, SPDIFOUT_CHSTS6, val); 20653eb4b7aSJerome Brunet 20753eb4b7aSJerome Brunet /* Clear channel status B bits [191 - 32] */ 20853eb4b7aSJerome Brunet for (offset = SPDIFOUT_CHSTS7; offset <= SPDIFOUT_CHSTSB; 20953eb4b7aSJerome Brunet offset += regmap_get_reg_stride(priv->map)) 21053eb4b7aSJerome Brunet regmap_write(priv->map, offset, 0); 21153eb4b7aSJerome Brunet 21253eb4b7aSJerome Brunet return 0; 21353eb4b7aSJerome Brunet } 21453eb4b7aSJerome Brunet 21553eb4b7aSJerome Brunet static int axg_spdifout_hw_params(struct snd_pcm_substream *substream, 21653eb4b7aSJerome Brunet struct snd_pcm_hw_params *params, 21753eb4b7aSJerome Brunet struct snd_soc_dai *dai) 21853eb4b7aSJerome Brunet { 21953eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 22053eb4b7aSJerome Brunet unsigned int rate = params_rate(params); 22153eb4b7aSJerome Brunet int ret; 22253eb4b7aSJerome Brunet 22353eb4b7aSJerome Brunet /* 2 * 32bits per subframe * 2 channels = 128 */ 22453eb4b7aSJerome Brunet ret = clk_set_rate(priv->mclk, rate * 128); 22553eb4b7aSJerome Brunet if (ret) { 22653eb4b7aSJerome Brunet dev_err(dai->dev, "failed to set spdif clock\n"); 22753eb4b7aSJerome Brunet return ret; 22853eb4b7aSJerome Brunet } 22953eb4b7aSJerome Brunet 23053eb4b7aSJerome Brunet ret = axg_spdifout_sample_fmt(params, dai); 23153eb4b7aSJerome Brunet if (ret) { 23253eb4b7aSJerome Brunet dev_err(dai->dev, "failed to setup sample format\n"); 23353eb4b7aSJerome Brunet return ret; 23453eb4b7aSJerome Brunet } 23553eb4b7aSJerome Brunet 23653eb4b7aSJerome Brunet ret = axg_spdifout_set_chsts(params, dai); 23753eb4b7aSJerome Brunet if (ret) { 23853eb4b7aSJerome Brunet dev_err(dai->dev, "failed to setup channel status words\n"); 23953eb4b7aSJerome Brunet return ret; 24053eb4b7aSJerome Brunet } 24153eb4b7aSJerome Brunet 24253eb4b7aSJerome Brunet return 0; 24353eb4b7aSJerome Brunet } 24453eb4b7aSJerome Brunet 24553eb4b7aSJerome Brunet static int axg_spdifout_startup(struct snd_pcm_substream *substream, 24653eb4b7aSJerome Brunet struct snd_soc_dai *dai) 24753eb4b7aSJerome Brunet { 24853eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 24953eb4b7aSJerome Brunet int ret; 25053eb4b7aSJerome Brunet 25153eb4b7aSJerome Brunet /* Clock the spdif output block */ 25253eb4b7aSJerome Brunet ret = clk_prepare_enable(priv->pclk); 25353eb4b7aSJerome Brunet if (ret) { 25453eb4b7aSJerome Brunet dev_err(dai->dev, "failed to enable pclk\n"); 25553eb4b7aSJerome Brunet return ret; 25653eb4b7aSJerome Brunet } 25753eb4b7aSJerome Brunet 25853eb4b7aSJerome Brunet /* Make sure the block is initially stopped */ 25953eb4b7aSJerome Brunet axg_spdifout_disable(priv->map); 26053eb4b7aSJerome Brunet 26153eb4b7aSJerome Brunet /* Insert data from bit 27 lsb first */ 26253eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL0, 26353eb4b7aSJerome Brunet SPDIFOUT_CTRL0_MSB_FIRST | SPDIFOUT_CTRL0_DATA_SEL, 26453eb4b7aSJerome Brunet 0); 26553eb4b7aSJerome Brunet 26653eb4b7aSJerome Brunet /* Manual control of V, C and U, U = 0 */ 26753eb4b7aSJerome Brunet regmap_update_bits(priv->map, SPDIFOUT_CTRL0, 26853eb4b7aSJerome Brunet SPDIFOUT_CTRL0_CHSTS_SEL | SPDIFOUT_CTRL0_VSEL | 26953eb4b7aSJerome Brunet SPDIFOUT_CTRL0_USEL | SPDIFOUT_CTRL0_USET, 27053eb4b7aSJerome Brunet 0); 27153eb4b7aSJerome Brunet 27253eb4b7aSJerome Brunet /* Static SWAP configuration ATM */ 27353eb4b7aSJerome Brunet regmap_write(priv->map, SPDIFOUT_SWAP, 0x10); 27453eb4b7aSJerome Brunet 27553eb4b7aSJerome Brunet return 0; 27653eb4b7aSJerome Brunet } 27753eb4b7aSJerome Brunet 27853eb4b7aSJerome Brunet static void axg_spdifout_shutdown(struct snd_pcm_substream *substream, 27953eb4b7aSJerome Brunet struct snd_soc_dai *dai) 28053eb4b7aSJerome Brunet { 28153eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai); 28253eb4b7aSJerome Brunet 28353eb4b7aSJerome Brunet clk_disable_unprepare(priv->pclk); 28453eb4b7aSJerome Brunet } 28553eb4b7aSJerome Brunet 28653eb4b7aSJerome Brunet static const struct snd_soc_dai_ops axg_spdifout_ops = { 28753eb4b7aSJerome Brunet .trigger = axg_spdifout_trigger, 288*f795ff78SKuninori Morimoto .mute_stream = axg_spdifout_mute, 28953eb4b7aSJerome Brunet .hw_params = axg_spdifout_hw_params, 29053eb4b7aSJerome Brunet .startup = axg_spdifout_startup, 29153eb4b7aSJerome Brunet .shutdown = axg_spdifout_shutdown, 292*f795ff78SKuninori Morimoto .no_capture_mute = 1, 29353eb4b7aSJerome Brunet }; 29453eb4b7aSJerome Brunet 29553eb4b7aSJerome Brunet static struct snd_soc_dai_driver axg_spdifout_dai_drv[] = { 29653eb4b7aSJerome Brunet { 29753eb4b7aSJerome Brunet .name = "SPDIF Output", 29853eb4b7aSJerome Brunet .playback = { 29953eb4b7aSJerome Brunet .stream_name = "Playback", 30053eb4b7aSJerome Brunet .channels_min = 1, 30153eb4b7aSJerome Brunet .channels_max = 2, 30253eb4b7aSJerome Brunet .rates = (SNDRV_PCM_RATE_32000 | 30353eb4b7aSJerome Brunet SNDRV_PCM_RATE_44100 | 30453eb4b7aSJerome Brunet SNDRV_PCM_RATE_48000 | 30553eb4b7aSJerome Brunet SNDRV_PCM_RATE_88200 | 30653eb4b7aSJerome Brunet SNDRV_PCM_RATE_96000 | 30753eb4b7aSJerome Brunet SNDRV_PCM_RATE_176400 | 30853eb4b7aSJerome Brunet SNDRV_PCM_RATE_192000), 30953eb4b7aSJerome Brunet .formats = (SNDRV_PCM_FMTBIT_S8 | 31053eb4b7aSJerome Brunet SNDRV_PCM_FMTBIT_S16_LE | 31153eb4b7aSJerome Brunet SNDRV_PCM_FMTBIT_S20_LE | 31253eb4b7aSJerome Brunet SNDRV_PCM_FMTBIT_S24_LE), 31353eb4b7aSJerome Brunet }, 31453eb4b7aSJerome Brunet .ops = &axg_spdifout_ops, 31553eb4b7aSJerome Brunet }, 31653eb4b7aSJerome Brunet }; 31753eb4b7aSJerome Brunet 31853eb4b7aSJerome Brunet static const char * const spdifout_sel_texts[] = { 31953eb4b7aSJerome Brunet "IN 0", "IN 1", "IN 2", 32053eb4b7aSJerome Brunet }; 32153eb4b7aSJerome Brunet 32253eb4b7aSJerome Brunet static SOC_ENUM_SINGLE_DECL(axg_spdifout_sel_enum, SPDIFOUT_CTRL1, 24, 32353eb4b7aSJerome Brunet spdifout_sel_texts); 32453eb4b7aSJerome Brunet 32553eb4b7aSJerome Brunet static const struct snd_kcontrol_new axg_spdifout_in_mux = 32653eb4b7aSJerome Brunet SOC_DAPM_ENUM("Input Source", axg_spdifout_sel_enum); 32753eb4b7aSJerome Brunet 32853eb4b7aSJerome Brunet static const struct snd_soc_dapm_widget axg_spdifout_dapm_widgets[] = { 32953eb4b7aSJerome Brunet SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0), 33053eb4b7aSJerome Brunet SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0), 33153eb4b7aSJerome Brunet SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0), 33253eb4b7aSJerome Brunet SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_spdifout_in_mux), 33353eb4b7aSJerome Brunet }; 33453eb4b7aSJerome Brunet 33553eb4b7aSJerome Brunet static const struct snd_soc_dapm_route axg_spdifout_dapm_routes[] = { 33653eb4b7aSJerome Brunet { "SRC SEL", "IN 0", "IN 0" }, 33753eb4b7aSJerome Brunet { "SRC SEL", "IN 1", "IN 1" }, 33853eb4b7aSJerome Brunet { "SRC SEL", "IN 2", "IN 2" }, 33953eb4b7aSJerome Brunet { "Playback", NULL, "SRC SEL" }, 34053eb4b7aSJerome Brunet }; 34153eb4b7aSJerome Brunet 34253eb4b7aSJerome Brunet static const struct snd_kcontrol_new axg_spdifout_controls[] = { 34353eb4b7aSJerome Brunet SOC_DOUBLE("Playback Volume", SPDIFOUT_GAIN0, 0, 8, 255, 0), 34453eb4b7aSJerome Brunet SOC_DOUBLE("Playback Switch", SPDIFOUT_CTRL0, 22, 21, 1, 1), 34553eb4b7aSJerome Brunet SOC_SINGLE("Playback Gain Enable Switch", 34653eb4b7aSJerome Brunet SPDIFOUT_CTRL1, 26, 1, 0), 34753eb4b7aSJerome Brunet SOC_SINGLE("Playback Channels Mix Switch", 34853eb4b7aSJerome Brunet SPDIFOUT_CTRL0, 23, 1, 0), 34953eb4b7aSJerome Brunet }; 35053eb4b7aSJerome Brunet 35153eb4b7aSJerome Brunet static int axg_spdifout_set_bias_level(struct snd_soc_component *component, 35253eb4b7aSJerome Brunet enum snd_soc_bias_level level) 35353eb4b7aSJerome Brunet { 35453eb4b7aSJerome Brunet struct axg_spdifout *priv = snd_soc_component_get_drvdata(component); 35553eb4b7aSJerome Brunet enum snd_soc_bias_level now = 35653eb4b7aSJerome Brunet snd_soc_component_get_bias_level(component); 35753eb4b7aSJerome Brunet int ret = 0; 35853eb4b7aSJerome Brunet 35953eb4b7aSJerome Brunet switch (level) { 36053eb4b7aSJerome Brunet case SND_SOC_BIAS_PREPARE: 36153eb4b7aSJerome Brunet if (now == SND_SOC_BIAS_STANDBY) 36253eb4b7aSJerome Brunet ret = clk_prepare_enable(priv->mclk); 36353eb4b7aSJerome Brunet break; 36453eb4b7aSJerome Brunet 36553eb4b7aSJerome Brunet case SND_SOC_BIAS_STANDBY: 36653eb4b7aSJerome Brunet if (now == SND_SOC_BIAS_PREPARE) 36753eb4b7aSJerome Brunet clk_disable_unprepare(priv->mclk); 36853eb4b7aSJerome Brunet break; 36953eb4b7aSJerome Brunet 37053eb4b7aSJerome Brunet case SND_SOC_BIAS_OFF: 37153eb4b7aSJerome Brunet case SND_SOC_BIAS_ON: 37253eb4b7aSJerome Brunet break; 37353eb4b7aSJerome Brunet } 37453eb4b7aSJerome Brunet 37553eb4b7aSJerome Brunet return ret; 37653eb4b7aSJerome Brunet } 37753eb4b7aSJerome Brunet 37853eb4b7aSJerome Brunet static const struct snd_soc_component_driver axg_spdifout_component_drv = { 37953eb4b7aSJerome Brunet .controls = axg_spdifout_controls, 38053eb4b7aSJerome Brunet .num_controls = ARRAY_SIZE(axg_spdifout_controls), 38153eb4b7aSJerome Brunet .dapm_widgets = axg_spdifout_dapm_widgets, 38253eb4b7aSJerome Brunet .num_dapm_widgets = ARRAY_SIZE(axg_spdifout_dapm_widgets), 38353eb4b7aSJerome Brunet .dapm_routes = axg_spdifout_dapm_routes, 38453eb4b7aSJerome Brunet .num_dapm_routes = ARRAY_SIZE(axg_spdifout_dapm_routes), 38553eb4b7aSJerome Brunet .set_bias_level = axg_spdifout_set_bias_level, 38653eb4b7aSJerome Brunet }; 38753eb4b7aSJerome Brunet 38853eb4b7aSJerome Brunet static const struct regmap_config axg_spdifout_regmap_cfg = { 38953eb4b7aSJerome Brunet .reg_bits = 32, 39053eb4b7aSJerome Brunet .val_bits = 32, 39153eb4b7aSJerome Brunet .reg_stride = 4, 39253eb4b7aSJerome Brunet .max_register = SPDIFOUT_MUTE_VAL, 39353eb4b7aSJerome Brunet }; 39453eb4b7aSJerome Brunet 39553eb4b7aSJerome Brunet static const struct of_device_id axg_spdifout_of_match[] = { 39653eb4b7aSJerome Brunet { .compatible = "amlogic,axg-spdifout", }, 39753eb4b7aSJerome Brunet {} 39853eb4b7aSJerome Brunet }; 39953eb4b7aSJerome Brunet MODULE_DEVICE_TABLE(of, axg_spdifout_of_match); 40053eb4b7aSJerome Brunet 40153eb4b7aSJerome Brunet static int axg_spdifout_probe(struct platform_device *pdev) 40253eb4b7aSJerome Brunet { 40353eb4b7aSJerome Brunet struct device *dev = &pdev->dev; 40453eb4b7aSJerome Brunet struct axg_spdifout *priv; 40553eb4b7aSJerome Brunet void __iomem *regs; 40653eb4b7aSJerome Brunet int ret; 40753eb4b7aSJerome Brunet 40853eb4b7aSJerome Brunet priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 40953eb4b7aSJerome Brunet if (!priv) 41053eb4b7aSJerome Brunet return -ENOMEM; 41153eb4b7aSJerome Brunet platform_set_drvdata(pdev, priv); 41253eb4b7aSJerome Brunet 413c43fd289SYueHaibing regs = devm_platform_ioremap_resource(pdev, 0); 41453eb4b7aSJerome Brunet if (IS_ERR(regs)) 41553eb4b7aSJerome Brunet return PTR_ERR(regs); 41653eb4b7aSJerome Brunet 41753eb4b7aSJerome Brunet priv->map = devm_regmap_init_mmio(dev, regs, &axg_spdifout_regmap_cfg); 41853eb4b7aSJerome Brunet if (IS_ERR(priv->map)) { 41953eb4b7aSJerome Brunet dev_err(dev, "failed to init regmap: %ld\n", 42053eb4b7aSJerome Brunet PTR_ERR(priv->map)); 42153eb4b7aSJerome Brunet return PTR_ERR(priv->map); 42253eb4b7aSJerome Brunet } 42353eb4b7aSJerome Brunet 42453eb4b7aSJerome Brunet priv->pclk = devm_clk_get(dev, "pclk"); 42553eb4b7aSJerome Brunet if (IS_ERR(priv->pclk)) { 42653eb4b7aSJerome Brunet ret = PTR_ERR(priv->pclk); 42753eb4b7aSJerome Brunet if (ret != -EPROBE_DEFER) 42853eb4b7aSJerome Brunet dev_err(dev, "failed to get pclk: %d\n", ret); 42953eb4b7aSJerome Brunet return ret; 43053eb4b7aSJerome Brunet } 43153eb4b7aSJerome Brunet 43253eb4b7aSJerome Brunet priv->mclk = devm_clk_get(dev, "mclk"); 43353eb4b7aSJerome Brunet if (IS_ERR(priv->mclk)) { 43453eb4b7aSJerome Brunet ret = PTR_ERR(priv->mclk); 43553eb4b7aSJerome Brunet if (ret != -EPROBE_DEFER) 43653eb4b7aSJerome Brunet dev_err(dev, "failed to get mclk: %d\n", ret); 43753eb4b7aSJerome Brunet return ret; 43853eb4b7aSJerome Brunet } 43953eb4b7aSJerome Brunet 44053eb4b7aSJerome Brunet return devm_snd_soc_register_component(dev, &axg_spdifout_component_drv, 44153eb4b7aSJerome Brunet axg_spdifout_dai_drv, ARRAY_SIZE(axg_spdifout_dai_drv)); 44253eb4b7aSJerome Brunet } 44353eb4b7aSJerome Brunet 44453eb4b7aSJerome Brunet static struct platform_driver axg_spdifout_pdrv = { 44553eb4b7aSJerome Brunet .probe = axg_spdifout_probe, 44653eb4b7aSJerome Brunet .driver = { 44753eb4b7aSJerome Brunet .name = "axg-spdifout", 44853eb4b7aSJerome Brunet .of_match_table = axg_spdifout_of_match, 44953eb4b7aSJerome Brunet }, 45053eb4b7aSJerome Brunet }; 45153eb4b7aSJerome Brunet module_platform_driver(axg_spdifout_pdrv); 45253eb4b7aSJerome Brunet 45353eb4b7aSJerome Brunet MODULE_DESCRIPTION("Amlogic AXG SPDIF Output driver"); 45453eb4b7aSJerome Brunet MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>"); 45553eb4b7aSJerome Brunet MODULE_LICENSE("GPL v2"); 456