xref: /linux/sound/soc/intel/avs/boards/max98357a.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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, int ssp_port, int tdm_slot,
50 			       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 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
61 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
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,
68 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
69 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "MX98357A:00");
70 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "HiFi");
71 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
72 		return -ENOMEM;
73 
74 	platform->name = dev_name(dev);
75 	dl->num_cpus = 1;
76 	dl->num_codecs = 1;
77 	dl->platforms = platform;
78 	dl->num_platforms = 1;
79 	dl->id = 0;
80 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
81 	dl->be_hw_params_fixup = avs_max98357a_be_fixup;
82 	dl->nonatomic = 1;
83 	dl->no_pcm = 1;
84 	dl->playback_only = 1;
85 
86 	*dai_link = dl;
87 
88 	return 0;
89 }
90 
91 static int avs_max98357a_probe(struct platform_device *pdev)
92 {
93 	struct snd_soc_dai_link *dai_link;
94 	struct snd_soc_acpi_mach *mach;
95 	struct avs_mach_pdata *pdata;
96 	struct snd_soc_card *card;
97 	struct device *dev = &pdev->dev;
98 	int ssp_port, tdm_slot, ret;
99 
100 	mach = dev_get_platdata(dev);
101 	pdata = mach->pdata;
102 
103 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
104 	if (ret)
105 		return ret;
106 
107 	ret = avs_create_dai_link(dev, ssp_port, tdm_slot, &dai_link);
108 	if (ret) {
109 		dev_err(dev, "Failed to create dai link: %d", ret);
110 		return ret;
111 	}
112 
113 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
114 	if (!card)
115 		return -ENOMEM;
116 
117 	if (pdata->obsolete_card_names) {
118 		card->name = "avs_max98357a";
119 	} else {
120 		card->driver_name = "avs_max98357a";
121 		card->long_name = card->name = "AVS I2S MAX98357A";
122 	}
123 	card->dev = dev;
124 	card->owner = THIS_MODULE;
125 	card->dai_link = dai_link;
126 	card->num_links = 1;
127 	card->controls = card_controls;
128 	card->num_controls = ARRAY_SIZE(card_controls);
129 	card->dapm_widgets = card_widgets;
130 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
131 	card->dapm_routes = card_base_routes;
132 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
133 	card->fully_routed = true;
134 
135 	return devm_snd_soc_register_deferrable_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