1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
5 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
6 * Roger Nilsson <roger.xr.nilsson@stericsson.com>,
7 * Sandeep Kaushik <sandeep.kaushik@st.com>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
17 #include <linux/platform_data/asoc-ux500-msp.h>
19 #include <sound/soc.h>
21 #include "ux500_msp_i2s.h"
23 /* Protocol desciptors */
24 static const struct msp_protdesc prot_descs[] = {
28 MSP_PHASE2_START_MODE_IMEDIATE,
29 MSP_PHASE2_START_MODE_IMEDIATE,
48 MSP_COMPRESS_MODE_LINEAR,
49 MSP_EXPAND_MODE_LINEAR,
57 MSP_PHASE2_START_MODE_FSYNC,
58 MSP_PHASE2_START_MODE_FSYNC,
77 MSP_COMPRESS_MODE_LINEAR,
78 MSP_EXPAND_MODE_LINEAR,
83 }, { /* Companded PCM */
86 MSP_PHASE2_START_MODE_FSYNC,
87 MSP_PHASE2_START_MODE_FSYNC,
102 MSP_FSYNC_POL_ACT_HI,
103 MSP_FSYNC_POL_ACT_HI,
106 MSP_COMPRESS_MODE_LINEAR,
107 MSP_EXPAND_MODE_LINEAR,
115 static void set_prot_desc_tx(struct ux500_msp *msp,
116 struct msp_protdesc *protdesc,
117 enum msp_data_size data_size)
121 temp_reg |= MSP_P2_ENABLE_BIT(protdesc->tx_phase_mode);
122 temp_reg |= MSP_P2_START_MODE_BIT(protdesc->tx_phase2_start_mode);
123 temp_reg |= MSP_P1_FRAME_LEN_BITS(protdesc->tx_frame_len_1);
124 temp_reg |= MSP_P2_FRAME_LEN_BITS(protdesc->tx_frame_len_2);
125 if (msp->def_elem_len) {
126 temp_reg |= MSP_P1_ELEM_LEN_BITS(protdesc->tx_elem_len_1);
127 temp_reg |= MSP_P2_ELEM_LEN_BITS(protdesc->tx_elem_len_2);
129 temp_reg |= MSP_P1_ELEM_LEN_BITS(data_size);
130 temp_reg |= MSP_P2_ELEM_LEN_BITS(data_size);
132 temp_reg |= MSP_DATA_DELAY_BITS(protdesc->tx_data_delay);
133 temp_reg |= MSP_SET_ENDIANNES_BIT(protdesc->tx_byte_order);
134 temp_reg |= MSP_FSYNC_POL(protdesc->tx_fsync_pol);
135 temp_reg |= MSP_DATA_WORD_SWAP(protdesc->tx_half_word_swap);
136 temp_reg |= MSP_SET_COMPANDING_MODE(protdesc->compression_mode);
137 temp_reg |= MSP_SET_FSYNC_IGNORE(protdesc->frame_sync_ignore);
139 writel(temp_reg, msp->registers + MSP_TCF);
142 static void set_prot_desc_rx(struct ux500_msp *msp,
143 struct msp_protdesc *protdesc,
144 enum msp_data_size data_size)
148 temp_reg |= MSP_P2_ENABLE_BIT(protdesc->rx_phase_mode);
149 temp_reg |= MSP_P2_START_MODE_BIT(protdesc->rx_phase2_start_mode);
150 temp_reg |= MSP_P1_FRAME_LEN_BITS(protdesc->rx_frame_len_1);
151 temp_reg |= MSP_P2_FRAME_LEN_BITS(protdesc->rx_frame_len_2);
152 if (msp->def_elem_len) {
153 temp_reg |= MSP_P1_ELEM_LEN_BITS(protdesc->rx_elem_len_1);
154 temp_reg |= MSP_P2_ELEM_LEN_BITS(protdesc->rx_elem_len_2);
156 temp_reg |= MSP_P1_ELEM_LEN_BITS(data_size);
157 temp_reg |= MSP_P2_ELEM_LEN_BITS(data_size);
160 temp_reg |= MSP_DATA_DELAY_BITS(protdesc->rx_data_delay);
161 temp_reg |= MSP_SET_ENDIANNES_BIT(protdesc->rx_byte_order);
162 temp_reg |= MSP_FSYNC_POL(protdesc->rx_fsync_pol);
163 temp_reg |= MSP_DATA_WORD_SWAP(protdesc->rx_half_word_swap);
164 temp_reg |= MSP_SET_COMPANDING_MODE(protdesc->expansion_mode);
165 temp_reg |= MSP_SET_FSYNC_IGNORE(protdesc->frame_sync_ignore);
167 writel(temp_reg, msp->registers + MSP_RCF);
170 static int configure_protocol(struct ux500_msp *msp,
171 struct ux500_msp_config *config)
173 struct msp_protdesc *protdesc;
174 enum msp_data_size data_size;
177 data_size = config->data_size;
178 msp->def_elem_len = config->def_elem_len;
179 if (config->default_protdesc == 1) {
180 if (config->protocol >= MSP_INVALID_PROTOCOL) {
181 dev_err(msp->dev, "%s: ERROR: Invalid protocol!\n",
186 (struct msp_protdesc *)&prot_descs[config->protocol];
188 protdesc = (struct msp_protdesc *)&config->protdesc;
191 if (data_size < MSP_DATA_BITS_DEFAULT || data_size > MSP_DATA_BITS_32) {
193 "%s: ERROR: Invalid data-size requested (data_size = %d)!\n",
194 __func__, data_size);
198 if (config->direction & MSP_DIR_TX)
199 set_prot_desc_tx(msp, protdesc, data_size);
200 if (config->direction & MSP_DIR_RX)
201 set_prot_desc_rx(msp, protdesc, data_size);
203 /* The code below should not be separated. */
204 temp_reg = readl(msp->registers + MSP_GCR) & ~TX_CLK_POL_RISING;
205 temp_reg |= MSP_TX_CLKPOL_BIT(~protdesc->tx_clk_pol);
206 writel(temp_reg, msp->registers + MSP_GCR);
207 temp_reg = readl(msp->registers + MSP_GCR) & ~RX_CLK_POL_RISING;
208 temp_reg |= MSP_RX_CLKPOL_BIT(protdesc->rx_clk_pol);
209 writel(temp_reg, msp->registers + MSP_GCR);
214 static int setup_bitclk(struct ux500_msp *msp, struct ux500_msp_config *config)
221 struct msp_protdesc *protdesc = NULL;
223 reg_val_GCR = readl(msp->registers + MSP_GCR);
224 writel(reg_val_GCR & ~SRG_ENABLE, msp->registers + MSP_GCR);
226 if (config->default_protdesc)
228 (struct msp_protdesc *)&prot_descs[config->protocol];
230 protdesc = (struct msp_protdesc *)&config->protdesc;
232 switch (config->protocol) {
233 case MSP_PCM_PROTOCOL:
234 case MSP_PCM_COMPAND_PROTOCOL:
235 frame_width = protdesc->frame_width;
236 sck_div = config->f_inputclk / (config->frame_freq *
237 (protdesc->clocks_per_frame));
238 frame_per = protdesc->frame_period;
240 case MSP_I2S_PROTOCOL:
241 frame_width = protdesc->frame_width;
242 sck_div = config->f_inputclk / (config->frame_freq *
243 (protdesc->clocks_per_frame));
244 frame_per = protdesc->frame_period;
247 dev_err(msp->dev, "%s: ERROR: Unknown protocol (%d)!\n",
253 temp_reg = (sck_div - 1) & SCK_DIV_MASK;
254 temp_reg |= FRAME_WIDTH_BITS(frame_width);
255 temp_reg |= FRAME_PERIOD_BITS(frame_per);
256 writel(temp_reg, msp->registers + MSP_SRG);
258 msp->f_bitclk = (config->f_inputclk)/(sck_div + 1);
260 /* Enable bit-clock */
262 reg_val_GCR = readl(msp->registers + MSP_GCR);
263 writel(reg_val_GCR | SRG_ENABLE, msp->registers + MSP_GCR);
269 static int configure_multichannel(struct ux500_msp *msp,
270 struct ux500_msp_config *config)
272 struct msp_protdesc *protdesc;
273 struct msp_multichannel_config *mcfg;
276 if (config->default_protdesc == 1) {
277 if (config->protocol >= MSP_INVALID_PROTOCOL) {
279 "%s: ERROR: Invalid protocol (%d)!\n",
280 __func__, config->protocol);
283 protdesc = (struct msp_protdesc *)
284 &prot_descs[config->protocol];
286 protdesc = (struct msp_protdesc *)&config->protdesc;
289 mcfg = &config->multichannel_config;
290 if (mcfg->tx_multichannel_enable) {
291 if (protdesc->tx_phase_mode == MSP_SINGLE_PHASE) {
292 reg_val_MCR = readl(msp->registers + MSP_MCR);
293 writel(reg_val_MCR | (mcfg->tx_multichannel_enable ?
295 msp->registers + MSP_MCR);
296 writel(mcfg->tx_channel_0_enable,
297 msp->registers + MSP_TCE0);
298 writel(mcfg->tx_channel_1_enable,
299 msp->registers + MSP_TCE1);
300 writel(mcfg->tx_channel_2_enable,
301 msp->registers + MSP_TCE2);
302 writel(mcfg->tx_channel_3_enable,
303 msp->registers + MSP_TCE3);
306 "%s: ERROR: Only single-phase supported (TX-mode: %d)!\n",
307 __func__, protdesc->tx_phase_mode);
311 if (mcfg->rx_multichannel_enable) {
312 if (protdesc->rx_phase_mode == MSP_SINGLE_PHASE) {
313 reg_val_MCR = readl(msp->registers + MSP_MCR);
314 writel(reg_val_MCR | (mcfg->rx_multichannel_enable ?
316 msp->registers + MSP_MCR);
317 writel(mcfg->rx_channel_0_enable,
318 msp->registers + MSP_RCE0);
319 writel(mcfg->rx_channel_1_enable,
320 msp->registers + MSP_RCE1);
321 writel(mcfg->rx_channel_2_enable,
322 msp->registers + MSP_RCE2);
323 writel(mcfg->rx_channel_3_enable,
324 msp->registers + MSP_RCE3);
327 "%s: ERROR: Only single-phase supported (RX-mode: %d)!\n",
328 __func__, protdesc->rx_phase_mode);
331 if (mcfg->rx_comparison_enable_mode) {
332 reg_val_MCR = readl(msp->registers + MSP_MCR);
334 (mcfg->rx_comparison_enable_mode << RCMPM_BIT),
335 msp->registers + MSP_MCR);
337 writel(mcfg->comparison_mask,
338 msp->registers + MSP_RCM);
339 writel(mcfg->comparison_value,
340 msp->registers + MSP_RCV);
348 static int enable_msp(struct ux500_msp *msp, struct ux500_msp_config *config)
351 u32 reg_val_DMACR, reg_val_GCR;
353 /* Configure msp with protocol dependent settings */
354 configure_protocol(msp, config);
355 setup_bitclk(msp, config);
356 if (config->multichannel_configured == 1) {
357 status = configure_multichannel(msp, config);
360 "%s: WARN: configure_multichannel failed (%d)!\n",
364 /* Make sure the correct DMA-directions are configured */
365 if ((config->direction & MSP_DIR_RX) &&
366 !msp->capture_dma_data.dma_cfg) {
367 dev_err(msp->dev, "%s: ERROR: MSP RX-mode is not configured!",
371 if ((config->direction == MSP_DIR_TX) &&
372 !msp->playback_dma_data.dma_cfg) {
373 dev_err(msp->dev, "%s: ERROR: MSP TX-mode is not configured!",
378 reg_val_DMACR = readl(msp->registers + MSP_DMACR);
379 if (config->direction & MSP_DIR_RX)
380 reg_val_DMACR |= RX_DMA_ENABLE;
381 if (config->direction & MSP_DIR_TX)
382 reg_val_DMACR |= TX_DMA_ENABLE;
383 writel(reg_val_DMACR, msp->registers + MSP_DMACR);
385 writel(config->iodelay, msp->registers + MSP_IODLY);
387 /* Enable frame generation logic */
388 reg_val_GCR = readl(msp->registers + MSP_GCR);
389 writel(reg_val_GCR | FRAME_GEN_ENABLE, msp->registers + MSP_GCR);
394 static void flush_fifo_rx(struct ux500_msp *msp)
396 u32 reg_val_GCR, reg_val_FLR;
399 reg_val_GCR = readl(msp->registers + MSP_GCR);
400 writel(reg_val_GCR | RX_ENABLE, msp->registers + MSP_GCR);
402 reg_val_FLR = readl(msp->registers + MSP_FLR);
403 while (!(reg_val_FLR & RX_FIFO_EMPTY) && limit--) {
404 readl(msp->registers + MSP_DR);
405 reg_val_FLR = readl(msp->registers + MSP_FLR);
408 writel(reg_val_GCR, msp->registers + MSP_GCR);
411 static void flush_fifo_tx(struct ux500_msp *msp)
413 u32 reg_val_GCR, reg_val_FLR;
416 reg_val_GCR = readl(msp->registers + MSP_GCR);
417 writel(reg_val_GCR | TX_ENABLE, msp->registers + MSP_GCR);
418 writel(MSP_ITCR_ITEN | MSP_ITCR_TESTFIFO, msp->registers + MSP_ITCR);
420 reg_val_FLR = readl(msp->registers + MSP_FLR);
421 while (!(reg_val_FLR & TX_FIFO_EMPTY) && limit--) {
422 readl(msp->registers + MSP_TSTDR);
423 reg_val_FLR = readl(msp->registers + MSP_FLR);
425 writel(0x0, msp->registers + MSP_ITCR);
426 writel(reg_val_GCR, msp->registers + MSP_GCR);
429 int ux500_msp_i2s_open(struct ux500_msp *msp,
430 struct ux500_msp_config *config)
432 u32 old_reg, new_reg, mask;
434 unsigned int tx_sel, rx_sel, tx_busy, rx_busy;
436 if (in_interrupt()) {
438 "%s: ERROR: Open called in interrupt context!\n",
443 tx_sel = (config->direction & MSP_DIR_TX) > 0;
444 rx_sel = (config->direction & MSP_DIR_RX) > 0;
445 if (!tx_sel && !rx_sel) {
446 dev_err(msp->dev, "%s: Error: No direction selected!\n",
451 tx_busy = (msp->dir_busy & MSP_DIR_TX) > 0;
452 rx_busy = (msp->dir_busy & MSP_DIR_RX) > 0;
453 if (tx_busy && tx_sel) {
454 dev_err(msp->dev, "%s: Error: TX is in use!\n", __func__);
457 if (rx_busy && rx_sel) {
458 dev_err(msp->dev, "%s: Error: RX is in use!\n", __func__);
462 msp->dir_busy |= (tx_sel ? MSP_DIR_TX : 0) | (rx_sel ? MSP_DIR_RX : 0);
464 /* First do the global config register */
465 mask = RX_CLK_SEL_MASK | TX_CLK_SEL_MASK | RX_FSYNC_MASK |
466 TX_FSYNC_MASK | RX_SYNC_SEL_MASK | TX_SYNC_SEL_MASK |
467 RX_FIFO_ENABLE_MASK | TX_FIFO_ENABLE_MASK | SRG_CLK_SEL_MASK |
468 LOOPBACK_MASK | TX_EXTRA_DELAY_MASK;
470 new_reg = (config->tx_clk_sel | config->rx_clk_sel |
471 config->rx_fsync_pol | config->tx_fsync_pol |
472 config->rx_fsync_sel | config->tx_fsync_sel |
473 config->rx_fifo_config | config->tx_fifo_config |
474 config->srg_clk_sel | config->loopback_enable |
475 config->tx_data_enable);
477 old_reg = readl(msp->registers + MSP_GCR);
480 writel(new_reg, msp->registers + MSP_GCR);
482 res = enable_msp(msp, config);
484 dev_err(msp->dev, "%s: ERROR: enable_msp failed (%d)!\n",
488 if (config->loopback_enable & 0x80)
489 msp->loopback_enable = 1;
495 msp->msp_state = MSP_STATE_CONFIGURED;
499 static void disable_msp_rx(struct ux500_msp *msp)
501 u32 reg_val_GCR, reg_val_DMACR, reg_val_IMSC;
503 reg_val_GCR = readl(msp->registers + MSP_GCR);
504 writel(reg_val_GCR & ~RX_ENABLE, msp->registers + MSP_GCR);
505 reg_val_DMACR = readl(msp->registers + MSP_DMACR);
506 writel(reg_val_DMACR & ~RX_DMA_ENABLE, msp->registers + MSP_DMACR);
507 reg_val_IMSC = readl(msp->registers + MSP_IMSC);
508 writel(reg_val_IMSC &
509 ~(RX_SERVICE_INT | RX_OVERRUN_ERROR_INT),
510 msp->registers + MSP_IMSC);
512 msp->dir_busy &= ~MSP_DIR_RX;
515 static void disable_msp_tx(struct ux500_msp *msp)
517 u32 reg_val_GCR, reg_val_DMACR, reg_val_IMSC;
519 reg_val_GCR = readl(msp->registers + MSP_GCR);
520 writel(reg_val_GCR & ~TX_ENABLE, msp->registers + MSP_GCR);
521 reg_val_DMACR = readl(msp->registers + MSP_DMACR);
522 writel(reg_val_DMACR & ~TX_DMA_ENABLE, msp->registers + MSP_DMACR);
523 reg_val_IMSC = readl(msp->registers + MSP_IMSC);
524 writel(reg_val_IMSC &
525 ~(TX_SERVICE_INT | TX_UNDERRUN_ERR_INT),
526 msp->registers + MSP_IMSC);
528 msp->dir_busy &= ~MSP_DIR_TX;
531 static int disable_msp(struct ux500_msp *msp, unsigned int dir)
534 unsigned int disable_tx, disable_rx;
536 reg_val_GCR = readl(msp->registers + MSP_GCR);
537 disable_tx = dir & MSP_DIR_TX;
538 disable_rx = dir & MSP_DIR_TX;
539 if (disable_tx && disable_rx) {
540 reg_val_GCR = readl(msp->registers + MSP_GCR);
541 writel(reg_val_GCR | LOOPBACK_MASK,
542 msp->registers + MSP_GCR);
547 /* Disable TX-channel */
548 writel((readl(msp->registers + MSP_GCR) &
549 (~TX_ENABLE)), msp->registers + MSP_GCR);
554 /* Disable Loopback and Receive channel */
555 writel((readl(msp->registers + MSP_GCR) &
556 (~(RX_ENABLE | LOOPBACK_MASK))),
557 msp->registers + MSP_GCR);
561 } else if (disable_tx)
569 int ux500_msp_i2s_trigger(struct ux500_msp *msp, int cmd, int direction)
571 u32 reg_val_GCR, enable_bit;
573 if (msp->msp_state == MSP_STATE_IDLE) {
574 dev_err(msp->dev, "%s: ERROR: MSP is not configured!\n",
580 case SNDRV_PCM_TRIGGER_START:
581 case SNDRV_PCM_TRIGGER_RESUME:
582 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
583 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
584 enable_bit = TX_ENABLE;
586 enable_bit = RX_ENABLE;
587 reg_val_GCR = readl(msp->registers + MSP_GCR);
588 writel(reg_val_GCR | enable_bit, msp->registers + MSP_GCR);
591 case SNDRV_PCM_TRIGGER_STOP:
592 case SNDRV_PCM_TRIGGER_SUSPEND:
593 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
594 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
606 int ux500_msp_i2s_close(struct ux500_msp *msp, unsigned int dir)
610 dev_dbg(msp->dev, "%s: Enter (dir = 0x%01x).\n", __func__, dir);
612 status = disable_msp(msp, dir);
613 if (msp->dir_busy == 0) {
614 /* disable sample rate and frame generators */
615 msp->msp_state = MSP_STATE_IDLE;
616 writel((readl(msp->registers + MSP_GCR) &
617 (~(FRAME_GEN_ENABLE | SRG_ENABLE))),
618 msp->registers + MSP_GCR);
620 writel(0, msp->registers + MSP_GCR);
621 writel(0, msp->registers + MSP_TCF);
622 writel(0, msp->registers + MSP_RCF);
623 writel(0, msp->registers + MSP_DMACR);
624 writel(0, msp->registers + MSP_SRG);
625 writel(0, msp->registers + MSP_MCR);
626 writel(0, msp->registers + MSP_RCM);
627 writel(0, msp->registers + MSP_RCV);
628 writel(0, msp->registers + MSP_TCE0);
629 writel(0, msp->registers + MSP_TCE1);
630 writel(0, msp->registers + MSP_TCE2);
631 writel(0, msp->registers + MSP_TCE3);
632 writel(0, msp->registers + MSP_RCE0);
633 writel(0, msp->registers + MSP_RCE1);
634 writel(0, msp->registers + MSP_RCE2);
635 writel(0, msp->registers + MSP_RCE3);
642 static int ux500_msp_i2s_of_init_msp(struct platform_device *pdev,
643 struct ux500_msp *msp,
644 struct msp_i2s_platform_data **platform_data)
646 struct msp_i2s_platform_data *pdata;
648 *platform_data = devm_kzalloc(&pdev->dev,
649 sizeof(struct msp_i2s_platform_data),
651 pdata = *platform_data;
655 msp->playback_dma_data.dma_cfg = devm_kzalloc(&pdev->dev,
656 sizeof(struct stedma40_chan_cfg),
658 if (!msp->playback_dma_data.dma_cfg)
661 msp->capture_dma_data.dma_cfg = devm_kzalloc(&pdev->dev,
662 sizeof(struct stedma40_chan_cfg),
664 if (!msp->capture_dma_data.dma_cfg)
670 int ux500_msp_i2s_init_msp(struct platform_device *pdev,
671 struct ux500_msp **msp_p,
672 struct msp_i2s_platform_data *platform_data)
674 struct resource *res = NULL;
675 struct device_node *np = pdev->dev.of_node;
676 struct ux500_msp *msp;
679 *msp_p = devm_kzalloc(&pdev->dev, sizeof(struct ux500_msp), GFP_KERNEL);
684 if (!platform_data) {
686 ret = ux500_msp_i2s_of_init_msp(pdev, msp,
693 msp->playback_dma_data.dma_cfg = platform_data->msp_i2s_dma_tx;
694 msp->capture_dma_data.dma_cfg = platform_data->msp_i2s_dma_rx;
695 msp->id = platform_data->id;
698 msp->dev = &pdev->dev;
700 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
702 dev_err(&pdev->dev, "%s: ERROR: Unable to get resource!\n",
707 msp->playback_dma_data.tx_rx_addr = res->start + MSP_DR;
708 msp->capture_dma_data.tx_rx_addr = res->start + MSP_DR;
710 msp->registers = devm_ioremap(&pdev->dev, res->start,
712 if (msp->registers == NULL) {
713 dev_err(&pdev->dev, "%s: ERROR: ioremap failed!\n", __func__);
717 msp->msp_state = MSP_STATE_IDLE;
718 msp->loopback_enable = 0;
723 void ux500_msp_i2s_cleanup_msp(struct platform_device *pdev,
724 struct ux500_msp *msp)
726 dev_dbg(msp->dev, "%s: Enter (id = %d).\n", __func__, msp->id);
729 MODULE_LICENSE("GPL v2");