1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for Microchip S/PDIF RX Controller
5 // Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
7 // Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spinlock.h>
15 #include <sound/dmaengine_pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
20 * ---- S/PDIF Receiver Controller Register map ----
22 #define SPDIFRX_CR 0x00 /* Control Register */
23 #define SPDIFRX_MR 0x04 /* Mode Register */
25 #define SPDIFRX_IER 0x10 /* Interrupt Enable Register */
26 #define SPDIFRX_IDR 0x14 /* Interrupt Disable Register */
27 #define SPDIFRX_IMR 0x18 /* Interrupt Mask Register */
28 #define SPDIFRX_ISR 0x1c /* Interrupt Status Register */
29 #define SPDIFRX_RSR 0x20 /* Status Register */
30 #define SPDIFRX_RHR 0x24 /* Holding Register */
32 #define SPDIFRX_CHSR(channel, reg) \
33 (0x30 + (channel) * 0x30 + (reg) * 4) /* Channel x Status Registers */
35 #define SPDIFRX_CHUD(channel, reg) \
36 (0x48 + (channel) * 0x30 + (reg) * 4) /* Channel x User Data Registers */
38 #define SPDIFRX_WPMR 0xE4 /* Write Protection Mode Register */
39 #define SPDIFRX_WPSR 0xE8 /* Write Protection Status Register */
41 #define SPDIFRX_VERSION 0xFC /* Version Register */
44 * ---- Control Register (Write-only) ----
46 #define SPDIFRX_CR_SWRST BIT(0) /* Software Reset */
49 * ---- Mode Register (Read/Write) ----
52 #define SPDIFRX_MR_RXEN_MASK GENMASK(0, 0)
53 #define SPDIFRX_MR_RXEN_DISABLE (0 << 0) /* SPDIF Receiver Disabled */
54 #define SPDIFRX_MR_RXEN_ENABLE (1 << 0) /* SPDIF Receiver Enabled */
56 /* Validity Bit Mode */
57 #define SPDIFRX_MR_VBMODE_MASK GENAMSK(1, 1)
58 #define SPDIFRX_MR_VBMODE_ALWAYS_LOAD \
59 (0 << 1) /* Load sample regardless of validity bit value */
60 #define SPDIFRX_MR_VBMODE_DISCARD_IF_VB1 \
61 (1 << 1) /* Load sample only if validity bit is 0 */
63 /* Data Word Endian Mode */
64 #define SPDIFRX_MR_ENDIAN_MASK GENMASK(2, 2)
65 #define SPDIFRX_MR_ENDIAN_LITTLE (0 << 2) /* Little Endian Mode */
66 #define SPDIFRX_MR_ENDIAN_BIG (1 << 2) /* Big Endian Mode */
69 #define SPDIFRX_MR_PBMODE_MASK GENMASK(3, 3)
70 #define SPDIFRX_MR_PBMODE_PARCHECK (0 << 3) /* Parity Check Enabled */
71 #define SPDIFRX_MR_PBMODE_NOPARCHECK (1 << 3) /* Parity Check Disabled */
73 /* Sample Data Width */
74 #define SPDIFRX_MR_DATAWIDTH_MASK GENMASK(5, 4)
75 #define SPDIFRX_MR_DATAWIDTH(width) \
76 (((6 - (width) / 4) << 4) & SPDIFRX_MR_DATAWIDTH_MASK)
78 /* Packed Data Mode in Receive Holding Register */
79 #define SPDIFRX_MR_PACK_MASK GENMASK(7, 7)
80 #define SPDIFRX_MR_PACK_DISABLED (0 << 7)
81 #define SPDIFRX_MR_PACK_ENABLED (1 << 7)
83 /* Start of Block Bit Mode */
84 #define SPDIFRX_MR_SBMODE_MASK GENMASK(8, 8)
85 #define SPDIFRX_MR_SBMODE_ALWAYS_LOAD (0 << 8)
86 #define SPDIFRX_MR_SBMODE_DISCARD (1 << 8)
88 /* Consecutive Preamble Error Threshold Automatic Restart */
89 #define SPDIFRX_MR_AUTORST_MASK GENMASK(24, 24)
90 #define SPDIFRX_MR_AUTORST_NOACTION (0 << 24)
91 #define SPDIFRX_MR_AUTORST_UNLOCK_ON_PRE_ERR (1 << 24)
94 * ---- Interrupt Enable/Disable/Mask/Status Register (Write/Read-only) ----
96 #define SPDIFRX_IR_RXRDY BIT(0)
97 #define SPDIFRX_IR_LOCKED BIT(1)
98 #define SPDIFRX_IR_LOSS BIT(2)
99 #define SPDIFRX_IR_BLOCKEND BIT(3)
100 #define SPDIFRX_IR_SFE BIT(4)
101 #define SPDIFRX_IR_PAR_ERR BIT(5)
102 #define SPDIFRX_IR_OVERRUN BIT(6)
103 #define SPDIFRX_IR_RXFULL BIT(7)
104 #define SPDIFRX_IR_CSC(ch) BIT((ch) + 8)
105 #define SPDIFRX_IR_SECE BIT(10)
106 #define SPDIFRX_IR_BLOCKST BIT(11)
107 #define SPDIFRX_IR_NRZ_ERR BIT(12)
108 #define SPDIFRX_IR_PRE_ERR BIT(13)
109 #define SPDIFRX_IR_CP_ERR BIT(14)
112 * ---- Receiver Status Register (Read/Write) ----
115 #define SPDIFRX_RSR_ULOCK BIT(0)
116 #define SPDIFRX_RSR_BADF BIT(1)
117 #define SPDIFRX_RSR_LOWF BIT(2)
118 #define SPDIFRX_RSR_NOSIGNAL BIT(3)
119 #define SPDIFRX_RSR_IFS_MASK GENMASK(27, 16)
120 #define SPDIFRX_RSR_IFS(reg) \
121 (((reg) & SPDIFRX_RSR_IFS_MASK) >> 16)
124 * ---- Version Register (Read-only) ----
126 #define SPDIFRX_VERSION_MASK GENMASK(11, 0)
127 #define SPDIFRX_VERSION_MFN_MASK GENMASK(18, 16)
128 #define SPDIFRX_VERSION_MFN(reg) (((reg) & SPDIFRX_VERSION_MFN_MASK) >> 16)
130 static bool mchp_spdifrx_readable_reg(struct device *dev, unsigned int reg)
137 case SPDIFRX_CHSR(0, 0):
138 case SPDIFRX_CHSR(0, 1):
139 case SPDIFRX_CHSR(0, 2):
140 case SPDIFRX_CHSR(0, 3):
141 case SPDIFRX_CHSR(0, 4):
142 case SPDIFRX_CHSR(0, 5):
143 case SPDIFRX_CHUD(0, 0):
144 case SPDIFRX_CHUD(0, 1):
145 case SPDIFRX_CHUD(0, 2):
146 case SPDIFRX_CHUD(0, 3):
147 case SPDIFRX_CHUD(0, 4):
148 case SPDIFRX_CHUD(0, 5):
149 case SPDIFRX_CHSR(1, 0):
150 case SPDIFRX_CHSR(1, 1):
151 case SPDIFRX_CHSR(1, 2):
152 case SPDIFRX_CHSR(1, 3):
153 case SPDIFRX_CHSR(1, 4):
154 case SPDIFRX_CHSR(1, 5):
155 case SPDIFRX_CHUD(1, 0):
156 case SPDIFRX_CHUD(1, 1):
157 case SPDIFRX_CHUD(1, 2):
158 case SPDIFRX_CHUD(1, 3):
159 case SPDIFRX_CHUD(1, 4):
160 case SPDIFRX_CHUD(1, 5):
163 case SPDIFRX_VERSION:
170 static bool mchp_spdifrx_writeable_reg(struct device *dev, unsigned int reg)
184 static bool mchp_spdifrx_precious_reg(struct device *dev, unsigned int reg)
195 static const struct regmap_config mchp_spdifrx_regmap_config = {
199 .max_register = SPDIFRX_VERSION,
200 .readable_reg = mchp_spdifrx_readable_reg,
201 .writeable_reg = mchp_spdifrx_writeable_reg,
202 .precious_reg = mchp_spdifrx_precious_reg,
205 #define SPDIFRX_GCLK_RATIO_MIN (12 * 64)
207 #define SPDIFRX_CS_BITS 192
208 #define SPDIFRX_UD_BITS 192
210 #define SPDIFRX_CHANNELS 2
212 struct mchp_spdifrx_ch_stat {
213 unsigned char data[SPDIFRX_CS_BITS / 8];
214 struct completion done;
217 struct mchp_spdifrx_user_data {
218 unsigned char data[SPDIFRX_UD_BITS / 8];
219 struct completion done;
222 struct mchp_spdifrx_mixer_control {
223 struct mchp_spdifrx_ch_stat ch_stat[SPDIFRX_CHANNELS];
224 struct mchp_spdifrx_user_data user_data[SPDIFRX_CHANNELS];
230 struct mchp_spdifrx_dev {
231 struct snd_dmaengine_dai_dma_data capture;
232 struct mchp_spdifrx_mixer_control control;
235 struct regmap *regmap;
239 unsigned int trigger_enabled;
240 unsigned int gclk_enabled:1;
243 static void mchp_spdifrx_channel_status_read(struct mchp_spdifrx_dev *dev,
246 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
247 u8 *ch_stat = &ctrl->ch_stat[channel].data[0];
251 for (i = 0; i < ARRAY_SIZE(ctrl->ch_stat[channel].data) / 4; i++) {
252 regmap_read(dev->regmap, SPDIFRX_CHSR(channel, i), &val);
253 *ch_stat++ = val & 0xFF;
254 *ch_stat++ = (val >> 8) & 0xFF;
255 *ch_stat++ = (val >> 16) & 0xFF;
256 *ch_stat++ = (val >> 24) & 0xFF;
260 static void mchp_spdifrx_channel_user_data_read(struct mchp_spdifrx_dev *dev,
263 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
264 u8 *user_data = &ctrl->user_data[channel].data[0];
268 for (i = 0; i < ARRAY_SIZE(ctrl->user_data[channel].data) / 4; i++) {
269 regmap_read(dev->regmap, SPDIFRX_CHUD(channel, i), &val);
270 *user_data++ = val & 0xFF;
271 *user_data++ = (val >> 8) & 0xFF;
272 *user_data++ = (val >> 16) & 0xFF;
273 *user_data++ = (val >> 24) & 0xFF;
277 static irqreturn_t mchp_spdif_interrupt(int irq, void *dev_id)
279 struct mchp_spdifrx_dev *dev = dev_id;
280 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
281 u32 sr, imr, pending;
282 irqreturn_t ret = IRQ_NONE;
285 regmap_read(dev->regmap, SPDIFRX_ISR, &sr);
286 regmap_read(dev->regmap, SPDIFRX_IMR, &imr);
288 dev_dbg(dev->dev, "ISR: %#x, IMR: %#x, pending: %#x\n", sr, imr,
294 if (pending & SPDIFRX_IR_BLOCKEND) {
295 for (ch = 0; ch < SPDIFRX_CHANNELS; ch++) {
296 mchp_spdifrx_channel_user_data_read(dev, ch);
297 complete(&ctrl->user_data[ch].done);
299 regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_BLOCKEND);
303 for (ch = 0; ch < SPDIFRX_CHANNELS; ch++) {
304 if (pending & SPDIFRX_IR_CSC(ch)) {
305 mchp_spdifrx_channel_status_read(dev, ch);
306 complete(&ctrl->ch_stat[ch].done);
307 regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_CSC(ch));
312 if (pending & SPDIFRX_IR_OVERRUN) {
313 dev_warn(dev->dev, "Overrun detected\n");
320 static int mchp_spdifrx_trigger(struct snd_pcm_substream *substream, int cmd,
321 struct snd_soc_dai *dai)
323 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
327 case SNDRV_PCM_TRIGGER_START:
328 case SNDRV_PCM_TRIGGER_RESUME:
329 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
330 mutex_lock(&dev->mlock);
331 /* Enable overrun interrupts */
332 regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_OVERRUN);
334 /* Enable receiver. */
335 regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK,
336 SPDIFRX_MR_RXEN_ENABLE);
337 dev->trigger_enabled = true;
338 mutex_unlock(&dev->mlock);
340 case SNDRV_PCM_TRIGGER_STOP:
341 case SNDRV_PCM_TRIGGER_SUSPEND:
342 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
343 mutex_lock(&dev->mlock);
344 /* Disable overrun interrupts */
345 regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_OVERRUN);
347 /* Disable receiver. */
348 regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK,
349 SPDIFRX_MR_RXEN_DISABLE);
350 dev->trigger_enabled = false;
351 mutex_unlock(&dev->mlock);
360 static int mchp_spdifrx_hw_params(struct snd_pcm_substream *substream,
361 struct snd_pcm_hw_params *params,
362 struct snd_soc_dai *dai)
364 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
368 dev_dbg(dev->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
369 __func__, params_rate(params), params_format(params),
370 params_width(params), params_channels(params));
372 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
373 dev_err(dev->dev, "Playback is not supported\n");
377 if (params_channels(params) != SPDIFRX_CHANNELS) {
378 dev_err(dev->dev, "unsupported number of channels: %d\n",
379 params_channels(params));
383 switch (params_format(params)) {
384 case SNDRV_PCM_FORMAT_S16_BE:
385 case SNDRV_PCM_FORMAT_S20_3BE:
386 case SNDRV_PCM_FORMAT_S24_3BE:
387 case SNDRV_PCM_FORMAT_S24_BE:
388 mr |= SPDIFRX_MR_ENDIAN_BIG;
390 case SNDRV_PCM_FORMAT_S16_LE:
391 case SNDRV_PCM_FORMAT_S20_3LE:
392 case SNDRV_PCM_FORMAT_S24_3LE:
393 case SNDRV_PCM_FORMAT_S24_LE:
394 mr |= SPDIFRX_MR_DATAWIDTH(params_width(params));
397 dev_err(dev->dev, "unsupported PCM format: %d\n",
398 params_format(params));
402 mutex_lock(&dev->mlock);
403 if (dev->trigger_enabled) {
404 dev_err(dev->dev, "PCM already running\n");
409 if (dev->gclk_enabled) {
410 clk_disable_unprepare(dev->gclk);
411 dev->gclk_enabled = 0;
413 ret = clk_set_min_rate(dev->gclk, params_rate(params) *
414 SPDIFRX_GCLK_RATIO_MIN + 1);
417 "unable to set gclk min rate: rate %u * ratio %u + 1\n",
418 params_rate(params), SPDIFRX_GCLK_RATIO_MIN);
421 ret = clk_prepare_enable(dev->gclk);
423 dev_err(dev->dev, "unable to enable gclk: %d\n", ret);
426 dev->gclk_enabled = 1;
428 dev_dbg(dev->dev, "GCLK range min set to %d\n",
429 params_rate(params) * SPDIFRX_GCLK_RATIO_MIN + 1);
431 ret = regmap_write(dev->regmap, SPDIFRX_MR, mr);
434 mutex_unlock(&dev->mlock);
439 static int mchp_spdifrx_hw_free(struct snd_pcm_substream *substream,
440 struct snd_soc_dai *dai)
442 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
444 mutex_lock(&dev->mlock);
445 if (dev->gclk_enabled) {
446 clk_disable_unprepare(dev->gclk);
447 dev->gclk_enabled = 0;
449 mutex_unlock(&dev->mlock);
453 static const struct snd_soc_dai_ops mchp_spdifrx_dai_ops = {
454 .trigger = mchp_spdifrx_trigger,
455 .hw_params = mchp_spdifrx_hw_params,
456 .hw_free = mchp_spdifrx_hw_free,
459 #define MCHP_SPDIF_RATES SNDRV_PCM_RATE_8000_192000
461 #define MCHP_SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
462 SNDRV_PCM_FMTBIT_U16_BE | \
463 SNDRV_PCM_FMTBIT_S20_3LE | \
464 SNDRV_PCM_FMTBIT_S20_3BE | \
465 SNDRV_PCM_FMTBIT_S24_3LE | \
466 SNDRV_PCM_FMTBIT_S24_3BE | \
467 SNDRV_PCM_FMTBIT_S24_LE | \
468 SNDRV_PCM_FMTBIT_S24_BE \
471 static int mchp_spdifrx_info(struct snd_kcontrol *kcontrol,
472 struct snd_ctl_elem_info *uinfo)
474 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
480 static int mchp_spdifrx_cs_get(struct mchp_spdifrx_dev *dev,
482 struct snd_ctl_elem_value *uvalue)
484 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
485 struct mchp_spdifrx_ch_stat *ch_stat = &ctrl->ch_stat[channel];
488 mutex_lock(&dev->mlock);
491 * We may reach this point with both clocks enabled but the receiver
492 * still disabled. To void waiting for completion and return with
493 * timeout check the dev->trigger_enabled.
496 * - if the receiver is enabled CSC IRQ will update the data in software
497 * caches (ch_stat->data)
498 * - otherwise we just update it here the software caches with latest
499 * available information and return it; in this case we don't need
500 * spin locking as the IRQ is disabled and will not be raised from
504 if (dev->trigger_enabled) {
505 reinit_completion(&ch_stat->done);
506 regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_CSC(channel));
507 /* Check for new data available */
508 ret = wait_for_completion_interruptible_timeout(&ch_stat->done,
509 msecs_to_jiffies(100));
510 /* Valid stream might not be present */
512 dev_dbg(dev->dev, "channel status for channel %d timeout\n",
514 regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_CSC(channel));
515 ret = ret ? : -ETIMEDOUT;
521 /* Update software cache with latest channel status. */
522 mchp_spdifrx_channel_status_read(dev, channel);
525 memcpy(uvalue->value.iec958.status, ch_stat->data,
526 sizeof(ch_stat->data));
529 mutex_unlock(&dev->mlock);
533 static int mchp_spdifrx_cs1_get(struct snd_kcontrol *kcontrol,
534 struct snd_ctl_elem_value *uvalue)
536 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
537 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
539 return mchp_spdifrx_cs_get(dev, 0, uvalue);
542 static int mchp_spdifrx_cs2_get(struct snd_kcontrol *kcontrol,
543 struct snd_ctl_elem_value *uvalue)
545 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
546 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
548 return mchp_spdifrx_cs_get(dev, 1, uvalue);
551 static int mchp_spdifrx_cs_mask(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *uvalue)
554 memset(uvalue->value.iec958.status, 0xff,
555 sizeof(uvalue->value.iec958.status));
560 static int mchp_spdifrx_subcode_ch_get(struct mchp_spdifrx_dev *dev,
562 struct snd_ctl_elem_value *uvalue)
564 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
565 struct mchp_spdifrx_user_data *user_data = &ctrl->user_data[channel];
568 mutex_lock(&dev->mlock);
571 * We may reach this point with both clocks enabled but the receiver
572 * still disabled. To void waiting for completion to just timeout we
573 * check here the dev->trigger_enabled flag.
576 * - if the receiver is enabled we need to wait for blockend IRQ to read
577 * data to and update it for us in software caches
578 * - otherwise reading the SPDIFRX_CHUD() registers is enough.
581 if (dev->trigger_enabled) {
582 reinit_completion(&user_data->done);
583 regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_BLOCKEND);
584 ret = wait_for_completion_interruptible_timeout(&user_data->done,
585 msecs_to_jiffies(100));
586 /* Valid stream might not be present. */
588 dev_dbg(dev->dev, "user data for channel %d timeout\n",
590 regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_BLOCKEND);
591 ret = ret ? : -ETIMEDOUT;
597 /* Update software cache with last available data. */
598 mchp_spdifrx_channel_user_data_read(dev, channel);
601 memcpy(uvalue->value.iec958.subcode, user_data->data,
602 sizeof(user_data->data));
605 mutex_unlock(&dev->mlock);
609 static int mchp_spdifrx_subcode_ch1_get(struct snd_kcontrol *kcontrol,
610 struct snd_ctl_elem_value *uvalue)
612 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
613 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
615 return mchp_spdifrx_subcode_ch_get(dev, 0, uvalue);
618 static int mchp_spdifrx_subcode_ch2_get(struct snd_kcontrol *kcontrol,
619 struct snd_ctl_elem_value *uvalue)
621 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
622 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
624 return mchp_spdifrx_subcode_ch_get(dev, 1, uvalue);
627 static int mchp_spdifrx_boolean_info(struct snd_kcontrol *kcontrol,
628 struct snd_ctl_elem_info *uinfo)
630 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
632 uinfo->value.integer.min = 0;
633 uinfo->value.integer.max = 1;
638 static int mchp_spdifrx_ulock_get(struct snd_kcontrol *kcontrol,
639 struct snd_ctl_elem_value *uvalue)
641 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
642 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
643 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
645 bool ulock_old = ctrl->ulock;
647 mutex_lock(&dev->mlock);
650 * The RSR.ULOCK has wrong value if both pclk and gclk are enabled
651 * and the receiver is disabled. Thus we take into account the
652 * dev->trigger_enabled here to return a real status.
654 if (dev->trigger_enabled) {
655 regmap_read(dev->regmap, SPDIFRX_RSR, &val);
656 ctrl->ulock = !(val & SPDIFRX_RSR_ULOCK);
661 uvalue->value.integer.value[0] = ctrl->ulock;
663 mutex_unlock(&dev->mlock);
665 return ulock_old != ctrl->ulock;
668 static int mchp_spdifrx_badf_get(struct snd_kcontrol *kcontrol,
669 struct snd_ctl_elem_value *uvalue)
671 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
672 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
673 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
675 bool badf_old = ctrl->badf;
677 mutex_lock(&dev->mlock);
680 * The RSR.ULOCK has wrong value if both pclk and gclk are enabled
681 * and the receiver is disabled. Thus we take into account the
682 * dev->trigger_enabled here to return a real status.
684 if (dev->trigger_enabled) {
685 regmap_read(dev->regmap, SPDIFRX_RSR, &val);
686 ctrl->badf = !!(val & SPDIFRX_RSR_BADF);
691 mutex_unlock(&dev->mlock);
693 uvalue->value.integer.value[0] = ctrl->badf;
695 return badf_old != ctrl->badf;
698 static int mchp_spdifrx_signal_get(struct snd_kcontrol *kcontrol,
699 struct snd_ctl_elem_value *uvalue)
701 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
702 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
703 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
704 u32 val = ~0U, loops = 10;
706 bool signal_old = ctrl->signal;
708 mutex_lock(&dev->mlock);
711 * To get the signal we need to have receiver enabled. This
712 * could be enabled also from trigger() function thus we need to
713 * take care of not disabling the receiver when it runs.
715 if (!dev->trigger_enabled) {
716 ret = clk_prepare_enable(dev->gclk);
720 regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK,
721 SPDIFRX_MR_RXEN_ENABLE);
723 /* Wait for RSR.ULOCK bit. */
725 regmap_read(dev->regmap, SPDIFRX_RSR, &val);
726 if (!(val & SPDIFRX_RSR_ULOCK))
728 usleep_range(100, 150);
731 regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK,
732 SPDIFRX_MR_RXEN_DISABLE);
734 clk_disable_unprepare(dev->gclk);
736 regmap_read(dev->regmap, SPDIFRX_RSR, &val);
740 mutex_unlock(&dev->mlock);
742 if (!(val & SPDIFRX_RSR_ULOCK))
743 ctrl->signal = !(val & SPDIFRX_RSR_NOSIGNAL);
746 uvalue->value.integer.value[0] = ctrl->signal;
748 return signal_old != ctrl->signal;
751 static int mchp_spdifrx_rate_info(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_info *uinfo)
754 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
756 uinfo->value.integer.min = 0;
757 uinfo->value.integer.max = 192000;
762 static int mchp_spdifrx_rate_get(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
765 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
766 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
770 mutex_lock(&dev->mlock);
773 * The RSR.ULOCK has wrong value if both pclk and gclk are enabled
774 * and the receiver is disabled. Thus we take into account the
775 * dev->trigger_enabled here to return a real status.
777 if (dev->trigger_enabled) {
778 regmap_read(dev->regmap, SPDIFRX_RSR, &val);
779 /* If the receiver is not locked, ISF data is invalid. */
780 if (val & SPDIFRX_RSR_ULOCK || !(val & SPDIFRX_RSR_IFS_MASK)) {
781 ucontrol->value.integer.value[0] = 0;
785 /* Reveicer is not locked, IFS data is invalid. */
786 ucontrol->value.integer.value[0] = 0;
790 rate = clk_get_rate(dev->gclk);
792 ucontrol->value.integer.value[0] = rate / (32 * SPDIFRX_RSR_IFS(val));
795 mutex_unlock(&dev->mlock);
799 static struct snd_kcontrol_new mchp_spdifrx_ctrls[] = {
800 /* Channel status controller */
802 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
803 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT)
805 .access = SNDRV_CTL_ELEM_ACCESS_READ |
806 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
807 .info = mchp_spdifrx_info,
808 .get = mchp_spdifrx_cs1_get,
811 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
812 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT)
814 .access = SNDRV_CTL_ELEM_ACCESS_READ |
815 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
816 .info = mchp_spdifrx_info,
817 .get = mchp_spdifrx_cs2_get,
820 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
821 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
822 .access = SNDRV_CTL_ELEM_ACCESS_READ,
823 .info = mchp_spdifrx_info,
824 .get = mchp_spdifrx_cs_mask,
826 /* User bits controller */
828 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
829 .name = "IEC958 Subcode Capture Default Channel 1",
830 .access = SNDRV_CTL_ELEM_ACCESS_READ |
831 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
832 .info = mchp_spdifrx_info,
833 .get = mchp_spdifrx_subcode_ch1_get,
836 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
837 .name = "IEC958 Subcode Capture Default Channel 2",
838 .access = SNDRV_CTL_ELEM_ACCESS_READ |
839 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
840 .info = mchp_spdifrx_info,
841 .get = mchp_spdifrx_subcode_ch2_get,
845 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
846 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) "Unlocked",
847 .access = SNDRV_CTL_ELEM_ACCESS_READ |
848 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
849 .info = mchp_spdifrx_boolean_info,
850 .get = mchp_spdifrx_ulock_get,
854 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
855 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE)"Bad Format",
856 .access = SNDRV_CTL_ELEM_ACCESS_READ |
857 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
858 .info = mchp_spdifrx_boolean_info,
859 .get = mchp_spdifrx_badf_get,
863 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
864 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) "Signal",
865 .access = SNDRV_CTL_ELEM_ACCESS_READ |
866 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
867 .info = mchp_spdifrx_boolean_info,
868 .get = mchp_spdifrx_signal_get,
872 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
873 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) "Rate",
874 .access = SNDRV_CTL_ELEM_ACCESS_READ |
875 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
876 .info = mchp_spdifrx_rate_info,
877 .get = mchp_spdifrx_rate_get,
881 static int mchp_spdifrx_dai_probe(struct snd_soc_dai *dai)
883 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
884 struct mchp_spdifrx_mixer_control *ctrl = &dev->control;
888 err = clk_prepare_enable(dev->pclk);
891 "failed to enable the peripheral clock: %d\n", err);
895 snd_soc_dai_init_dma_data(dai, NULL, &dev->capture);
897 /* Software reset the IP */
898 regmap_write(dev->regmap, SPDIFRX_CR, SPDIFRX_CR_SWRST);
900 /* Default configuration */
901 regmap_write(dev->regmap, SPDIFRX_MR,
902 SPDIFRX_MR_VBMODE_DISCARD_IF_VB1 |
903 SPDIFRX_MR_SBMODE_DISCARD |
904 SPDIFRX_MR_AUTORST_NOACTION |
905 SPDIFRX_MR_PACK_DISABLED);
907 for (ch = 0; ch < SPDIFRX_CHANNELS; ch++) {
908 init_completion(&ctrl->ch_stat[ch].done);
909 init_completion(&ctrl->user_data[ch].done);
913 snd_soc_add_dai_controls(dai, mchp_spdifrx_ctrls,
914 ARRAY_SIZE(mchp_spdifrx_ctrls));
919 static int mchp_spdifrx_dai_remove(struct snd_soc_dai *dai)
921 struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai);
923 /* Disable interrupts */
924 regmap_write(dev->regmap, SPDIFRX_IDR, GENMASK(14, 0));
926 clk_disable_unprepare(dev->pclk);
931 static struct snd_soc_dai_driver mchp_spdifrx_dai = {
932 .name = "mchp-spdifrx",
933 .probe = mchp_spdifrx_dai_probe,
934 .remove = mchp_spdifrx_dai_remove,
936 .stream_name = "S/PDIF Capture",
937 .channels_min = SPDIFRX_CHANNELS,
938 .channels_max = SPDIFRX_CHANNELS,
939 .rates = MCHP_SPDIF_RATES,
940 .formats = MCHP_SPDIF_FORMATS,
942 .ops = &mchp_spdifrx_dai_ops,
945 static const struct snd_soc_component_driver mchp_spdifrx_component = {
946 .name = "mchp-spdifrx",
947 .legacy_dai_naming = 1,
950 static const struct of_device_id mchp_spdifrx_dt_ids[] = {
952 .compatible = "microchip,sama7g5-spdifrx",
956 MODULE_DEVICE_TABLE(of, mchp_spdifrx_dt_ids);
958 static int mchp_spdifrx_probe(struct platform_device *pdev)
960 struct mchp_spdifrx_dev *dev;
961 struct resource *mem;
962 struct regmap *regmap;
968 /* Get memory for driver data. */
969 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
973 /* Map I/O registers. */
974 base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
976 return PTR_ERR(base);
978 regmap = devm_regmap_init_mmio(&pdev->dev, base,
979 &mchp_spdifrx_regmap_config);
981 return PTR_ERR(regmap);
984 irq = platform_get_irq(pdev, 0);
988 err = devm_request_irq(&pdev->dev, irq, mchp_spdif_interrupt, 0,
989 dev_name(&pdev->dev), dev);
993 /* Get the peripheral clock */
994 dev->pclk = devm_clk_get(&pdev->dev, "pclk");
995 if (IS_ERR(dev->pclk)) {
996 err = PTR_ERR(dev->pclk);
997 dev_err(&pdev->dev, "failed to get the peripheral clock: %d\n",
1002 /* Get the generated clock */
1003 dev->gclk = devm_clk_get(&pdev->dev, "gclk");
1004 if (IS_ERR(dev->gclk)) {
1005 err = PTR_ERR(dev->gclk);
1007 "failed to get the PMC generated clock: %d\n", err);
1012 * Signal control need a valid rate on gclk. hw_params() configures
1013 * it propertly but requesting signal before any hw_params() has been
1014 * called lead to invalid value returned for signal. Thus, configure
1015 * gclk at a valid rate, here, in initialization, to simplify the
1018 clk_set_min_rate(dev->gclk, 48000 * SPDIFRX_GCLK_RATIO_MIN + 1);
1020 mutex_init(&dev->mlock);
1022 dev->dev = &pdev->dev;
1023 dev->regmap = regmap;
1024 platform_set_drvdata(pdev, dev);
1026 dev->capture.addr = (dma_addr_t)mem->start + SPDIFRX_RHR;
1027 dev->capture.maxburst = 1;
1029 err = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1031 dev_err(&pdev->dev, "failed to register PCM: %d\n", err);
1035 err = devm_snd_soc_register_component(&pdev->dev,
1036 &mchp_spdifrx_component,
1037 &mchp_spdifrx_dai, 1);
1039 dev_err(&pdev->dev, "fail to register dai\n");
1043 regmap_read(regmap, SPDIFRX_VERSION, &vers);
1044 dev_info(&pdev->dev, "hw version: %#lx\n", vers & SPDIFRX_VERSION_MASK);
1049 static struct platform_driver mchp_spdifrx_driver = {
1050 .probe = mchp_spdifrx_probe,
1052 .name = "mchp_spdifrx",
1053 .of_match_table = of_match_ptr(mchp_spdifrx_dt_ids),
1057 module_platform_driver(mchp_spdifrx_driver);
1059 MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
1060 MODULE_DESCRIPTION("Microchip S/PDIF RX Controller Driver");
1061 MODULE_LICENSE("GPL v2");