1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f3bd847eSArnaud Pouliquen /* 3f3bd847eSArnaud Pouliquen * Copyright (C) STMicroelectronics SA 2015 4f3bd847eSArnaud Pouliquen * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com> 5f3bd847eSArnaud Pouliquen * for STMicroelectronics. 6f3bd847eSArnaud Pouliquen */ 7f3bd847eSArnaud Pouliquen 8f3bd847eSArnaud Pouliquen #include <linux/module.h> 9bf9185ddSMark Brown #include <linux/pinctrl/consumer.h> 104c88f89fSArnaud Pouliquen #include <linux/delay.h> 11f3bd847eSArnaud Pouliquen 12f3bd847eSArnaud Pouliquen #include "uniperif.h" 13f3bd847eSArnaud Pouliquen 14f3bd847eSArnaud Pouliquen /* 1544f948bdSMoise Gergaud * User frame size shall be 2, 4, 6 or 8 32-bits words length 1644f948bdSMoise Gergaud * (i.e. 8, 16, 24 or 32 bytes) 1744f948bdSMoise Gergaud * This constraint comes from allowed values for 1844f948bdSMoise Gergaud * UNIPERIF_I2S_FMT_NUM_CH register 1944f948bdSMoise Gergaud */ 2044f948bdSMoise Gergaud #define UNIPERIF_MAX_FRAME_SZ 0x20 2144f948bdSMoise Gergaud #define UNIPERIF_ALLOWED_FRAME_SZ (0x08 | 0x10 | 0x18 | UNIPERIF_MAX_FRAME_SZ) 2244f948bdSMoise Gergaud 235a4326d1SArnaud Pouliquen struct sti_uniperiph_dev_data { 245a4326d1SArnaud Pouliquen unsigned int id; /* Nb available player instances */ 255a4326d1SArnaud Pouliquen unsigned int version; /* player IP version */ 265a4326d1SArnaud Pouliquen unsigned int stream; 275a4326d1SArnaud Pouliquen const char *dai_names; 285a4326d1SArnaud Pouliquen enum uniperif_type type; 295a4326d1SArnaud Pouliquen }; 305a4326d1SArnaud Pouliquen 315a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_uniplayer_hdmi = { 325a4326d1SArnaud Pouliquen .id = 0, 335a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0, 345a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_PLAYBACK, 355a4326d1SArnaud Pouliquen .dai_names = "Uni Player #0 (HDMI)", 365a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_HDMI 375a4326d1SArnaud Pouliquen }; 385a4326d1SArnaud Pouliquen 395a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_uniplayer_pcm_out = { 405a4326d1SArnaud Pouliquen .id = 1, 415a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0, 425a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_PLAYBACK, 435a4326d1SArnaud Pouliquen .dai_names = "Uni Player #1 (PCM OUT)", 445a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM, 455a4326d1SArnaud Pouliquen }; 465a4326d1SArnaud Pouliquen 475a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_uniplayer_dac = { 485a4326d1SArnaud Pouliquen .id = 2, 495a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0, 505a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_PLAYBACK, 515a4326d1SArnaud Pouliquen .dai_names = "Uni Player #2 (DAC)", 525a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_PCM, 535a4326d1SArnaud Pouliquen }; 545a4326d1SArnaud Pouliquen 555a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_uniplayer_spdif = { 565a4326d1SArnaud Pouliquen .id = 3, 575a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0, 585a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_PLAYBACK, 595a4326d1SArnaud Pouliquen .dai_names = "Uni Player #3 (SPDIF)", 605a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_SPDIF 615a4326d1SArnaud Pouliquen }; 625a4326d1SArnaud Pouliquen 635a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_unireader_pcm_in = { 645a4326d1SArnaud Pouliquen .id = 0, 655a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0, 665a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_CAPTURE, 675a4326d1SArnaud Pouliquen .dai_names = "Uni Reader #0 (PCM IN)", 685a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM, 695a4326d1SArnaud Pouliquen }; 705a4326d1SArnaud Pouliquen 715a4326d1SArnaud Pouliquen static const struct sti_uniperiph_dev_data sti_unireader_hdmi_in = { 725a4326d1SArnaud Pouliquen .id = 1, 735a4326d1SArnaud Pouliquen .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0, 745a4326d1SArnaud Pouliquen .stream = SNDRV_PCM_STREAM_CAPTURE, 755a4326d1SArnaud Pouliquen .dai_names = "Uni Reader #1 (HDMI IN)", 765a4326d1SArnaud Pouliquen .type = SND_ST_UNIPERIF_TYPE_PCM, 775a4326d1SArnaud Pouliquen }; 785a4326d1SArnaud Pouliquen 795a4326d1SArnaud Pouliquen static const struct of_device_id snd_soc_sti_match[] = { 805a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-player-hdmi", 815a4326d1SArnaud Pouliquen .data = &sti_uniplayer_hdmi 825a4326d1SArnaud Pouliquen }, 835a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-player-pcm-out", 845a4326d1SArnaud Pouliquen .data = &sti_uniplayer_pcm_out 855a4326d1SArnaud Pouliquen }, 865a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-player-dac", 875a4326d1SArnaud Pouliquen .data = &sti_uniplayer_dac 885a4326d1SArnaud Pouliquen }, 895a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-player-spdif", 905a4326d1SArnaud Pouliquen .data = &sti_uniplayer_spdif 915a4326d1SArnaud Pouliquen }, 925a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-reader-pcm_in", 935a4326d1SArnaud Pouliquen .data = &sti_unireader_pcm_in 945a4326d1SArnaud Pouliquen }, 955a4326d1SArnaud Pouliquen { .compatible = "st,stih407-uni-reader-hdmi", 965a4326d1SArnaud Pouliquen .data = &sti_unireader_hdmi_in 975a4326d1SArnaud Pouliquen }, 985a4326d1SArnaud Pouliquen {}, 995a4326d1SArnaud Pouliquen }; 100462c47c2SChen Lifu MODULE_DEVICE_TABLE(of, snd_soc_sti_match); 1015a4326d1SArnaud Pouliquen 1024c88f89fSArnaud Pouliquen int sti_uniperiph_reset(struct uniperif *uni) 1034c88f89fSArnaud Pouliquen { 1044c88f89fSArnaud Pouliquen int count = 10; 1054c88f89fSArnaud Pouliquen 1064c88f89fSArnaud Pouliquen /* Reset uniperipheral uni */ 1074c88f89fSArnaud Pouliquen SET_UNIPERIF_SOFT_RST_SOFT_RST(uni); 1084c88f89fSArnaud Pouliquen 1094c88f89fSArnaud Pouliquen if (uni->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) { 1104c88f89fSArnaud Pouliquen while (GET_UNIPERIF_SOFT_RST_SOFT_RST(uni) && count) { 1114c88f89fSArnaud Pouliquen udelay(5); 1124c88f89fSArnaud Pouliquen count--; 1134c88f89fSArnaud Pouliquen } 1144c88f89fSArnaud Pouliquen } 1154c88f89fSArnaud Pouliquen 1164c88f89fSArnaud Pouliquen if (!count) { 1174c88f89fSArnaud Pouliquen dev_err(uni->dev, "Failed to reset uniperif\n"); 1184c88f89fSArnaud Pouliquen return -EIO; 1194c88f89fSArnaud Pouliquen } 1204c88f89fSArnaud Pouliquen 1214c88f89fSArnaud Pouliquen return 0; 1224c88f89fSArnaud Pouliquen } 1234c88f89fSArnaud Pouliquen 12444f948bdSMoise Gergaud int sti_uniperiph_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 12544f948bdSMoise Gergaud unsigned int rx_mask, int slots, 12644f948bdSMoise Gergaud int slot_width) 12744f948bdSMoise Gergaud { 12844f948bdSMoise Gergaud struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 12944f948bdSMoise Gergaud struct uniperif *uni = priv->dai_data.uni; 13044f948bdSMoise Gergaud int i, frame_size, avail_slots; 13144f948bdSMoise Gergaud 13244f948bdSMoise Gergaud if (!UNIPERIF_TYPE_IS_TDM(uni)) { 13344f948bdSMoise Gergaud dev_err(uni->dev, "cpu dai not in tdm mode\n"); 13444f948bdSMoise Gergaud return -EINVAL; 13544f948bdSMoise Gergaud } 13644f948bdSMoise Gergaud 13744f948bdSMoise Gergaud /* store info in unip context */ 13844f948bdSMoise Gergaud uni->tdm_slot.slots = slots; 13944f948bdSMoise Gergaud uni->tdm_slot.slot_width = slot_width; 14044f948bdSMoise Gergaud /* unip is unidirectionnal */ 14144f948bdSMoise Gergaud uni->tdm_slot.mask = (tx_mask != 0) ? tx_mask : rx_mask; 14244f948bdSMoise Gergaud 14344f948bdSMoise Gergaud /* number of available timeslots */ 14444f948bdSMoise Gergaud for (i = 0, avail_slots = 0; i < uni->tdm_slot.slots; i++) { 14544f948bdSMoise Gergaud if ((uni->tdm_slot.mask >> i) & 0x01) 14644f948bdSMoise Gergaud avail_slots++; 14744f948bdSMoise Gergaud } 14844f948bdSMoise Gergaud uni->tdm_slot.avail_slots = avail_slots; 14944f948bdSMoise Gergaud 15044f948bdSMoise Gergaud /* frame size in bytes */ 15144f948bdSMoise Gergaud frame_size = uni->tdm_slot.avail_slots * uni->tdm_slot.slot_width / 8; 15244f948bdSMoise Gergaud 15344f948bdSMoise Gergaud /* check frame size is allowed */ 15444f948bdSMoise Gergaud if ((frame_size > UNIPERIF_MAX_FRAME_SZ) || 15544f948bdSMoise Gergaud (frame_size & ~(int)UNIPERIF_ALLOWED_FRAME_SZ)) { 15644f948bdSMoise Gergaud dev_err(uni->dev, "frame size not allowed: %d bytes\n", 15744f948bdSMoise Gergaud frame_size); 15844f948bdSMoise Gergaud return -EINVAL; 15944f948bdSMoise Gergaud } 16044f948bdSMoise Gergaud 16144f948bdSMoise Gergaud return 0; 16244f948bdSMoise Gergaud } 16344f948bdSMoise Gergaud 16472199787SMoise Gergaud int sti_uniperiph_fix_tdm_chan(struct snd_pcm_hw_params *params, 16572199787SMoise Gergaud struct snd_pcm_hw_rule *rule) 16672199787SMoise Gergaud { 16772199787SMoise Gergaud struct uniperif *uni = rule->private; 16872199787SMoise Gergaud struct snd_interval t; 16972199787SMoise Gergaud 17072199787SMoise Gergaud t.min = uni->tdm_slot.avail_slots; 17172199787SMoise Gergaud t.max = uni->tdm_slot.avail_slots; 17272199787SMoise Gergaud t.openmin = 0; 17372199787SMoise Gergaud t.openmax = 0; 17472199787SMoise Gergaud t.integer = 0; 17572199787SMoise Gergaud 17672199787SMoise Gergaud return snd_interval_refine(hw_param_interval(params, rule->var), &t); 17772199787SMoise Gergaud } 17872199787SMoise Gergaud 17972199787SMoise Gergaud int sti_uniperiph_fix_tdm_format(struct snd_pcm_hw_params *params, 18072199787SMoise Gergaud struct snd_pcm_hw_rule *rule) 18172199787SMoise Gergaud { 18272199787SMoise Gergaud struct uniperif *uni = rule->private; 18372199787SMoise Gergaud struct snd_mask *maskp = hw_param_mask(params, rule->var); 18472199787SMoise Gergaud u64 format; 18572199787SMoise Gergaud 18672199787SMoise Gergaud switch (uni->tdm_slot.slot_width) { 18772199787SMoise Gergaud case 16: 18872199787SMoise Gergaud format = SNDRV_PCM_FMTBIT_S16_LE; 18972199787SMoise Gergaud break; 19072199787SMoise Gergaud case 32: 19172199787SMoise Gergaud format = SNDRV_PCM_FMTBIT_S32_LE; 19272199787SMoise Gergaud break; 19372199787SMoise Gergaud default: 19472199787SMoise Gergaud dev_err(uni->dev, "format not supported: %d bits\n", 19572199787SMoise Gergaud uni->tdm_slot.slot_width); 19672199787SMoise Gergaud return -EINVAL; 19772199787SMoise Gergaud } 19872199787SMoise Gergaud 19972199787SMoise Gergaud maskp->bits[0] &= (u_int32_t)format; 20072199787SMoise Gergaud maskp->bits[1] &= (u_int32_t)(format >> 32); 20172199787SMoise Gergaud /* clear remaining indexes */ 20272199787SMoise Gergaud memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX - 64) / 8); 20372199787SMoise Gergaud 20472199787SMoise Gergaud if (!maskp->bits[0] && !maskp->bits[1]) 20572199787SMoise Gergaud return -EINVAL; 20672199787SMoise Gergaud 20772199787SMoise Gergaud return 0; 20872199787SMoise Gergaud } 20972199787SMoise Gergaud 21044f948bdSMoise Gergaud int sti_uniperiph_get_tdm_word_pos(struct uniperif *uni, 21144f948bdSMoise Gergaud unsigned int *word_pos) 21244f948bdSMoise Gergaud { 21344f948bdSMoise Gergaud int slot_width = uni->tdm_slot.slot_width / 8; 21444f948bdSMoise Gergaud int slots_num = uni->tdm_slot.slots; 21544f948bdSMoise Gergaud unsigned int slots_mask = uni->tdm_slot.mask; 21644f948bdSMoise Gergaud int i, j, k; 21744f948bdSMoise Gergaud unsigned int word16_pos[4]; 21844f948bdSMoise Gergaud 21944f948bdSMoise Gergaud /* word16_pos: 22044f948bdSMoise Gergaud * word16_pos[0] = WORDX_LSB 22144f948bdSMoise Gergaud * word16_pos[1] = WORDX_MSB, 22244f948bdSMoise Gergaud * word16_pos[2] = WORDX+1_LSB 22344f948bdSMoise Gergaud * word16_pos[3] = WORDX+1_MSB 22444f948bdSMoise Gergaud */ 22544f948bdSMoise Gergaud 22644f948bdSMoise Gergaud /* set unip word position */ 22744f948bdSMoise Gergaud for (i = 0, j = 0, k = 0; (i < slots_num) && (k < WORD_MAX); i++) { 22844f948bdSMoise Gergaud if ((slots_mask >> i) & 0x01) { 22944f948bdSMoise Gergaud word16_pos[j] = i * slot_width; 23044f948bdSMoise Gergaud 23144f948bdSMoise Gergaud if (slot_width == 4) { 23244f948bdSMoise Gergaud word16_pos[j + 1] = word16_pos[j] + 2; 23344f948bdSMoise Gergaud j++; 23444f948bdSMoise Gergaud } 23544f948bdSMoise Gergaud j++; 23644f948bdSMoise Gergaud 23744f948bdSMoise Gergaud if (j > 3) { 23844f948bdSMoise Gergaud word_pos[k] = word16_pos[1] | 23944f948bdSMoise Gergaud (word16_pos[0] << 8) | 24044f948bdSMoise Gergaud (word16_pos[3] << 16) | 24144f948bdSMoise Gergaud (word16_pos[2] << 24); 24244f948bdSMoise Gergaud j = 0; 24344f948bdSMoise Gergaud k++; 24444f948bdSMoise Gergaud } 24544f948bdSMoise Gergaud } 24644f948bdSMoise Gergaud } 24744f948bdSMoise Gergaud 24844f948bdSMoise Gergaud return 0; 24944f948bdSMoise Gergaud } 25044f948bdSMoise Gergaud 25144f948bdSMoise Gergaud /* 252f3bd847eSArnaud Pouliquen * sti_uniperiph_dai_create_ctrl 253f3bd847eSArnaud Pouliquen * This function is used to create Ctrl associated to DAI but also pcm device. 254f3bd847eSArnaud Pouliquen * Request is done by front end to associate ctrl with pcm device id 255f3bd847eSArnaud Pouliquen */ 256f2da4542Skbuild test robot static int sti_uniperiph_dai_create_ctrl(struct snd_soc_dai *dai) 257f3bd847eSArnaud Pouliquen { 258f3bd847eSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 259f3bd847eSArnaud Pouliquen struct uniperif *uni = priv->dai_data.uni; 260f3bd847eSArnaud Pouliquen struct snd_kcontrol_new *ctrl; 261f3bd847eSArnaud Pouliquen int i; 262f3bd847eSArnaud Pouliquen 263f3bd847eSArnaud Pouliquen if (!uni->num_ctrls) 264f3bd847eSArnaud Pouliquen return 0; 265f3bd847eSArnaud Pouliquen 266f3bd847eSArnaud Pouliquen for (i = 0; i < uni->num_ctrls; i++) { 267f3bd847eSArnaud Pouliquen /* 268f3bd847eSArnaud Pouliquen * Several Control can have same name. Controls are indexed on 269f3bd847eSArnaud Pouliquen * Uniperipheral instance ID 270f3bd847eSArnaud Pouliquen */ 271f3bd847eSArnaud Pouliquen ctrl = &uni->snd_ctrls[i]; 2725a4326d1SArnaud Pouliquen ctrl->index = uni->id; 2735a4326d1SArnaud Pouliquen ctrl->device = uni->id; 274f3bd847eSArnaud Pouliquen } 275f3bd847eSArnaud Pouliquen 276f3bd847eSArnaud Pouliquen return snd_soc_add_dai_controls(dai, uni->snd_ctrls, uni->num_ctrls); 277f3bd847eSArnaud Pouliquen } 278f3bd847eSArnaud Pouliquen 279f3bd847eSArnaud Pouliquen /* 280f3bd847eSArnaud Pouliquen * DAI 281f3bd847eSArnaud Pouliquen */ 282f3bd847eSArnaud Pouliquen int sti_uniperiph_dai_hw_params(struct snd_pcm_substream *substream, 283f3bd847eSArnaud Pouliquen struct snd_pcm_hw_params *params, 284f3bd847eSArnaud Pouliquen struct snd_soc_dai *dai) 285f3bd847eSArnaud Pouliquen { 2868d8b1e2eSMoise Gergaud struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 2878d8b1e2eSMoise Gergaud struct uniperif *uni = priv->dai_data.uni; 288f3bd847eSArnaud Pouliquen struct snd_dmaengine_dai_dma_data *dma_data; 289f3bd847eSArnaud Pouliquen int transfer_size; 290f3bd847eSArnaud Pouliquen 2915a4326d1SArnaud Pouliquen if (uni->type == SND_ST_UNIPERIF_TYPE_TDM) 2928d8b1e2eSMoise Gergaud /* transfer size = user frame size (in 32-bits FIFO cell) */ 2938d8b1e2eSMoise Gergaud transfer_size = snd_soc_params_to_frame_size(params) / 32; 2948d8b1e2eSMoise Gergaud else 295f3bd847eSArnaud Pouliquen transfer_size = params_channels(params) * UNIPERIF_FIFO_FRAMES; 296f3bd847eSArnaud Pouliquen 297f3bd847eSArnaud Pouliquen dma_data = snd_soc_dai_get_dma_data(dai, substream); 298f3bd847eSArnaud Pouliquen dma_data->maxburst = transfer_size; 299f3bd847eSArnaud Pouliquen 300f3bd847eSArnaud Pouliquen return 0; 301f3bd847eSArnaud Pouliquen } 302f3bd847eSArnaud Pouliquen 303f3bd847eSArnaud Pouliquen int sti_uniperiph_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 304f3bd847eSArnaud Pouliquen { 305f3bd847eSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 306f3bd847eSArnaud Pouliquen 307f3bd847eSArnaud Pouliquen priv->dai_data.uni->daifmt = fmt; 308f3bd847eSArnaud Pouliquen 309f3bd847eSArnaud Pouliquen return 0; 310f3bd847eSArnaud Pouliquen } 311f3bd847eSArnaud Pouliquen 3124a58fb71SKuninori Morimoto static int sti_uniperiph_suspend(struct snd_soc_component *component) 313f3bd847eSArnaud Pouliquen { 3144a58fb71SKuninori Morimoto struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component); 315f3bd847eSArnaud Pouliquen struct uniperif *uni = priv->dai_data.uni; 316f3bd847eSArnaud Pouliquen int ret; 317f3bd847eSArnaud Pouliquen 318f3bd847eSArnaud Pouliquen /* The uniperipheral should be in stopped state */ 319f3bd847eSArnaud Pouliquen if (uni->state != UNIPERIF_STATE_STOPPED) { 320748abba8SArnaud Pouliquen dev_err(uni->dev, "%s: invalid uni state( %d)\n", 321f3bd847eSArnaud Pouliquen __func__, (int)uni->state); 322f3bd847eSArnaud Pouliquen return -EBUSY; 323f3bd847eSArnaud Pouliquen } 324f3bd847eSArnaud Pouliquen 325f3bd847eSArnaud Pouliquen /* Pinctrl: switch pinstate to sleep */ 326f3bd847eSArnaud Pouliquen ret = pinctrl_pm_select_sleep_state(uni->dev); 327f3bd847eSArnaud Pouliquen if (ret) 328748abba8SArnaud Pouliquen dev_err(uni->dev, "%s: failed to select pinctrl state\n", 329f3bd847eSArnaud Pouliquen __func__); 330f3bd847eSArnaud Pouliquen 331f3bd847eSArnaud Pouliquen return ret; 332f3bd847eSArnaud Pouliquen } 333f3bd847eSArnaud Pouliquen 3344a58fb71SKuninori Morimoto static int sti_uniperiph_resume(struct snd_soc_component *component) 335f3bd847eSArnaud Pouliquen { 3364a58fb71SKuninori Morimoto struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component); 337f3bd847eSArnaud Pouliquen struct uniperif *uni = priv->dai_data.uni; 338f3bd847eSArnaud Pouliquen int ret; 339f3bd847eSArnaud Pouliquen 3405a4326d1SArnaud Pouliquen if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) { 341f3bd847eSArnaud Pouliquen ret = uni_player_resume(uni); 342f3bd847eSArnaud Pouliquen if (ret) 343f3bd847eSArnaud Pouliquen return ret; 344f3bd847eSArnaud Pouliquen } 345f3bd847eSArnaud Pouliquen 346f3bd847eSArnaud Pouliquen /* pinctrl: switch pinstate to default */ 347f3bd847eSArnaud Pouliquen ret = pinctrl_pm_select_default_state(uni->dev); 348f3bd847eSArnaud Pouliquen if (ret) 349748abba8SArnaud Pouliquen dev_err(uni->dev, "%s: failed to select pinctrl state\n", 350f3bd847eSArnaud Pouliquen __func__); 351f3bd847eSArnaud Pouliquen 352f3bd847eSArnaud Pouliquen return ret; 353f3bd847eSArnaud Pouliquen } 354f3bd847eSArnaud Pouliquen 355f3bd847eSArnaud Pouliquen static int sti_uniperiph_dai_probe(struct snd_soc_dai *dai) 356f3bd847eSArnaud Pouliquen { 357f3bd847eSArnaud Pouliquen struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 358f3bd847eSArnaud Pouliquen struct sti_uniperiph_dai *dai_data = &priv->dai_data; 359f3bd847eSArnaud Pouliquen 360f3bd847eSArnaud Pouliquen /* DMA settings*/ 3615a4326d1SArnaud Pouliquen if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) 362f3bd847eSArnaud Pouliquen snd_soc_dai_init_dma_data(dai, &dai_data->dma_data, NULL); 363f3bd847eSArnaud Pouliquen else 364f3bd847eSArnaud Pouliquen snd_soc_dai_init_dma_data(dai, NULL, &dai_data->dma_data); 365f3bd847eSArnaud Pouliquen 366f3bd847eSArnaud Pouliquen dai_data->dma_data.addr = dai_data->uni->fifo_phys_address; 367f3bd847eSArnaud Pouliquen dai_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 368f3bd847eSArnaud Pouliquen 369f3bd847eSArnaud Pouliquen return sti_uniperiph_dai_create_ctrl(dai); 370f3bd847eSArnaud Pouliquen } 371f3bd847eSArnaud Pouliquen 372f3bd847eSArnaud Pouliquen static const struct snd_soc_dai_driver sti_uniperiph_dai_template = { 373f3bd847eSArnaud Pouliquen .probe = sti_uniperiph_dai_probe, 374f3bd847eSArnaud Pouliquen }; 375f3bd847eSArnaud Pouliquen 376f3bd847eSArnaud Pouliquen static const struct snd_soc_component_driver sti_uniperiph_dai_component = { 377f3bd847eSArnaud Pouliquen .name = "sti_cpu_dai", 3784a58fb71SKuninori Morimoto .suspend = sti_uniperiph_suspend, 379*ad483da7SCharles Keepax .resume = sti_uniperiph_resume, 380*ad483da7SCharles Keepax .legacy_dai_naming = 1, 381f3bd847eSArnaud Pouliquen }; 382f3bd847eSArnaud Pouliquen 383f3bd847eSArnaud Pouliquen static int sti_uniperiph_cpu_dai_of(struct device_node *node, 384f3bd847eSArnaud Pouliquen struct sti_uniperiph_data *priv) 385f3bd847eSArnaud Pouliquen { 386f3bd847eSArnaud Pouliquen struct device *dev = &priv->pdev->dev; 387f3bd847eSArnaud Pouliquen struct sti_uniperiph_dai *dai_data = &priv->dai_data; 388f3bd847eSArnaud Pouliquen struct snd_soc_dai_driver *dai = priv->dai; 389f3bd847eSArnaud Pouliquen struct snd_soc_pcm_stream *stream; 390f3bd847eSArnaud Pouliquen struct uniperif *uni; 3915a4326d1SArnaud Pouliquen const struct of_device_id *of_id; 3925a4326d1SArnaud Pouliquen const struct sti_uniperiph_dev_data *dev_data; 3935a4326d1SArnaud Pouliquen const char *mode; 394748abba8SArnaud Pouliquen int ret; 3955a4326d1SArnaud Pouliquen 3965a4326d1SArnaud Pouliquen /* Populate data structure depending on compatibility */ 3975a4326d1SArnaud Pouliquen of_id = of_match_node(snd_soc_sti_match, node); 3985a4326d1SArnaud Pouliquen if (!of_id->data) { 399748abba8SArnaud Pouliquen dev_err(dev, "data associated to device is missing\n"); 4005a4326d1SArnaud Pouliquen return -EINVAL; 4015a4326d1SArnaud Pouliquen } 4025a4326d1SArnaud Pouliquen dev_data = (struct sti_uniperiph_dev_data *)of_id->data; 403f3bd847eSArnaud Pouliquen 404f3bd847eSArnaud Pouliquen uni = devm_kzalloc(dev, sizeof(*uni), GFP_KERNEL); 405f3bd847eSArnaud Pouliquen if (!uni) 406f3bd847eSArnaud Pouliquen return -ENOMEM; 407f3bd847eSArnaud Pouliquen 4085a4326d1SArnaud Pouliquen uni->id = dev_data->id; 4095a4326d1SArnaud Pouliquen uni->ver = dev_data->version; 4105a4326d1SArnaud Pouliquen 411f3bd847eSArnaud Pouliquen *dai = sti_uniperiph_dai_template; 4125a4326d1SArnaud Pouliquen dai->name = dev_data->dai_names; 413f3bd847eSArnaud Pouliquen 41406e6d904SYang Yingliang /* Get resources and base address */ 41506e6d904SYang Yingliang uni->base = devm_platform_get_and_ioremap_resource(priv->pdev, 0, &uni->mem_region); 416f3bd847eSArnaud Pouliquen if (IS_ERR(uni->base)) 417f3bd847eSArnaud Pouliquen return PTR_ERR(uni->base); 418f3bd847eSArnaud Pouliquen 419f3bd847eSArnaud Pouliquen uni->fifo_phys_address = uni->mem_region->start + 420f3bd847eSArnaud Pouliquen UNIPERIF_FIFO_DATA_OFFSET(uni); 421f3bd847eSArnaud Pouliquen 422f3bd847eSArnaud Pouliquen uni->irq = platform_get_irq(priv->pdev, 0); 423cf9441adSStephen Boyd if (uni->irq < 0) 424f3bd847eSArnaud Pouliquen return -ENXIO; 425f3bd847eSArnaud Pouliquen 4265a4326d1SArnaud Pouliquen uni->type = dev_data->type; 427f3bd847eSArnaud Pouliquen 4285a4326d1SArnaud Pouliquen /* check if player should be configured for tdm */ 4295a4326d1SArnaud Pouliquen if (dev_data->type & SND_ST_UNIPERIF_TYPE_TDM) { 4305a4326d1SArnaud Pouliquen if (!of_property_read_string(node, "st,tdm-mode", &mode)) 4315a4326d1SArnaud Pouliquen uni->type = SND_ST_UNIPERIF_TYPE_TDM; 4325a4326d1SArnaud Pouliquen else 4335a4326d1SArnaud Pouliquen uni->type = SND_ST_UNIPERIF_TYPE_PCM; 4345a4326d1SArnaud Pouliquen } 4355a4326d1SArnaud Pouliquen 4365a4326d1SArnaud Pouliquen dai_data->uni = uni; 4375a4326d1SArnaud Pouliquen dai_data->stream = dev_data->stream; 4385a4326d1SArnaud Pouliquen 4395a4326d1SArnaud Pouliquen if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) { 440748abba8SArnaud Pouliquen ret = uni_player_init(priv->pdev, uni); 441f3bd847eSArnaud Pouliquen stream = &dai->playback; 442f3bd847eSArnaud Pouliquen } else { 443748abba8SArnaud Pouliquen ret = uni_reader_init(priv->pdev, uni); 444f3bd847eSArnaud Pouliquen stream = &dai->capture; 445f3bd847eSArnaud Pouliquen } 446748abba8SArnaud Pouliquen if (ret < 0) 447748abba8SArnaud Pouliquen return ret; 448748abba8SArnaud Pouliquen 449f3bd847eSArnaud Pouliquen dai->ops = uni->dai_ops; 450f3bd847eSArnaud Pouliquen 451f3bd847eSArnaud Pouliquen stream->stream_name = dai->name; 452f3bd847eSArnaud Pouliquen stream->channels_min = uni->hw->channels_min; 453f3bd847eSArnaud Pouliquen stream->channels_max = uni->hw->channels_max; 454f3bd847eSArnaud Pouliquen stream->rates = uni->hw->rates; 455f3bd847eSArnaud Pouliquen stream->formats = uni->hw->formats; 456f3bd847eSArnaud Pouliquen 457f3bd847eSArnaud Pouliquen return 0; 458f3bd847eSArnaud Pouliquen } 459f3bd847eSArnaud Pouliquen 460f3bd847eSArnaud Pouliquen static const struct snd_dmaengine_pcm_config dmaengine_pcm_config = { 461f3bd847eSArnaud Pouliquen .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, 462f3bd847eSArnaud Pouliquen }; 463f3bd847eSArnaud Pouliquen 464f3bd847eSArnaud Pouliquen static int sti_uniperiph_probe(struct platform_device *pdev) 465f3bd847eSArnaud Pouliquen { 466f3bd847eSArnaud Pouliquen struct sti_uniperiph_data *priv; 467f3bd847eSArnaud Pouliquen struct device_node *node = pdev->dev.of_node; 468f3bd847eSArnaud Pouliquen int ret; 469f3bd847eSArnaud Pouliquen 470f3bd847eSArnaud Pouliquen /* Allocate the private data and the CPU_DAI array */ 471f3bd847eSArnaud Pouliquen priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 472f3bd847eSArnaud Pouliquen if (!priv) 473f3bd847eSArnaud Pouliquen return -ENOMEM; 474f3bd847eSArnaud Pouliquen priv->dai = devm_kzalloc(&pdev->dev, sizeof(*priv->dai), GFP_KERNEL); 475f3bd847eSArnaud Pouliquen if (!priv->dai) 476f3bd847eSArnaud Pouliquen return -ENOMEM; 477f3bd847eSArnaud Pouliquen 478f3bd847eSArnaud Pouliquen priv->pdev = pdev; 479f3bd847eSArnaud Pouliquen 480f3bd847eSArnaud Pouliquen ret = sti_uniperiph_cpu_dai_of(node, priv); 481bf2a6b44SPierre-Louis Bossart if (ret < 0) 482bf2a6b44SPierre-Louis Bossart return ret; 483f3bd847eSArnaud Pouliquen 484f3bd847eSArnaud Pouliquen dev_set_drvdata(&pdev->dev, priv); 485f3bd847eSArnaud Pouliquen 486e1290910SAxel Lin ret = devm_snd_soc_register_component(&pdev->dev, 487f3bd847eSArnaud Pouliquen &sti_uniperiph_dai_component, 488f3bd847eSArnaud Pouliquen priv->dai, 1); 489f3bd847eSArnaud Pouliquen if (ret < 0) 490f3bd847eSArnaud Pouliquen return ret; 491f3bd847eSArnaud Pouliquen 492f3bd847eSArnaud Pouliquen return devm_snd_dmaengine_pcm_register(&pdev->dev, 493f3bd847eSArnaud Pouliquen &dmaengine_pcm_config, 0); 494f3bd847eSArnaud Pouliquen } 495f3bd847eSArnaud Pouliquen 496f3bd847eSArnaud Pouliquen static struct platform_driver sti_uniperiph_driver = { 497f3bd847eSArnaud Pouliquen .driver = { 498f3bd847eSArnaud Pouliquen .name = "sti-uniperiph-dai", 499f3bd847eSArnaud Pouliquen .of_match_table = snd_soc_sti_match, 500f3bd847eSArnaud Pouliquen }, 501f3bd847eSArnaud Pouliquen .probe = sti_uniperiph_probe, 502f3bd847eSArnaud Pouliquen }; 503f3bd847eSArnaud Pouliquen module_platform_driver(sti_uniperiph_driver); 504f3bd847eSArnaud Pouliquen 505f3bd847eSArnaud Pouliquen MODULE_DESCRIPTION("uniperipheral DAI driver"); 506f3bd847eSArnaud Pouliquen MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>"); 507f3bd847eSArnaud Pouliquen MODULE_LICENSE("GPL v2"); 508