Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / dacberry400.c
1 /*
2  * ASoC Driver for Dacberry400 soundcard
3  * Author:
4  *      Ashish Vara<ashishhvara@gmail.com>
5  *      Copyright 2022
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  */
16
17 #include <linux/module.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/jack.h>
25 #include <linux/i2c.h>
26 #include <linux/acpi.h>
27 #include <linux/slab.h>
28 #include "../sound/soc/codecs/tlv320aic3x.h"
29
30 static const struct snd_kcontrol_new dacberry400_controls[] = {
31         SOC_DAPM_PIN_SWITCH("MIC Jack"),
32         SOC_DAPM_PIN_SWITCH("Line In"),
33         SOC_DAPM_PIN_SWITCH("Line Out"),
34         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
35 };
36
37 static const struct snd_soc_dapm_widget dacberry400_widgets[] = {
38         SND_SOC_DAPM_HP("Headphone Jack", NULL),
39         SND_SOC_DAPM_MIC("MIC Jack", NULL),
40         SND_SOC_DAPM_LINE("Line In", NULL),
41         SND_SOC_DAPM_LINE("Line Out", NULL),
42 };
43
44 static const struct snd_soc_dapm_route dacberry400_audio_map[] = {
45         {"Headphone Jack", NULL, "HPLOUT"},
46         {"Headphone Jack", NULL, "HPROUT"},
47
48         {"LINE1L", NULL, "Line In"},
49         {"LINE1R", NULL, "Line In"},
50
51         {"Line Out", NULL, "LLOUT"},
52         {"Line Out", NULL, "RLOUT"},
53
54         {"MIC3L", NULL, "MIC Jack"},
55         {"MIC3R", NULL, "MIC Jack"},
56 };
57
58 static int snd_rpi_dacberry400_init(struct snd_soc_pcm_runtime *rtd)
59 {
60         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
61         struct snd_soc_component *component = codec_dai->component;
62         int ret;
63
64         ret = snd_soc_dai_set_sysclk(codec_dai, 2, 12000000,
65                                         SND_SOC_CLOCK_OUT);
66
67         if (ret && ret != -ENOTSUPP)
68                 goto err;
69
70         snd_soc_component_write(component, HPRCOM_CFG, 0x20);
71         snd_soc_component_write(component, DACL1_2_HPLOUT_VOL, 0x80);
72         snd_soc_component_write(component, DACR1_2_HPROUT_VOL, 0x80);
73 err:
74         return ret;
75 }
76
77 static int snd_rpi_dacberry400_set_bias_level(struct snd_soc_card *card,
78         struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
79 {
80         struct snd_soc_pcm_runtime *rtd;
81         struct snd_soc_dai *codec_dai;
82         struct snd_soc_component *component;
83         struct dacberry_priv *aic3x;
84         u8 hpcom_reg = 0;
85
86         rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
87         codec_dai = asoc_rtd_to_codec(rtd, 0);
88         component = codec_dai->component;
89         aic3x = snd_soc_component_get_drvdata(component);
90         if (dapm->dev != codec_dai->dev)
91                 return 0;
92
93         switch (level) {
94         case SND_SOC_BIAS_PREPARE:
95                 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
96                         break;
97                 /* UNMUTE ADC/DAC */
98                 hpcom_reg = snd_soc_component_read(component, HPLCOM_CFG);
99                 snd_soc_component_write(component, HPLCOM_CFG, hpcom_reg | 0x20);
100                 snd_soc_component_write(component, LINE1R_2_RADC_CTRL, 0x04);
101                 snd_soc_component_write(component, LINE1L_2_LADC_CTRL, 0x04);
102                 snd_soc_component_write(component, LADC_VOL, 0x00);
103                 snd_soc_component_write(component, RADC_VOL, 0x00);
104                 pr_info("%s: unmute ADC/DAC\n", __func__);
105                 break;
106
107         case SND_SOC_BIAS_STANDBY:
108                 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
109                         break;
110                 /* MUTE ADC/DAC */
111                 snd_soc_component_write(component, LDAC_VOL, 0x80);
112                 snd_soc_component_write(component, RDAC_VOL, 0x80);
113                 snd_soc_component_write(component, LADC_VOL, 0x80);
114                 snd_soc_component_write(component, RADC_VOL, 0x80);
115                 snd_soc_component_write(component, LINE1R_2_RADC_CTRL, 0x00);
116                 snd_soc_component_write(component, LINE1L_2_LADC_CTRL, 0x00);
117                 snd_soc_component_write(component, HPLCOM_CFG, 0x00);
118                 pr_info("%s: mute ADC/DAC\n", __func__);
119                 break;
120         default:
121                 break;
122         }
123
124         return 0;
125 }
126
127 static int snd_rpi_dacberry400_hw_params(struct snd_pcm_substream *substream,
128                                            struct snd_pcm_hw_params *params)
129 {
130         int ret = 0;
131         u8 data;
132         struct snd_soc_pcm_runtime *rtd = substream->private_data;
133         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
134         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
135         struct snd_soc_component *component = codec_dai->component;
136         int fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
137         int channels = params_channels(params);
138         int width = 32;
139         u8 clock = 0;
140
141         data = (LDAC2LCH | RDAC2RCH);
142         data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
143         if (params_rate(params) >= 64000)
144                 data |= DUAL_RATE_MODE;
145         ret = snd_soc_component_write(component, 0x7, data);
146         width = params_width(params);
147
148         clock = snd_soc_component_read(component, 2);
149
150         ret = snd_soc_dai_set_bclk_ratio(cpu_dai, channels*width);
151
152         return ret;
153 }
154
155 static const struct snd_soc_ops snd_rpi_dacberry400_ops = {
156         .hw_params = snd_rpi_dacberry400_hw_params,
157 };
158
159
160 SND_SOC_DAILINK_DEFS(rpi_dacberry400,
161         DAILINK_COMP_ARRAY(COMP_CPU("bcm2835-i2s.0")),
162         DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x.1-0018", "tlv320aic3x-hifi")),
163         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
164
165 static struct snd_soc_dai_link snd_rpi_dacberry400_dai[] = {
166 {
167         .dai_fmt                = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
168                                         SND_SOC_DAIFMT_CBS_CFS,
169         .init                   = snd_rpi_dacberry400_init,
170         .ops                    = &snd_rpi_dacberry400_ops,
171         .symmetric_rate         = 1,
172         SND_SOC_DAILINK_REG(rpi_dacberry400),
173 },
174 };
175
176 static struct snd_soc_card snd_rpi_dacberry400 = {
177         .owner                  = THIS_MODULE,
178         .dai_link               = snd_rpi_dacberry400_dai,
179         .num_links              = ARRAY_SIZE(snd_rpi_dacberry400_dai),
180         .controls               = dacberry400_controls,
181         .num_controls           = ARRAY_SIZE(dacberry400_controls),
182         .dapm_widgets           = dacberry400_widgets,
183         .num_dapm_widgets       = ARRAY_SIZE(dacberry400_widgets),
184         .dapm_routes            = dacberry400_audio_map,
185         .num_dapm_routes        = ARRAY_SIZE(dacberry400_audio_map),
186         .set_bias_level         = snd_rpi_dacberry400_set_bias_level,
187 };
188
189 static int snd_rpi_dacberry400_probe(struct platform_device *pdev)
190 {
191         int ret = 0;
192
193         snd_rpi_dacberry400.dev = &pdev->dev;
194
195         if (pdev->dev.of_node) {
196                 struct device_node *i2s_node;
197                 struct snd_soc_card *card = &snd_rpi_dacberry400;
198                 struct snd_soc_dai_link *dai = &snd_rpi_dacberry400_dai[0];
199
200                 i2s_node = of_parse_phandle(pdev->dev.of_node,
201                                             "i2s-controller", 0);
202                 if (i2s_node) {
203                         dai->cpus->dai_name = NULL;
204                         dai->cpus->of_node = i2s_node;
205                         dai->platforms->name = NULL;
206                         dai->platforms->of_node = i2s_node;
207                         of_node_put(i2s_node);
208                 }
209
210                 if (of_property_read_string(pdev->dev.of_node, "card_name",
211                                             &card->name))
212                         card->name = "tlvaudioCODEC";
213
214                 if (of_property_read_string(pdev->dev.of_node, "dai_name",
215                                             &dai->name))
216                         dai->name = "tlvaudio CODEC";
217
218         }
219
220         ret = snd_soc_register_card(&snd_rpi_dacberry400);
221         if (ret) {
222                 if (ret != -EPROBE_DEFER)
223                         dev_err(&pdev->dev,
224                                 "snd_soc_register_card() failed: %d\n", ret);
225                 return ret;
226         }
227
228         return 0;
229 }
230
231 static int snd_rpi_dacberry400_remove(struct platform_device *pdev)
232 {
233         return snd_soc_unregister_card(&snd_rpi_dacberry400);
234 }
235
236 static const struct of_device_id dacberry400_match_id[] = {
237         { .compatible = "osaelectronics,dacberry400",},
238         {},
239 };
240 MODULE_DEVICE_TABLE(of, dacberry400_match_id);
241
242 static struct platform_driver snd_rpi_dacberry400_driver = {
243         .driver = {
244                 .name = "snd-rpi-dacberry400",
245                 .owner = THIS_MODULE,
246                 .of_match_table = dacberry400_match_id,
247         },
248         .probe = snd_rpi_dacberry400_probe,
249         .remove = snd_rpi_dacberry400_remove,
250 };
251
252 module_platform_driver(snd_rpi_dacberry400_driver);
253
254 MODULE_AUTHOR("Ashish Vara");
255 MODULE_DESCRIPTION("Dacberry400 sound card driver");
256 MODULE_LICENSE("GPL");
257 MODULE_ALIAS("platform:dacberry400");
258 MODULE_SOFTDEP("pre: snd-soc-tlv320aic3x");