ALSA: usb-audio: Validate MS endpoint descriptors
[linux-2.6-microblaze.git] / sound / soc / samsung / rx1950_uda1380.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // rx1950.c - ALSA SoC Audio Layer
4 //
5 // Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6 //
7 // Based on smdk2440.c and magician.c
8 //
9 // Authors: Graeme Gregory graeme.gregory@wolfsonmicro.com
10 //          Philipp Zabel <philipp.zabel@gmail.com>
11 //          Denis Grigoriev <dgreenday@gmail.com>
12 //          Vasily Khoruzhick <anarsoul@gmail.com>
13
14 #include <linux/types.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17
18 #include <sound/soc.h>
19 #include <sound/jack.h>
20
21 #include "regs-iis.h"
22 #include "s3c24xx-i2s.h"
23
24 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
25 static int rx1950_startup(struct snd_pcm_substream *substream);
26 static int rx1950_hw_params(struct snd_pcm_substream *substream,
27                                 struct snd_pcm_hw_params *params);
28 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
29                                 struct snd_kcontrol *kcontrol, int event);
30
31 static const unsigned int rates[] = {
32         16000,
33         44100,
34         48000,
35 };
36
37 static const struct snd_pcm_hw_constraint_list hw_rates = {
38         .count = ARRAY_SIZE(rates),
39         .list = rates,
40 };
41
42 static struct snd_soc_jack hp_jack;
43
44 static struct snd_soc_jack_pin hp_jack_pins[] = {
45         {
46                 .pin    = "Headphone Jack",
47                 .mask   = SND_JACK_HEADPHONE,
48         },
49         {
50                 .pin    = "Speaker",
51                 .mask   = SND_JACK_HEADPHONE,
52                 .invert = 1,
53         },
54 };
55
56 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
57         [0] = {
58                 .name                   = "hp-gpio",
59                 .report                 = SND_JACK_HEADPHONE,
60                 .invert                 = 1,
61                 .debounce_time          = 200,
62         },
63 };
64
65 static struct snd_soc_ops rx1950_ops = {
66         .startup        = rx1950_startup,
67         .hw_params      = rx1950_hw_params,
68 };
69
70 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
71 SND_SOC_DAILINK_DEFS(uda1380,
72         DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
73         DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a",
74                                       "uda1380-hifi")),
75         DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
76
77 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
78         {
79                 .name           = "uda1380",
80                 .stream_name    = "UDA1380 Duplex",
81                 .init           = rx1950_uda1380_init,
82                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
83                                   SND_SOC_DAIFMT_CBS_CFS,
84                 .ops            = &rx1950_ops,
85                 SND_SOC_DAILINK_REG(uda1380),
86         },
87 };
88
89 /* rx1950 machine dapm widgets */
90 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
91         SND_SOC_DAPM_HP("Headphone Jack", NULL),
92         SND_SOC_DAPM_MIC("Mic Jack", NULL),
93         SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
94 };
95
96 /* rx1950 machine audio_map */
97 static const struct snd_soc_dapm_route audio_map[] = {
98         /* headphone connected to VOUTLHP, VOUTRHP */
99         {"Headphone Jack", NULL, "VOUTLHP"},
100         {"Headphone Jack", NULL, "VOUTRHP"},
101
102         /* ext speaker connected to VOUTL, VOUTR  */
103         {"Speaker", NULL, "VOUTL"},
104         {"Speaker", NULL, "VOUTR"},
105
106         /* mic is connected to VINM */
107         {"VINM", NULL, "Mic Jack"},
108 };
109
110 static struct snd_soc_card rx1950_asoc = {
111         .name = "rx1950",
112         .owner = THIS_MODULE,
113         .dai_link = rx1950_uda1380_dai,
114         .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
115
116         .dapm_widgets = uda1380_dapm_widgets,
117         .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
118         .dapm_routes = audio_map,
119         .num_dapm_routes = ARRAY_SIZE(audio_map),
120 };
121
122 static int rx1950_startup(struct snd_pcm_substream *substream)
123 {
124         struct snd_pcm_runtime *runtime = substream->runtime;
125
126         return snd_pcm_hw_constraint_list(runtime, 0,
127                                         SNDRV_PCM_HW_PARAM_RATE,
128                                         &hw_rates);
129 }
130
131 struct gpio_desc *gpiod_speaker_power;
132
133 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
134                                 struct snd_kcontrol *kcontrol, int event)
135 {
136         if (SND_SOC_DAPM_EVENT_ON(event))
137                 gpiod_set_value(gpiod_speaker_power, 1);
138         else
139                 gpiod_set_value(gpiod_speaker_power, 0);
140
141         return 0;
142 }
143
144 static int rx1950_hw_params(struct snd_pcm_substream *substream,
145                                 struct snd_pcm_hw_params *params)
146 {
147         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
149         int div;
150         int ret;
151         unsigned int rate = params_rate(params);
152         int clk_source, fs_mode;
153
154         switch (rate) {
155         case 16000:
156         case 48000:
157                 clk_source = S3C24XX_CLKSRC_PCLK;
158                 fs_mode = S3C2410_IISMOD_256FS;
159                 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
160                 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
161                         div++;
162                 break;
163         case 44100:
164         case 88200:
165                 clk_source = S3C24XX_CLKSRC_MPLL;
166                 fs_mode = S3C2410_IISMOD_384FS;
167                 div = 1;
168                 break;
169         default:
170                 printk(KERN_ERR "%s: rate %d is not supported\n",
171                         __func__, rate);
172                 return -EINVAL;
173         }
174
175         /* select clock source */
176         ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
177                         SND_SOC_CLOCK_OUT);
178         if (ret < 0)
179                 return ret;
180
181         /* set MCLK division for sample rate */
182         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
183                 fs_mode);
184         if (ret < 0)
185                 return ret;
186
187         /* set BCLK division for sample rate */
188         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
189                 S3C2410_IISMOD_32FS);
190         if (ret < 0)
191                 return ret;
192
193         /* set prescaler division for sample rate */
194         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
195                 S3C24XX_PRESCALE(div, div));
196         if (ret < 0)
197                 return ret;
198
199         return 0;
200 }
201
202 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
203 {
204         snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
205                 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
206
207         snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
208                 hp_jack_gpios);
209
210         return 0;
211 }
212
213 static int rx1950_probe(struct platform_device *pdev)
214 {
215         struct device *dev = &pdev->dev;
216
217         /* configure some gpios */
218         gpiod_speaker_power = devm_gpiod_get(dev, "speaker-power", GPIOD_OUT_LOW);
219         if (IS_ERR(gpiod_speaker_power)) {
220                 dev_err(dev, "cannot get gpio\n");
221                 return PTR_ERR(gpiod_speaker_power);
222         }
223
224         hp_jack_gpios[0].gpiod_dev = dev;
225         rx1950_asoc.dev = dev;
226
227         return devm_snd_soc_register_card(dev, &rx1950_asoc);
228 }
229
230 struct platform_driver rx1950_audio = {
231         .driver = {
232                 .name = "rx1950-audio",
233                 .pm = &snd_soc_pm_ops,
234         },
235         .probe = rx1950_probe,
236 };
237
238 module_platform_driver(rx1950_audio);
239
240 /* Module information */
241 MODULE_AUTHOR("Vasily Khoruzhick");
242 MODULE_DESCRIPTION("ALSA SoC RX1950");
243 MODULE_LICENSE("GPL");
244 MODULE_ALIAS("platform:rx1950-audio");