1 // SPDX-License-Identifier: GPL-2.0
3 // Socionext UniPhier AIO ALSA CPU DAI driver.
5 // Copyright (c) 2016-2018 Socionext Inc.
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/reset.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
23 static bool is_valid_pll(struct uniphier_aio_chip *chip, int pll_id)
25 struct device *dev = &chip->pdev->dev;
27 if (pll_id < 0 || chip->num_plls <= pll_id) {
28 dev_err(dev, "PLL(%d) is not supported\n", pll_id);
32 return chip->plls[pll_id].enable;
36 * find_volume - find volume supported HW port by HW port number
37 * @chip: the AIO chip pointer
38 * @oport_hw: HW port number, one of AUD_HW_XXXX
40 * Find AIO device from device list by HW port number. Volume feature is
41 * available only in Output and PCM ports, this limitation comes from HW
44 * Return: The pointer of AIO substream if successful, otherwise NULL on error.
46 static struct uniphier_aio_sub *find_volume(struct uniphier_aio_chip *chip,
51 for (i = 0; i < chip->num_aios; i++) {
52 struct uniphier_aio_sub *sub = &chip->aios[i].sub[0];
57 if (sub->swm->oport.hw == oport_hw)
64 static bool match_spec(const struct uniphier_aio_spec *spec,
65 const char *name, int dir)
67 if (dir == SNDRV_PCM_STREAM_PLAYBACK &&
68 spec->swm.dir != PORT_DIR_OUTPUT) {
72 if (dir == SNDRV_PCM_STREAM_CAPTURE &&
73 spec->swm.dir != PORT_DIR_INPUT) {
77 if (spec->name && strcmp(spec->name, name) == 0)
80 if (spec->gname && strcmp(spec->gname, name) == 0)
87 * find_spec - find HW specification info by name
88 * @aio: the AIO device pointer
89 * @name: name of device
90 * @direction: the direction of substream, SNDRV_PCM_STREAM_*
92 * Find hardware specification information from list by device name. This
93 * information is used for telling the difference of SoCs to driver.
95 * Specification list is array of 'struct uniphier_aio_spec' which is defined
96 * in each drivers (see: aio-i2s.c).
98 * Return: The pointer of hardware specification of AIO if successful,
99 * otherwise NULL on error.
101 static const struct uniphier_aio_spec *find_spec(struct uniphier_aio *aio,
105 const struct uniphier_aio_chip_spec *chip_spec = aio->chip->chip_spec;
108 for (i = 0; i < chip_spec->num_specs; i++) {
109 const struct uniphier_aio_spec *spec = &chip_spec->specs[i];
111 if (match_spec(spec, name, direction))
119 * find_divider - find clock divider by frequency
120 * @aio: the AIO device pointer
121 * @pll_id: PLL ID, should be AUD_PLL_XX
122 * @freq: required frequency
124 * Find suitable clock divider by frequency.
126 * Return: The ID of PLL if successful, otherwise negative error value.
128 static int find_divider(struct uniphier_aio *aio, int pll_id, unsigned int freq)
130 struct uniphier_aio_pll *pll;
131 static const int mul[] = { 1, 1, 1, 2, };
132 static const int div[] = { 2, 3, 1, 3, };
135 if (!is_valid_pll(aio->chip, pll_id))
138 pll = &aio->chip->plls[pll_id];
139 for (i = 0; i < ARRAY_SIZE(mul); i++)
140 if (pll->freq * mul[i] / div[i] == freq)
146 static int uniphier_aio_set_sysclk(struct snd_soc_dai *dai, int clk_id,
147 unsigned int freq, int dir)
149 struct uniphier_aio *aio = uniphier_priv(dai);
150 struct device *dev = &aio->chip->pdev->dev;
151 bool pll_auto = false;
178 pll_id = AUD_PLL_APLL;
181 pll_id = AUD_PLL_RX0;
184 pll_id = AUD_PLL_USB0;
187 pll_id = AUD_PLL_HSC0;
190 dev_err(dev, "Sysclk(%d) is not supported\n", clk_id);
195 for (pll_id = 0; pll_id < aio->chip->num_plls; pll_id++) {
196 div_id = find_divider(aio, pll_id, freq);
198 aio->plldiv = div_id;
202 if (pll_id == aio->chip->num_plls) {
203 dev_err(dev, "Sysclk frequency is not supported(%d)\n",
209 if (dir == SND_SOC_CLOCK_OUT)
210 aio->pll_out = pll_id;
212 aio->pll_in = pll_id;
217 static int uniphier_aio_set_pll(struct snd_soc_dai *dai, int pll_id,
218 int source, unsigned int freq_in,
219 unsigned int freq_out)
221 struct uniphier_aio *aio = uniphier_priv(dai);
224 if (!is_valid_pll(aio->chip, pll_id))
227 ret = aio_chip_set_pll(aio->chip, pll_id, freq_out);
234 static int uniphier_aio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
236 struct uniphier_aio *aio = uniphier_priv(dai);
237 struct device *dev = &aio->chip->pdev->dev;
239 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
240 case SND_SOC_DAIFMT_LEFT_J:
241 case SND_SOC_DAIFMT_RIGHT_J:
242 case SND_SOC_DAIFMT_I2S:
243 aio->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
246 dev_err(dev, "Format is not supported(%d)\n",
247 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
254 static int uniphier_aio_startup(struct snd_pcm_substream *substream,
255 struct snd_soc_dai *dai)
257 struct uniphier_aio *aio = uniphier_priv(dai);
258 struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
260 sub->substream = substream;
261 sub->pass_through = 0;
262 sub->use_mmap = true;
264 return aio_init(sub);
267 static void uniphier_aio_shutdown(struct snd_pcm_substream *substream,
268 struct snd_soc_dai *dai)
270 struct uniphier_aio *aio = uniphier_priv(dai);
271 struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
273 sub->substream = NULL;
276 static int uniphier_aio_hw_params(struct snd_pcm_substream *substream,
277 struct snd_pcm_hw_params *params,
278 struct snd_soc_dai *dai)
280 struct uniphier_aio *aio = uniphier_priv(dai);
281 struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
282 struct device *dev = &aio->chip->pdev->dev;
285 switch (params_rate(params)) {
296 dev_err(dev, "Rate is not supported(%d)\n",
297 params_rate(params));
300 ret = snd_soc_dai_set_sysclk(dai, AUD_CLK_A,
301 freq, SND_SOC_CLOCK_OUT);
305 sub->params = *params;
309 aio_port_set_volume(sub, sub->vol);
315 static int uniphier_aio_hw_free(struct snd_pcm_substream *substream,
316 struct snd_soc_dai *dai)
318 struct uniphier_aio *aio = uniphier_priv(dai);
319 struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
326 static int uniphier_aio_prepare(struct snd_pcm_substream *substream,
327 struct snd_soc_dai *dai)
329 struct uniphier_aio *aio = uniphier_priv(dai);
330 struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
333 ret = aio_port_set_param(sub, sub->pass_through, &sub->params);
336 ret = aio_src_set_param(sub, &sub->params);
339 aio_port_set_enable(sub, 1);
341 ret = aio_if_set_param(sub, sub->pass_through);
345 if (sub->swm->type == PORT_TYPE_CONV) {
346 ret = aio_srcif_set_param(sub);
349 ret = aio_srcch_set_param(sub);
352 aio_srcch_set_enable(sub, 1);
358 const struct snd_soc_dai_ops uniphier_aio_i2s_ops = {
359 .set_sysclk = uniphier_aio_set_sysclk,
360 .set_pll = uniphier_aio_set_pll,
361 .set_fmt = uniphier_aio_set_fmt,
362 .startup = uniphier_aio_startup,
363 .shutdown = uniphier_aio_shutdown,
364 .hw_params = uniphier_aio_hw_params,
365 .hw_free = uniphier_aio_hw_free,
366 .prepare = uniphier_aio_prepare,
368 EXPORT_SYMBOL_GPL(uniphier_aio_i2s_ops);
370 const struct snd_soc_dai_ops uniphier_aio_spdif_ops = {
371 .set_sysclk = uniphier_aio_set_sysclk,
372 .set_pll = uniphier_aio_set_pll,
373 .startup = uniphier_aio_startup,
374 .shutdown = uniphier_aio_shutdown,
375 .hw_params = uniphier_aio_hw_params,
376 .hw_free = uniphier_aio_hw_free,
377 .prepare = uniphier_aio_prepare,
379 EXPORT_SYMBOL_GPL(uniphier_aio_spdif_ops);
381 int uniphier_aio_dai_probe(struct snd_soc_dai *dai)
383 struct uniphier_aio *aio = uniphier_priv(dai);
386 for (i = 0; i < ARRAY_SIZE(aio->sub); i++) {
387 struct uniphier_aio_sub *sub = &aio->sub[i];
388 const struct uniphier_aio_spec *spec;
390 spec = find_spec(aio, dai->name, i);
394 sub->swm = &spec->swm;
397 sub->vol = AUD_VOL_INIT;
400 aio_iecout_set_enable(aio->chip, true);
401 aio_chip_init(aio->chip);
402 aio->chip->active = 1;
406 EXPORT_SYMBOL_GPL(uniphier_aio_dai_probe);
408 int uniphier_aio_dai_remove(struct snd_soc_dai *dai)
410 struct uniphier_aio *aio = uniphier_priv(dai);
412 aio->chip->active = 0;
416 EXPORT_SYMBOL_GPL(uniphier_aio_dai_remove);
418 static void uniphier_aio_dai_suspend(struct snd_soc_dai *dai)
420 struct uniphier_aio *aio = uniphier_priv(dai);
422 if (!snd_soc_dai_active(dai))
425 aio->chip->num_wup_aios--;
426 if (!aio->chip->num_wup_aios) {
427 reset_control_assert(aio->chip->rst);
428 clk_disable_unprepare(aio->chip->clk);
432 static int uniphier_aio_suspend(struct snd_soc_component *component)
434 struct snd_soc_dai *dai;
436 for_each_component_dais(component, dai)
437 uniphier_aio_dai_suspend(dai);
441 static int uniphier_aio_dai_resume(struct snd_soc_dai *dai)
443 struct uniphier_aio *aio = uniphier_priv(dai);
446 if (!snd_soc_dai_active(dai))
449 if (!aio->chip->active)
452 if (!aio->chip->num_wup_aios) {
453 ret = clk_prepare_enable(aio->chip->clk);
457 ret = reset_control_deassert(aio->chip->rst);
462 aio_iecout_set_enable(aio->chip, true);
463 aio_chip_init(aio->chip);
465 for (i = 0; i < ARRAY_SIZE(aio->sub); i++) {
466 struct uniphier_aio_sub *sub = &aio->sub[i];
468 if (!sub->spec || !sub->substream)
481 aio->chip->num_wup_aios++;
486 if (!aio->chip->num_wup_aios)
487 reset_control_assert(aio->chip->rst);
489 if (!aio->chip->num_wup_aios)
490 clk_disable_unprepare(aio->chip->clk);
495 static int uniphier_aio_resume(struct snd_soc_component *component)
497 struct snd_soc_dai *dai;
500 for_each_component_dais(component, dai)
501 ret |= uniphier_aio_dai_resume(dai);
505 static int uniphier_aio_vol_info(struct snd_kcontrol *kcontrol,
506 struct snd_ctl_elem_info *uinfo)
508 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
510 uinfo->value.integer.min = 0;
511 uinfo->value.integer.max = AUD_VOL_MAX;
516 static int uniphier_aio_vol_get(struct snd_kcontrol *kcontrol,
517 struct snd_ctl_elem_value *ucontrol)
519 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
520 struct uniphier_aio_chip *chip = snd_soc_component_get_drvdata(comp);
521 struct uniphier_aio_sub *sub;
522 int oport_hw = kcontrol->private_value;
524 sub = find_volume(chip, oport_hw);
528 ucontrol->value.integer.value[0] = sub->vol;
533 static int uniphier_aio_vol_put(struct snd_kcontrol *kcontrol,
534 struct snd_ctl_elem_value *ucontrol)
536 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
537 struct uniphier_aio_chip *chip = snd_soc_component_get_drvdata(comp);
538 struct uniphier_aio_sub *sub;
539 int oport_hw = kcontrol->private_value;
541 sub = find_volume(chip, oport_hw);
545 if (sub->vol == ucontrol->value.integer.value[0])
547 sub->vol = ucontrol->value.integer.value[0];
549 aio_port_set_volume(sub, sub->vol);
554 static const struct snd_kcontrol_new uniphier_aio_controls[] = {
556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
557 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
558 .name = "HPCMOUT1 Volume",
559 .info = uniphier_aio_vol_info,
560 .get = uniphier_aio_vol_get,
561 .put = uniphier_aio_vol_put,
562 .private_value = AUD_HW_HPCMOUT1,
565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
566 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
567 .name = "PCMOUT1 Volume",
568 .info = uniphier_aio_vol_info,
569 .get = uniphier_aio_vol_get,
570 .put = uniphier_aio_vol_put,
571 .private_value = AUD_HW_PCMOUT1,
574 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
575 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
576 .name = "PCMOUT2 Volume",
577 .info = uniphier_aio_vol_info,
578 .get = uniphier_aio_vol_get,
579 .put = uniphier_aio_vol_put,
580 .private_value = AUD_HW_PCMOUT2,
583 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
584 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
585 .name = "PCMOUT3 Volume",
586 .info = uniphier_aio_vol_info,
587 .get = uniphier_aio_vol_get,
588 .put = uniphier_aio_vol_put,
589 .private_value = AUD_HW_PCMOUT3,
592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
593 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
594 .name = "HIECOUT1 Volume",
595 .info = uniphier_aio_vol_info,
596 .get = uniphier_aio_vol_get,
597 .put = uniphier_aio_vol_put,
598 .private_value = AUD_HW_HIECOUT1,
601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
602 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
603 .name = "IECOUT1 Volume",
604 .info = uniphier_aio_vol_info,
605 .get = uniphier_aio_vol_get,
606 .put = uniphier_aio_vol_put,
607 .private_value = AUD_HW_IECOUT1,
611 static const struct snd_soc_component_driver uniphier_aio_component = {
612 .name = "uniphier-aio",
613 .controls = uniphier_aio_controls,
614 .num_controls = ARRAY_SIZE(uniphier_aio_controls),
615 .suspend = uniphier_aio_suspend,
616 .resume = uniphier_aio_resume,
619 int uniphier_aio_probe(struct platform_device *pdev)
621 struct uniphier_aio_chip *chip;
622 struct device *dev = &pdev->dev;
625 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
629 chip->chip_spec = of_device_get_match_data(dev);
630 if (!chip->chip_spec)
633 chip->regmap_sg = syscon_regmap_lookup_by_phandle(dev->of_node,
635 if (IS_ERR(chip->regmap_sg)) {
636 if (PTR_ERR(chip->regmap_sg) == -EPROBE_DEFER)
637 return -EPROBE_DEFER;
638 chip->regmap_sg = NULL;
641 chip->clk = devm_clk_get(dev, "aio");
642 if (IS_ERR(chip->clk))
643 return PTR_ERR(chip->clk);
645 chip->rst = devm_reset_control_get_shared(dev, "aio");
646 if (IS_ERR(chip->rst))
647 return PTR_ERR(chip->rst);
649 chip->num_aios = chip->chip_spec->num_dais;
650 chip->num_wup_aios = chip->num_aios;
651 chip->aios = devm_kcalloc(dev,
652 chip->num_aios, sizeof(struct uniphier_aio),
657 chip->num_plls = chip->chip_spec->num_plls;
658 chip->plls = devm_kcalloc(dev,
660 sizeof(struct uniphier_aio_pll),
664 memcpy(chip->plls, chip->chip_spec->plls,
665 sizeof(struct uniphier_aio_pll) * chip->num_plls);
667 for (i = 0; i < chip->num_aios; i++) {
668 struct uniphier_aio *aio = &chip->aios[i];
671 aio->fmt = SND_SOC_DAIFMT_I2S;
673 for (j = 0; j < ARRAY_SIZE(aio->sub); j++) {
674 struct uniphier_aio_sub *sub = &aio->sub[j];
677 spin_lock_init(&sub->lock);
682 platform_set_drvdata(pdev, chip);
684 ret = clk_prepare_enable(chip->clk);
688 ret = reset_control_deassert(chip->rst);
692 ret = devm_snd_soc_register_component(dev, &uniphier_aio_component,
693 chip->chip_spec->dais,
694 chip->chip_spec->num_dais);
696 dev_err(dev, "Register component failed.\n");
700 ret = uniphier_aiodma_soc_register_platform(pdev);
702 dev_err(dev, "Register platform failed.\n");
709 reset_control_assert(chip->rst);
712 clk_disable_unprepare(chip->clk);
716 EXPORT_SYMBOL_GPL(uniphier_aio_probe);
718 int uniphier_aio_remove(struct platform_device *pdev)
720 struct uniphier_aio_chip *chip = platform_get_drvdata(pdev);
722 reset_control_assert(chip->rst);
723 clk_disable_unprepare(chip->clk);
727 EXPORT_SYMBOL_GPL(uniphier_aio_remove);
729 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
730 MODULE_DESCRIPTION("UniPhier AIO CPU DAI driver.");
731 MODULE_LICENSE("GPL v2");