Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.13.7.
  1// SPDX-License-Identifier: GPL-2.0-only
  2// Copyright (c) 2022 Intel Corporation
  3
  4/*
  5 *  sof_sdw_rt_amp - Helpers to handle RT1308/RT1316/RT1318 from generic machine driver
  6 */
  7
  8#include <linux/device.h>
  9#include <linux/errno.h>
 10#include <sound/control.h>
 11#include <sound/soc.h>
 12#include <sound/soc-acpi.h>
 13#include <sound/soc-dapm.h>
 14#include <linux/soundwire/sdw.h>
 15#include <linux/soundwire/sdw_type.h>
 16#include <linux/dmi.h>
 17#include "sof_sdw_common.h"
 18#include "sof_sdw_amp_coeff_tables.h"
 19#include "../../codecs/rt1308.h"
 20
 21#define CODEC_NAME_SIZE	7
 22
 23/* choose a larger value to resolve compatibility issues */
 24#define RT_AMP_MAX_BQ_REG RT1316_MAX_BQ_REG
 25
 26struct rt_amp_platform_data {
 27	const unsigned char *bq_params;
 28	const unsigned int bq_params_cnt;
 29};
 30
 31static const struct rt_amp_platform_data dell_0a5d_platform_data = {
 32	.bq_params = dell_0a5d_bq_params,
 33	.bq_params_cnt = ARRAY_SIZE(dell_0a5d_bq_params),
 34};
 35
 36static const struct rt_amp_platform_data dell_0b00_platform_data = {
 37	.bq_params = dell_0b00_bq_params,
 38	.bq_params_cnt = ARRAY_SIZE(dell_0b00_bq_params),
 39};
 40
 41static const struct dmi_system_id dmi_platform_data[] = {
 42	/* CometLake devices */
 43	{
 44		.matches = {
 45			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 46			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0990")
 47		},
 48		.driver_data = (void *)&dell_0a5d_platform_data,
 49	},
 50	{
 51		.matches = {
 52			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 53			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "098F")
 54		},
 55		.driver_data = (void *)&dell_0a5d_platform_data,
 56	},
 57	/* TigerLake devices */
 58	{
 59		.matches = {
 60			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 61			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5D")
 62		},
 63		.driver_data = (void *)&dell_0a5d_platform_data,
 64	},
 65	{
 66		.matches = {
 67			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 68			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0A5E")
 69		},
 70		.driver_data = (void *)&dell_0a5d_platform_data,
 71	},
 72	/* AlderLake devices */
 73	{
 74		.matches = {
 75			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 76			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B00")
 77		},
 78		.driver_data = (void *)&dell_0b00_platform_data,
 79	},
 80	{
 81		.matches = {
 82			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 83			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0B01")
 84		},
 85		.driver_data = (void *)&dell_0b00_platform_data,
 86	},
 87	{
 88		.matches = {
 89			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 90			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFF")
 91		},
 92		.driver_data = (void *)&dell_0b00_platform_data,
 93	},
 94	{
 95		.matches = {
 96			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),
 97			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0AFE")
 98		},
 99		.driver_data = (void *)&dell_0b00_platform_data,
