xref: /linux/sound/soc/intel/avs/boards/ssm4567.c (revision ef9226cd56b718c79184a3466d32984a51cb449c)
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/core.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include "../../../codecs/nau8825.h"
17 #include "../utils.h"
18 
19 #define SKL_SSM_CODEC_DAI	"ssm4567-hifi"
20 
21 static struct snd_soc_codec_conf card_codec_conf[] = {
22 	{
23 		.dlc = COMP_CODEC_CONF("i2c-INT343B:00"),
24 		.name_prefix = "Left",
25 	},
26 	{
27 		.dlc = COMP_CODEC_CONF("i2c-INT343B:01"),
28 		.name_prefix = "Right",
29 	},
30 };
31 
32 static const struct snd_kcontrol_new card_controls[] = {
33 	SOC_DAPM_PIN_SWITCH("Left Speaker"),
34 	SOC_DAPM_PIN_SWITCH("Right Speaker"),
35 };
36 
37 static const struct snd_soc_dapm_widget card_widgets[] = {
38 	SND_SOC_DAPM_SPK("Left Speaker", NULL),
39 	SND_SOC_DAPM_SPK("Right Speaker", NULL),
40 	SND_SOC_DAPM_SPK("DP1", NULL),
41 	SND_SOC_DAPM_SPK("DP2", NULL),
42 };
43 
44 static const struct snd_soc_dapm_route card_base_routes[] = {
45 	{"Left Speaker", NULL, "Left OUT"},
46 	{"Right Speaker", NULL, "Right OUT"},
47 };
48 
49 static int avs_ssm4567_codec_init(struct snd_soc_pcm_runtime *runtime)
50 {
51 	int ret;
52 
53 	/* Slot 1 for left */
54 	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_codec(runtime, 0), 0x01, 0x01, 2, 48);
55 	if (ret < 0)
56 		return ret;
57 
58 	/* Slot 2 for right */
59 	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_codec(runtime, 1), 0x02, 0x02, 2, 48);
60 	if (ret < 0)
61 		return ret;
62 
63 	return 0;
64 }
65 
66 static int
67 avs_ssm4567_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
68 {
69 	struct snd_interval *rate, *channels;
70 	struct snd_mask *fmt;
71 
72 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
73 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
74 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
75 
76 	/* The ADSP will convert the FE rate to 48k, stereo */
77 	rate->min = rate->max = 48000;
78 	channels->min = channels->max = 2;
79 
80 	/* set SSP0 to 24 bit */
81 	snd_mask_none(fmt);
82 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
83 	return 0;
84 }
85 
86 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
87 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
88 {
89 	struct snd_soc_dai_link_component *platform;
90 	struct snd_soc_dai_link *dl;
91 
92 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
93 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
94 	if (!dl || !platform)
95 		return -ENOMEM;
96 
97 	platform->name = platform_name;
98 
99 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
100 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
101 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
102 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
103 	if (!dl->name || !dl->cpus || !dl->codecs)
104 		return -ENOMEM;
105 
106 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
107 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
108 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:00");
109 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
110 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:01");
111 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
112 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
113 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
114 		return -ENOMEM;
115 
116 	dl->num_cpus = 1;
117 	dl->num_codecs = 2;
118 	dl->platforms = platform;
119 	dl->num_platforms = 1;
120 	dl->id = 0;
121 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS;
122 	dl->init = avs_ssm4567_codec_init;
123 	dl->be_hw_params_fixup = avs_ssm4567_be_fixup;
124 	dl->nonatomic = 1;
125 	dl->no_pcm = 1;
126 	dl->dpcm_capture = 1;
127 	dl->dpcm_playback = 1;
128 	dl->ignore_pmdown_time = 1;
129 
130 	*dai_link = dl;
131 
132 	return 0;
133 }
134 
135 static int avs_ssm4567_probe(struct platform_device *pdev)
136 {
137 	struct snd_soc_dai_link *dai_link;
138 	struct snd_soc_acpi_mach *mach;
139 	struct snd_soc_card *card;
140 	struct device *dev = &pdev->dev;
141 	const char *pname;
142 	int ssp_port, tdm_slot, ret;
143 
144 	mach = dev_get_platdata(dev);
145 	pname = mach->mach_params.platform;
146 
147 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
148 	if (ret)
149 		return ret;
150 
151 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
152 	if (ret) {
153 		dev_err(dev, "Failed to create dai link: %d", ret);
154 		return ret;
155 	}
156 
157 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
158 	if (!card)
159 		return -ENOMEM;
160 
161 	card->name = "avs_ssm4567-adi";
162 	card->dev = dev;
163 	card->owner = THIS_MODULE;
164 	card->dai_link = dai_link;
165 	card->num_links = 1;
166 	card->codec_conf = card_codec_conf;
167 	card->num_configs = ARRAY_SIZE(card_codec_conf);
168 	card->controls = card_controls;
169 	card->num_controls = ARRAY_SIZE(card_controls);
170 	card->dapm_widgets = card_widgets;
171 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
172 	card->dapm_routes = card_base_routes;
173 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
174 	card->fully_routed = true;
175 	card->disable_route_checks = true;
176 
177 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
178 	if (ret)
179 		return ret;
180 
181 	return devm_snd_soc_register_card(dev, card);
182 }
183 
184 static const struct platform_device_id avs_ssm4567_driver_ids[] = {
185 	{
186 		.name = "avs_ssm4567",
187 	},
188 	{},
189 };
190 MODULE_DEVICE_TABLE(platform, avs_ssm4567_driver_ids);
191 
192 static struct platform_driver avs_ssm4567_driver = {
193 	.probe = avs_ssm4567_probe,
194 	.driver = {
195 		.name = "avs_ssm4567",
196 		.pm = &snd_soc_pm_ops,
197 	},
198 	.id_table = avs_ssm4567_driver_ids,
199 };
200 
201 module_platform_driver(avs_ssm4567_driver)
202 
203 MODULE_DESCRIPTION("Intel ssm4567 machine driver");
204 MODULE_LICENSE("GPL");
205