1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15
16 static const struct snd_kcontrol_new card_controls[] = {
17 SOC_DAPM_PIN_SWITCH("Spk"),
18 };
19
20 static const struct snd_soc_dapm_widget card_widgets[] = {
21 SND_SOC_DAPM_SPK("Spk", NULL),
22 };
23
24 static const struct snd_soc_dapm_route card_base_routes[] = {
25 { "Spk", NULL, "Speaker" },
26 };
27
28 static int
avs_max98357a_be_fixup(struct snd_soc_pcm_runtime * runrime,struct snd_pcm_hw_params * params)29 avs_max98357a_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
30 {
31 struct snd_interval *rate, *channels;
32 struct snd_mask *fmt;
33
34 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
35 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
36 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
37
38 /* The ADSP will convert the FE rate to 48k, stereo */
39 rate->min = rate->max = 48000;
40 channels->min = channels->max = 2;
41
42 /* set SSP0 to 16 bit */
43 snd_mask_none(fmt);
44 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
45 return 0;
46 }
47
avs_create_dai_link(struct device * dev,const char * platform_name,int ssp_port,struct snd_soc_dai_link ** dai_link)48 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
49 struct snd_soc_dai_link **dai_link)
50 {
51 struct snd_soc_dai_link_component *platform;
52 struct snd_soc_dai_link *dl;
53
54 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
55 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
56 if (!dl || !platform)
57 return -ENOMEM;
58
59 platform->name = platform_name;
60
61 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
62 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
63 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
64 if (!dl->name || !dl->cpus || !dl->codecs)
65 return -ENOMEM;
66
67 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
68 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "MX98357A:00");
69 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "HiFi");
70 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
71 return -ENOMEM;
72
73 dl->num_cpus = 1;
74 dl->num_codecs = 1;
75 dl->platforms = platform;
76 dl->num_platforms = 1;
77 dl->id = 0;
78 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
79 dl->be_hw_params_fixup = avs_max98357a_be_fixup;
80 dl->nonatomic = 1;
81 dl->no_pcm = 1;
82 dl->dpcm_playback = 1;
83
84 *dai_link = dl;
85
86 return 0;
87 }
88
avs_max98357a_probe(struct platform_device * pdev)89 static int avs_max98357a_probe(struct platform_device *pdev)
90 {
91 struct snd_soc_dai_link *dai_link;
92 struct snd_soc_acpi_mach *mach;
93 struct snd_soc_card *card;
94 struct device *dev = &pdev->dev;
95 const char *pname;
96 int ssp_port, ret;
97
98 mach = dev_get_platdata(dev);
99 pname = mach->mach_params.platform;
100 ssp_port = __ffs(mach->mach_params.i2s_link_mask);
101
102 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
103 if (ret) {
104 dev_err(dev, "Failed to create dai link: %d", ret);
105 return ret;
106 }
107
108 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
109 if (!card)
110 return -ENOMEM;
111
112 card->name = "avs_max98357a";
113 card->dev = dev;
114 card->owner = THIS_MODULE;
115 card->dai_link = dai_link;
116 card->num_links = 1;
117 card->controls = card_controls;
118 card->num_controls = ARRAY_SIZE(card_controls);
119 card->dapm_widgets = card_widgets;
120 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
121 card->dapm_routes = card_base_routes;
122 card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
123 card->fully_routed = true;
124
125 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
126 if (ret)
127 return ret;
128
129 return devm_snd_soc_register_card(dev, card);
130 }
131
132 static struct platform_driver avs_max98357a_driver = {
133 .probe = avs_max98357a_probe,
134 .driver = {
135 .name = "avs_max98357a",
136 .pm = &snd_soc_pm_ops,
137 },
138 };
139
140 module_platform_driver(avs_max98357a_driver)
141
142 MODULE_LICENSE("GPL");
143 MODULE_ALIAS("platform:avs_max98357a");
144