xref: /openbmc/linux/sound/soc/ux500/ux500_msp_dai.c (revision 8fcf1e5eb460d8f032c699280f5a33fbf3716c29)
13592b7f6SOla Lilja /*
23592b7f6SOla Lilja  * Copyright (C) ST-Ericsson SA 2012
33592b7f6SOla Lilja  *
43592b7f6SOla Lilja  * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
53592b7f6SOla Lilja  *         Roger Nilsson <roger.xr.nilsson@stericsson.com>
63592b7f6SOla Lilja  *         for ST-Ericsson.
73592b7f6SOla Lilja  *
83592b7f6SOla Lilja  * License terms:
93592b7f6SOla Lilja  *
103592b7f6SOla Lilja  * This program is free software; you can redistribute it and/or modify
113592b7f6SOla Lilja  * it under the terms of the GNU General Public License version 2 as published
123592b7f6SOla Lilja  * by the Free Software Foundation.
133592b7f6SOla Lilja  */
143592b7f6SOla Lilja 
153592b7f6SOla Lilja #include <linux/module.h>
163592b7f6SOla Lilja #include <linux/slab.h>
173592b7f6SOla Lilja #include <linux/bitops.h>
183592b7f6SOla Lilja #include <linux/platform_device.h>
193592b7f6SOla Lilja #include <linux/clk.h>
20f382acbeSLee Jones #include <linux/of.h>
213592b7f6SOla Lilja #include <linux/regulator/consumer.h>
223592b7f6SOla Lilja #include <linux/mfd/dbx500-prcmu.h>
23ab0fc6ceSArnd Bergmann #include <linux/platform_data/asoc-ux500-msp.h>
243592b7f6SOla Lilja 
253592b7f6SOla Lilja #include <sound/soc.h>
263592b7f6SOla Lilja #include <sound/soc-dai.h>
27f382acbeSLee Jones #include <sound/dmaengine_pcm.h>
283592b7f6SOla Lilja 
293592b7f6SOla Lilja #include "ux500_msp_i2s.h"
303592b7f6SOla Lilja #include "ux500_msp_dai.h"
311428c20fSLee Jones #include "ux500_pcm.h"
323592b7f6SOla Lilja 
333592b7f6SOla Lilja static int setup_pcm_multichan(struct snd_soc_dai *dai,
343592b7f6SOla Lilja 			struct ux500_msp_config *msp_config)
353592b7f6SOla Lilja {
363592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
373592b7f6SOla Lilja 	struct msp_multichannel_config *multi =
383592b7f6SOla Lilja 					&msp_config->multichannel_config;
393592b7f6SOla Lilja 
403592b7f6SOla Lilja 	if (drvdata->slots > 1) {
413592b7f6SOla Lilja 		msp_config->multichannel_configured = 1;
423592b7f6SOla Lilja 
433592b7f6SOla Lilja 		multi->tx_multichannel_enable = true;
443592b7f6SOla Lilja 		multi->rx_multichannel_enable = true;
453592b7f6SOla Lilja 		multi->rx_comparison_enable_mode = MSP_COMPARISON_DISABLED;
463592b7f6SOla Lilja 
473592b7f6SOla Lilja 		multi->tx_channel_0_enable = drvdata->tx_mask;
483592b7f6SOla Lilja 		multi->tx_channel_1_enable = 0;
493592b7f6SOla Lilja 		multi->tx_channel_2_enable = 0;
503592b7f6SOla Lilja 		multi->tx_channel_3_enable = 0;
513592b7f6SOla Lilja 
523592b7f6SOla Lilja 		multi->rx_channel_0_enable = drvdata->rx_mask;
533592b7f6SOla Lilja 		multi->rx_channel_1_enable = 0;
543592b7f6SOla Lilja 		multi->rx_channel_2_enable = 0;
553592b7f6SOla Lilja 		multi->rx_channel_3_enable = 0;
563592b7f6SOla Lilja 
573592b7f6SOla Lilja 		dev_dbg(dai->dev,
583592b7f6SOla Lilja 			"%s: Multichannel enabled. Slots: %d, TX: %u, RX: %u\n",
593592b7f6SOla Lilja 			__func__, drvdata->slots, multi->tx_channel_0_enable,
603592b7f6SOla Lilja 			multi->rx_channel_0_enable);
613592b7f6SOla Lilja 	}
623592b7f6SOla Lilja 
633592b7f6SOla Lilja 	return 0;
643592b7f6SOla Lilja }
653592b7f6SOla Lilja 
663592b7f6SOla Lilja static int setup_frameper(struct snd_soc_dai *dai, unsigned int rate,
673592b7f6SOla Lilja 			struct msp_protdesc *prot_desc)
683592b7f6SOla Lilja {
693592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
703592b7f6SOla Lilja 
713592b7f6SOla Lilja 	switch (drvdata->slots) {
723592b7f6SOla Lilja 	case 1:
733592b7f6SOla Lilja 		switch (rate) {
743592b7f6SOla Lilja 		case 8000:
753592b7f6SOla Lilja 			prot_desc->frame_period =
763592b7f6SOla Lilja 				FRAME_PER_SINGLE_SLOT_8_KHZ;
773592b7f6SOla Lilja 			break;
783592b7f6SOla Lilja 
793592b7f6SOla Lilja 		case 16000:
803592b7f6SOla Lilja 			prot_desc->frame_period =
813592b7f6SOla Lilja 				FRAME_PER_SINGLE_SLOT_16_KHZ;
823592b7f6SOla Lilja 			break;
833592b7f6SOla Lilja 
843592b7f6SOla Lilja 		case 44100:
853592b7f6SOla Lilja 			prot_desc->frame_period =
863592b7f6SOla Lilja 				FRAME_PER_SINGLE_SLOT_44_1_KHZ;
873592b7f6SOla Lilja 			break;
883592b7f6SOla Lilja 
893592b7f6SOla Lilja 		case 48000:
903592b7f6SOla Lilja 			prot_desc->frame_period =
913592b7f6SOla Lilja 				FRAME_PER_SINGLE_SLOT_48_KHZ;
923592b7f6SOla Lilja 			break;
933592b7f6SOla Lilja 
943592b7f6SOla Lilja 		default:
953592b7f6SOla Lilja 			dev_err(dai->dev,
963592b7f6SOla Lilja 				"%s: Error: Unsupported sample-rate (freq = %d)!\n",
973592b7f6SOla Lilja 				__func__, rate);
983592b7f6SOla Lilja 			return -EINVAL;
993592b7f6SOla Lilja 		}
1003592b7f6SOla Lilja 		break;
1013592b7f6SOla Lilja 
1023592b7f6SOla Lilja 	case 2:
1033592b7f6SOla Lilja 		prot_desc->frame_period = FRAME_PER_2_SLOTS;
1043592b7f6SOla Lilja 		break;
1053592b7f6SOla Lilja 
1063592b7f6SOla Lilja 	case 8:
1073592b7f6SOla Lilja 		prot_desc->frame_period = FRAME_PER_8_SLOTS;
1083592b7f6SOla Lilja 		break;
1093592b7f6SOla Lilja 
1103592b7f6SOla Lilja 	case 16:
1113592b7f6SOla Lilja 		prot_desc->frame_period = FRAME_PER_16_SLOTS;
1123592b7f6SOla Lilja 		break;
1133592b7f6SOla Lilja 	default:
1143592b7f6SOla Lilja 		dev_err(dai->dev,
1153592b7f6SOla Lilja 			"%s: Error: Unsupported slot-count (slots = %d)!\n",
1163592b7f6SOla Lilja 			__func__, drvdata->slots);
1173592b7f6SOla Lilja 		return -EINVAL;
1183592b7f6SOla Lilja 	}
1193592b7f6SOla Lilja 
1203592b7f6SOla Lilja 	prot_desc->clocks_per_frame =
1213592b7f6SOla Lilja 			prot_desc->frame_period+1;
1223592b7f6SOla Lilja 
1233592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: Clocks per frame: %u\n",
1243592b7f6SOla Lilja 		__func__,
1253592b7f6SOla Lilja 		prot_desc->clocks_per_frame);
1263592b7f6SOla Lilja 
1273592b7f6SOla Lilja 	return 0;
1283592b7f6SOla Lilja }
1293592b7f6SOla Lilja 
1303592b7f6SOla Lilja static int setup_pcm_framing(struct snd_soc_dai *dai, unsigned int rate,
1313592b7f6SOla Lilja 			struct msp_protdesc *prot_desc)
1323592b7f6SOla Lilja {
1333592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
1343592b7f6SOla Lilja 
1353592b7f6SOla Lilja 	u32 frame_length = MSP_FRAME_LEN_1;
1363592b7f6SOla Lilja 	prot_desc->frame_width = 0;
1373592b7f6SOla Lilja 
1383592b7f6SOla Lilja 	switch (drvdata->slots) {
1393592b7f6SOla Lilja 	case 1:
1403592b7f6SOla Lilja 		frame_length = MSP_FRAME_LEN_1;
1413592b7f6SOla Lilja 		break;
1423592b7f6SOla Lilja 
1433592b7f6SOla Lilja 	case 2:
1443592b7f6SOla Lilja 		frame_length = MSP_FRAME_LEN_2;
1453592b7f6SOla Lilja 		break;
1463592b7f6SOla Lilja 
1473592b7f6SOla Lilja 	case 8:
1483592b7f6SOla Lilja 		frame_length = MSP_FRAME_LEN_8;
1493592b7f6SOla Lilja 		break;
1503592b7f6SOla Lilja 
1513592b7f6SOla Lilja 	case 16:
1523592b7f6SOla Lilja 		frame_length = MSP_FRAME_LEN_16;
1533592b7f6SOla Lilja 		break;
1543592b7f6SOla Lilja 	default:
1553592b7f6SOla Lilja 		dev_err(dai->dev,
1563592b7f6SOla Lilja 			"%s: Error: Unsupported slot-count (slots = %d)!\n",
1573592b7f6SOla Lilja 			__func__, drvdata->slots);
1583592b7f6SOla Lilja 		return -EINVAL;
1593592b7f6SOla Lilja 	}
1603592b7f6SOla Lilja 
1613592b7f6SOla Lilja 	prot_desc->tx_frame_len_1 = frame_length;
1623592b7f6SOla Lilja 	prot_desc->rx_frame_len_1 = frame_length;
1633592b7f6SOla Lilja 	prot_desc->tx_frame_len_2 = frame_length;
1643592b7f6SOla Lilja 	prot_desc->rx_frame_len_2 = frame_length;
1653592b7f6SOla Lilja 
1663592b7f6SOla Lilja 	prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
1673592b7f6SOla Lilja 	prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
1683592b7f6SOla Lilja 	prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
1693592b7f6SOla Lilja 	prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
1703592b7f6SOla Lilja 
1713592b7f6SOla Lilja 	return setup_frameper(dai, rate, prot_desc);
1723592b7f6SOla Lilja }
1733592b7f6SOla Lilja 
1743592b7f6SOla Lilja static int setup_clocking(struct snd_soc_dai *dai,
1753592b7f6SOla Lilja 			unsigned int fmt,
1763592b7f6SOla Lilja 			struct ux500_msp_config *msp_config)
1773592b7f6SOla Lilja {
1783592b7f6SOla Lilja 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1793592b7f6SOla Lilja 	case SND_SOC_DAIFMT_NB_NF:
1803592b7f6SOla Lilja 		break;
1813592b7f6SOla Lilja 
1823592b7f6SOla Lilja 	case SND_SOC_DAIFMT_NB_IF:
1833592b7f6SOla Lilja 		msp_config->tx_fsync_pol ^= 1 << TFSPOL_SHIFT;
1843592b7f6SOla Lilja 		msp_config->rx_fsync_pol ^= 1 << RFSPOL_SHIFT;
1853592b7f6SOla Lilja 
1863592b7f6SOla Lilja 		break;
1873592b7f6SOla Lilja 
1883592b7f6SOla Lilja 	default:
1893592b7f6SOla Lilja 		dev_err(dai->dev,
190c3d7abcaSColin Ian King 			"%s: Error: Unsupported inversion (fmt = 0x%x)!\n",
1913592b7f6SOla Lilja 			__func__, fmt);
1923592b7f6SOla Lilja 
1933592b7f6SOla Lilja 		return -EINVAL;
1943592b7f6SOla Lilja 	}
1953592b7f6SOla Lilja 
1963592b7f6SOla Lilja 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1973592b7f6SOla Lilja 	case SND_SOC_DAIFMT_CBM_CFM:
1983592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: Codec is master.\n", __func__);
1993592b7f6SOla Lilja 
2003592b7f6SOla Lilja 		msp_config->iodelay = 0x20;
2013592b7f6SOla Lilja 		msp_config->rx_fsync_sel = 0;
2023592b7f6SOla Lilja 		msp_config->tx_fsync_sel = 1 << TFSSEL_SHIFT;
2033592b7f6SOla Lilja 		msp_config->tx_clk_sel = 0;
2043592b7f6SOla Lilja 		msp_config->rx_clk_sel = 0;
2053592b7f6SOla Lilja 		msp_config->srg_clk_sel = 0x2 << SCKSEL_SHIFT;
2063592b7f6SOla Lilja 
2073592b7f6SOla Lilja 		break;
2083592b7f6SOla Lilja 
2093592b7f6SOla Lilja 	case SND_SOC_DAIFMT_CBS_CFS:
2103592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: Codec is slave.\n", __func__);
2113592b7f6SOla Lilja 
2123592b7f6SOla Lilja 		msp_config->tx_clk_sel = TX_CLK_SEL_SRG;
2133592b7f6SOla Lilja 		msp_config->tx_fsync_sel = TX_SYNC_SRG_PROG;
2143592b7f6SOla Lilja 		msp_config->rx_clk_sel = RX_CLK_SEL_SRG;
2153592b7f6SOla Lilja 		msp_config->rx_fsync_sel = RX_SYNC_SRG;
2163592b7f6SOla Lilja 		msp_config->srg_clk_sel = 1 << SCKSEL_SHIFT;
2173592b7f6SOla Lilja 
2183592b7f6SOla Lilja 		break;
2193592b7f6SOla Lilja 
2203592b7f6SOla Lilja 	default:
221c3d7abcaSColin Ian King 		dev_err(dai->dev, "%s: Error: Unsupported master (fmt = 0x%x)!\n",
2223592b7f6SOla Lilja 			__func__, fmt);
2233592b7f6SOla Lilja 
2243592b7f6SOla Lilja 		return -EINVAL;
2253592b7f6SOla Lilja 	}
2263592b7f6SOla Lilja 
2273592b7f6SOla Lilja 	return 0;
2283592b7f6SOla Lilja }
2293592b7f6SOla Lilja 
2303592b7f6SOla Lilja static int setup_pcm_protdesc(struct snd_soc_dai *dai,
2313592b7f6SOla Lilja 				unsigned int fmt,
2323592b7f6SOla Lilja 				struct msp_protdesc *prot_desc)
2333592b7f6SOla Lilja {
2343592b7f6SOla Lilja 	prot_desc->rx_phase_mode = MSP_SINGLE_PHASE;
2353592b7f6SOla Lilja 	prot_desc->tx_phase_mode = MSP_SINGLE_PHASE;
2363592b7f6SOla Lilja 	prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
2373592b7f6SOla Lilja 	prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_IMEDIATE;
2383592b7f6SOla Lilja 	prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
2393592b7f6SOla Lilja 	prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
2403592b7f6SOla Lilja 	prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_HI);
2413592b7f6SOla Lilja 	prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_HI << RFSPOL_SHIFT;
2423592b7f6SOla Lilja 
2433592b7f6SOla Lilja 	if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A) {
2443592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: DSP_A.\n", __func__);
2453592b7f6SOla Lilja 		prot_desc->rx_clk_pol = MSP_RISING_EDGE;
2463592b7f6SOla Lilja 		prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
2473592b7f6SOla Lilja 
2483592b7f6SOla Lilja 		prot_desc->rx_data_delay = MSP_DELAY_1;
2493592b7f6SOla Lilja 		prot_desc->tx_data_delay = MSP_DELAY_1;
2503592b7f6SOla Lilja 	} else {
2513592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: DSP_B.\n", __func__);
2523592b7f6SOla Lilja 		prot_desc->rx_clk_pol = MSP_FALLING_EDGE;
2533592b7f6SOla Lilja 		prot_desc->tx_clk_pol = MSP_RISING_EDGE;
2543592b7f6SOla Lilja 
2553592b7f6SOla Lilja 		prot_desc->rx_data_delay = MSP_DELAY_0;
2563592b7f6SOla Lilja 		prot_desc->tx_data_delay = MSP_DELAY_0;
2573592b7f6SOla Lilja 	}
2583592b7f6SOla Lilja 
2593592b7f6SOla Lilja 	prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
2603592b7f6SOla Lilja 	prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
2613592b7f6SOla Lilja 	prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
2623592b7f6SOla Lilja 	prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
2633592b7f6SOla Lilja 	prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
2643592b7f6SOla Lilja 
2653592b7f6SOla Lilja 	return 0;
2663592b7f6SOla Lilja }
2673592b7f6SOla Lilja 
2683592b7f6SOla Lilja static int setup_i2s_protdesc(struct msp_protdesc *prot_desc)
2693592b7f6SOla Lilja {
2703592b7f6SOla Lilja 	prot_desc->rx_phase_mode = MSP_DUAL_PHASE;
2713592b7f6SOla Lilja 	prot_desc->tx_phase_mode = MSP_DUAL_PHASE;
2723592b7f6SOla Lilja 	prot_desc->rx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
2733592b7f6SOla Lilja 	prot_desc->tx_phase2_start_mode = MSP_PHASE2_START_MODE_FSYNC;
2743592b7f6SOla Lilja 	prot_desc->rx_byte_order = MSP_BTF_MS_BIT_FIRST;
2753592b7f6SOla Lilja 	prot_desc->tx_byte_order = MSP_BTF_MS_BIT_FIRST;
2763592b7f6SOla Lilja 	prot_desc->tx_fsync_pol = MSP_FSYNC_POL(MSP_FSYNC_POL_ACT_LO);
2773592b7f6SOla Lilja 	prot_desc->rx_fsync_pol = MSP_FSYNC_POL_ACT_LO << RFSPOL_SHIFT;
2783592b7f6SOla Lilja 
2793592b7f6SOla Lilja 	prot_desc->rx_frame_len_1 = MSP_FRAME_LEN_1;
2803592b7f6SOla Lilja 	prot_desc->rx_frame_len_2 = MSP_FRAME_LEN_1;
2813592b7f6SOla Lilja 	prot_desc->tx_frame_len_1 = MSP_FRAME_LEN_1;
2823592b7f6SOla Lilja 	prot_desc->tx_frame_len_2 = MSP_FRAME_LEN_1;
2833592b7f6SOla Lilja 	prot_desc->rx_elem_len_1 = MSP_ELEM_LEN_16;
2843592b7f6SOla Lilja 	prot_desc->rx_elem_len_2 = MSP_ELEM_LEN_16;
2853592b7f6SOla Lilja 	prot_desc->tx_elem_len_1 = MSP_ELEM_LEN_16;
2863592b7f6SOla Lilja 	prot_desc->tx_elem_len_2 = MSP_ELEM_LEN_16;
2873592b7f6SOla Lilja 
2883592b7f6SOla Lilja 	prot_desc->rx_clk_pol = MSP_RISING_EDGE;
2893592b7f6SOla Lilja 	prot_desc->tx_clk_pol = MSP_FALLING_EDGE;
2903592b7f6SOla Lilja 
2913592b7f6SOla Lilja 	prot_desc->rx_data_delay = MSP_DELAY_0;
2923592b7f6SOla Lilja 	prot_desc->tx_data_delay = MSP_DELAY_0;
2933592b7f6SOla Lilja 
2943592b7f6SOla Lilja 	prot_desc->tx_half_word_swap = MSP_SWAP_NONE;
2953592b7f6SOla Lilja 	prot_desc->rx_half_word_swap = MSP_SWAP_NONE;
2963592b7f6SOla Lilja 	prot_desc->compression_mode = MSP_COMPRESS_MODE_LINEAR;
2973592b7f6SOla Lilja 	prot_desc->expansion_mode = MSP_EXPAND_MODE_LINEAR;
2983592b7f6SOla Lilja 	prot_desc->frame_sync_ignore = MSP_FSYNC_IGNORE;
2993592b7f6SOla Lilja 
3003592b7f6SOla Lilja 	return 0;
3013592b7f6SOla Lilja }
3023592b7f6SOla Lilja 
3033592b7f6SOla Lilja static int setup_msp_config(struct snd_pcm_substream *substream,
3043592b7f6SOla Lilja 			struct snd_soc_dai *dai,
3053592b7f6SOla Lilja 			struct ux500_msp_config *msp_config)
3063592b7f6SOla Lilja {
3073592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
3083592b7f6SOla Lilja 	struct msp_protdesc *prot_desc = &msp_config->protdesc;
3093592b7f6SOla Lilja 	struct snd_pcm_runtime *runtime = substream->runtime;
3103592b7f6SOla Lilja 	unsigned int fmt = drvdata->fmt;
3113592b7f6SOla Lilja 	int ret;
3123592b7f6SOla Lilja 
3133592b7f6SOla Lilja 	memset(msp_config, 0, sizeof(*msp_config));
3143592b7f6SOla Lilja 
3153592b7f6SOla Lilja 	msp_config->f_inputclk = drvdata->master_clk;
3163592b7f6SOla Lilja 
3173592b7f6SOla Lilja 	msp_config->tx_fifo_config = TX_FIFO_ENABLE;
3183592b7f6SOla Lilja 	msp_config->rx_fifo_config = RX_FIFO_ENABLE;
3193592b7f6SOla Lilja 	msp_config->def_elem_len = 1;
3203592b7f6SOla Lilja 	msp_config->direction = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
3213592b7f6SOla Lilja 				MSP_DIR_TX : MSP_DIR_RX;
3223592b7f6SOla Lilja 	msp_config->data_size = MSP_DATA_BITS_32;
3233592b7f6SOla Lilja 	msp_config->frame_freq = runtime->rate;
3243592b7f6SOla Lilja 
3253592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: f_inputclk = %u, frame_freq = %u.\n",
3263592b7f6SOla Lilja 	       __func__, msp_config->f_inputclk, msp_config->frame_freq);
3273592b7f6SOla Lilja 	/* To avoid division by zero */
3283592b7f6SOla Lilja 	prot_desc->clocks_per_frame = 1;
3293592b7f6SOla Lilja 
3303592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: rate: %u, channels: %d.\n", __func__,
3313592b7f6SOla Lilja 		runtime->rate, runtime->channels);
3323592b7f6SOla Lilja 	switch (fmt &
3333592b7f6SOla Lilja 		(SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
3343592b7f6SOla Lilja 	case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
3353592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
3363592b7f6SOla Lilja 
3373592b7f6SOla Lilja 		msp_config->default_protdesc = 1;
3383592b7f6SOla Lilja 		msp_config->protocol = MSP_I2S_PROTOCOL;
3393592b7f6SOla Lilja 		break;
3403592b7f6SOla Lilja 
3413592b7f6SOla Lilja 	case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
3423592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: SND_SOC_DAIFMT_I2S.\n", __func__);
3433592b7f6SOla Lilja 
3443592b7f6SOla Lilja 		msp_config->data_size = MSP_DATA_BITS_16;
3453592b7f6SOla Lilja 		msp_config->protocol = MSP_I2S_PROTOCOL;
3463592b7f6SOla Lilja 
3473592b7f6SOla Lilja 		ret = setup_i2s_protdesc(prot_desc);
3483592b7f6SOla Lilja 		if (ret < 0)
3493592b7f6SOla Lilja 			return ret;
3503592b7f6SOla Lilja 
3513592b7f6SOla Lilja 		break;
3523592b7f6SOla Lilja 
3533592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
3543592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
3553592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
3563592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
3573592b7f6SOla Lilja 		dev_dbg(dai->dev, "%s: PCM format.\n", __func__);
3583592b7f6SOla Lilja 
3593592b7f6SOla Lilja 		msp_config->data_size = MSP_DATA_BITS_16;
3603592b7f6SOla Lilja 		msp_config->protocol = MSP_PCM_PROTOCOL;
3613592b7f6SOla Lilja 
3623592b7f6SOla Lilja 		ret = setup_pcm_protdesc(dai, fmt, prot_desc);
3633592b7f6SOla Lilja 		if (ret < 0)
3643592b7f6SOla Lilja 			return ret;
3653592b7f6SOla Lilja 
3663592b7f6SOla Lilja 		ret = setup_pcm_multichan(dai, msp_config);
3673592b7f6SOla Lilja 		if (ret < 0)
3683592b7f6SOla Lilja 			return ret;
3693592b7f6SOla Lilja 
3703592b7f6SOla Lilja 		ret = setup_pcm_framing(dai, runtime->rate, prot_desc);
3713592b7f6SOla Lilja 		if (ret < 0)
3723592b7f6SOla Lilja 			return ret;
3733592b7f6SOla Lilja 
3743592b7f6SOla Lilja 		break;
3753592b7f6SOla Lilja 
3763592b7f6SOla Lilja 	default:
377c3d7abcaSColin Ian King 		dev_err(dai->dev, "%s: Error: Unsupported format (%d)!\n",
3783592b7f6SOla Lilja 			__func__, fmt);
3793592b7f6SOla Lilja 		return -EINVAL;
3803592b7f6SOla Lilja 	}
3813592b7f6SOla Lilja 
3823592b7f6SOla Lilja 	return setup_clocking(dai, fmt, msp_config);
3833592b7f6SOla Lilja }
3843592b7f6SOla Lilja 
3853592b7f6SOla Lilja static int ux500_msp_dai_startup(struct snd_pcm_substream *substream,
3863592b7f6SOla Lilja 				struct snd_soc_dai *dai)
3873592b7f6SOla Lilja {
3883592b7f6SOla Lilja 	int ret = 0;
3893592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
3903592b7f6SOla Lilja 
3913592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
3923592b7f6SOla Lilja 		snd_pcm_stream_str(substream));
3933592b7f6SOla Lilja 
3943592b7f6SOla Lilja 	/* Enable regulator */
3953592b7f6SOla Lilja 	ret = regulator_enable(drvdata->reg_vape);
3963592b7f6SOla Lilja 	if (ret != 0) {
3973592b7f6SOla Lilja 		dev_err(drvdata->msp->dev,
3983592b7f6SOla Lilja 			"%s: Failed to enable regulator!\n", __func__);
3993592b7f6SOla Lilja 		return ret;
4003592b7f6SOla Lilja 	}
4013592b7f6SOla Lilja 
402f61ab093SUlf Hansson 	/* Prepare and enable clocks */
403f61ab093SUlf Hansson 	dev_dbg(dai->dev, "%s: Enabling MSP-clocks.\n", __func__);
404f61ab093SUlf Hansson 	ret = clk_prepare_enable(drvdata->pclk);
405f61ab093SUlf Hansson 	if (ret) {
406f61ab093SUlf Hansson 		dev_err(drvdata->msp->dev,
407f61ab093SUlf Hansson 			"%s: Failed to prepare/enable pclk!\n", __func__);
408f61ab093SUlf Hansson 		goto err_pclk;
409f61ab093SUlf Hansson 	}
4103592b7f6SOla Lilja 
411f61ab093SUlf Hansson 	ret = clk_prepare_enable(drvdata->clk);
412f61ab093SUlf Hansson 	if (ret) {
413f61ab093SUlf Hansson 		dev_err(drvdata->msp->dev,
414f61ab093SUlf Hansson 			"%s: Failed to prepare/enable clk!\n", __func__);
415f61ab093SUlf Hansson 		goto err_clk;
416f61ab093SUlf Hansson 	}
417f61ab093SUlf Hansson 
418f61ab093SUlf Hansson 	return ret;
419f61ab093SUlf Hansson err_clk:
420f61ab093SUlf Hansson 	clk_disable_unprepare(drvdata->pclk);
421f61ab093SUlf Hansson err_pclk:
422f61ab093SUlf Hansson 	regulator_disable(drvdata->reg_vape);
423fe36a0b2SUlf Hansson 	return ret;
4243592b7f6SOla Lilja }
4253592b7f6SOla Lilja 
4263592b7f6SOla Lilja static void ux500_msp_dai_shutdown(struct snd_pcm_substream *substream,
4273592b7f6SOla Lilja 				struct snd_soc_dai *dai)
4283592b7f6SOla Lilja {
4293592b7f6SOla Lilja 	int ret;
4303592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
4313592b7f6SOla Lilja 	bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
4323592b7f6SOla Lilja 
4333592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
4343592b7f6SOla Lilja 		snd_pcm_stream_str(substream));
4353592b7f6SOla Lilja 
4363592b7f6SOla Lilja 	if (drvdata->vape_opp_constraint == 1) {
4373592b7f6SOla Lilja 		prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
4383592b7f6SOla Lilja 					"ux500_msp_i2s", 50);
4393592b7f6SOla Lilja 		drvdata->vape_opp_constraint = 0;
4403592b7f6SOla Lilja 	}
4413592b7f6SOla Lilja 
4423592b7f6SOla Lilja 	if (ux500_msp_i2s_close(drvdata->msp,
4433592b7f6SOla Lilja 				is_playback ? MSP_DIR_TX : MSP_DIR_RX)) {
4443592b7f6SOla Lilja 		dev_err(dai->dev,
4453592b7f6SOla Lilja 			"%s: Error: MSP %d (%s): Unable to close i2s.\n",
4463592b7f6SOla Lilja 			__func__, dai->id, snd_pcm_stream_str(substream));
4473592b7f6SOla Lilja 	}
4483592b7f6SOla Lilja 
449f61ab093SUlf Hansson 	/* Disable and unprepare clocks */
450fe36a0b2SUlf Hansson 	clk_disable_unprepare(drvdata->clk);
451f61ab093SUlf Hansson 	clk_disable_unprepare(drvdata->pclk);
4523592b7f6SOla Lilja 
4533592b7f6SOla Lilja 	/* Disable regulator */
4543592b7f6SOla Lilja 	ret = regulator_disable(drvdata->reg_vape);
4553592b7f6SOla Lilja 	if (ret < 0)
4563592b7f6SOla Lilja 		dev_err(dai->dev,
4573592b7f6SOla Lilja 			"%s: ERROR: Failed to disable regulator (%d)!\n",
4583592b7f6SOla Lilja 			__func__, ret);
4593592b7f6SOla Lilja }
4603592b7f6SOla Lilja 
4613592b7f6SOla Lilja static int ux500_msp_dai_prepare(struct snd_pcm_substream *substream,
4623592b7f6SOla Lilja 				struct snd_soc_dai *dai)
4633592b7f6SOla Lilja {
4643592b7f6SOla Lilja 	int ret = 0;
4653592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
4663592b7f6SOla Lilja 	struct snd_pcm_runtime *runtime = substream->runtime;
4673592b7f6SOla Lilja 	struct ux500_msp_config msp_config;
4683592b7f6SOla Lilja 
4693592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (rate = %d).\n", __func__,
4703592b7f6SOla Lilja 		dai->id, snd_pcm_stream_str(substream), runtime->rate);
4713592b7f6SOla Lilja 
4723592b7f6SOla Lilja 	setup_msp_config(substream, dai, &msp_config);
4733592b7f6SOla Lilja 
4743592b7f6SOla Lilja 	ret = ux500_msp_i2s_open(drvdata->msp, &msp_config);
4753592b7f6SOla Lilja 	if (ret < 0) {
4763592b7f6SOla Lilja 		dev_err(dai->dev, "%s: Error: msp_setup failed (ret = %d)!\n",
4773592b7f6SOla Lilja 			__func__, ret);
4783592b7f6SOla Lilja 		return ret;
4793592b7f6SOla Lilja 	}
4803592b7f6SOla Lilja 
4813592b7f6SOla Lilja 	/* Set OPP-level */
4823592b7f6SOla Lilja 	if ((drvdata->fmt & SND_SOC_DAIFMT_MASTER_MASK) &&
4833592b7f6SOla Lilja 		(drvdata->msp->f_bitclk > 19200000)) {
4843592b7f6SOla Lilja 		/* If the bit-clock is higher than 19.2MHz, Vape should be
485*8fcf1e5eSCodrut Grosu 		 * run in 100% OPP. Only when bit-clock is used (MSP master)
486*8fcf1e5eSCodrut Grosu 		 */
4873592b7f6SOla Lilja 		prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
4883592b7f6SOla Lilja 					"ux500-msp-i2s", 100);
4893592b7f6SOla Lilja 		drvdata->vape_opp_constraint = 1;
4903592b7f6SOla Lilja 	} else {
4913592b7f6SOla Lilja 		prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
4923592b7f6SOla Lilja 					"ux500-msp-i2s", 50);
4933592b7f6SOla Lilja 		drvdata->vape_opp_constraint = 0;
4943592b7f6SOla Lilja 	}
4953592b7f6SOla Lilja 
4963592b7f6SOla Lilja 	return ret;
4973592b7f6SOla Lilja }
4983592b7f6SOla Lilja 
4993592b7f6SOla Lilja static int ux500_msp_dai_hw_params(struct snd_pcm_substream *substream,
5003592b7f6SOla Lilja 				struct snd_pcm_hw_params *params,
5013592b7f6SOla Lilja 				struct snd_soc_dai *dai)
5023592b7f6SOla Lilja {
5033592b7f6SOla Lilja 	unsigned int mask, slots_active;
5043592b7f6SOla Lilja 	struct snd_pcm_runtime *runtime = substream->runtime;
5053592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
5063592b7f6SOla Lilja 
5073592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d (%s): Enter.\n",
5083592b7f6SOla Lilja 			__func__, dai->id, snd_pcm_stream_str(substream));
5093592b7f6SOla Lilja 
5103592b7f6SOla Lilja 	switch (drvdata->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
5113592b7f6SOla Lilja 	case SND_SOC_DAIFMT_I2S:
5123592b7f6SOla Lilja 		snd_pcm_hw_constraint_minmax(runtime,
5133592b7f6SOla Lilja 				SNDRV_PCM_HW_PARAM_CHANNELS,
5143592b7f6SOla Lilja 				1, 2);
5153592b7f6SOla Lilja 		break;
5163592b7f6SOla Lilja 
5173592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_B:
5183592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_A:
5193592b7f6SOla Lilja 		mask = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5203592b7f6SOla Lilja 			drvdata->tx_mask :
5213592b7f6SOla Lilja 			drvdata->rx_mask;
5223592b7f6SOla Lilja 
5233592b7f6SOla Lilja 		slots_active = hweight32(mask);
5243592b7f6SOla Lilja 		dev_dbg(dai->dev, "TDM-slots active: %d", slots_active);
5253592b7f6SOla Lilja 
5261bf2d35bSLars-Peter Clausen 		snd_pcm_hw_constraint_single(runtime,
5273592b7f6SOla Lilja 				SNDRV_PCM_HW_PARAM_CHANNELS,
5281bf2d35bSLars-Peter Clausen 				slots_active);
5293592b7f6SOla Lilja 		break;
5303592b7f6SOla Lilja 
5313592b7f6SOla Lilja 	default:
5323592b7f6SOla Lilja 		dev_err(dai->dev,
5333592b7f6SOla Lilja 			"%s: Error: Unsupported protocol (fmt = 0x%x)!\n",
5343592b7f6SOla Lilja 			__func__, drvdata->fmt);
5353592b7f6SOla Lilja 		return -EINVAL;
5363592b7f6SOla Lilja 	}
5373592b7f6SOla Lilja 
5383592b7f6SOla Lilja 	return 0;
5393592b7f6SOla Lilja }
5403592b7f6SOla Lilja 
5413592b7f6SOla Lilja static int ux500_msp_dai_set_dai_fmt(struct snd_soc_dai *dai,
5423592b7f6SOla Lilja 				unsigned int fmt)
5433592b7f6SOla Lilja {
5443592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
5453592b7f6SOla Lilja 
5463592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d: Enter.\n", __func__, dai->id);
5473592b7f6SOla Lilja 
5483592b7f6SOla Lilja 	switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
5493592b7f6SOla Lilja 		SND_SOC_DAIFMT_MASTER_MASK)) {
5503592b7f6SOla Lilja 	case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
5513592b7f6SOla Lilja 	case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
5523592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBS_CFS:
5533592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_CBM_CFM:
5543592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
5553592b7f6SOla Lilja 	case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
5563592b7f6SOla Lilja 		break;
5573592b7f6SOla Lilja 
5583592b7f6SOla Lilja 	default:
5593592b7f6SOla Lilja 		dev_err(dai->dev,
5603592b7f6SOla Lilja 			"%s: Error: Unsupported protocol/master (fmt = 0x%x)!\n",
5613592b7f6SOla Lilja 			__func__, drvdata->fmt);
5623592b7f6SOla Lilja 		return -EINVAL;
5633592b7f6SOla Lilja 	}
5643592b7f6SOla Lilja 
5653592b7f6SOla Lilja 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
5663592b7f6SOla Lilja 	case SND_SOC_DAIFMT_NB_NF:
5673592b7f6SOla Lilja 	case SND_SOC_DAIFMT_NB_IF:
5683592b7f6SOla Lilja 	case SND_SOC_DAIFMT_IB_IF:
5693592b7f6SOla Lilja 		break;
5703592b7f6SOla Lilja 
5713592b7f6SOla Lilja 	default:
5723592b7f6SOla Lilja 		dev_err(dai->dev,
5733592b7f6SOla Lilja 			"%s: Error: Unsupported inversion (fmt = 0x%x)!\n",
5743592b7f6SOla Lilja 			__func__, drvdata->fmt);
5753592b7f6SOla Lilja 		return -EINVAL;
5763592b7f6SOla Lilja 	}
5773592b7f6SOla Lilja 
5783592b7f6SOla Lilja 	drvdata->fmt = fmt;
5793592b7f6SOla Lilja 	return 0;
5803592b7f6SOla Lilja }
5813592b7f6SOla Lilja 
5823592b7f6SOla Lilja static int ux500_msp_dai_set_tdm_slot(struct snd_soc_dai *dai,
5833592b7f6SOla Lilja 				unsigned int tx_mask,
5843592b7f6SOla Lilja 				unsigned int rx_mask,
5853592b7f6SOla Lilja 				int slots, int slot_width)
5863592b7f6SOla Lilja {
5873592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
5883592b7f6SOla Lilja 	unsigned int cap;
5893592b7f6SOla Lilja 
5903592b7f6SOla Lilja 	switch (slots) {
5913592b7f6SOla Lilja 	case 1:
5923592b7f6SOla Lilja 		cap = 0x01;
5933592b7f6SOla Lilja 		break;
5943592b7f6SOla Lilja 	case 2:
5953592b7f6SOla Lilja 		cap = 0x03;
5963592b7f6SOla Lilja 		break;
5973592b7f6SOla Lilja 	case 8:
5983592b7f6SOla Lilja 		cap = 0xFF;
5993592b7f6SOla Lilja 		break;
6003592b7f6SOla Lilja 	case 16:
6013592b7f6SOla Lilja 		cap = 0xFFFF;
6023592b7f6SOla Lilja 		break;
6033592b7f6SOla Lilja 	default:
6043592b7f6SOla Lilja 		dev_err(dai->dev, "%s: Error: Unsupported slot-count (%d)!\n",
6053592b7f6SOla Lilja 			__func__, slots);
6063592b7f6SOla Lilja 		return -EINVAL;
6073592b7f6SOla Lilja 	}
6083592b7f6SOla Lilja 	drvdata->slots = slots;
6093592b7f6SOla Lilja 
6103592b7f6SOla Lilja 	if (!(slot_width == 16)) {
6113592b7f6SOla Lilja 		dev_err(dai->dev, "%s: Error: Unsupported slot-width (%d)!\n",
6123592b7f6SOla Lilja 			__func__, slot_width);
6133592b7f6SOla Lilja 		return -EINVAL;
6143592b7f6SOla Lilja 	}
6153592b7f6SOla Lilja 	drvdata->slot_width = slot_width;
6163592b7f6SOla Lilja 
6173592b7f6SOla Lilja 	drvdata->tx_mask = tx_mask & cap;
6183592b7f6SOla Lilja 	drvdata->rx_mask = rx_mask & cap;
6193592b7f6SOla Lilja 
6203592b7f6SOla Lilja 	return 0;
6213592b7f6SOla Lilja }
6223592b7f6SOla Lilja 
6233592b7f6SOla Lilja static int ux500_msp_dai_set_dai_sysclk(struct snd_soc_dai *dai,
6243592b7f6SOla Lilja 					int clk_id, unsigned int freq, int dir)
6253592b7f6SOla Lilja {
6263592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
6273592b7f6SOla Lilja 
6283592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d: Enter. clk-id: %d, freq: %u.\n",
6293592b7f6SOla Lilja 		__func__, dai->id, clk_id, freq);
6303592b7f6SOla Lilja 
6313592b7f6SOla Lilja 	switch (clk_id) {
6323592b7f6SOla Lilja 	case UX500_MSP_MASTER_CLOCK:
6333592b7f6SOla Lilja 		drvdata->master_clk = freq;
6343592b7f6SOla Lilja 		break;
6353592b7f6SOla Lilja 
6363592b7f6SOla Lilja 	default:
6373592b7f6SOla Lilja 		dev_err(dai->dev, "%s: MSP %d: Invalid clk-id (%d)!\n",
6383592b7f6SOla Lilja 			__func__, dai->id, clk_id);
6393592b7f6SOla Lilja 		return -EINVAL;
6403592b7f6SOla Lilja 	}
6413592b7f6SOla Lilja 
6423592b7f6SOla Lilja 	return 0;
6433592b7f6SOla Lilja }
6443592b7f6SOla Lilja 
6453592b7f6SOla Lilja static int ux500_msp_dai_trigger(struct snd_pcm_substream *substream,
6463592b7f6SOla Lilja 				int cmd, struct snd_soc_dai *dai)
6473592b7f6SOla Lilja {
6483592b7f6SOla Lilja 	int ret = 0;
6493592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
6503592b7f6SOla Lilja 
6513592b7f6SOla Lilja 	dev_dbg(dai->dev, "%s: MSP %d (%s): Enter (msp->id = %d, cmd = %d).\n",
6523592b7f6SOla Lilja 		__func__, dai->id, snd_pcm_stream_str(substream),
6533592b7f6SOla Lilja 		(int)drvdata->msp->id, cmd);
6543592b7f6SOla Lilja 
6553592b7f6SOla Lilja 	ret = ux500_msp_i2s_trigger(drvdata->msp, cmd, substream->stream);
6563592b7f6SOla Lilja 
6573592b7f6SOla Lilja 	return ret;
6583592b7f6SOla Lilja }
6593592b7f6SOla Lilja 
660f382acbeSLee Jones static int ux500_msp_dai_of_probe(struct snd_soc_dai *dai)
661f382acbeSLee Jones {
662f382acbeSLee Jones 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
663f382acbeSLee Jones 	struct snd_dmaengine_dai_dma_data *playback_dma_data;
664f382acbeSLee Jones 	struct snd_dmaengine_dai_dma_data *capture_dma_data;
665f382acbeSLee Jones 
666f382acbeSLee Jones 	playback_dma_data = devm_kzalloc(dai->dev,
667f382acbeSLee Jones 					 sizeof(*playback_dma_data),
668f382acbeSLee Jones 					 GFP_KERNEL);
669f382acbeSLee Jones 	if (!playback_dma_data)
670f382acbeSLee Jones 		return -ENOMEM;
671f382acbeSLee Jones 
672f382acbeSLee Jones 	capture_dma_data = devm_kzalloc(dai->dev,
673f382acbeSLee Jones 					sizeof(*capture_dma_data),
674f382acbeSLee Jones 					GFP_KERNEL);
675f382acbeSLee Jones 	if (!capture_dma_data)
676f382acbeSLee Jones 		return -ENOMEM;
677f382acbeSLee Jones 
678f382acbeSLee Jones 	playback_dma_data->addr = drvdata->msp->playback_dma_data.tx_rx_addr;
679f382acbeSLee Jones 	capture_dma_data->addr = drvdata->msp->capture_dma_data.tx_rx_addr;
680f382acbeSLee Jones 
681f382acbeSLee Jones 	playback_dma_data->maxburst = 4;
682f382acbeSLee Jones 	capture_dma_data->maxburst = 4;
683f382acbeSLee Jones 
684f382acbeSLee Jones 	snd_soc_dai_init_dma_data(dai, playback_dma_data, capture_dma_data);
685f382acbeSLee Jones 
686f382acbeSLee Jones 	return 0;
687f382acbeSLee Jones }
688f382acbeSLee Jones 
6893592b7f6SOla Lilja static int ux500_msp_dai_probe(struct snd_soc_dai *dai)
6903592b7f6SOla Lilja {
6913592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(dai->dev);
692f382acbeSLee Jones 	struct msp_i2s_platform_data *pdata = dai->dev->platform_data;
693f382acbeSLee Jones 	int ret;
6943592b7f6SOla Lilja 
695f382acbeSLee Jones 	if (!pdata) {
696f382acbeSLee Jones 		ret = ux500_msp_dai_of_probe(dai);
697f382acbeSLee Jones 		return ret;
698f382acbeSLee Jones 	}
6993592b7f6SOla Lilja 
700f3fe53ddSFabio Baltieri 	drvdata->msp->playback_dma_data.data_size = drvdata->slot_width;
701f3fe53ddSFabio Baltieri 	drvdata->msp->capture_dma_data.data_size = drvdata->slot_width;
7023592b7f6SOla Lilja 
703f382acbeSLee Jones 	snd_soc_dai_init_dma_data(dai,
704f382acbeSLee Jones 				  &drvdata->msp->playback_dma_data,
705f382acbeSLee Jones 				  &drvdata->msp->capture_dma_data);
7063592b7f6SOla Lilja 	return 0;
7073592b7f6SOla Lilja }
7083592b7f6SOla Lilja 
7093592b7f6SOla Lilja static struct snd_soc_dai_ops ux500_msp_dai_ops[] = {
7103592b7f6SOla Lilja 	{
7113592b7f6SOla Lilja 		.set_sysclk = ux500_msp_dai_set_dai_sysclk,
7123592b7f6SOla Lilja 		.set_fmt = ux500_msp_dai_set_dai_fmt,
7133592b7f6SOla Lilja 		.set_tdm_slot = ux500_msp_dai_set_tdm_slot,
7143592b7f6SOla Lilja 		.startup = ux500_msp_dai_startup,
7153592b7f6SOla Lilja 		.shutdown = ux500_msp_dai_shutdown,
7163592b7f6SOla Lilja 		.prepare = ux500_msp_dai_prepare,
7173592b7f6SOla Lilja 		.trigger = ux500_msp_dai_trigger,
7183592b7f6SOla Lilja 		.hw_params = ux500_msp_dai_hw_params,
7193592b7f6SOla Lilja 	}
7203592b7f6SOla Lilja };
7213592b7f6SOla Lilja 
72233899b19SLee Jones static struct snd_soc_dai_driver ux500_msp_dai_drv = {
7233592b7f6SOla Lilja 	.probe                 = ux500_msp_dai_probe,
7243592b7f6SOla Lilja 	.suspend               = NULL,
7253592b7f6SOla Lilja 	.resume                = NULL,
72633899b19SLee Jones 	.playback.channels_min = UX500_MSP_MIN_CHANNELS,
72733899b19SLee Jones 	.playback.channels_max = UX500_MSP_MAX_CHANNELS,
72833899b19SLee Jones 	.playback.rates        = UX500_I2S_RATES,
72933899b19SLee Jones 	.playback.formats      = UX500_I2S_FORMATS,
73033899b19SLee Jones 	.capture.channels_min  = UX500_MSP_MIN_CHANNELS,
73133899b19SLee Jones 	.capture.channels_max  = UX500_MSP_MAX_CHANNELS,
73233899b19SLee Jones 	.capture.rates         = UX500_I2S_RATES,
73333899b19SLee Jones 	.capture.formats       = UX500_I2S_FORMATS,
7343592b7f6SOla Lilja 	.ops                   = ux500_msp_dai_ops,
7353592b7f6SOla Lilja };
7363592b7f6SOla Lilja 
73742277bddSKuninori Morimoto static const struct snd_soc_component_driver ux500_msp_component = {
73842277bddSKuninori Morimoto 	.name		= "ux500-msp",
73942277bddSKuninori Morimoto };
74042277bddSKuninori Morimoto 
74142277bddSKuninori Morimoto 
742da794876SBill Pemberton static int ux500_msp_drv_probe(struct platform_device *pdev)
7433592b7f6SOla Lilja {
7443592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata;
745a61f9e31SLee Jones 	struct msp_i2s_platform_data *pdata = pdev->dev.platform_data;
746a61f9e31SLee Jones 	struct device_node *np = pdev->dev.of_node;
7473592b7f6SOla Lilja 	int ret = 0;
7483592b7f6SOla Lilja 
749a61f9e31SLee Jones 	if (!pdata && !np) {
750a61f9e31SLee Jones 		dev_err(&pdev->dev, "No platform data or Device Tree found\n");
751a61f9e31SLee Jones 		return -ENODEV;
752a61f9e31SLee Jones 	}
7533592b7f6SOla Lilja 
7543592b7f6SOla Lilja 	drvdata = devm_kzalloc(&pdev->dev,
7553592b7f6SOla Lilja 				sizeof(struct ux500_msp_i2s_drvdata),
7563592b7f6SOla Lilja 				GFP_KERNEL);
7570dcd4742SLee Jones 	if (!drvdata)
7580dcd4742SLee Jones 		return -ENOMEM;
7590dcd4742SLee Jones 
7603592b7f6SOla Lilja 	drvdata->fmt = 0;
7613592b7f6SOla Lilja 	drvdata->slots = 1;
7623592b7f6SOla Lilja 	drvdata->tx_mask = 0x01;
7633592b7f6SOla Lilja 	drvdata->rx_mask = 0x01;
7643592b7f6SOla Lilja 	drvdata->slot_width = 16;
7653592b7f6SOla Lilja 	drvdata->master_clk = MSP_INPUT_FREQ_APB;
7663592b7f6SOla Lilja 
7673592b7f6SOla Lilja 	drvdata->reg_vape = devm_regulator_get(&pdev->dev, "v-ape");
7683592b7f6SOla Lilja 	if (IS_ERR(drvdata->reg_vape)) {
7693592b7f6SOla Lilja 		ret = (int)PTR_ERR(drvdata->reg_vape);
7703592b7f6SOla Lilja 		dev_err(&pdev->dev,
7713592b7f6SOla Lilja 			"%s: ERROR: Failed to get Vape supply (%d)!\n",
7723592b7f6SOla Lilja 			__func__, ret);
7733592b7f6SOla Lilja 		return ret;
7743592b7f6SOla Lilja 	}
7753592b7f6SOla Lilja 	prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP, (char *)pdev->name, 50);
7763592b7f6SOla Lilja 
7774313489cSJulia Lawall 	drvdata->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
778f61ab093SUlf Hansson 	if (IS_ERR(drvdata->pclk)) {
779f61ab093SUlf Hansson 		ret = (int)PTR_ERR(drvdata->pclk);
7804313489cSJulia Lawall 		dev_err(&pdev->dev,
7814313489cSJulia Lawall 			"%s: ERROR: devm_clk_get of pclk failed (%d)!\n",
782f61ab093SUlf Hansson 			__func__, ret);
7834313489cSJulia Lawall 		return ret;
784f61ab093SUlf Hansson 	}
785f61ab093SUlf Hansson 
7864313489cSJulia Lawall 	drvdata->clk = devm_clk_get(&pdev->dev, NULL);
7873592b7f6SOla Lilja 	if (IS_ERR(drvdata->clk)) {
7883592b7f6SOla Lilja 		ret = (int)PTR_ERR(drvdata->clk);
7894313489cSJulia Lawall 		dev_err(&pdev->dev,
7904313489cSJulia Lawall 			"%s: ERROR: devm_clk_get failed (%d)!\n",
7913592b7f6SOla Lilja 			__func__, ret);
7924313489cSJulia Lawall 		return ret;
7933592b7f6SOla Lilja 	}
7943592b7f6SOla Lilja 
7953592b7f6SOla Lilja 	ret = ux500_msp_i2s_init_msp(pdev, &drvdata->msp,
7963592b7f6SOla Lilja 				pdev->dev.platform_data);
7973592b7f6SOla Lilja 	if (!drvdata->msp) {
7983592b7f6SOla Lilja 		dev_err(&pdev->dev,
7993592b7f6SOla Lilja 			"%s: ERROR: Failed to init MSP-struct (%d)!",
8003592b7f6SOla Lilja 			__func__, ret);
8014313489cSJulia Lawall 		return ret;
8023592b7f6SOla Lilja 	}
8033592b7f6SOla Lilja 	dev_set_drvdata(&pdev->dev, drvdata);
8043592b7f6SOla Lilja 
80542277bddSKuninori Morimoto 	ret = snd_soc_register_component(&pdev->dev, &ux500_msp_component,
80633899b19SLee Jones 					 &ux500_msp_dai_drv, 1);
8073592b7f6SOla Lilja 	if (ret < 0) {
8083592b7f6SOla Lilja 		dev_err(&pdev->dev, "Error: %s: Failed to register MSP%d!\n",
8093592b7f6SOla Lilja 			__func__, drvdata->msp->id);
8104313489cSJulia Lawall 		return ret;
8113592b7f6SOla Lilja 	}
8123592b7f6SOla Lilja 
8131428c20fSLee Jones 	ret = ux500_pcm_register_platform(pdev);
8141428c20fSLee Jones 	if (ret < 0) {
8151428c20fSLee Jones 		dev_err(&pdev->dev,
8161428c20fSLee Jones 			"Error: %s: Failed to register PCM platform device!\n",
8171428c20fSLee Jones 			__func__);
8181428c20fSLee Jones 		goto err_reg_plat;
8191428c20fSLee Jones 	}
8201428c20fSLee Jones 
8213592b7f6SOla Lilja 	return 0;
8223592b7f6SOla Lilja 
8231428c20fSLee Jones err_reg_plat:
82442277bddSKuninori Morimoto 	snd_soc_unregister_component(&pdev->dev);
8253592b7f6SOla Lilja 	return ret;
8263592b7f6SOla Lilja }
8273592b7f6SOla Lilja 
828da794876SBill Pemberton static int ux500_msp_drv_remove(struct platform_device *pdev)
8293592b7f6SOla Lilja {
8303592b7f6SOla Lilja 	struct ux500_msp_i2s_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
8313592b7f6SOla Lilja 
8321428c20fSLee Jones 	ux500_pcm_unregister_platform(pdev);
8331428c20fSLee Jones 
83442277bddSKuninori Morimoto 	snd_soc_unregister_component(&pdev->dev);
8353592b7f6SOla Lilja 
8363592b7f6SOla Lilja 	prcmu_qos_remove_requirement(PRCMU_QOS_APE_OPP, "ux500_msp_i2s");
8373592b7f6SOla Lilja 
8383592b7f6SOla Lilja 	ux500_msp_i2s_cleanup_msp(pdev, drvdata->msp);
8393592b7f6SOla Lilja 
8403592b7f6SOla Lilja 	return 0;
8413592b7f6SOla Lilja }
8423592b7f6SOla Lilja 
84349731c23SLee Jones static const struct of_device_id ux500_msp_i2s_match[] = {
84449731c23SLee Jones 	{ .compatible = "stericsson,ux500-msp-i2s", },
84549731c23SLee Jones 	{},
84649731c23SLee Jones };
847c81740e0SLuis de Bethencourt MODULE_DEVICE_TABLE(of, ux500_msp_i2s_match);
84849731c23SLee Jones 
8493592b7f6SOla Lilja static struct platform_driver msp_i2s_driver = {
8503592b7f6SOla Lilja 	.driver = {
8513592b7f6SOla Lilja 		.name = "ux500-msp-i2s",
85249731c23SLee Jones 		.of_match_table = ux500_msp_i2s_match,
8533592b7f6SOla Lilja 	},
8543592b7f6SOla Lilja 	.probe = ux500_msp_drv_probe,
8553592b7f6SOla Lilja 	.remove = ux500_msp_drv_remove,
8563592b7f6SOla Lilja };
8573592b7f6SOla Lilja module_platform_driver(msp_i2s_driver);
8583592b7f6SOla Lilja 
8593592b7f6SOla Lilja MODULE_LICENSE("GPL v2");
860