100	},
101	{},
102};
103
104static int rt_amp_add_device_props(struct device *sdw_dev)
105{
106	struct property_entry props[3] = {};
107	struct fwnode_handle *fwnode;
108	const struct dmi_system_id *dmi_data;
109	const struct rt_amp_platform_data *pdata;
110	unsigned char params[RT_AMP_MAX_BQ_REG];
111	int ret;
112
113	dmi_data = dmi_first_match(dmi_platform_data);
114	if (!dmi_data)
115		return 0;
116
117	pdata = dmi_data->driver_data;
118	memcpy(&params, pdata->bq_params, sizeof(unsigned char) * pdata->bq_params_cnt);
119
120	props[0] = PROPERTY_ENTRY_U8_ARRAY("realtek,bq-params", params);
121	props[1] = PROPERTY_ENTRY_U32("realtek,bq-params-cnt", pdata->bq_params_cnt);
122
123	fwnode = fwnode_create_software_node(props, NULL);
124	if (IS_ERR(fwnode))
125		return PTR_ERR(fwnode);
126
127	ret = device_add_software_node(sdw_dev, to_software_node(fwnode));
128
129	fwnode_handle_put(fwnode);
130
131	return ret;
132}
133
134static const struct snd_kcontrol_new rt_amp_controls[] = {
135	SOC_DAPM_PIN_SWITCH("Speaker"),
136};
137
138static const struct snd_soc_dapm_widget rt_amp_widgets[] = {
139	SND_SOC_DAPM_SPK("Speaker", NULL),
140};
141
142/*
143 * dapm routes for rt1308/rt1316/rt1318 will be registered dynamically
144 * according to the number of rt1308/rt1316/rt1318 used. The first two
145 * entries will be registered for one codec case, and the last two entries
146 * are also registered if two 1308s/1316s/1318s are used.
147 */
148static const struct snd_soc_dapm_route rt1308_map[] = {
149	{ "Speaker", NULL, "rt1308-1 SPOL" },
150	{ "Speaker", NULL, "rt1308-1 SPOR" },
151	{ "Speaker", NULL, "rt1308-2 SPOL" },
152	{ "Speaker", NULL, "rt1308-2 SPOR" },
153};
154
155static const struct snd_soc_dapm_route rt1316_map[] = {
156	{ "Speaker", NULL, "rt1316-1 SPOL" },
157	{ "Speaker", NULL, "rt1316-1 SPOR" },
158	{ "Speaker", NULL, "rt1316-2 SPOL" },
159	{ "Speaker", NULL, "rt1316-2 SPOR" },
160};
161
162static const struct snd_soc_dapm_route rt1318_map[] = {
163	{ "Speaker", NULL, "rt1318-1 SPOL" },
164	{ "Speaker", NULL, "rt1318-1 SPOR" },
165	{ "Speaker", NULL, "rt1318-2 SPOL" },
166	{ "Speaker", NULL, "rt1318-2 SPOR" },
167};
168
169static const struct snd_soc_dapm_route *get_codec_name_and_route(struct snd_soc_pcm_runtime *rtd,
170								 char *codec_name)
171{
172	const char *dai_name;
173
174	dai_name = rtd->dai_link->codecs->dai_name;
175
176	/* get the codec name */
177	snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai_name);
178
179	/* choose the right codec's map  */
180	if (strcmp(codec_name, "rt1308") == 0)
181		return rt1308_map;
182	else if (strcmp(codec_name, "rt1316") == 0)
183		return rt1316_map;
184	else
185		return rt1318_map;
186}
187
188int rt_amp_spk_rtd_init(struct snd_soc_pcm_runtime *rtd)
189{
190	struct snd_soc_card *card = rtd->card;
191	const struct snd_soc_dapm_route *rt_amp_map;
192	char codec_name[CODEC_NAME_SIZE];
193	struct snd_soc_dai *dai;
194	int ret;
195	int i;
196
197	rt_amp_map = get_codec_name_and_route(rtd, codec_name);
198
199	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
200					  "%s spk:%s",
201					  card->components, codec_name);
202	if (!card->components)
203		return -ENOMEM;
204
205	ret = snd_soc_add_card_controls(card, rt_amp_controls,
206					ARRAY_SIZE(rt_amp_controls));
207	if (ret) {
208		dev_err(card->dev, "%s controls addition failed: %d\n", codec_name, ret);
209		return ret;
210	}
211
212	ret = snd_soc_dapm_new_controls(&card->dapm, rt_amp_widgets,
213					ARRAY_SIZE(rt_amp_widgets));
214	if (ret) {
215		dev_err(card->dev, "%s widgets addition failed: %d\n", codec_name, ret);
216		return ret;
217	}
218
219	for_each_rtd_codec_dais(rtd, i, dai) {
220		if (strstr(dai->component->name_prefix, "-1"))
221			ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map, 2);
222		else if (strstr(dai->component->name_prefix, "-2"))
223			ret = snd_soc_dapm_add_routes(&card->dapm, rt_amp_map + 2, 2);
224	}
225
226	return ret;
227}
228
229static int rt1308_i2s_hw_params(struct snd_pcm_substream *substream,
230				struct snd_pcm_hw_params *params)
231{
232	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
233	struct snd_soc_card *card = rtd->card;
234	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
235	int clk_id, clk_freq, pll_out;
236	int err;
237
238	clk_id = RT1308_PLL_S_MCLK;
239	clk_freq = 38400000;
240
241	pll_out = params_rate(params) * 512;
242
243	/* Set rt1308 pll */
244	err = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
245	if (err < 0) {
246		dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", err);
247		return err;
248	}
249
250	/* Set rt1308 sysclk */
251	err = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
252				     SND_SOC_CLOCK_IN);
253	if (err < 0) {
254		dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", err);
255		return err;
256	}
257
258	return 0;
259}
260
261/* machine stream operations */
262struct snd_soc_ops sof_sdw_rt1308_i2s_ops = {
263	.hw_params = rt1308_i2s_hw_params,
264};
265
266int sof_sdw_rt_amp_exit(struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
267{
268	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
269
270	if (ctx->amp_dev1) {
271		device_remove_software_node(ctx->amp_dev1);
272		put_device(ctx->amp_dev1);
273	}
274
275	if (ctx->amp_dev2) {
276		device_remove_software_node(ctx->amp_dev2);
277		put_device(ctx->amp_dev2);
278	}
279
280	return 0;
281}
282
283int sof_sdw_rt_amp_init(struct snd_soc_card *card,
284			const struct snd_soc_acpi_link_adr *link,
285			struct snd_soc_dai_link *dai_links,
286			struct sof_sdw_codec_info *info,
287			bool playback)
288{
289	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
290	struct device *sdw_dev1, *sdw_dev2;
291	int ret;
292
293	/* Count amp number and do init on playback link only. */
294	if (!playback)
295		return 0;
296
297	info->amp_num++;
298
299	if (info->amp_num == 2) {
300		sdw_dev1 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name);
301		if (!sdw_dev1)
302			return -EPROBE_DEFER;
303
304		ret = rt_amp_add_device_props(sdw_dev1);
305		if (ret < 0) {
306			put_device(sdw_dev1);
307			return ret;
308		}
309		ctx->amp_dev1 = sdw_dev1;
310
311		sdw_dev2 = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[1].name);
312		if (!sdw_dev2)
313			return -EPROBE_DEFER;
314
315		ret = rt_amp_add_device_props(sdw_dev2);
316		if (ret < 0) {
317			put_device(sdw_dev2);
318			return ret;
319		}
320		ctx->amp_dev2 = sdw_dev2;
321	}
322
323	return 0;
324}