Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / audiosense-pi.c
1 /*
2  * ASoC Driver for AudioSense add on soundcard
3  * Author:
4  *      Bhargav A K <anur.bhargav@gmail.com>
5  *      Copyright 2017
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/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/i2c.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/jack.h>
26 #include <sound/control.h>
27
28 #include <sound/tlv320aic32x4.h>
29 #include "../codecs/tlv320aic32x4.h"
30
31 #define AIC32X4_SYSCLK_XTAL     0x00
32
33 /*
34  * Setup Codec Sample Rates and Channels
35  * Supported Rates:
36  *      8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000,
37  */
38 static const unsigned int audiosense_pi_rate[] = {
39         48000,
40 };
41
42 static struct snd_pcm_hw_constraint_list audiosense_constraints_rates = {
43         .list = audiosense_pi_rate,
44         .count = ARRAY_SIZE(audiosense_pi_rate),
45 };
46
47 static const unsigned int audiosense_pi_channels[] = {
48         2,
49 };
50
51 static struct snd_pcm_hw_constraint_list audiosense_constraints_ch = {
52         .count = ARRAY_SIZE(audiosense_pi_channels),
53         .list = audiosense_pi_channels,
54         .mask = 0,
55 };
56
57 /* Setup DAPM widgets and paths */
58 static const struct snd_soc_dapm_widget audiosense_pi_dapm_widgets[] = {
59         SND_SOC_DAPM_HP("Headphone Jack", NULL),
60         SND_SOC_DAPM_LINE("Line Out", NULL),
61         SND_SOC_DAPM_LINE("Line In", NULL),
62         SND_SOC_DAPM_INPUT("CM_L"),
63         SND_SOC_DAPM_INPUT("CM_R"),
64 };
65
66 static const struct snd_soc_dapm_route audiosense_pi_audio_map[] = {
67         /* Line Inputs are connected to
68          * (IN1_L | IN1_R)
69          * (IN2_L | IN2_R)
70          * (IN3_L | IN3_R)
71          */
72         {"IN1_L", NULL, "Line In"},
73         {"IN1_R", NULL, "Line In"},
74         {"IN2_L", NULL, "Line In"},
75         {"IN2_R", NULL, "Line In"},
76         {"IN3_L", NULL, "Line In"},
77         {"IN3_R", NULL, "Line In"},
78
79         /* Mic is connected to IN2_L and IN2_R */
80         {"Left ADC", NULL, "Mic Bias"},
81         {"Right ADC", NULL, "Mic Bias"},
82
83         /* Headphone connected to HPL, HPR */
84         {"Headphone Jack", NULL, "HPL"},
85         {"Headphone Jack", NULL, "HPR"},
86
87         /* Speakers connected to LOL and LOR */
88         {"Line Out", NULL, "LOL"},
89         {"Line Out", NULL, "LOR"},
90 };
91
92 static int audiosense_pi_card_init(struct snd_soc_pcm_runtime *rtd)
93 {
94         /* TODO: init of the codec specific dapm data, ignore suspend/resume */
95         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
96
97         snd_soc_component_update_bits(component, AIC32X4_MICBIAS, 0x78,
98                                       AIC32X4_MICBIAS_LDOIN |
99                                       AIC32X4_MICBIAS_2075V);
100         snd_soc_component_update_bits(component, AIC32X4_PWRCFG, 0x08,
101                                       AIC32X4_AVDDWEAKDISABLE);
102         snd_soc_component_update_bits(component, AIC32X4_LDOCTL, 0x01,
103                                       AIC32X4_LDOCTLEN);
104
105         return 0;
106 }
107
108 static int audiosense_pi_card_hw_params(
109                 struct snd_pcm_substream *substream,
110                 struct snd_pcm_hw_params *params)
111 {
112         int ret = 0;
113         struct snd_soc_pcm_runtime *rtd = substream->private_data;
114         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
115
116         /* Set the codec system clock, there is a 12 MHz XTAL on the board */
117         ret = snd_soc_dai_set_sysclk(codec_dai, AIC32X4_SYSCLK_XTAL,
118                                      12000000, SND_SOC_CLOCK_IN);
119         if (ret) {
120                 dev_err(rtd->card->dev,
121                         "could not set codec driver clock params\n");
122                 return ret;
123         }
124         return 0;
125 }
126
127 static int audiosense_pi_card_startup(struct snd_pcm_substream *substream)
128 {
129         struct snd_pcm_runtime *runtime = substream->runtime;
130
131         /*
132          * Set codec to 48Khz Sampling, Stereo I/O and 16 bit audio
133          */
134         runtime->hw.channels_max = 2;
135         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
136                                    &audiosense_constraints_ch);
137
138         runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
139         snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
140
141
142         snd_pcm_hw_constraint_list(substream->runtime, 0,
143                                    SNDRV_PCM_HW_PARAM_RATE,
144                                    &audiosense_constraints_rates);
145         return 0;
146 }
147
148 static struct snd_soc_ops audiosense_pi_card_ops = {
149         .startup = audiosense_pi_card_startup,
150         .hw_params = audiosense_pi_card_hw_params,
151 };
152
153 SND_SOC_DAILINK_DEFS(audiosense_pi,
154         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
155         DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic32x4.1-0018", "tlv320aic32x4-hifi")),
156         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
157
158 static struct snd_soc_dai_link audiosense_pi_card_dai[] = {
159         {
160                 .name           = "TLV320AIC3204 Audio",
161                 .stream_name    = "TLV320AIC3204 Hifi Audio",
162                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
163                         SND_SOC_DAIFMT_CBM_CFM,
164                 .ops            = &audiosense_pi_card_ops,
165                 .init           = audiosense_pi_card_init,
166                 SND_SOC_DAILINK_REG(audiosense_pi),
167         },
168 };
169
170 static struct snd_soc_card audiosense_pi_card = {
171         .name           = "audiosense-pi",
172         .driver_name    = "audiosense-pi",
173         .dai_link       = audiosense_pi_card_dai,
174         .owner          = THIS_MODULE,
175         .num_links      = ARRAY_SIZE(audiosense_pi_card_dai),
176         .dapm_widgets   = audiosense_pi_dapm_widgets,
177         .num_dapm_widgets = ARRAY_SIZE(audiosense_pi_dapm_widgets),
178         .dapm_routes    = audiosense_pi_audio_map,
179         .num_dapm_routes = ARRAY_SIZE(audiosense_pi_audio_map),
180 };
181
182 static int audiosense_pi_card_probe(struct platform_device *pdev)
183 {
184         int ret = 0;
185         struct snd_soc_card *card = &audiosense_pi_card;
186         struct snd_soc_dai_link *dai = &audiosense_pi_card_dai[0];
187         struct device_node *i2s_node = pdev->dev.of_node;
188
189         card->dev = &pdev->dev;
190
191         if (!dai) {
192                 dev_err(&pdev->dev, "DAI not found. Missing or Invalid\n");
193                 return -EINVAL;
194         }
195
196         i2s_node = of_parse_phandle(pdev->dev.of_node, "i2s-controller", 0);
197         if (!i2s_node) {
198                 dev_err(&pdev->dev,
199                         "Property 'i2s-controller' missing or invalid\n");
200                 return -EINVAL;
201         }
202
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
208         of_node_put(i2s_node);
209
210         ret = snd_soc_register_card(card);
211         if (ret && ret != -EPROBE_DEFER)
212                 dev_err(&pdev->dev,
213                         "snd_soc_register_card() failed: %d\n", ret);
214
215         return ret;
216 }
217
218 static int audiosense_pi_card_remove(struct platform_device *pdev)
219 {
220         struct snd_soc_card *card = platform_get_drvdata(pdev);
221
222         return snd_soc_unregister_card(card);
223
224 }
225
226 static const struct of_device_id audiosense_pi_card_of_match[] = {
227         { .compatible = "as,audiosense-pi", },
228         {},
229 };
230 MODULE_DEVICE_TABLE(of, audiosense_pi_card_of_match);
231
232 static struct platform_driver audiosense_pi_card_driver = {
233         .driver = {
234                 .name = "audiosense-snd-card",
235                 .owner = THIS_MODULE,
236                 .of_match_table = audiosense_pi_card_of_match,
237         },
238         .probe = audiosense_pi_card_probe,
239         .remove = audiosense_pi_card_remove,
240 };
241
242 module_platform_driver(audiosense_pi_card_driver);
243
244 MODULE_AUTHOR("Bhargav AK <anur.bhargav@gmail.com>");
245 MODULE_DESCRIPTION("ASoC Driver for TLV320AIC3204 Audio");
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("platform:audiosense-pi");
248