ASoC: samsung: use asoc_substream_to_rtd()
[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.h>
16 #include <linux/module.h>
17
18 #include <sound/soc.h>
19 #include <sound/jack.h>
20
21 #include <mach/gpio-samsung.h>
22 #include "regs-iis.h"
23 #include <asm/mach-types.h>
24
25 #include "s3c24xx-i2s.h"
26
27 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
28 static int rx1950_startup(struct snd_pcm_substream *substream);
29 static int rx1950_hw_params(struct snd_pcm_substream *substream,
30                                 struct snd_pcm_hw_params *params);
31 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
32                                 struct snd_kcontrol *kcontrol, int event);
33
34 static const unsigned int rates[] = {
35         16000,
36         44100,
37         48000,
38 };
39
40 static const struct snd_pcm_hw_constraint_list hw_rates = {
41         .count = ARRAY_SIZE(rates),
42         .list = rates,
43 };
44
45 static struct snd_soc_jack hp_jack;
46
47 static struct snd_soc_jack_pin hp_jack_pins[] = {
48         {
49                 .pin    = "Headphone Jack",
50                 .mask   = SND_JACK_HEADPHONE,
51         },
52         {
53                 .pin    = "Speaker",
54                 .mask   = SND_JACK_HEADPHONE,
55                 .invert = 1,
56         },
57 };
58
59 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
60         [0] = {
61                 .gpio                   = S3C2410_GPG(12),
62                 .name                   = "hp-gpio",
63                 .report                 = SND_JACK_HEADPHONE,
64                 .invert                 = 1,
65                 .debounce_time          = 200,
66         },
67 };
68
69 static struct snd_soc_ops rx1950_ops = {
70         .startup        = rx1950_startup,
71         .hw_params      = rx1950_hw_params,
72 };
73
74 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
75 SND_SOC_DAILINK_DEFS(uda1380,
76         DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
77         DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-001a",
78                                       "uda1380-hifi")),
79         DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
80
81 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
82         {
83                 .name           = "uda1380",
84                 .stream_name    = "UDA1380 Duplex",
85                 .init           = rx1950_uda1380_init,
86                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
87                                   SND_SOC_DAIFMT_CBS_CFS,
88                 .ops            = &rx1950_ops,
89                 SND_SOC_DAILINK_REG(uda1380),
90         },
91 };
92
93 /* rx1950 machine dapm widgets */
94 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
95         SND_SOC_DAPM_HP("Headphone Jack", NULL),
96         SND_SOC_DAPM_MIC("Mic Jack", NULL),
97         SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
98 };
99
100 /* rx1950 machine audio_map */
101 static const struct snd_soc_dapm_route audio_map[] = {
102         /* headphone connected to VOUTLHP, VOUTRHP */
103         {"Headphone Jack", NULL, "VOUTLHP"},
104         {"Headphone Jack", NULL, "VOUTRHP"},
105
106         /* ext speaker connected to VOUTL, VOUTR  */
107         {"Speaker", NULL, "VOUTL"},
108         {"Speaker", NULL, "VOUTR"},
109
110         /* mic is connected to VINM */
111         {"VINM", NULL, "Mic Jack"},
112 };
113
114 static struct snd_soc_card rx1950_asoc = {
115         .name = "rx1950",
116         .owner = THIS_MODULE,
117         .dai_link = rx1950_uda1380_dai,
118         .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
119
120         .dapm_widgets = uda1380_dapm_widgets,
121         .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
122         .dapm_routes = audio_map,
123         .num_dapm_routes = ARRAY_SIZE(audio_map),
124 };
125
126 static struct platform_device *s3c24xx_snd_device;
127
128 static int rx1950_startup(struct snd_pcm_substream *substream)
129 {
130         struct snd_pcm_runtime *runtime = substream->runtime;
131
132         return snd_pcm_hw_constraint_list(runtime, 0,
133                                         SNDRV_PCM_HW_PARAM_RATE,
134                                         &hw_rates);
135 }
136
137 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
138                                 struct snd_kcontrol *kcontrol, int event)
139 {
140         if (SND_SOC_DAPM_EVENT_ON(event))
141                 gpio_set_value(S3C2410_GPA(1), 1);
142         else
143                 gpio_set_value(S3C2410_GPA(1), 0);
144
145         return 0;
146 }
147
148 static int rx1950_hw_params(struct snd_pcm_substream *substream,
149                                 struct snd_pcm_hw_params *params)
150 {
151         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
152         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
153         int div;
154         int ret;
155         unsigned int rate = params_rate(params);
156         int clk_source, fs_mode;
157
158         switch (rate) {
159         case 16000:
160         case 48000:
161                 clk_source = S3C24XX_CLKSRC_PCLK;
162                 fs_mode = S3C2410_IISMOD_256FS;
163                 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
164                 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
165                         div++;
166                 break;
167         case 44100:
168         case 88200:
169                 clk_source = S3C24XX_CLKSRC_MPLL;
170                 fs_mode = S3C2410_IISMOD_384FS;
171                 div = 1;
172                 break;
173         default:
174                 printk(KERN_ERR "%s: rate %d is not supported\n",
175                         __func__, rate);
176                 return -EINVAL;
177         }
178
179         /* select clock source */
180         ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
181                         SND_SOC_CLOCK_OUT);
182         if (ret < 0)
183                 return ret;
184
185         /* set MCLK division for sample rate */
186         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
187                 fs_mode);
188         if (ret < 0)
189                 return ret;
190
191         /* set BCLK division for sample rate */
192         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
193                 S3C2410_IISMOD_32FS);
194         if (ret < 0)
195                 return ret;
196
197         /* set prescaler division for sample rate */
198         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
199                 S3C24XX_PRESCALE(div, div));
200         if (ret < 0)
201                 return ret;
202
203         return 0;
204 }
205
206 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
207 {
208         snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
209                 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
210
211         snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
212                 hp_jack_gpios);
213
214         return 0;
215 }
216
217 static int __init rx1950_init(void)
218 {
219         int ret;
220
221         if (!machine_is_rx1950())
222                 return -ENODEV;
223
224         /* configure some gpios */
225         ret = gpio_request(S3C2410_GPA(1), "speaker-power");
226         if (ret)
227                 goto err_gpio;
228
229         ret = gpio_direction_output(S3C2410_GPA(1), 0);
230         if (ret)
231                 goto err_gpio_conf;
232
233         s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
234         if (!s3c24xx_snd_device) {
235                 ret = -ENOMEM;
236                 goto err_plat_alloc;
237         }
238
239         platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
240         ret = platform_device_add(s3c24xx_snd_device);
241
242         if (ret) {
243                 platform_device_put(s3c24xx_snd_device);
244                 goto err_plat_add;
245         }
246
247         return 0;
248
249 err_plat_add:
250 err_plat_alloc:
251 err_gpio_conf:
252         gpio_free(S3C2410_GPA(1));
253
254 err_gpio:
255         return ret;
256 }
257
258 static void __exit rx1950_exit(void)
259 {
260         platform_device_unregister(s3c24xx_snd_device);
261         gpio_free(S3C2410_GPA(1));
262 }
263
264 module_init(rx1950_init);
265 module_exit(rx1950_exit);
266
267 /* Module information */
268 MODULE_AUTHOR("Vasily Khoruzhick");
269 MODULE_DESCRIPTION("ALSA SoC RX1950");
270 MODULE_LICENSE("GPL");