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