1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) STMicroelectronics SA 2015
4 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
5 * for STMicroelectronics.
8 #include <linux/module.h>
9 #include <linux/pinctrl/consumer.h>
10 #include <linux/delay.h>
15 * User frame size shall be 2, 4, 6 or 8 32-bits words length
16 * (i.e. 8, 16, 24 or 32 bytes)
17 * This constraint comes from allowed values for
18 * UNIPERIF_I2S_FMT_NUM_CH register
20 #define UNIPERIF_MAX_FRAME_SZ 0x20
21 #define UNIPERIF_ALLOWED_FRAME_SZ (0x08 | 0x10 | 0x18 | UNIPERIF_MAX_FRAME_SZ)
23 struct sti_uniperiph_dev_data {
24 unsigned int id; /* Nb available player instances */
25 unsigned int version; /* player IP version */
27 const char *dai_names;
28 enum uniperif_type type;
31 static const struct sti_uniperiph_dev_data sti_uniplayer_hdmi = {
33 .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
34 .stream = SNDRV_PCM_STREAM_PLAYBACK,
35 .dai_names = "Uni Player #0 (HDMI)",
36 .type = SND_ST_UNIPERIF_TYPE_HDMI
39 static const struct sti_uniperiph_dev_data sti_uniplayer_pcm_out = {
41 .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
42 .stream = SNDRV_PCM_STREAM_PLAYBACK,
43 .dai_names = "Uni Player #1 (PCM OUT)",
44 .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM,
47 static const struct sti_uniperiph_dev_data sti_uniplayer_dac = {
49 .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
50 .stream = SNDRV_PCM_STREAM_PLAYBACK,
51 .dai_names = "Uni Player #2 (DAC)",
52 .type = SND_ST_UNIPERIF_TYPE_PCM,
55 static const struct sti_uniperiph_dev_data sti_uniplayer_spdif = {
57 .version = SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0,
58 .stream = SNDRV_PCM_STREAM_PLAYBACK,
59 .dai_names = "Uni Player #3 (SPDIF)",
60 .type = SND_ST_UNIPERIF_TYPE_SPDIF
63 static const struct sti_uniperiph_dev_data sti_unireader_pcm_in = {
65 .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0,
66 .stream = SNDRV_PCM_STREAM_CAPTURE,
67 .dai_names = "Uni Reader #0 (PCM IN)",
68 .type = SND_ST_UNIPERIF_TYPE_PCM | SND_ST_UNIPERIF_TYPE_TDM,
71 static const struct sti_uniperiph_dev_data sti_unireader_hdmi_in = {
73 .version = SND_ST_UNIPERIF_VERSION_UNI_RDR_1_0,
74 .stream = SNDRV_PCM_STREAM_CAPTURE,
75 .dai_names = "Uni Reader #1 (HDMI IN)",
76 .type = SND_ST_UNIPERIF_TYPE_PCM,
79 static const struct of_device_id snd_soc_sti_match[] = {
80 { .compatible = "st,stih407-uni-player-hdmi",
81 .data = &sti_uniplayer_hdmi
83 { .compatible = "st,stih407-uni-player-pcm-out",
84 .data = &sti_uniplayer_pcm_out
86 { .compatible = "st,stih407-uni-player-dac",
87 .data = &sti_uniplayer_dac
89 { .compatible = "st,stih407-uni-player-spdif",
90 .data = &sti_uniplayer_spdif
92 { .compatible = "st,stih407-uni-reader-pcm_in",
93 .data = &sti_unireader_pcm_in
95 { .compatible = "st,stih407-uni-reader-hdmi",
96 .data = &sti_unireader_hdmi_in
100 MODULE_DEVICE_TABLE(of, snd_soc_sti_match);
102 int sti_uniperiph_reset(struct uniperif *uni)
106 /* Reset uniperipheral uni */
107 SET_UNIPERIF_SOFT_RST_SOFT_RST(uni);
109 if (uni->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
110 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(uni) && count) {
117 dev_err(uni->dev, "Failed to reset uniperif\n");
124 int sti_uniperiph_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
125 unsigned int rx_mask, int slots,
128 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
129 struct uniperif *uni = priv->dai_data.uni;
130 int i, frame_size, avail_slots;
132 if (!UNIPERIF_TYPE_IS_TDM(uni)) {
133 dev_err(uni->dev, "cpu dai not in tdm mode\n");
137 /* store info in unip context */
138 uni->tdm_slot.slots = slots;
139 uni->tdm_slot.slot_width = slot_width;
140 /* unip is unidirectionnal */
141 uni->tdm_slot.mask = (tx_mask != 0) ? tx_mask : rx_mask;
143 /* number of available timeslots */
144 for (i = 0, avail_slots = 0; i < uni->tdm_slot.slots; i++) {
145 if ((uni->tdm_slot.mask >> i) & 0x01)
148 uni->tdm_slot.avail_slots = avail_slots;
150 /* frame size in bytes */
151 frame_size = uni->tdm_slot.avail_slots * uni->tdm_slot.slot_width / 8;
153 /* check frame size is allowed */
154 if ((frame_size > UNIPERIF_MAX_FRAME_SZ) ||
155 (frame_size & ~(int)UNIPERIF_ALLOWED_FRAME_SZ)) {
156 dev_err(uni->dev, "frame size not allowed: %d bytes\n",
164 int sti_uniperiph_fix_tdm_chan(struct snd_pcm_hw_params *params,
165 struct snd_pcm_hw_rule *rule)
167 struct uniperif *uni = rule->private;
168 struct snd_interval t;
170 t.min = uni->tdm_slot.avail_slots;
171 t.max = uni->tdm_slot.avail_slots;
176 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
179 int sti_uniperiph_fix_tdm_format(struct snd_pcm_hw_params *params,
180 struct snd_pcm_hw_rule *rule)
182 struct uniperif *uni = rule->private;
183 struct snd_mask *maskp = hw_param_mask(params, rule->var);
186 switch (uni->tdm_slot.slot_width) {
188 format = SNDRV_PCM_FMTBIT_S16_LE;
191 format = SNDRV_PCM_FMTBIT_S32_LE;
194 dev_err(uni->dev, "format not supported: %d bits\n",
195 uni->tdm_slot.slot_width);
199 maskp->bits[0] &= (u_int32_t)format;
200 maskp->bits[1] &= (u_int32_t)(format >> 32);
201 /* clear remaining indexes */
202 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX - 64) / 8);
204 if (!maskp->bits[0] && !maskp->bits[1])
210 int sti_uniperiph_get_tdm_word_pos(struct uniperif *uni,
211 unsigned int *word_pos)
213 int slot_width = uni->tdm_slot.slot_width / 8;
214 int slots_num = uni->tdm_slot.slots;
215 unsigned int slots_mask = uni->tdm_slot.mask;
217 unsigned int word16_pos[4];
220 * word16_pos[0] = WORDX_LSB
221 * word16_pos[1] = WORDX_MSB,
222 * word16_pos[2] = WORDX+1_LSB
223 * word16_pos[3] = WORDX+1_MSB
226 /* set unip word position */
227 for (i = 0, j = 0, k = 0; (i < slots_num) && (k < WORD_MAX); i++) {
228 if ((slots_mask >> i) & 0x01) {
229 word16_pos[j] = i * slot_width;
231 if (slot_width == 4) {
232 word16_pos[j + 1] = word16_pos[j] + 2;
238 word_pos[k] = word16_pos[1] |
239 (word16_pos[0] << 8) |
240 (word16_pos[3] << 16) |
241 (word16_pos[2] << 24);
252 * sti_uniperiph_dai_create_ctrl
253 * This function is used to create Ctrl associated to DAI but also pcm device.
254 * Request is done by front end to associate ctrl with pcm device id
256 static int sti_uniperiph_dai_create_ctrl(struct snd_soc_dai *dai)
258 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
259 struct uniperif *uni = priv->dai_data.uni;
260 struct snd_kcontrol_new *ctrl;
266 for (i = 0; i < uni->num_ctrls; i++) {
268 * Several Control can have same name. Controls are indexed on
269 * Uniperipheral instance ID
271 ctrl = &uni->snd_ctrls[i];
272 ctrl->index = uni->id;
273 ctrl->device = uni->id;
276 return snd_soc_add_dai_controls(dai, uni->snd_ctrls, uni->num_ctrls);
282 int sti_uniperiph_dai_hw_params(struct snd_pcm_substream *substream,
283 struct snd_pcm_hw_params *params,
284 struct snd_soc_dai *dai)
286 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
287 struct uniperif *uni = priv->dai_data.uni;
288 struct snd_dmaengine_dai_dma_data *dma_data;
291 if (uni->type == SND_ST_UNIPERIF_TYPE_TDM)
292 /* transfer size = user frame size (in 32-bits FIFO cell) */
293 transfer_size = snd_soc_params_to_frame_size(params) / 32;
295 transfer_size = params_channels(params) * UNIPERIF_FIFO_FRAMES;
297 dma_data = snd_soc_dai_get_dma_data(dai, substream);
298 dma_data->maxburst = transfer_size;
303 int sti_uniperiph_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
305 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
307 priv->dai_data.uni->daifmt = fmt;
312 static int sti_uniperiph_suspend(struct snd_soc_component *component)
314 struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component);
315 struct uniperif *uni = priv->dai_data.uni;
318 /* The uniperipheral should be in stopped state */
319 if (uni->state != UNIPERIF_STATE_STOPPED) {
320 dev_err(uni->dev, "%s: invalid uni state( %d)\n",
321 __func__, (int)uni->state);
325 /* Pinctrl: switch pinstate to sleep */
326 ret = pinctrl_pm_select_sleep_state(uni->dev);
328 dev_err(uni->dev, "%s: failed to select pinctrl state\n",
334 static int sti_uniperiph_resume(struct snd_soc_component *component)
336 struct sti_uniperiph_data *priv = snd_soc_component_get_drvdata(component);
337 struct uniperif *uni = priv->dai_data.uni;
340 if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
341 ret = uni_player_resume(uni);
346 /* pinctrl: switch pinstate to default */
347 ret = pinctrl_pm_select_default_state(uni->dev);
349 dev_err(uni->dev, "%s: failed to select pinctrl state\n",
355 static int sti_uniperiph_dai_probe(struct snd_soc_dai *dai)
357 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
358 struct sti_uniperiph_dai *dai_data = &priv->dai_data;
361 if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK)
362 snd_soc_dai_init_dma_data(dai, &dai_data->dma_data, NULL);
364 snd_soc_dai_init_dma_data(dai, NULL, &dai_data->dma_data);
366 dai_data->dma_data.addr = dai_data->uni->fifo_phys_address;
367 dai_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
369 return sti_uniperiph_dai_create_ctrl(dai);
372 static const struct snd_soc_dai_ops sti_uniperiph_dai_ops = {
373 .probe = sti_uniperiph_dai_probe,
376 static const struct snd_soc_dai_driver sti_uniperiph_dai_template = {
377 .ops = &sti_uniperiph_dai_ops,
380 static const struct snd_soc_component_driver sti_uniperiph_dai_component = {
381 .name = "sti_cpu_dai",
382 .suspend = sti_uniperiph_suspend,
383 .resume = sti_uniperiph_resume,
384 .legacy_dai_naming = 1,
387 static int sti_uniperiph_cpu_dai_of(struct device_node *node,
388 struct sti_uniperiph_data *priv)
390 struct device *dev = &priv->pdev->dev;
391 struct sti_uniperiph_dai *dai_data = &priv->dai_data;
392 struct snd_soc_dai_driver *dai = priv->dai;
393 struct snd_soc_pcm_stream *stream;
394 struct uniperif *uni;
395 const struct of_device_id *of_id;
396 const struct sti_uniperiph_dev_data *dev_data;
400 /* Populate data structure depending on compatibility */
401 of_id = of_match_node(snd_soc_sti_match, node);
403 dev_err(dev, "data associated to device is missing\n");
406 dev_data = (struct sti_uniperiph_dev_data *)of_id->data;
408 uni = devm_kzalloc(dev, sizeof(*uni), GFP_KERNEL);
412 uni->id = dev_data->id;
413 uni->ver = dev_data->version;
415 *dai = sti_uniperiph_dai_template;
416 dai->name = dev_data->dai_names;
418 /* Get resources and base address */
419 uni->base = devm_platform_get_and_ioremap_resource(priv->pdev, 0, &uni->mem_region);
420 if (IS_ERR(uni->base))
421 return PTR_ERR(uni->base);
423 uni->fifo_phys_address = uni->mem_region->start +
424 UNIPERIF_FIFO_DATA_OFFSET(uni);
426 uni->irq = platform_get_irq(priv->pdev, 0);
430 uni->type = dev_data->type;
432 /* check if player should be configured for tdm */
433 if (dev_data->type & SND_ST_UNIPERIF_TYPE_TDM) {
434 if (!of_property_read_string(node, "st,tdm-mode", &mode))
435 uni->type = SND_ST_UNIPERIF_TYPE_TDM;
437 uni->type = SND_ST_UNIPERIF_TYPE_PCM;
441 dai_data->stream = dev_data->stream;
443 if (priv->dai_data.stream == SNDRV_PCM_STREAM_PLAYBACK) {
444 ret = uni_player_init(priv->pdev, uni);
445 stream = &dai->playback;
447 ret = uni_reader_init(priv->pdev, uni);
448 stream = &dai->capture;
453 dai->ops = uni->dai_ops;
455 stream->stream_name = dai->name;
456 stream->channels_min = uni->hw->channels_min;
457 stream->channels_max = uni->hw->channels_max;
458 stream->rates = uni->hw->rates;
459 stream->formats = uni->hw->formats;
464 static const struct snd_dmaengine_pcm_config dmaengine_pcm_config = {
465 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
468 static int sti_uniperiph_probe(struct platform_device *pdev)
470 struct sti_uniperiph_data *priv;
471 struct device_node *node = pdev->dev.of_node;
474 /* Allocate the private data and the CPU_DAI array */
475 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
478 priv->dai = devm_kzalloc(&pdev->dev, sizeof(*priv->dai), GFP_KERNEL);
484 ret = sti_uniperiph_cpu_dai_of(node, priv);
488 dev_set_drvdata(&pdev->dev, priv);
490 ret = devm_snd_soc_register_component(&pdev->dev,
491 &sti_uniperiph_dai_component,
496 return devm_snd_dmaengine_pcm_register(&pdev->dev,
497 &dmaengine_pcm_config, 0);
500 static struct platform_driver sti_uniperiph_driver = {
502 .name = "sti-uniperiph-dai",
503 .of_match_table = snd_soc_sti_match,
505 .probe = sti_uniperiph_probe,
507 module_platform_driver(sti_uniperiph_driver);
509 MODULE_DESCRIPTION("uniperipheral DAI driver");
510 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
511 MODULE_LICENSE("GPL v2");