1c5477e96SSia Jee Heng // SPDX-License-Identifier: GPL-2.0-only 2c5477e96SSia Jee Heng // 3c5477e96SSia Jee Heng // Copyright (C) 2020 Intel Corporation. 4c5477e96SSia Jee Heng // 5c5477e96SSia Jee Heng // Intel KeemBay Platform driver. 6c5477e96SSia Jee Heng // 7c5477e96SSia Jee Heng 8c5477e96SSia Jee Heng #include <linux/clk.h> 9c5477e96SSia Jee Heng #include <linux/io.h> 10c5477e96SSia Jee Heng #include <linux/module.h> 11c5477e96SSia Jee Heng #include <sound/pcm.h> 12c5477e96SSia Jee Heng #include <sound/pcm_params.h> 13c5477e96SSia Jee Heng #include <sound/soc.h> 14c5477e96SSia Jee Heng #include "kmb_platform.h" 15c5477e96SSia Jee Heng 16c5477e96SSia Jee Heng #define PERIODS_MIN 2 17c5477e96SSia Jee Heng #define PERIODS_MAX 48 18c5477e96SSia Jee Heng #define PERIOD_BYTES_MIN 4096 19c5477e96SSia Jee Heng #define BUFFER_BYTES_MAX (PERIODS_MAX * PERIOD_BYTES_MIN) 20c5477e96SSia Jee Heng #define TDM_OPERATION 1 21c5477e96SSia Jee Heng #define I2S_OPERATION 0 22c5477e96SSia Jee Heng #define DATA_WIDTH_CONFIG_BIT 6 23c5477e96SSia Jee Heng #define TDM_CHANNEL_CONFIG_BIT 3 24c5477e96SSia Jee Heng 25c5477e96SSia Jee Heng static const struct snd_pcm_hardware kmb_pcm_hardware = { 26c5477e96SSia Jee Heng .info = SNDRV_PCM_INFO_INTERLEAVED | 27c5477e96SSia Jee Heng SNDRV_PCM_INFO_MMAP | 28c5477e96SSia Jee Heng SNDRV_PCM_INFO_MMAP_VALID | 29c5477e96SSia Jee Heng SNDRV_PCM_INFO_BATCH | 30c5477e96SSia Jee Heng SNDRV_PCM_INFO_BLOCK_TRANSFER, 31c5477e96SSia Jee Heng .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000, 32c5477e96SSia Jee Heng .rate_min = 16000, 33c5477e96SSia Jee Heng .rate_max = 48000, 34c5477e96SSia Jee Heng .formats = SNDRV_PCM_FMTBIT_S16_LE | 35c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S24_LE | 36c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S32_LE, 37c5477e96SSia Jee Heng .channels_min = 2, 38c5477e96SSia Jee Heng .channels_max = 2, 39c5477e96SSia Jee Heng .buffer_bytes_max = BUFFER_BYTES_MAX, 40c5477e96SSia Jee Heng .period_bytes_min = PERIOD_BYTES_MIN, 41c5477e96SSia Jee Heng .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN, 42c5477e96SSia Jee Heng .periods_min = PERIODS_MIN, 43c5477e96SSia Jee Heng .periods_max = PERIODS_MAX, 44c5477e96SSia Jee Heng .fifo_size = 16, 45c5477e96SSia Jee Heng }; 46c5477e96SSia Jee Heng 47c5477e96SSia Jee Heng static unsigned int kmb_pcm_tx_fn(struct kmb_i2s_info *kmb_i2s, 48c5477e96SSia Jee Heng struct snd_pcm_runtime *runtime, 49c5477e96SSia Jee Heng unsigned int tx_ptr, bool *period_elapsed) 50c5477e96SSia Jee Heng { 51c5477e96SSia Jee Heng unsigned int period_pos = tx_ptr % runtime->period_size; 52c5477e96SSia Jee Heng void __iomem *i2s_base = kmb_i2s->i2s_base; 53c5477e96SSia Jee Heng void *buf = runtime->dma_area; 54c5477e96SSia Jee Heng int i; 55c5477e96SSia Jee Heng 56c5477e96SSia Jee Heng /* KMB i2s uses two separate L/R FIFO */ 57c5477e96SSia Jee Heng for (i = 0; i < kmb_i2s->fifo_th; i++) { 58c5477e96SSia Jee Heng if (kmb_i2s->config.data_width == 16) { 59c5477e96SSia Jee Heng writel(((u16(*)[2])buf)[tx_ptr][0], i2s_base + LRBR_LTHR(0)); 60c5477e96SSia Jee Heng writel(((u16(*)[2])buf)[tx_ptr][1], i2s_base + RRBR_RTHR(0)); 61c5477e96SSia Jee Heng } else { 62c5477e96SSia Jee Heng writel(((u32(*)[2])buf)[tx_ptr][0], i2s_base + LRBR_LTHR(0)); 63c5477e96SSia Jee Heng writel(((u32(*)[2])buf)[tx_ptr][1], i2s_base + RRBR_RTHR(0)); 64c5477e96SSia Jee Heng } 65c5477e96SSia Jee Heng 66c5477e96SSia Jee Heng period_pos++; 67c5477e96SSia Jee Heng 68c5477e96SSia Jee Heng if (++tx_ptr >= runtime->buffer_size) 69c5477e96SSia Jee Heng tx_ptr = 0; 70c5477e96SSia Jee Heng } 71c5477e96SSia Jee Heng 72c5477e96SSia Jee Heng *period_elapsed = period_pos >= runtime->period_size; 73c5477e96SSia Jee Heng 74c5477e96SSia Jee Heng return tx_ptr; 75c5477e96SSia Jee Heng } 76c5477e96SSia Jee Heng 77c5477e96SSia Jee Heng static unsigned int kmb_pcm_rx_fn(struct kmb_i2s_info *kmb_i2s, 78c5477e96SSia Jee Heng struct snd_pcm_runtime *runtime, 79c5477e96SSia Jee Heng unsigned int rx_ptr, bool *period_elapsed) 80c5477e96SSia Jee Heng { 81c5477e96SSia Jee Heng unsigned int period_pos = rx_ptr % runtime->period_size; 82c5477e96SSia Jee Heng void __iomem *i2s_base = kmb_i2s->i2s_base; 83c5477e96SSia Jee Heng void *buf = runtime->dma_area; 84c5477e96SSia Jee Heng int i; 85c5477e96SSia Jee Heng 86c5477e96SSia Jee Heng /* KMB i2s uses two separate L/R FIFO */ 87c5477e96SSia Jee Heng for (i = 0; i < kmb_i2s->fifo_th; i++) { 88c5477e96SSia Jee Heng if (kmb_i2s->config.data_width == 16) { 89c5477e96SSia Jee Heng ((u16(*)[2])buf)[rx_ptr][0] = readl(i2s_base + LRBR_LTHR(0)); 90c5477e96SSia Jee Heng ((u16(*)[2])buf)[rx_ptr][1] = readl(i2s_base + RRBR_RTHR(0)); 91c5477e96SSia Jee Heng } else { 92c5477e96SSia Jee Heng ((u32(*)[2])buf)[rx_ptr][0] = readl(i2s_base + LRBR_LTHR(0)); 93c5477e96SSia Jee Heng ((u32(*)[2])buf)[rx_ptr][1] = readl(i2s_base + RRBR_RTHR(0)); 94c5477e96SSia Jee Heng } 95c5477e96SSia Jee Heng 96c5477e96SSia Jee Heng period_pos++; 97c5477e96SSia Jee Heng 98c5477e96SSia Jee Heng if (++rx_ptr >= runtime->buffer_size) 99c5477e96SSia Jee Heng rx_ptr = 0; 100c5477e96SSia Jee Heng } 101c5477e96SSia Jee Heng 102c5477e96SSia Jee Heng *period_elapsed = period_pos >= runtime->period_size; 103c5477e96SSia Jee Heng 104c5477e96SSia Jee Heng return rx_ptr; 105c5477e96SSia Jee Heng } 106c5477e96SSia Jee Heng 107c5477e96SSia Jee Heng static inline void kmb_i2s_disable_channels(struct kmb_i2s_info *kmb_i2s, 108c5477e96SSia Jee Heng u32 stream) 109c5477e96SSia Jee Heng { 110c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 111c5477e96SSia Jee Heng u32 i; 112c5477e96SSia Jee Heng 113c5477e96SSia Jee Heng if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 114c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) 115c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + TER(i)); 116c5477e96SSia Jee Heng } else { 117c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) 118c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + RER(i)); 119c5477e96SSia Jee Heng } 120c5477e96SSia Jee Heng } 121c5477e96SSia Jee Heng 122c5477e96SSia Jee Heng static inline void kmb_i2s_clear_irqs(struct kmb_i2s_info *kmb_i2s, u32 stream) 123c5477e96SSia Jee Heng { 124c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 125c5477e96SSia Jee Heng u32 i; 126c5477e96SSia Jee Heng 127c5477e96SSia Jee Heng if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 128c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) 129c5477e96SSia Jee Heng readl(kmb_i2s->i2s_base + TOR(i)); 130c5477e96SSia Jee Heng } else { 131c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) 132c5477e96SSia Jee Heng readl(kmb_i2s->i2s_base + ROR(i)); 133c5477e96SSia Jee Heng } 134c5477e96SSia Jee Heng } 135c5477e96SSia Jee Heng 136c5477e96SSia Jee Heng static inline void kmb_i2s_irq_trigger(struct kmb_i2s_info *kmb_i2s, 137c5477e96SSia Jee Heng u32 stream, int chan_nr, bool trigger) 138c5477e96SSia Jee Heng { 139c5477e96SSia Jee Heng u32 i, irq; 140c5477e96SSia Jee Heng u32 flag; 141c5477e96SSia Jee Heng 142c5477e96SSia Jee Heng if (stream == SNDRV_PCM_STREAM_PLAYBACK) 143c5477e96SSia Jee Heng flag = TX_INT_FLAG; 144c5477e96SSia Jee Heng else 145c5477e96SSia Jee Heng flag = RX_INT_FLAG; 146c5477e96SSia Jee Heng 147c5477e96SSia Jee Heng for (i = 0; i < chan_nr / 2; i++) { 148c5477e96SSia Jee Heng irq = readl(kmb_i2s->i2s_base + IMR(i)); 149c5477e96SSia Jee Heng 150c5477e96SSia Jee Heng if (trigger) 151c5477e96SSia Jee Heng irq = irq & ~flag; 152c5477e96SSia Jee Heng else 153c5477e96SSia Jee Heng irq = irq | flag; 154c5477e96SSia Jee Heng 155c5477e96SSia Jee Heng writel(irq, kmb_i2s->i2s_base + IMR(i)); 156c5477e96SSia Jee Heng } 157c5477e96SSia Jee Heng } 158c5477e96SSia Jee Heng 159c5477e96SSia Jee Heng static void kmb_pcm_operation(struct kmb_i2s_info *kmb_i2s, bool playback) 160c5477e96SSia Jee Heng { 161c5477e96SSia Jee Heng struct snd_pcm_substream *substream; 162c5477e96SSia Jee Heng bool period_elapsed; 163c5477e96SSia Jee Heng unsigned int new_ptr; 164c5477e96SSia Jee Heng unsigned int ptr; 165c5477e96SSia Jee Heng 166c5477e96SSia Jee Heng if (playback) 167c5477e96SSia Jee Heng substream = kmb_i2s->tx_substream; 168c5477e96SSia Jee Heng else 169c5477e96SSia Jee Heng substream = kmb_i2s->rx_substream; 170c5477e96SSia Jee Heng 171c5477e96SSia Jee Heng if (!substream || !snd_pcm_running(substream)) 172c5477e96SSia Jee Heng return; 173c5477e96SSia Jee Heng 174c5477e96SSia Jee Heng if (playback) { 175c5477e96SSia Jee Heng ptr = kmb_i2s->tx_ptr; 176c5477e96SSia Jee Heng new_ptr = kmb_pcm_tx_fn(kmb_i2s, substream->runtime, 177c5477e96SSia Jee Heng ptr, &period_elapsed); 178c5477e96SSia Jee Heng cmpxchg(&kmb_i2s->tx_ptr, ptr, new_ptr); 179c5477e96SSia Jee Heng } else { 180c5477e96SSia Jee Heng ptr = kmb_i2s->rx_ptr; 181c5477e96SSia Jee Heng new_ptr = kmb_pcm_rx_fn(kmb_i2s, substream->runtime, 182c5477e96SSia Jee Heng ptr, &period_elapsed); 183c5477e96SSia Jee Heng cmpxchg(&kmb_i2s->rx_ptr, ptr, new_ptr); 184c5477e96SSia Jee Heng } 185c5477e96SSia Jee Heng 186c5477e96SSia Jee Heng if (period_elapsed) 187c5477e96SSia Jee Heng snd_pcm_period_elapsed(substream); 188c5477e96SSia Jee Heng } 189c5477e96SSia Jee Heng 190c5477e96SSia Jee Heng static int kmb_pcm_open(struct snd_soc_component *component, 191c5477e96SSia Jee Heng struct snd_pcm_substream *substream) 192c5477e96SSia Jee Heng { 193c5477e96SSia Jee Heng struct snd_pcm_runtime *runtime = substream->runtime; 194*2ab9a409SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 195c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s; 196c5477e96SSia Jee Heng 197c5477e96SSia Jee Heng kmb_i2s = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0)); 198c5477e96SSia Jee Heng snd_soc_set_runtime_hwparams(substream, &kmb_pcm_hardware); 199c5477e96SSia Jee Heng snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 200c5477e96SSia Jee Heng runtime->private_data = kmb_i2s; 201c5477e96SSia Jee Heng 202c5477e96SSia Jee Heng return 0; 203c5477e96SSia Jee Heng } 204c5477e96SSia Jee Heng 205c5477e96SSia Jee Heng static int kmb_pcm_trigger(struct snd_soc_component *component, 206c5477e96SSia Jee Heng struct snd_pcm_substream *substream, int cmd) 207c5477e96SSia Jee Heng { 208c5477e96SSia Jee Heng struct snd_pcm_runtime *runtime = substream->runtime; 209c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = runtime->private_data; 210c5477e96SSia Jee Heng 211c5477e96SSia Jee Heng switch (cmd) { 212c5477e96SSia Jee Heng case SNDRV_PCM_TRIGGER_START: 213c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 214c5477e96SSia Jee Heng kmb_i2s->tx_ptr = 0; 215c5477e96SSia Jee Heng kmb_i2s->tx_substream = substream; 216c5477e96SSia Jee Heng } else { 217c5477e96SSia Jee Heng kmb_i2s->rx_ptr = 0; 218c5477e96SSia Jee Heng kmb_i2s->rx_substream = substream; 219c5477e96SSia Jee Heng } 220c5477e96SSia Jee Heng break; 221c5477e96SSia Jee Heng case SNDRV_PCM_TRIGGER_STOP: 222c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 223c5477e96SSia Jee Heng kmb_i2s->tx_substream = NULL; 224c5477e96SSia Jee Heng else 225c5477e96SSia Jee Heng kmb_i2s->rx_substream = NULL; 226c5477e96SSia Jee Heng break; 227c5477e96SSia Jee Heng default: 228c5477e96SSia Jee Heng return -EINVAL; 229c5477e96SSia Jee Heng } 230c5477e96SSia Jee Heng 231c5477e96SSia Jee Heng return 0; 232c5477e96SSia Jee Heng } 233c5477e96SSia Jee Heng 234c5477e96SSia Jee Heng static irqreturn_t kmb_i2s_irq_handler(int irq, void *dev_id) 235c5477e96SSia Jee Heng { 236c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = dev_id; 237c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 238c5477e96SSia Jee Heng irqreturn_t ret = IRQ_NONE; 239c5477e96SSia Jee Heng u32 isr[4]; 240c5477e96SSia Jee Heng int i; 241c5477e96SSia Jee Heng 242c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) 243c5477e96SSia Jee Heng isr[i] = readl(kmb_i2s->i2s_base + ISR(i)); 244c5477e96SSia Jee Heng 245c5477e96SSia Jee Heng kmb_i2s_clear_irqs(kmb_i2s, SNDRV_PCM_STREAM_PLAYBACK); 246c5477e96SSia Jee Heng kmb_i2s_clear_irqs(kmb_i2s, SNDRV_PCM_STREAM_CAPTURE); 247c5477e96SSia Jee Heng 248c5477e96SSia Jee Heng for (i = 0; i < config->chan_nr / 2; i++) { 249c5477e96SSia Jee Heng /* 250c5477e96SSia Jee Heng * Check if TX fifo is empty. If empty fill FIFO with samples 251c5477e96SSia Jee Heng */ 252c5477e96SSia Jee Heng if ((isr[i] & ISR_TXFE)) { 253c5477e96SSia Jee Heng kmb_pcm_operation(kmb_i2s, true); 254c5477e96SSia Jee Heng ret = IRQ_HANDLED; 255c5477e96SSia Jee Heng } 256c5477e96SSia Jee Heng /* 257c5477e96SSia Jee Heng * Data available. Retrieve samples from FIFO 258c5477e96SSia Jee Heng */ 259c5477e96SSia Jee Heng if ((isr[i] & ISR_RXDA)) { 260c5477e96SSia Jee Heng kmb_pcm_operation(kmb_i2s, false); 261c5477e96SSia Jee Heng ret = IRQ_HANDLED; 262c5477e96SSia Jee Heng } 263c5477e96SSia Jee Heng /* Error Handling: TX */ 264c5477e96SSia Jee Heng if (isr[i] & ISR_TXFO) { 265c5477e96SSia Jee Heng dev_dbg(kmb_i2s->dev, "TX overrun (ch_id=%d)\n", i); 266c5477e96SSia Jee Heng ret = IRQ_HANDLED; 267c5477e96SSia Jee Heng } 268c5477e96SSia Jee Heng /* Error Handling: RX */ 269c5477e96SSia Jee Heng if (isr[i] & ISR_RXFO) { 270c5477e96SSia Jee Heng dev_dbg(kmb_i2s->dev, "RX overrun (ch_id=%d)\n", i); 271c5477e96SSia Jee Heng ret = IRQ_HANDLED; 272c5477e96SSia Jee Heng } 273c5477e96SSia Jee Heng } 274c5477e96SSia Jee Heng 275c5477e96SSia Jee Heng return ret; 276c5477e96SSia Jee Heng } 277c5477e96SSia Jee Heng 278c5477e96SSia Jee Heng static int kmb_platform_pcm_new(struct snd_soc_component *component, 279c5477e96SSia Jee Heng struct snd_soc_pcm_runtime *soc_runtime) 280c5477e96SSia Jee Heng { 281c5477e96SSia Jee Heng size_t size = kmb_pcm_hardware.buffer_bytes_max; 282c5477e96SSia Jee Heng /* Use SNDRV_DMA_TYPE_CONTINUOUS as KMB doesn't use PCI sg buffer */ 283c5477e96SSia Jee Heng snd_pcm_set_managed_buffer_all(soc_runtime->pcm, 284c5477e96SSia Jee Heng SNDRV_DMA_TYPE_CONTINUOUS, 285c5477e96SSia Jee Heng NULL, size, size); 286c5477e96SSia Jee Heng return 0; 287c5477e96SSia Jee Heng } 288c5477e96SSia Jee Heng 289c5477e96SSia Jee Heng static snd_pcm_uframes_t kmb_pcm_pointer(struct snd_soc_component *component, 290c5477e96SSia Jee Heng struct snd_pcm_substream *substream) 291c5477e96SSia Jee Heng { 292c5477e96SSia Jee Heng struct snd_pcm_runtime *runtime = substream->runtime; 293c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = runtime->private_data; 294c5477e96SSia Jee Heng snd_pcm_uframes_t pos; 295c5477e96SSia Jee Heng 296c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 297c5477e96SSia Jee Heng pos = kmb_i2s->tx_ptr; 298c5477e96SSia Jee Heng else 299c5477e96SSia Jee Heng pos = kmb_i2s->rx_ptr; 300c5477e96SSia Jee Heng 301c5477e96SSia Jee Heng return pos < runtime->buffer_size ? pos : 0; 302c5477e96SSia Jee Heng } 303c5477e96SSia Jee Heng 304c5477e96SSia Jee Heng static const struct snd_soc_component_driver kmb_component = { 305c5477e96SSia Jee Heng .name = "kmb", 306c5477e96SSia Jee Heng .pcm_construct = kmb_platform_pcm_new, 307c5477e96SSia Jee Heng .open = kmb_pcm_open, 308c5477e96SSia Jee Heng .trigger = kmb_pcm_trigger, 309c5477e96SSia Jee Heng .pointer = kmb_pcm_pointer, 310c5477e96SSia Jee Heng }; 311c5477e96SSia Jee Heng 312c5477e96SSia Jee Heng static void kmb_i2s_start(struct kmb_i2s_info *kmb_i2s, 313c5477e96SSia Jee Heng struct snd_pcm_substream *substream) 314c5477e96SSia Jee Heng { 315c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 316c5477e96SSia Jee Heng 317c5477e96SSia Jee Heng /* I2S Programming sequence in Keem_Bay_VPU_DB_v1.1 */ 318c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + IER); 319c5477e96SSia Jee Heng 320c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 321c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + ITER); 322c5477e96SSia Jee Heng else 323c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + IRER); 324c5477e96SSia Jee Heng 325c5477e96SSia Jee Heng kmb_i2s_irq_trigger(kmb_i2s, substream->stream, config->chan_nr, true); 326c5477e96SSia Jee Heng 327c5477e96SSia Jee Heng if (kmb_i2s->master) 328c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + CER); 329c5477e96SSia Jee Heng else 330c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + CER); 331c5477e96SSia Jee Heng } 332c5477e96SSia Jee Heng 333c5477e96SSia Jee Heng static void kmb_i2s_stop(struct kmb_i2s_info *kmb_i2s, 334c5477e96SSia Jee Heng struct snd_pcm_substream *substream) 335c5477e96SSia Jee Heng { 336c5477e96SSia Jee Heng /* I2S Programming sequence in Keem_Bay_VPU_DB_v1.1 */ 337c5477e96SSia Jee Heng kmb_i2s_clear_irqs(kmb_i2s, substream->stream); 338c5477e96SSia Jee Heng 339c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 340c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + ITER); 341c5477e96SSia Jee Heng else 342c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + IRER); 343c5477e96SSia Jee Heng 344c5477e96SSia Jee Heng kmb_i2s_irq_trigger(kmb_i2s, substream->stream, 8, false); 345c5477e96SSia Jee Heng 346c5477e96SSia Jee Heng if (!kmb_i2s->active) { 347c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + CER); 348c5477e96SSia Jee Heng writel(0, kmb_i2s->i2s_base + IER); 349c5477e96SSia Jee Heng } 350c5477e96SSia Jee Heng } 351c5477e96SSia Jee Heng 352c5477e96SSia Jee Heng static void kmb_disable_clk(void *clk) 353c5477e96SSia Jee Heng { 354c5477e96SSia Jee Heng clk_disable_unprepare(clk); 355c5477e96SSia Jee Heng } 356c5477e96SSia Jee Heng 357c5477e96SSia Jee Heng static int kmb_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 358c5477e96SSia Jee Heng { 359c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai); 360c5477e96SSia Jee Heng int ret; 361c5477e96SSia Jee Heng 362c5477e96SSia Jee Heng switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 363c5477e96SSia Jee Heng case SND_SOC_DAIFMT_CBM_CFM: 364c5477e96SSia Jee Heng kmb_i2s->master = false; 365c5477e96SSia Jee Heng ret = 0; 366c5477e96SSia Jee Heng break; 367c5477e96SSia Jee Heng case SND_SOC_DAIFMT_CBS_CFS: 368c5477e96SSia Jee Heng writel(MASTER_MODE, kmb_i2s->pss_base + I2S_GEN_CFG_0); 369c5477e96SSia Jee Heng 370c5477e96SSia Jee Heng ret = clk_prepare_enable(kmb_i2s->clk_i2s); 371c5477e96SSia Jee Heng if (ret < 0) 372c5477e96SSia Jee Heng return ret; 373c5477e96SSia Jee Heng 374c5477e96SSia Jee Heng ret = devm_add_action_or_reset(kmb_i2s->dev, kmb_disable_clk, 375c5477e96SSia Jee Heng kmb_i2s->clk_i2s); 376c5477e96SSia Jee Heng if (ret) 377c5477e96SSia Jee Heng return ret; 378c5477e96SSia Jee Heng 379c5477e96SSia Jee Heng kmb_i2s->master = true; 380c5477e96SSia Jee Heng break; 381c5477e96SSia Jee Heng default: 382c5477e96SSia Jee Heng return -EINVAL; 383c5477e96SSia Jee Heng } 384c5477e96SSia Jee Heng 385c5477e96SSia Jee Heng return ret; 386c5477e96SSia Jee Heng } 387c5477e96SSia Jee Heng 388c5477e96SSia Jee Heng static int kmb_dai_trigger(struct snd_pcm_substream *substream, 389c5477e96SSia Jee Heng int cmd, struct snd_soc_dai *cpu_dai) 390c5477e96SSia Jee Heng { 391c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai); 392c5477e96SSia Jee Heng 393c5477e96SSia Jee Heng switch (cmd) { 394c5477e96SSia Jee Heng case SNDRV_PCM_TRIGGER_START: 395c5477e96SSia Jee Heng /* Keep track of i2s activity before turn off 396c5477e96SSia Jee Heng * the i2s interface 397c5477e96SSia Jee Heng */ 398c5477e96SSia Jee Heng kmb_i2s->active++; 399c5477e96SSia Jee Heng kmb_i2s_start(kmb_i2s, substream); 400c5477e96SSia Jee Heng break; 401c5477e96SSia Jee Heng case SNDRV_PCM_TRIGGER_STOP: 402c5477e96SSia Jee Heng kmb_i2s->active--; 403c5477e96SSia Jee Heng kmb_i2s_stop(kmb_i2s, substream); 404c5477e96SSia Jee Heng break; 405c5477e96SSia Jee Heng default: 406c5477e96SSia Jee Heng return -EINVAL; 407c5477e96SSia Jee Heng } 408c5477e96SSia Jee Heng 409c5477e96SSia Jee Heng return 0; 410c5477e96SSia Jee Heng } 411c5477e96SSia Jee Heng 412c5477e96SSia Jee Heng static void kmb_i2s_config(struct kmb_i2s_info *kmb_i2s, int stream) 413c5477e96SSia Jee Heng { 414c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 415c5477e96SSia Jee Heng u32 ch_reg; 416c5477e96SSia Jee Heng 417c5477e96SSia Jee Heng kmb_i2s_disable_channels(kmb_i2s, stream); 418c5477e96SSia Jee Heng 419c5477e96SSia Jee Heng for (ch_reg = 0; ch_reg < config->chan_nr / 2; ch_reg++) { 420c5477e96SSia Jee Heng if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 421c5477e96SSia Jee Heng writel(kmb_i2s->xfer_resolution, 422c5477e96SSia Jee Heng kmb_i2s->i2s_base + TCR(ch_reg)); 423c5477e96SSia Jee Heng 424c5477e96SSia Jee Heng writel(kmb_i2s->fifo_th - 1, 425c5477e96SSia Jee Heng kmb_i2s->i2s_base + TFCR(ch_reg)); 426c5477e96SSia Jee Heng 427c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + TER(ch_reg)); 428c5477e96SSia Jee Heng } else { 429c5477e96SSia Jee Heng writel(kmb_i2s->xfer_resolution, 430c5477e96SSia Jee Heng kmb_i2s->i2s_base + RCR(ch_reg)); 431c5477e96SSia Jee Heng 432c5477e96SSia Jee Heng writel(kmb_i2s->fifo_th - 1, 433c5477e96SSia Jee Heng kmb_i2s->i2s_base + RFCR(ch_reg)); 434c5477e96SSia Jee Heng 435c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + RER(ch_reg)); 436c5477e96SSia Jee Heng } 437c5477e96SSia Jee Heng } 438c5477e96SSia Jee Heng } 439c5477e96SSia Jee Heng 440c5477e96SSia Jee Heng static int kmb_dai_hw_params(struct snd_pcm_substream *substream, 441c5477e96SSia Jee Heng struct snd_pcm_hw_params *hw_params, 442c5477e96SSia Jee Heng struct snd_soc_dai *cpu_dai) 443c5477e96SSia Jee Heng { 444c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai); 445c5477e96SSia Jee Heng struct i2s_clk_config_data *config = &kmb_i2s->config; 446c5477e96SSia Jee Heng u32 register_val, write_val; 447c5477e96SSia Jee Heng int ret; 448c5477e96SSia Jee Heng 449c5477e96SSia Jee Heng switch (params_format(hw_params)) { 450c5477e96SSia Jee Heng case SNDRV_PCM_FORMAT_S16_LE: 451c5477e96SSia Jee Heng config->data_width = 16; 452c5477e96SSia Jee Heng kmb_i2s->ccr = 0x00; 453c5477e96SSia Jee Heng kmb_i2s->xfer_resolution = 0x02; 454c5477e96SSia Jee Heng break; 455c5477e96SSia Jee Heng case SNDRV_PCM_FORMAT_S24_LE: 456c5477e96SSia Jee Heng config->data_width = 24; 457c5477e96SSia Jee Heng kmb_i2s->ccr = 0x08; 458c5477e96SSia Jee Heng kmb_i2s->xfer_resolution = 0x04; 459c5477e96SSia Jee Heng break; 460c5477e96SSia Jee Heng case SNDRV_PCM_FORMAT_S32_LE: 461c5477e96SSia Jee Heng config->data_width = 32; 462c5477e96SSia Jee Heng kmb_i2s->ccr = 0x10; 463c5477e96SSia Jee Heng kmb_i2s->xfer_resolution = 0x05; 464c5477e96SSia Jee Heng break; 465c5477e96SSia Jee Heng default: 466c5477e96SSia Jee Heng dev_err(kmb_i2s->dev, "kmb: unsupported PCM fmt"); 467c5477e96SSia Jee Heng return -EINVAL; 468c5477e96SSia Jee Heng } 469c5477e96SSia Jee Heng 470c5477e96SSia Jee Heng config->chan_nr = params_channels(hw_params); 471c5477e96SSia Jee Heng 472c5477e96SSia Jee Heng switch (config->chan_nr) { 473c5477e96SSia Jee Heng /* TODO: This switch case will handle up to TDM8 in the near future */ 474c5477e96SSia Jee Heng case TWO_CHANNEL_SUPPORT: 475c5477e96SSia Jee Heng write_val = ((config->chan_nr / 2) << TDM_CHANNEL_CONFIG_BIT) | 476c5477e96SSia Jee Heng (config->data_width << DATA_WIDTH_CONFIG_BIT) | 477c5477e96SSia Jee Heng MASTER_MODE | I2S_OPERATION; 478c5477e96SSia Jee Heng 479c5477e96SSia Jee Heng writel(write_val, kmb_i2s->pss_base + I2S_GEN_CFG_0); 480c5477e96SSia Jee Heng 481c5477e96SSia Jee Heng register_val = readl(kmb_i2s->pss_base + I2S_GEN_CFG_0); 482c5477e96SSia Jee Heng dev_dbg(kmb_i2s->dev, "pss register = 0x%X", register_val); 483c5477e96SSia Jee Heng break; 484c5477e96SSia Jee Heng default: 485c5477e96SSia Jee Heng dev_dbg(kmb_i2s->dev, "channel not supported\n"); 486c5477e96SSia Jee Heng return -EINVAL; 487c5477e96SSia Jee Heng } 488c5477e96SSia Jee Heng 489c5477e96SSia Jee Heng kmb_i2s_config(kmb_i2s, substream->stream); 490c5477e96SSia Jee Heng 491c5477e96SSia Jee Heng writel(kmb_i2s->ccr, kmb_i2s->i2s_base + CCR); 492c5477e96SSia Jee Heng 493c5477e96SSia Jee Heng config->sample_rate = params_rate(hw_params); 494c5477e96SSia Jee Heng 495c5477e96SSia Jee Heng if (kmb_i2s->master) { 496c5477e96SSia Jee Heng /* Only 2 ch supported in Master mode */ 497c5477e96SSia Jee Heng u32 bitclk = config->sample_rate * config->data_width * 2; 498c5477e96SSia Jee Heng 499c5477e96SSia Jee Heng ret = clk_set_rate(kmb_i2s->clk_i2s, bitclk); 500c5477e96SSia Jee Heng if (ret) { 501c5477e96SSia Jee Heng dev_err(kmb_i2s->dev, 502c5477e96SSia Jee Heng "Can't set I2S clock rate: %d\n", ret); 503c5477e96SSia Jee Heng return ret; 504c5477e96SSia Jee Heng } 505c5477e96SSia Jee Heng } 506c5477e96SSia Jee Heng 507c5477e96SSia Jee Heng return 0; 508c5477e96SSia Jee Heng } 509c5477e96SSia Jee Heng 510c5477e96SSia Jee Heng static int kmb_dai_prepare(struct snd_pcm_substream *substream, 511c5477e96SSia Jee Heng struct snd_soc_dai *cpu_dai) 512c5477e96SSia Jee Heng { 513c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s = snd_soc_dai_get_drvdata(cpu_dai); 514c5477e96SSia Jee Heng 515c5477e96SSia Jee Heng if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 516c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + TXFFR); 517c5477e96SSia Jee Heng else 518c5477e96SSia Jee Heng writel(1, kmb_i2s->i2s_base + RXFFR); 519c5477e96SSia Jee Heng 520c5477e96SSia Jee Heng return 0; 521c5477e96SSia Jee Heng } 522c5477e96SSia Jee Heng 523c5477e96SSia Jee Heng static struct snd_soc_dai_ops kmb_dai_ops = { 524c5477e96SSia Jee Heng .trigger = kmb_dai_trigger, 525c5477e96SSia Jee Heng .hw_params = kmb_dai_hw_params, 526c5477e96SSia Jee Heng .prepare = kmb_dai_prepare, 527c5477e96SSia Jee Heng .set_fmt = kmb_set_dai_fmt, 528c5477e96SSia Jee Heng }; 529c5477e96SSia Jee Heng 530c5477e96SSia Jee Heng static struct snd_soc_dai_driver intel_kmb_platform_dai[] = { 531c5477e96SSia Jee Heng { 532c5477e96SSia Jee Heng .name = "kmb-plat-dai", 533c5477e96SSia Jee Heng .playback = { 534c5477e96SSia Jee Heng .channels_min = 2, 535c5477e96SSia Jee Heng .channels_max = 2, 536c5477e96SSia Jee Heng .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000, 537c5477e96SSia Jee Heng .rate_min = 16000, 538c5477e96SSia Jee Heng .rate_max = 48000, 539c5477e96SSia Jee Heng .formats = (SNDRV_PCM_FMTBIT_S32_LE | 540c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S24_LE | 541c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S16_LE), 542c5477e96SSia Jee Heng }, 543c5477e96SSia Jee Heng .capture = { 544c5477e96SSia Jee Heng .channels_min = 2, 545c5477e96SSia Jee Heng .channels_max = 2, 546c5477e96SSia Jee Heng .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000, 547c5477e96SSia Jee Heng .rate_min = 16000, 548c5477e96SSia Jee Heng .rate_max = 48000, 549c5477e96SSia Jee Heng .formats = (SNDRV_PCM_FMTBIT_S32_LE | 550c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S24_LE | 551c5477e96SSia Jee Heng SNDRV_PCM_FMTBIT_S16_LE), 552c5477e96SSia Jee Heng }, 553c5477e96SSia Jee Heng .ops = &kmb_dai_ops, 554c5477e96SSia Jee Heng }, 555c5477e96SSia Jee Heng }; 556c5477e96SSia Jee Heng 557c5477e96SSia Jee Heng static int kmb_plat_dai_probe(struct platform_device *pdev) 558c5477e96SSia Jee Heng { 559c5477e96SSia Jee Heng struct snd_soc_dai_driver *kmb_i2s_dai; 560c5477e96SSia Jee Heng struct device *dev = &pdev->dev; 561c5477e96SSia Jee Heng struct kmb_i2s_info *kmb_i2s; 562c5477e96SSia Jee Heng int ret, irq; 563c5477e96SSia Jee Heng u32 comp1_reg; 564c5477e96SSia Jee Heng 565c5477e96SSia Jee Heng kmb_i2s = devm_kzalloc(dev, sizeof(*kmb_i2s), GFP_KERNEL); 566c5477e96SSia Jee Heng if (!kmb_i2s) 567c5477e96SSia Jee Heng return -ENOMEM; 568c5477e96SSia Jee Heng 569c5477e96SSia Jee Heng kmb_i2s_dai = devm_kzalloc(dev, sizeof(*kmb_i2s_dai), GFP_KERNEL); 570c5477e96SSia Jee Heng if (!kmb_i2s_dai) 571c5477e96SSia Jee Heng return -ENOMEM; 572c5477e96SSia Jee Heng 573c5477e96SSia Jee Heng kmb_i2s_dai->ops = &kmb_dai_ops; 574c5477e96SSia Jee Heng 575c5477e96SSia Jee Heng /* Prepare the related clocks */ 576c5477e96SSia Jee Heng kmb_i2s->clk_apb = devm_clk_get(dev, "apb_clk"); 577c5477e96SSia Jee Heng if (IS_ERR(kmb_i2s->clk_apb)) { 578c5477e96SSia Jee Heng dev_err(dev, "Failed to get apb clock\n"); 579c5477e96SSia Jee Heng return PTR_ERR(kmb_i2s->clk_apb); 580c5477e96SSia Jee Heng } 581c5477e96SSia Jee Heng 582c5477e96SSia Jee Heng ret = clk_prepare_enable(kmb_i2s->clk_apb); 583c5477e96SSia Jee Heng if (ret < 0) 584c5477e96SSia Jee Heng return ret; 585c5477e96SSia Jee Heng 586c5477e96SSia Jee Heng ret = devm_add_action_or_reset(dev, kmb_disable_clk, kmb_i2s->clk_apb); 587c5477e96SSia Jee Heng if (ret) { 588c5477e96SSia Jee Heng dev_err(dev, "Failed to add clk_apb reset action\n"); 589c5477e96SSia Jee Heng return ret; 590c5477e96SSia Jee Heng } 591c5477e96SSia Jee Heng 592c5477e96SSia Jee Heng kmb_i2s->clk_i2s = devm_clk_get(dev, "osc"); 593c5477e96SSia Jee Heng if (IS_ERR(kmb_i2s->clk_i2s)) { 594c5477e96SSia Jee Heng dev_err(dev, "Failed to get osc clock\n"); 595c5477e96SSia Jee Heng return PTR_ERR(kmb_i2s->clk_i2s); 596c5477e96SSia Jee Heng } 597c5477e96SSia Jee Heng 598c5477e96SSia Jee Heng kmb_i2s->i2s_base = devm_platform_ioremap_resource(pdev, 0); 599c5477e96SSia Jee Heng if (IS_ERR(kmb_i2s->i2s_base)) 600c5477e96SSia Jee Heng return PTR_ERR(kmb_i2s->i2s_base); 601c5477e96SSia Jee Heng 602c5477e96SSia Jee Heng kmb_i2s->pss_base = devm_platform_ioremap_resource(pdev, 1); 603c5477e96SSia Jee Heng if (IS_ERR(kmb_i2s->pss_base)) 604c5477e96SSia Jee Heng return PTR_ERR(kmb_i2s->pss_base); 605c5477e96SSia Jee Heng 606c5477e96SSia Jee Heng kmb_i2s->dev = &pdev->dev; 607c5477e96SSia Jee Heng 608c5477e96SSia Jee Heng irq = platform_get_irq_optional(pdev, 0); 609c5477e96SSia Jee Heng if (irq > 0) { 610c5477e96SSia Jee Heng ret = devm_request_irq(dev, irq, kmb_i2s_irq_handler, 0, 611c5477e96SSia Jee Heng pdev->name, kmb_i2s); 612c5477e96SSia Jee Heng if (ret < 0) { 613c5477e96SSia Jee Heng dev_err(dev, "failed to request irq\n"); 614c5477e96SSia Jee Heng return ret; 615c5477e96SSia Jee Heng } 616c5477e96SSia Jee Heng } 617c5477e96SSia Jee Heng 618c5477e96SSia Jee Heng comp1_reg = readl(kmb_i2s->i2s_base + I2S_COMP_PARAM_1); 619c5477e96SSia Jee Heng 620c5477e96SSia Jee Heng kmb_i2s->fifo_th = (1 << COMP1_FIFO_DEPTH(comp1_reg)) / 2; 621c5477e96SSia Jee Heng 622c5477e96SSia Jee Heng ret = devm_snd_soc_register_component(dev, &kmb_component, 623c5477e96SSia Jee Heng intel_kmb_platform_dai, 624c5477e96SSia Jee Heng ARRAY_SIZE(intel_kmb_platform_dai)); 625c5477e96SSia Jee Heng if (ret) { 626c5477e96SSia Jee Heng dev_err(dev, "not able to register dai\n"); 627c5477e96SSia Jee Heng return ret; 628c5477e96SSia Jee Heng } 629c5477e96SSia Jee Heng 630c5477e96SSia Jee Heng dev_set_drvdata(dev, kmb_i2s); 631c5477e96SSia Jee Heng 632c5477e96SSia Jee Heng return ret; 633c5477e96SSia Jee Heng } 634c5477e96SSia Jee Heng 635c5477e96SSia Jee Heng static const struct of_device_id kmb_plat_of_match[] = { 636c5477e96SSia Jee Heng { .compatible = "intel,keembay-i2s", }, 637c5477e96SSia Jee Heng {} 638c5477e96SSia Jee Heng }; 639c5477e96SSia Jee Heng 640c5477e96SSia Jee Heng static struct platform_driver kmb_plat_dai_driver = { 641c5477e96SSia Jee Heng .driver = { 642c5477e96SSia Jee Heng .name = "kmb-plat-dai", 643c5477e96SSia Jee Heng .of_match_table = kmb_plat_of_match, 644c5477e96SSia Jee Heng }, 645c5477e96SSia Jee Heng .probe = kmb_plat_dai_probe, 646c5477e96SSia Jee Heng }; 647c5477e96SSia Jee Heng 648c5477e96SSia Jee Heng module_platform_driver(kmb_plat_dai_driver); 649c5477e96SSia Jee Heng 650c5477e96SSia Jee Heng MODULE_DESCRIPTION("ASoC Intel KeemBay Platform driver"); 651c5477e96SSia Jee Heng MODULE_AUTHOR("Sia Jee Heng <jee.heng.sia@intel.com>"); 652c5477e96SSia Jee Heng MODULE_AUTHOR("Sit, Michael Wei Hong <michael.wei.hong.sit@intel.com>"); 653c5477e96SSia Jee Heng MODULE_LICENSE("GPL v2"); 654c5477e96SSia Jee Heng MODULE_ALIAS("platform:kmb_platform"); 655