MAINTAINERS: update the LSM maintainer info
[platform/kernel/linux-starfive.git] / sound / soc / intel / boards / sof_cirrus_common.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file defines data structures and functions used in Machine
4  * Driver for Intel platforms with Cirrus Logic Codecs.
5  *
6  * Copyright 2022 Intel Corporation.
7  */
8 #include <linux/module.h>
9 #include <sound/sof.h>
10 #include "../../codecs/cs35l41.h"
11 #include "sof_cirrus_common.h"
12
13 /*
14  * Cirrus Logic CS35L41/CS35L53
15  */
16 static const struct snd_kcontrol_new cs35l41_kcontrols[] = {
17         SOC_DAPM_PIN_SWITCH("WL Spk"),
18         SOC_DAPM_PIN_SWITCH("WR Spk"),
19         SOC_DAPM_PIN_SWITCH("TL Spk"),
20         SOC_DAPM_PIN_SWITCH("TR Spk"),
21 };
22
23 static const struct snd_soc_dapm_widget cs35l41_dapm_widgets[] = {
24         SND_SOC_DAPM_SPK("WL Spk", NULL),
25         SND_SOC_DAPM_SPK("WR Spk", NULL),
26         SND_SOC_DAPM_SPK("TL Spk", NULL),
27         SND_SOC_DAPM_SPK("TR Spk", NULL),
28 };
29
30 static const struct snd_soc_dapm_route cs35l41_dapm_routes[] = {
31         /* speaker */
32         {"WL Spk", NULL, "WL SPK"},
33         {"WR Spk", NULL, "WR SPK"},
34         {"TL Spk", NULL, "TL SPK"},
35         {"TR Spk", NULL, "TR SPK"},
36 };
37
38 static struct snd_soc_dai_link_component cs35l41_components[] = {
39         {
40                 .name = CS35L41_DEV0_NAME,
41                 .dai_name = CS35L41_CODEC_DAI,
42         },
43         {
44                 .name = CS35L41_DEV1_NAME,
45                 .dai_name = CS35L41_CODEC_DAI,
46         },
47         {
48                 .name = CS35L41_DEV2_NAME,
49                 .dai_name = CS35L41_CODEC_DAI,
50         },
51         {
52                 .name = CS35L41_DEV3_NAME,
53                 .dai_name = CS35L41_CODEC_DAI,
54         },
55 };
56
57 static struct snd_soc_codec_conf cs35l41_codec_conf[] = {
58         {
59                 .dlc = COMP_CODEC_CONF(CS35L41_DEV0_NAME),
60                 .name_prefix = "WL",
61         },
62         {
63                 .dlc = COMP_CODEC_CONF(CS35L41_DEV1_NAME),
64                 .name_prefix = "WR",
65         },
66         {
67                 .dlc = COMP_CODEC_CONF(CS35L41_DEV2_NAME),
68                 .name_prefix = "TL",
69         },
70         {
71                 .dlc = COMP_CODEC_CONF(CS35L41_DEV3_NAME),
72                 .name_prefix = "TR",
73         },
74 };
75
76 static int cs35l41_init(struct snd_soc_pcm_runtime *rtd)
77 {
78         struct snd_soc_card *card = rtd->card;
79         int ret;
80
81         ret = snd_soc_dapm_new_controls(&card->dapm, cs35l41_dapm_widgets,
82                                         ARRAY_SIZE(cs35l41_dapm_widgets));
83         if (ret) {
84                 dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
85                 return ret;
86         }
87
88         ret = snd_soc_add_card_controls(card, cs35l41_kcontrols,
89                                         ARRAY_SIZE(cs35l41_kcontrols));
90         if (ret) {
91                 dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
92                 return ret;
93         }
94
95         ret = snd_soc_dapm_add_routes(&card->dapm, cs35l41_dapm_routes,
96                                       ARRAY_SIZE(cs35l41_dapm_routes));
97
98         if (ret)
99                 dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
100
101         return ret;
102 }
103
104 static int cs35l41_hw_params(struct snd_pcm_substream *substream,
105                              struct snd_pcm_hw_params *params)
106 {
107         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
108         struct snd_soc_dai *codec_dai;
109         int clk_freq, i, ret;
110
111         clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
112
113         if (clk_freq <= 0) {
114                 dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
115                 return -EINVAL;
116         }
117
118         for_each_rtd_codec_dais(rtd, i, codec_dai) {
119                 /* call dai driver's set_sysclk() callback */
120                 ret = snd_soc_dai_set_sysclk(codec_dai, CS35L41_CLKID_SCLK,
121                                              clk_freq, SND_SOC_CLOCK_IN);
122                 if (ret < 0) {
123                         dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
124                                 ret);
125                         return ret;
126                 }
127
128                 /* call component driver's set_sysclk() callback */
129                 ret = snd_soc_component_set_sysclk(codec_dai->component,
130                                                    CS35L41_CLKID_SCLK, 0,
131                                                    clk_freq, SND_SOC_CLOCK_IN);
132                 if (ret < 0) {
133                         dev_err(codec_dai->dev, "fail to set component sysclk, ret %d\n",
134                                 ret);
135                         return ret;
136                 }
137         }
138
139         return 0;
140 }
141
142 static const struct snd_soc_ops cs35l41_ops = {
143         .hw_params = cs35l41_hw_params,
144 };
145
146 void cs35l41_set_dai_link(struct snd_soc_dai_link *link)
147 {
148         link->codecs = cs35l41_components;
149         link->num_codecs = ARRAY_SIZE(cs35l41_components);
150         link->init = cs35l41_init;
151         link->ops = &cs35l41_ops;
152 }
153 EXPORT_SYMBOL_NS(cs35l41_set_dai_link, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
154
155 void cs35l41_set_codec_conf(struct snd_soc_card *card)
156 {
157         card->codec_conf = cs35l41_codec_conf;
158         card->num_configs = ARRAY_SIZE(cs35l41_codec_conf);
159 }
160 EXPORT_SYMBOL_NS(cs35l41_set_codec_conf, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
161
162 MODULE_DESCRIPTION("ASoC Intel SOF Cirrus Logic helpers");
163 MODULE_LICENSE("GPL");