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