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