WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / intel / boards / sof_sdw_rt1316.c
blobd6e1ebf18d5787d826905300cb97750292038bd9
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
4 /*
5 * sof_sdw_rt1316 - Helpers to handle RT1316 from generic machine driver
6 */
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 "sof_sdw_common.h"
16 static const struct snd_soc_dapm_widget rt1316_widgets[] = {
17 SND_SOC_DAPM_SPK("Speaker", NULL),
21 * dapm routes for rt1316 will be registered dynamically according
22 * to the number of rt1316 used. The first two entries will be registered
23 * for one codec case, and the last two entries are also registered
24 * if two 1316s are used.
26 static const struct snd_soc_dapm_route rt1316_map[] = {
27 { "Speaker", NULL, "rt1316-1 SPOL" },
28 { "Speaker", NULL, "rt1316-1 SPOR" },
29 { "Speaker", NULL, "rt1316-2 SPOL" },
30 { "Speaker", NULL, "rt1316-2 SPOR" },
33 static const struct snd_kcontrol_new rt1316_controls[] = {
34 SOC_DAPM_PIN_SWITCH("Speaker"),
37 static int first_spk_init(struct snd_soc_pcm_runtime *rtd)
39 struct snd_soc_card *card = rtd->card;
40 int ret;
42 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
43 "%s spk:rt1316",
44 card->components);
45 if (!card->components)
46 return -ENOMEM;
48 ret = snd_soc_add_card_controls(card, rt1316_controls,
49 ARRAY_SIZE(rt1316_controls));
50 if (ret) {
51 dev_err(card->dev, "rt1316 controls addition failed: %d\n", ret);
52 return ret;
55 ret = snd_soc_dapm_new_controls(&card->dapm, rt1316_widgets,
56 ARRAY_SIZE(rt1316_widgets));
57 if (ret) {
58 dev_err(card->dev, "rt1316 widgets addition failed: %d\n", ret);
59 return ret;
62 ret = snd_soc_dapm_add_routes(&card->dapm, rt1316_map, 2);
63 if (ret)
64 dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
66 return ret;
69 static int second_spk_init(struct snd_soc_pcm_runtime *rtd)
71 struct snd_soc_card *card = rtd->card;
72 int ret;
74 ret = snd_soc_dapm_add_routes(&card->dapm, rt1316_map + 2, 2);
75 if (ret)
76 dev_err(rtd->dev, "failed to add second SPK map: %d\n", ret);
78 return ret;
81 static int all_spk_init(struct snd_soc_pcm_runtime *rtd)
83 int ret;
85 ret = first_spk_init(rtd);
86 if (ret)
87 return ret;
89 return second_spk_init(rtd);
92 int sof_sdw_rt1316_init(const struct snd_soc_acpi_link_adr *link,
93 struct snd_soc_dai_link *dai_links,
94 struct sof_sdw_codec_info *info,
95 bool playback)
97 /* Count amp number and do init on playback link only. */
98 if (!playback)
99 return 0;
101 info->amp_num++;
102 if (info->amp_num == 1)
103 dai_links->init = first_spk_init;
105 if (info->amp_num == 2) {
107 * if two 1316s are in one dai link, the init function
108 * in this dai link will be first set for the first speaker,
109 * and it should be reset to initialize all speakers when
110 * the second speaker is found.
112 if (dai_links->init)
113 dai_links->init = all_spk_init;
114 else
115 dai_links->init = second_spk_init;
118 return 0;