1 // SPDX-License-Identifier: GPL-2.0-only
3 * IMG SPDIF input controller driver
5 * Copyright (C) 2015 Imagination Technologies Ltd.
7 * Author: Damien Horsley <Damien.Horsley@imgtec.com>
10 #include <linux/clk.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
19 #include <sound/core.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/initval.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
26 #define IMG_SPDIF_IN_RX_FIFO_OFFSET 0
28 #define IMG_SPDIF_IN_CTL 0x4
29 #define IMG_SPDIF_IN_CTL_LOCKLO_MASK 0xff
30 #define IMG_SPDIF_IN_CTL_LOCKLO_SHIFT 0
31 #define IMG_SPDIF_IN_CTL_LOCKHI_MASK 0xff00
32 #define IMG_SPDIF_IN_CTL_LOCKHI_SHIFT 8
33 #define IMG_SPDIF_IN_CTL_TRK_MASK 0xff0000
34 #define IMG_SPDIF_IN_CTL_TRK_SHIFT 16
35 #define IMG_SPDIF_IN_CTL_SRD_MASK 0x70000000
36 #define IMG_SPDIF_IN_CTL_SRD_SHIFT 28
37 #define IMG_SPDIF_IN_CTL_SRT_MASK BIT(31)
39 #define IMG_SPDIF_IN_STATUS 0x8
40 #define IMG_SPDIF_IN_STATUS_SAM_MASK 0x7000
41 #define IMG_SPDIF_IN_STATUS_SAM_SHIFT 12
42 #define IMG_SPDIF_IN_STATUS_LOCK_MASK BIT(15)
43 #define IMG_SPDIF_IN_STATUS_LOCK_SHIFT 15
45 #define IMG_SPDIF_IN_CLKGEN 0x1c
46 #define IMG_SPDIF_IN_CLKGEN_NOM_MASK 0x3ff
47 #define IMG_SPDIF_IN_CLKGEN_NOM_SHIFT 0
48 #define IMG_SPDIF_IN_CLKGEN_HLD_MASK 0x3ff0000
49 #define IMG_SPDIF_IN_CLKGEN_HLD_SHIFT 16
51 #define IMG_SPDIF_IN_CSL 0x20
53 #define IMG_SPDIF_IN_CSH 0x24
54 #define IMG_SPDIF_IN_CSH_MASK 0xff
55 #define IMG_SPDIF_IN_CSH_SHIFT 0
57 #define IMG_SPDIF_IN_SOFT_RESET 0x28
58 #define IMG_SPDIF_IN_SOFT_RESET_MASK BIT(0)
60 #define IMG_SPDIF_IN_ACLKGEN_START 0x2c
61 #define IMG_SPDIF_IN_ACLKGEN_NOM_MASK 0x3ff
62 #define IMG_SPDIF_IN_ACLKGEN_NOM_SHIFT 0
63 #define IMG_SPDIF_IN_ACLKGEN_HLD_MASK 0xffc00
64 #define IMG_SPDIF_IN_ACLKGEN_HLD_SHIFT 10
65 #define IMG_SPDIF_IN_ACLKGEN_TRK_MASK 0xff00000
66 #define IMG_SPDIF_IN_ACLKGEN_TRK_SHIFT 20
68 #define IMG_SPDIF_IN_NUM_ACLKGEN 4
74 struct snd_dmaengine_dai_dma_data dma_data;
80 unsigned int single_freq;
81 unsigned int multi_freqs[IMG_SPDIF_IN_NUM_ACLKGEN];
86 /* Write-only registers */
87 unsigned int aclkgen_regs[IMG_SPDIF_IN_NUM_ACLKGEN];
90 static int img_spdif_in_runtime_suspend(struct device *dev)
92 struct img_spdif_in *spdif = dev_get_drvdata(dev);
94 clk_disable_unprepare(spdif->clk_sys);
99 static int img_spdif_in_runtime_resume(struct device *dev)
101 struct img_spdif_in *spdif = dev_get_drvdata(dev);
104 ret = clk_prepare_enable(spdif->clk_sys);
106 dev_err(dev, "Unable to enable sys clock\n");
113 static inline void img_spdif_in_writel(struct img_spdif_in *spdif,
116 writel(val, spdif->base + reg);
119 static inline u32 img_spdif_in_readl(struct img_spdif_in *spdif, u32 reg)
121 return readl(spdif->base + reg);
124 static inline void img_spdif_in_aclkgen_writel(struct img_spdif_in *spdif,
127 img_spdif_in_writel(spdif, spdif->aclkgen_regs[index],
128 IMG_SPDIF_IN_ACLKGEN_START + (index * 0x4));
131 static int img_spdif_in_check_max_rate(struct img_spdif_in *spdif,
132 unsigned int sample_rate, unsigned long *actual_freq)
134 unsigned long min_freq, freq_t;
136 /* Clock rate must be at least 24x the bit rate */
137 min_freq = sample_rate * 2 * 32 * 24;
139 freq_t = clk_get_rate(spdif->clk_sys);
141 if (freq_t < min_freq)
144 *actual_freq = freq_t;
149 static int img_spdif_in_do_clkgen_calc(unsigned int rate, unsigned int *pnom,
150 unsigned int *phld, unsigned long clk_rate)
152 unsigned int ori, nom, hld;
155 * Calculate oversampling ratio, nominal phase increment and hold
156 * increment for the given rate / frequency
162 ori = clk_rate / (rate * 64);
167 nom = (4096 / ori) + 1;
169 hld = 4096 - (--nom * (ori - 1));
178 static int img_spdif_in_do_clkgen_single(struct img_spdif_in *spdif,
181 unsigned int nom, hld;
182 unsigned long flags, clk_rate;
186 ret = img_spdif_in_check_max_rate(spdif, rate, &clk_rate);
190 ret = img_spdif_in_do_clkgen_calc(rate, &nom, &hld, clk_rate);
194 reg = (nom << IMG_SPDIF_IN_CLKGEN_NOM_SHIFT) &
195 IMG_SPDIF_IN_CLKGEN_NOM_MASK;
196 reg |= (hld << IMG_SPDIF_IN_CLKGEN_HLD_SHIFT) &
197 IMG_SPDIF_IN_CLKGEN_HLD_MASK;
199 spin_lock_irqsave(&spdif->lock, flags);
202 spin_unlock_irqrestore(&spdif->lock, flags);
206 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CLKGEN);
208 spdif->single_freq = rate;
210 spin_unlock_irqrestore(&spdif->lock, flags);
215 static int img_spdif_in_do_clkgen_multi(struct img_spdif_in *spdif,
216 unsigned int multi_freqs[])
218 unsigned int nom, hld, rate, max_rate = 0;
219 unsigned long flags, clk_rate;
221 u32 reg, trk_reg, temp_regs[IMG_SPDIF_IN_NUM_ACLKGEN];
223 for (i = 0; i < IMG_SPDIF_IN_NUM_ACLKGEN; i++)
224 if (multi_freqs[i] > max_rate)
225 max_rate = multi_freqs[i];
227 ret = img_spdif_in_check_max_rate(spdif, max_rate, &clk_rate);
231 for (i = 0; i < IMG_SPDIF_IN_NUM_ACLKGEN; i++) {
232 rate = multi_freqs[i];
234 ret = img_spdif_in_do_clkgen_calc(rate, &nom, &hld, clk_rate);
238 reg = (nom << IMG_SPDIF_IN_ACLKGEN_NOM_SHIFT) &
239 IMG_SPDIF_IN_ACLKGEN_NOM_MASK;
240 reg |= (hld << IMG_SPDIF_IN_ACLKGEN_HLD_SHIFT) &
241 IMG_SPDIF_IN_ACLKGEN_HLD_MASK;
245 spin_lock_irqsave(&spdif->lock, flags);
248 spin_unlock_irqrestore(&spdif->lock, flags);
252 trk_reg = spdif->trk << IMG_SPDIF_IN_ACLKGEN_TRK_SHIFT;
254 for (i = 0; i < IMG_SPDIF_IN_NUM_ACLKGEN; i++) {
255 spdif->aclkgen_regs[i] = temp_regs[i] | trk_reg;
256 img_spdif_in_aclkgen_writel(spdif, i);
259 spdif->multi_freq = true;
260 spdif->multi_freqs[0] = multi_freqs[0];
261 spdif->multi_freqs[1] = multi_freqs[1];
262 spdif->multi_freqs[2] = multi_freqs[2];
263 spdif->multi_freqs[3] = multi_freqs[3];
265 spin_unlock_irqrestore(&spdif->lock, flags);
270 static int img_spdif_in_iec958_info(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_info *uinfo)
273 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
279 static int img_spdif_in_get_status_mask(struct snd_kcontrol *kcontrol,
280 struct snd_ctl_elem_value *ucontrol)
282 ucontrol->value.iec958.status[0] = 0xff;
283 ucontrol->value.iec958.status[1] = 0xff;
284 ucontrol->value.iec958.status[2] = 0xff;
285 ucontrol->value.iec958.status[3] = 0xff;
286 ucontrol->value.iec958.status[4] = 0xff;
291 static int img_spdif_in_get_status(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_value *ucontrol)
294 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
295 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
298 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CSL);
299 ucontrol->value.iec958.status[0] = reg & 0xff;
300 ucontrol->value.iec958.status[1] = (reg >> 8) & 0xff;
301 ucontrol->value.iec958.status[2] = (reg >> 16) & 0xff;
302 ucontrol->value.iec958.status[3] = (reg >> 24) & 0xff;
303 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CSH);
304 ucontrol->value.iec958.status[4] = (reg & IMG_SPDIF_IN_CSH_MASK)
305 >> IMG_SPDIF_IN_CSH_SHIFT;
310 static int img_spdif_in_info_multi_freq(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_info *uinfo)
313 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
314 uinfo->count = IMG_SPDIF_IN_NUM_ACLKGEN;
315 uinfo->value.integer.min = 0;
316 uinfo->value.integer.max = LONG_MAX;
321 static int img_spdif_in_get_multi_freq(struct snd_kcontrol *kcontrol,
322 struct snd_ctl_elem_value *ucontrol)
324 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
325 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
328 spin_lock_irqsave(&spdif->lock, flags);
329 if (spdif->multi_freq) {
330 ucontrol->value.integer.value[0] = spdif->multi_freqs[0];
331 ucontrol->value.integer.value[1] = spdif->multi_freqs[1];
332 ucontrol->value.integer.value[2] = spdif->multi_freqs[2];
333 ucontrol->value.integer.value[3] = spdif->multi_freqs[3];
335 ucontrol->value.integer.value[0] = 0;
336 ucontrol->value.integer.value[1] = 0;
337 ucontrol->value.integer.value[2] = 0;
338 ucontrol->value.integer.value[3] = 0;
340 spin_unlock_irqrestore(&spdif->lock, flags);
345 static int img_spdif_in_set_multi_freq(struct snd_kcontrol *kcontrol,
346 struct snd_ctl_elem_value *ucontrol)
348 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
349 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
350 unsigned int multi_freqs[IMG_SPDIF_IN_NUM_ACLKGEN];
354 if ((ucontrol->value.integer.value[0] == 0) &&
355 (ucontrol->value.integer.value[1] == 0) &&
356 (ucontrol->value.integer.value[2] == 0) &&
357 (ucontrol->value.integer.value[3] == 0)) {
360 multi_freqs[0] = ucontrol->value.integer.value[0];
361 multi_freqs[1] = ucontrol->value.integer.value[1];
362 multi_freqs[2] = ucontrol->value.integer.value[2];
363 multi_freqs[3] = ucontrol->value.integer.value[3];
368 return img_spdif_in_do_clkgen_multi(spdif, multi_freqs);
370 spin_lock_irqsave(&spdif->lock, flags);
373 spin_unlock_irqrestore(&spdif->lock, flags);
377 spdif->multi_freq = false;
379 spin_unlock_irqrestore(&spdif->lock, flags);
384 static int img_spdif_in_info_lock_freq(struct snd_kcontrol *kcontrol,
385 struct snd_ctl_elem_info *uinfo)
387 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
389 uinfo->value.integer.min = 0;
390 uinfo->value.integer.max = LONG_MAX;
395 static int img_spdif_in_get_lock_freq(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *uc)
398 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
399 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
404 spin_lock_irqsave(&spdif->lock, flags);
406 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_STATUS);
407 if (reg & IMG_SPDIF_IN_STATUS_LOCK_MASK) {
408 if (spdif->multi_freq) {
409 i = ((reg & IMG_SPDIF_IN_STATUS_SAM_MASK) >>
410 IMG_SPDIF_IN_STATUS_SAM_SHIFT) - 1;
411 uc->value.integer.value[0] = spdif->multi_freqs[i];
413 uc->value.integer.value[0] = spdif->single_freq;
416 uc->value.integer.value[0] = 0;
419 spin_unlock_irqrestore(&spdif->lock, flags);
424 static int img_spdif_in_info_trk(struct snd_kcontrol *kcontrol,
425 struct snd_ctl_elem_info *uinfo)
427 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
429 uinfo->value.integer.min = 0;
430 uinfo->value.integer.max = 255;
435 static int img_spdif_in_get_trk(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol)
438 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
439 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
441 ucontrol->value.integer.value[0] = spdif->trk;
446 static int img_spdif_in_set_trk(struct snd_kcontrol *kcontrol,
447 struct snd_ctl_elem_value *ucontrol)
449 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
450 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
455 spin_lock_irqsave(&spdif->lock, flags);
458 spin_unlock_irqrestore(&spdif->lock, flags);
462 spdif->trk = ucontrol->value.integer.value[0];
464 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
465 reg &= ~IMG_SPDIF_IN_CTL_TRK_MASK;
466 reg |= spdif->trk << IMG_SPDIF_IN_CTL_TRK_SHIFT;
467 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
469 for (i = 0; i < IMG_SPDIF_IN_NUM_ACLKGEN; i++) {
470 spdif->aclkgen_regs[i] = (spdif->aclkgen_regs[i] &
471 ~IMG_SPDIF_IN_ACLKGEN_TRK_MASK) |
472 (spdif->trk << IMG_SPDIF_IN_ACLKGEN_TRK_SHIFT);
474 img_spdif_in_aclkgen_writel(spdif, i);
477 spin_unlock_irqrestore(&spdif->lock, flags);
482 static int img_spdif_in_info_lock(struct snd_kcontrol *kcontrol,
483 struct snd_ctl_elem_info *uinfo)
485 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
487 uinfo->value.integer.min = -128;
488 uinfo->value.integer.max = 127;
493 static int img_spdif_in_get_lock_acquire(struct snd_kcontrol *kcontrol,
494 struct snd_ctl_elem_value *ucontrol)
496 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
497 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
499 ucontrol->value.integer.value[0] = spdif->lock_acquire;
504 static int img_spdif_in_set_lock_acquire(struct snd_kcontrol *kcontrol,
505 struct snd_ctl_elem_value *ucontrol)
507 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
508 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
512 spin_lock_irqsave(&spdif->lock, flags);
515 spin_unlock_irqrestore(&spdif->lock, flags);
519 spdif->lock_acquire = ucontrol->value.integer.value[0];
521 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
522 reg &= ~IMG_SPDIF_IN_CTL_LOCKHI_MASK;
523 reg |= (spdif->lock_acquire << IMG_SPDIF_IN_CTL_LOCKHI_SHIFT) &
524 IMG_SPDIF_IN_CTL_LOCKHI_MASK;
525 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
527 spin_unlock_irqrestore(&spdif->lock, flags);
532 static int img_spdif_in_get_lock_release(struct snd_kcontrol *kcontrol,
533 struct snd_ctl_elem_value *ucontrol)
535 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
536 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
538 ucontrol->value.integer.value[0] = spdif->lock_release;
543 static int img_spdif_in_set_lock_release(struct snd_kcontrol *kcontrol,
544 struct snd_ctl_elem_value *ucontrol)
546 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
547 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(cpu_dai);
551 spin_lock_irqsave(&spdif->lock, flags);
554 spin_unlock_irqrestore(&spdif->lock, flags);
558 spdif->lock_release = ucontrol->value.integer.value[0];
560 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
561 reg &= ~IMG_SPDIF_IN_CTL_LOCKLO_MASK;
562 reg |= (spdif->lock_release << IMG_SPDIF_IN_CTL_LOCKLO_SHIFT) &
563 IMG_SPDIF_IN_CTL_LOCKLO_MASK;
564 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
566 spin_unlock_irqrestore(&spdif->lock, flags);
571 static struct snd_kcontrol_new img_spdif_in_controls[] = {
573 .access = SNDRV_CTL_ELEM_ACCESS_READ,
574 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
575 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
576 .info = img_spdif_in_iec958_info,
577 .get = img_spdif_in_get_status_mask
580 .access = SNDRV_CTL_ELEM_ACCESS_READ |
581 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
582 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
583 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
584 .info = img_spdif_in_iec958_info,
585 .get = img_spdif_in_get_status
588 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
589 .name = "SPDIF In Multi Frequency Acquire",
590 .info = img_spdif_in_info_multi_freq,
591 .get = img_spdif_in_get_multi_freq,
592 .put = img_spdif_in_set_multi_freq
595 .access = SNDRV_CTL_ELEM_ACCESS_READ |
596 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
597 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
598 .name = "SPDIF In Lock Frequency",
599 .info = img_spdif_in_info_lock_freq,
600 .get = img_spdif_in_get_lock_freq
603 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
604 .name = "SPDIF In Lock TRK",
605 .info = img_spdif_in_info_trk,
606 .get = img_spdif_in_get_trk,
607 .put = img_spdif_in_set_trk
610 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
611 .name = "SPDIF In Lock Acquire Threshold",
612 .info = img_spdif_in_info_lock,
613 .get = img_spdif_in_get_lock_acquire,
614 .put = img_spdif_in_set_lock_acquire
617 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
618 .name = "SPDIF In Lock Release Threshold",
619 .info = img_spdif_in_info_lock,
620 .get = img_spdif_in_get_lock_release,
621 .put = img_spdif_in_set_lock_release
625 static int img_spdif_in_trigger(struct snd_pcm_substream *substream, int cmd,
626 struct snd_soc_dai *dai)
629 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(dai);
633 spin_lock_irqsave(&spdif->lock, flags);
636 case SNDRV_PCM_TRIGGER_START:
637 case SNDRV_PCM_TRIGGER_RESUME:
638 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
639 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
640 if (spdif->multi_freq)
641 reg &= ~IMG_SPDIF_IN_CTL_SRD_MASK;
643 reg |= (1UL << IMG_SPDIF_IN_CTL_SRD_SHIFT);
644 reg |= IMG_SPDIF_IN_CTL_SRT_MASK;
645 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
646 spdif->active = true;
648 case SNDRV_PCM_TRIGGER_STOP:
649 case SNDRV_PCM_TRIGGER_SUSPEND:
650 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
651 reg = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
652 reg &= ~IMG_SPDIF_IN_CTL_SRT_MASK;
653 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
654 spdif->active = false;
660 spin_unlock_irqrestore(&spdif->lock, flags);
665 static int img_spdif_in_hw_params(struct snd_pcm_substream *substream,
666 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
668 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(dai);
669 unsigned int rate, channels;
670 snd_pcm_format_t format;
672 rate = params_rate(params);
673 channels = params_channels(params);
674 format = params_format(params);
676 if (format != SNDRV_PCM_FORMAT_S32_LE)
682 return img_spdif_in_do_clkgen_single(spdif, rate);
685 static const struct snd_soc_dai_ops img_spdif_in_dai_ops = {
686 .trigger = img_spdif_in_trigger,
687 .hw_params = img_spdif_in_hw_params
690 static int img_spdif_in_dai_probe(struct snd_soc_dai *dai)
692 struct img_spdif_in *spdif = snd_soc_dai_get_drvdata(dai);
694 snd_soc_dai_init_dma_data(dai, NULL, &spdif->dma_data);
696 snd_soc_add_dai_controls(dai, img_spdif_in_controls,
697 ARRAY_SIZE(img_spdif_in_controls));
702 static struct snd_soc_dai_driver img_spdif_in_dai = {
703 .probe = img_spdif_in_dai_probe,
707 .rates = SNDRV_PCM_RATE_8000_192000,
708 .formats = SNDRV_PCM_FMTBIT_S32_LE
710 .ops = &img_spdif_in_dai_ops
713 static const struct snd_soc_component_driver img_spdif_in_component = {
714 .name = "img-spdif-in"
717 static int img_spdif_in_probe(struct platform_device *pdev)
719 struct img_spdif_in *spdif;
720 struct resource *res;
723 struct reset_control *rst;
725 struct device *dev = &pdev->dev;
727 spdif = devm_kzalloc(&pdev->dev, sizeof(*spdif), GFP_KERNEL);
731 platform_set_drvdata(pdev, spdif);
733 spdif->dev = &pdev->dev;
735 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
736 base = devm_ioremap_resource(&pdev->dev, res);
738 return PTR_ERR(base);
742 spdif->clk_sys = devm_clk_get(dev, "sys");
743 if (IS_ERR(spdif->clk_sys)) {
744 if (PTR_ERR(spdif->clk_sys) != -EPROBE_DEFER)
745 dev_err(dev, "Failed to acquire clock 'sys'\n");
746 return PTR_ERR(spdif->clk_sys);
749 pm_runtime_enable(&pdev->dev);
750 if (!pm_runtime_enabled(&pdev->dev)) {
751 ret = img_spdif_in_runtime_resume(&pdev->dev);
755 ret = pm_runtime_get_sync(&pdev->dev);
757 pm_runtime_put_noidle(&pdev->dev);
761 rst = devm_reset_control_get_exclusive(&pdev->dev, "rst");
763 if (PTR_ERR(rst) == -EPROBE_DEFER) {
767 dev_dbg(dev, "No top level reset found\n");
768 img_spdif_in_writel(spdif, IMG_SPDIF_IN_SOFT_RESET_MASK,
769 IMG_SPDIF_IN_SOFT_RESET);
770 img_spdif_in_writel(spdif, 0, IMG_SPDIF_IN_SOFT_RESET);
772 reset_control_assert(rst);
773 reset_control_deassert(rst);
776 spin_lock_init(&spdif->lock);
778 spdif->dma_data.addr = res->start + IMG_SPDIF_IN_RX_FIFO_OFFSET;
779 spdif->dma_data.addr_width = 4;
780 spdif->dma_data.maxburst = 4;
782 spdif->lock_acquire = 4;
783 spdif->lock_release = -128;
785 reg = (spdif->lock_acquire << IMG_SPDIF_IN_CTL_LOCKHI_SHIFT) &
786 IMG_SPDIF_IN_CTL_LOCKHI_MASK;
787 reg |= (spdif->lock_release << IMG_SPDIF_IN_CTL_LOCKLO_SHIFT) &
788 IMG_SPDIF_IN_CTL_LOCKLO_MASK;
789 reg |= (spdif->trk << IMG_SPDIF_IN_CTL_TRK_SHIFT) &
790 IMG_SPDIF_IN_CTL_TRK_MASK;
791 img_spdif_in_writel(spdif, reg, IMG_SPDIF_IN_CTL);
793 pm_runtime_put(&pdev->dev);
795 ret = devm_snd_soc_register_component(&pdev->dev,
796 &img_spdif_in_component, &img_spdif_in_dai, 1);
800 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
807 pm_runtime_put(&pdev->dev);
809 if (!pm_runtime_enabled(&pdev->dev))
810 img_spdif_in_runtime_suspend(&pdev->dev);
812 pm_runtime_disable(&pdev->dev);
817 static int img_spdif_in_dev_remove(struct platform_device *pdev)
819 pm_runtime_disable(&pdev->dev);
820 if (!pm_runtime_status_suspended(&pdev->dev))
821 img_spdif_in_runtime_suspend(&pdev->dev);
826 #ifdef CONFIG_PM_SLEEP
827 static int img_spdif_in_suspend(struct device *dev)
829 struct img_spdif_in *spdif = dev_get_drvdata(dev);
832 if (pm_runtime_status_suspended(dev)) {
833 ret = img_spdif_in_runtime_resume(dev);
838 spdif->suspend_clkgen = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CLKGEN);
839 spdif->suspend_ctl = img_spdif_in_readl(spdif, IMG_SPDIF_IN_CTL);
841 img_spdif_in_runtime_suspend(dev);
846 static int img_spdif_in_resume(struct device *dev)
848 struct img_spdif_in *spdif = dev_get_drvdata(dev);
851 ret = img_spdif_in_runtime_resume(dev);
855 for (i = 0; i < IMG_SPDIF_IN_NUM_ACLKGEN; i++)
856 img_spdif_in_aclkgen_writel(spdif, i);
858 img_spdif_in_writel(spdif, spdif->suspend_clkgen, IMG_SPDIF_IN_CLKGEN);
859 img_spdif_in_writel(spdif, spdif->suspend_ctl, IMG_SPDIF_IN_CTL);
861 if (pm_runtime_status_suspended(dev))
862 img_spdif_in_runtime_suspend(dev);
868 static const struct of_device_id img_spdif_in_of_match[] = {
869 { .compatible = "img,spdif-in" },
872 MODULE_DEVICE_TABLE(of, img_spdif_in_of_match);
874 static const struct dev_pm_ops img_spdif_in_pm_ops = {
875 SET_RUNTIME_PM_OPS(img_spdif_in_runtime_suspend,
876 img_spdif_in_runtime_resume, NULL)
877 SET_SYSTEM_SLEEP_PM_OPS(img_spdif_in_suspend, img_spdif_in_resume)
880 static struct platform_driver img_spdif_in_driver = {
882 .name = "img-spdif-in",
883 .of_match_table = img_spdif_in_of_match,
884 .pm = &img_spdif_in_pm_ops
886 .probe = img_spdif_in_probe,
887 .remove = img_spdif_in_dev_remove
889 module_platform_driver(img_spdif_in_driver);
891 MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
892 MODULE_DESCRIPTION("IMG SPDIF Input driver");
893 MODULE_LICENSE("GPL v2");