Merge tag 'socfpga_arm32_defconfig_for_v5.2' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.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 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
76         {
77                 .name           = "uda1380",
78                 .stream_name    = "UDA1380 Duplex",
79                 .cpu_dai_name   = "s3c24xx-iis",
80                 .codec_dai_name = "uda1380-hifi",
81                 .init           = rx1950_uda1380_init,
82                 .platform_name  = "s3c24xx-iis",
83                 .codec_name     = "uda1380-codec.0-001a",
84                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
85                                   SND_SOC_DAIFMT_CBS_CFS,
86                 .ops            = &rx1950_ops,
87         },
88 };
89
90 /* rx1950 machine dapm widgets */
91 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
92         SND_SOC_DAPM_HP("Headphone Jack", NULL),
93         SND_SOC_DAPM_MIC("Mic Jack", NULL),
94         SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
95 };
96
97 /* rx1950 machine audio_map */
98 static const struct snd_soc_dapm_route audio_map[] = {
99         /* headphone connected to VOUTLHP, VOUTRHP */
100         {"Headphone Jack", NULL, "VOUTLHP"},
101         {"Headphone Jack", NULL, "VOUTRHP"},
102
103         /* ext speaker connected to VOUTL, VOUTR  */
104         {"Speaker", NULL, "VOUTL"},
105         {"Speaker", NULL, "VOUTR"},
106
107         /* mic is connected to VINM */
108         {"VINM", NULL, "Mic Jack"},
109 };
110
111 static struct snd_soc_card rx1950_asoc = {
112         .name = "rx1950",
113         .owner = THIS_MODULE,
114         .dai_link = rx1950_uda1380_dai,
115         .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
116
117         .dapm_widgets = uda1380_dapm_widgets,
118         .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
119         .dapm_routes = audio_map,
120         .num_dapm_routes = ARRAY_SIZE(audio_map),
121 };
122
123 static struct platform_device *s3c24xx_snd_device;
124
125 static int rx1950_startup(struct snd_pcm_substream *substream)
126 {
127         struct snd_pcm_runtime *runtime = substream->runtime;
128
129         return snd_pcm_hw_constraint_list(runtime, 0,
130                                         SNDRV_PCM_HW_PARAM_RATE,
131                                         &hw_rates);
132 }
133
134 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
135                                 struct snd_kcontrol *kcontrol, int event)
136 {
137         if (SND_SOC_DAPM_EVENT_ON(event))
138                 gpio_set_value(S3C2410_GPA(1), 1);
139         else
140                 gpio_set_value(S3C2410_GPA(1), 0);
141
142         return 0;
143 }
144
145 static int rx1950_hw_params(struct snd_pcm_substream *substream,
146                                 struct snd_pcm_hw_params *params)
147 {
148         struct snd_soc_pcm_runtime *rtd = substream->private_data;
149         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
150         int div;
151         int ret;
152         unsigned int rate = params_rate(params);
153         int clk_source, fs_mode;
154
155         switch (rate) {
156         case 16000:
157         case 48000:
158                 clk_source = S3C24XX_CLKSRC_PCLK;
159                 fs_mode = S3C2410_IISMOD_256FS;
160                 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
161                 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
162                         div++;
163                 break;
164         case 44100:
165         case 88200:
166                 clk_source = S3C24XX_CLKSRC_MPLL;
167                 fs_mode = S3C2410_IISMOD_384FS;
168                 div = 1;
169                 break;
170         default:
171                 printk(KERN_ERR "%s: rate %d is not supported\n",
172                         __func__, rate);
173                 return -EINVAL;
174         }
175
176         /* select clock source */
177         ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
178                         SND_SOC_CLOCK_OUT);
179         if (ret < 0)
180                 return ret;
181
182         /* set MCLK division for sample rate */
183         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
184                 fs_mode);
185         if (ret < 0)
186                 return ret;
187
188         /* set BCLK division for sample rate */
189         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
190                 S3C2410_IISMOD_32FS);
191         if (ret < 0)
192                 return ret;
193
194         /* set prescaler division for sample rate */
195         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
196                 S3C24XX_PRESCALE(div, div));
197         if (ret < 0)
198                 return ret;
199
200         return 0;
201 }
202
203 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
204 {
205         snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
206                 &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
207
208         snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
209                 hp_jack_gpios);
210
211         return 0;
212 }
213
214 static int __init rx1950_init(void)
215 {
216         int ret;
217
218         if (!machine_is_rx1950())
219                 return -ENODEV;
220
221         /* configure some gpios */
222         ret = gpio_request(S3C2410_GPA(1), "speaker-power");
223         if (ret)
224                 goto err_gpio;
225
226         ret = gpio_direction_output(S3C2410_GPA(1), 0);
227         if (ret)
228                 goto err_gpio_conf;
229
230         s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
231         if (!s3c24xx_snd_device) {
232                 ret = -ENOMEM;
233                 goto err_plat_alloc;
234         }
235
236         platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
237         ret = platform_device_add(s3c24xx_snd_device);
238
239         if (ret) {
240                 platform_device_put(s3c24xx_snd_device);
241                 goto err_plat_add;
242         }
243
244         return 0;
245
246 err_plat_add:
247 err_plat_alloc:
248 err_gpio_conf:
249         gpio_free(S3C2410_GPA(1));
250
251 err_gpio:
252         return ret;
253 }
254
255 static void __exit rx1950_exit(void)
256 {
257         platform_device_unregister(s3c24xx_snd_device);
258         gpio_free(S3C2410_GPA(1));
259 }
260
261 module_init(rx1950_init);
262 module_exit(rx1950_exit);
263
264 /* Module information */
265 MODULE_AUTHOR("Vasily Khoruzhick");
266 MODULE_DESCRIPTION("ALSA SoC RX1950");
267 MODULE_LICENSE("GPL");