Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / audioinjector-pi-soundcard.c
1 /*
2  * ASoC Driver for AudioInjector Pi add on soundcard
3  *
4  *  Created on: 13-May-2016
5  *      Author: flatmax@flatmax.org
6  *              based on code by  Cliff Cai <Cliff.Cai@analog.com> for the ssm2602 machine blackfin.
7  *              with help from Lars-Peter Clausen for simplifying the original code to use the dai_fmt field.
8  *              i2s_node code taken from the other sound/soc/bcm machine drivers.
9  *
10  * Copyright (C) 2016 Flatmax Pty. Ltd.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24
25 #include <sound/core.h>
26 #include <sound/soc.h>
27 #include <sound/pcm_params.h>
28 #include <sound/control.h>
29
30 #include "../codecs/wm8731.h"
31
32 static const unsigned int bcm2835_rates_12000000[] = {
33         8000, 16000, 32000, 44100, 48000, 96000, 88200,
34 };
35
36 static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
37         .list = bcm2835_rates_12000000,
38         .count = ARRAY_SIZE(bcm2835_rates_12000000),
39 };
40
41 static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
42 {
43         /* Setup constraints, because there is a 12 MHz XTAL on the board */
44         snd_pcm_hw_constraint_list(substream->runtime, 0,
45                                 SNDRV_PCM_HW_PARAM_RATE,
46                                 &bcm2835_constraints_12000000);
47         return 0;
48 }
49
50 static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
51                                        struct snd_pcm_hw_params *params)
52 {
53         struct snd_soc_pcm_runtime *rtd = substream->private_data;
54         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
55
56         switch (params_rate(params)){
57                 case 8000:
58                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
59                 case 16000:
60                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
61                 case 32000:
62                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
63                 case 44100:
64                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
65                 case 48000:
66                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
67                 case 88200:
68                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
69                 case 96000:
70                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
71                 default:
72                         return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
73         }
74 }
75
76 /* machine stream operations */
77 static struct snd_soc_ops snd_audioinjector_pi_soundcard_ops = {
78         .startup = snd_audioinjector_pi_soundcard_startup,
79         .hw_params = snd_audioinjector_pi_soundcard_hw_params,
80 };
81
82 static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
83 {
84         return snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0), WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
85 }
86
87 SND_SOC_DAILINK_DEFS(audioinjector_pi,
88         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
89         DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.1-001a", "wm8731-hifi")),
90         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
91
92 static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
93         {
94                 .name = "AudioInjector audio",
95                 .stream_name = "AudioInjector audio",
96                 .ops = &snd_audioinjector_pi_soundcard_ops,
97                 .init = audioinjector_pi_soundcard_dai_init,
98                 .dai_fmt = SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF,
99                 SND_SOC_DAILINK_REG(audioinjector_pi),
100         },
101 };
102
103 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
104         SND_SOC_DAPM_HP("Headphone Jack", NULL),
105         SND_SOC_DAPM_SPK("Ext Spk", NULL),
106         SND_SOC_DAPM_LINE("Line In Jacks", NULL),
107         SND_SOC_DAPM_MIC("Microphone", NULL),
108 };
109
110 static const struct snd_soc_dapm_route audioinjector_audio_map[] = {
111         /* headphone connected to LHPOUT, RHPOUT */
112         {"Headphone Jack", NULL, "LHPOUT"},
113         {"Headphone Jack", NULL, "RHPOUT"},
114
115         /* speaker connected to LOUT, ROUT */
116         {"Ext Spk", NULL, "ROUT"},
117         {"Ext Spk", NULL, "LOUT"},
118
119         /* line inputs */
120         {"Line In Jacks", NULL, "Line Input"},
121
122         /* mic is connected to Mic Jack, with WM8731 Mic Bias */
123         {"Microphone", NULL, "Mic Bias"},
124 };
125
126 static struct snd_soc_card snd_soc_audioinjector = {
127         .name = "audioinjector-pi-soundcard",
128         .dai_link = audioinjector_pi_soundcard_dai,
129         .num_links = ARRAY_SIZE(audioinjector_pi_soundcard_dai),
130
131         .dapm_widgets = wm8731_dapm_widgets,
132         .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
133         .dapm_routes = audioinjector_audio_map,
134         .num_dapm_routes = ARRAY_SIZE(audioinjector_audio_map),
135 };
136
137 static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
138 {
139         struct snd_soc_card *card = &snd_soc_audioinjector;
140         int ret;
141         
142         card->dev = &pdev->dev;
143
144         if (pdev->dev.of_node) {
145                 struct snd_soc_dai_link *dai = &audioinjector_pi_soundcard_dai[0];
146                 struct device_node *i2s_node = of_parse_phandle(pdev->dev.of_node,
147                                                                 "i2s-controller", 0);
148
149                 if (i2s_node) {
150                         dai->cpus->dai_name = NULL;
151                         dai->cpus->of_node = i2s_node;
152                         dai->platforms->name = NULL;
153                         dai->platforms->of_node = i2s_node;
154                 } else
155                         if (!dai->cpus->of_node) {
156                                 dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
157                                 return -EINVAL;
158                         }
159         }
160
161         if ((ret = devm_snd_soc_register_card(&pdev->dev, card)))
162                 return dev_err_probe(&pdev->dev, ret, "%s\n", __func__);
163
164         dev_info(&pdev->dev, "successfully loaded\n");
165
166         return ret;
167 }
168
169 static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
170         { .compatible = "ai,audioinjector-pi-soundcard", },
171         {},
172 };
173 MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
174
175 static struct platform_driver audioinjector_pi_soundcard_driver = {
176        .driver         = {
177                 .name   = "audioinjector-stereo",
178                 .owner  = THIS_MODULE,
179                 .of_match_table = audioinjector_pi_soundcard_of_match,
180        },
181        .probe          = audioinjector_pi_soundcard_probe,
182 };
183
184 module_platform_driver(audioinjector_pi_soundcard_driver);
185 MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
186 MODULE_DESCRIPTION("AudioInjector.net Pi Soundcard");
187 MODULE_LICENSE("GPL v2");
188 MODULE_ALIAS("platform:audioinjector-pi-soundcard");
189