ASoC: z2: Register jacks at the card level
[deliverable/linux.git] / sound / soc / samsung / h1940_uda1380.c
CommitLineData
1957668b
VK
1/*
2 * h1940-uda1380.c -- ALSA Soc Audio Layer
3 *
4 * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
5 * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6 *
7 * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
fd049755 16#include <linux/types.h>
1957668b 17#include <linux/gpio.h>
da155d5b 18#include <linux/module.h>
1957668b
VK
19
20#include <sound/soc.h>
1957668b
VK
21#include <sound/jack.h>
22
5d229ce5 23#include "regs-iis.h"
1957668b
VK
24#include <asm/mach-types.h>
25
abffae64 26#include <mach/gpio-samsung.h>
1957668b 27#include "s3c24xx-i2s.h"
1957668b
VK
28
29static unsigned int rates[] = {
30 11025,
31 22050,
32 44100,
33};
34
35static struct snd_pcm_hw_constraint_list hw_rates = {
36 .count = ARRAY_SIZE(rates),
37 .list = rates,
38 .mask = 0,
39};
40
41static struct snd_soc_jack hp_jack;
42
43static struct snd_soc_jack_pin hp_jack_pins[] = {
44 {
45 .pin = "Headphone Jack",
46 .mask = SND_JACK_HEADPHONE,
47 },
48 {
49 .pin = "Speaker",
50 .mask = SND_JACK_HEADPHONE,
51 .invert = 1,
52 },
53};
54
55static struct snd_soc_jack_gpio hp_jack_gpios[] = {
56 {
57 .gpio = S3C2410_GPG(4),
58 .name = "hp-gpio",
59 .report = SND_JACK_HEADPHONE,
60 .invert = 1,
61 .debounce_time = 200,
62 },
63};
64
65static int h1940_startup(struct snd_pcm_substream *substream)
66{
67 struct snd_pcm_runtime *runtime = substream->runtime;
68
1957668b
VK
69 return snd_pcm_hw_constraint_list(runtime, 0,
70 SNDRV_PCM_HW_PARAM_RATE,
71 &hw_rates);
72}
73
74static int h1940_hw_params(struct snd_pcm_substream *substream,
75 struct snd_pcm_hw_params *params)
76{
77 struct snd_soc_pcm_runtime *rtd = substream->private_data;
78 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1957668b
VK
79 int div;
80 int ret;
81 unsigned int rate = params_rate(params);
82
83 switch (rate) {
84 case 11025:
85 case 22050:
86 case 44100:
87 div = s3c24xx_i2s_get_clockrate() / (384 * rate);
88 if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
89 div++;
90 break;
91 default:
2046d453 92 dev_err(rtd->dev, "%s: rate %d is not supported\n",
1957668b
VK
93 __func__, rate);
94 return -EINVAL;
95 }
96
1957668b
VK
97 /* select clock source */
98 ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
99 SND_SOC_CLOCK_OUT);
100 if (ret < 0)
101 return ret;
102
103 /* set MCLK division for sample rate */
104 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
105 S3C2410_IISMOD_384FS);
106 if (ret < 0)
107 return ret;
108
109 /* set BCLK division for sample rate */
110 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
111 S3C2410_IISMOD_32FS);
112 if (ret < 0)
113 return ret;
114
115 /* set prescaler division for sample rate */
116 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
117 S3C24XX_PRESCALE(div, div));
118 if (ret < 0)
119 return ret;
120
121 return 0;
122}
123
124static struct snd_soc_ops h1940_ops = {
125 .startup = h1940_startup,
126 .hw_params = h1940_hw_params,
127};
128
129static int h1940_spk_power(struct snd_soc_dapm_widget *w,
130 struct snd_kcontrol *kcontrol, int event)
131{
132 if (SND_SOC_DAPM_EVENT_ON(event))
232910d6 133 gpio_set_value(S3C_GPIO_END + 9, 1);
1957668b 134 else
232910d6 135 gpio_set_value(S3C_GPIO_END + 9, 0);
1957668b
VK
136
137 return 0;
138}
139
140/* h1940 machine dapm widgets */
141static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
142 SND_SOC_DAPM_HP("Headphone Jack", NULL),
143 SND_SOC_DAPM_MIC("Mic Jack", NULL),
144 SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
145};
146
147/* h1940 machine audio_map */
148static const struct snd_soc_dapm_route audio_map[] = {
149 /* headphone connected to VOUTLHP, VOUTRHP */
150 {"Headphone Jack", NULL, "VOUTLHP"},
151 {"Headphone Jack", NULL, "VOUTRHP"},
152
153 /* ext speaker connected to VOUTL, VOUTR */
154 {"Speaker", NULL, "VOUTL"},
155 {"Speaker", NULL, "VOUTR"},
156
157 /* mic is connected to VINM */
158 {"VINM", NULL, "Mic Jack"},
159};
160
161static struct platform_device *s3c24xx_snd_device;
162
163static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
164{
165 struct snd_soc_codec *codec = rtd->codec;
1957668b 166
1957668b
VK
167 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
168 &hp_jack);
169
170 snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
171 hp_jack_pins);
172
173 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
174 hp_jack_gpios);
175
176 return 0;
177}
178
16088cb6 179static int h1940_uda1380_card_remove(struct snd_soc_card *card)
e1d4d3c8
SW
180{
181 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
182 hp_jack_gpios);
183
184 return 0;
185}
186
1957668b
VK
187/* s3c24xx digital audio interface glue - connects codec <--> CPU */
188static struct snd_soc_dai_link h1940_uda1380_dai[] = {
189 {
190 .name = "uda1380",
191 .stream_name = "UDA1380 Duplex",
192 .cpu_dai_name = "s3c24xx-iis",
193 .codec_dai_name = "uda1380-hifi",
194 .init = h1940_uda1380_init,
a08485d8 195 .platform_name = "s3c24xx-iis",
1957668b 196 .codec_name = "uda1380-codec.0-001a",
5cc10b9b
LPC
197 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
198 SND_SOC_DAIFMT_CBS_CFS,
1957668b
VK
199 .ops = &h1940_ops,
200 },
201};
202
203static struct snd_soc_card h1940_asoc = {
204 .name = "h1940",
095d79dc 205 .owner = THIS_MODULE,
e1d4d3c8 206 .remove = h1940_uda1380_card_remove,
1957668b
VK
207 .dai_link = h1940_uda1380_dai,
208 .num_links = ARRAY_SIZE(h1940_uda1380_dai),
6119d016
MB
209
210 .dapm_widgets = uda1380_dapm_widgets,
211 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
212 .dapm_routes = audio_map,
213 .num_dapm_routes = ARRAY_SIZE(audio_map),
1957668b
VK
214};
215
216static int __init h1940_init(void)
217{
218 int ret;
219
220 if (!machine_is_h1940())
221 return -ENODEV;
222
223 /* configure some gpios */
232910d6 224 ret = gpio_request(S3C_GPIO_END + 9, "speaker-power");
1957668b
VK
225 if (ret)
226 goto err_out;
227
232910d6 228 ret = gpio_direction_output(S3C_GPIO_END + 9, 0);
1957668b
VK
229 if (ret)
230 goto err_gpio;
231
232 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
233 if (!s3c24xx_snd_device) {
234 ret = -ENOMEM;
235 goto err_gpio;
236 }
237
238 platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
239 ret = platform_device_add(s3c24xx_snd_device);
240
241 if (ret)
242 goto err_plat;
243
244 return 0;
245
246err_plat:
247 platform_device_put(s3c24xx_snd_device);
248err_gpio:
232910d6 249 gpio_free(S3C_GPIO_END + 9);
1957668b
VK
250
251err_out:
252 return ret;
253}
254
255static void __exit h1940_exit(void)
256{
257 platform_device_unregister(s3c24xx_snd_device);
232910d6 258 gpio_free(S3C_GPIO_END + 9);
1957668b
VK
259}
260
261module_init(h1940_init);
262module_exit(h1940_exit);
263
264/* Module information */
265MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
266MODULE_DESCRIPTION("ALSA SoC H1940");
267MODULE_LICENSE("GPL");
This page took 0.212913 seconds and 5 git commands to generate.