ASoC: Replace remaining uses of snd_soc_cnew with snd_soc_add_controls.
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / sound / soc / pxa / corgi.c
1 /*
2 * corgi.c -- SoC audio for Corgi
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 *
7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Richard Purdie <richard@openedhand.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/timer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27
28 #include <asm/mach-types.h>
29 #include <mach/pxa-regs.h>
30 #include <mach/hardware.h>
31 #include <mach/corgi.h>
32 #include <mach/audio.h>
33
34 #include "../codecs/wm8731.h"
35 #include "pxa2xx-pcm.h"
36 #include "pxa2xx-i2s.h"
37
38 #define CORGI_HP 0
39 #define CORGI_MIC 1
40 #define CORGI_LINE 2
41 #define CORGI_HEADSET 3
42 #define CORGI_HP_OFF 4
43 #define CORGI_SPK_ON 0
44 #define CORGI_SPK_OFF 1
45
46 /* audio clock in Hz - rounded from 12.235MHz */
47 #define CORGI_AUDIO_CLOCK 12288000
48
49 static int corgi_jack_func;
50 static int corgi_spk_func;
51
52 static void corgi_ext_control(struct snd_soc_codec *codec)
53 {
54 /* set up jack connection */
55 switch (corgi_jack_func) {
56 case CORGI_HP:
57 /* set = unmute headphone */
58 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
59 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
60 snd_soc_dapm_disable_pin(codec, "Mic Jack");
61 snd_soc_dapm_disable_pin(codec, "Line Jack");
62 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
63 snd_soc_dapm_disable_pin(codec, "Headset Jack");
64 break;
65 case CORGI_MIC:
66 /* reset = mute headphone */
67 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
68 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
69 snd_soc_dapm_enable_pin(codec, "Mic Jack");
70 snd_soc_dapm_disable_pin(codec, "Line Jack");
71 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
72 snd_soc_dapm_disable_pin(codec, "Headset Jack");
73 break;
74 case CORGI_LINE:
75 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
76 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
77 snd_soc_dapm_disable_pin(codec, "Mic Jack");
78 snd_soc_dapm_enable_pin(codec, "Line Jack");
79 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
80 snd_soc_dapm_disable_pin(codec, "Headset Jack");
81 break;
82 case CORGI_HEADSET:
83 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
84 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
85 snd_soc_dapm_enable_pin(codec, "Mic Jack");
86 snd_soc_dapm_disable_pin(codec, "Line Jack");
87 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
88 snd_soc_dapm_enable_pin(codec, "Headset Jack");
89 break;
90 }
91
92 if (corgi_spk_func == CORGI_SPK_ON)
93 snd_soc_dapm_enable_pin(codec, "Ext Spk");
94 else
95 snd_soc_dapm_disable_pin(codec, "Ext Spk");
96
97 /* signal a DAPM event */
98 snd_soc_dapm_sync(codec);
99 }
100
101 static int corgi_startup(struct snd_pcm_substream *substream)
102 {
103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
104 struct snd_soc_codec *codec = rtd->socdev->card->codec;
105
106 /* check the jack status at stream startup */
107 corgi_ext_control(codec);
108 return 0;
109 }
110
111 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
112 static void corgi_shutdown(struct snd_pcm_substream *substream)
113 {
114 /* set = unmute headphone */
115 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
116 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
117 }
118
119 static int corgi_hw_params(struct snd_pcm_substream *substream,
120 struct snd_pcm_hw_params *params)
121 {
122 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
124 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
125 unsigned int clk = 0;
126 int ret = 0;
127
128 switch (params_rate(params)) {
129 case 8000:
130 case 16000:
131 case 48000:
132 case 96000:
133 clk = 12288000;
134 break;
135 case 11025:
136 case 22050:
137 case 44100:
138 clk = 11289600;
139 break;
140 }
141
142 /* set codec DAI configuration */
143 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
144 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
145 if (ret < 0)
146 return ret;
147
148 /* set cpu DAI configuration */
149 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
150 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
151 if (ret < 0)
152 return ret;
153
154 /* set the codec system clock for DAC and ADC */
155 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
156 SND_SOC_CLOCK_IN);
157 if (ret < 0)
158 return ret;
159
160 /* set the I2S system clock as input (unused) */
161 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
162 SND_SOC_CLOCK_IN);
163 if (ret < 0)
164 return ret;
165
166 return 0;
167 }
168
169 static struct snd_soc_ops corgi_ops = {
170 .startup = corgi_startup,
171 .hw_params = corgi_hw_params,
172 .shutdown = corgi_shutdown,
173 };
174
175 static int corgi_get_jack(struct snd_kcontrol *kcontrol,
176 struct snd_ctl_elem_value *ucontrol)
177 {
178 ucontrol->value.integer.value[0] = corgi_jack_func;
179 return 0;
180 }
181
182 static int corgi_set_jack(struct snd_kcontrol *kcontrol,
183 struct snd_ctl_elem_value *ucontrol)
184 {
185 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
186
187 if (corgi_jack_func == ucontrol->value.integer.value[0])
188 return 0;
189
190 corgi_jack_func = ucontrol->value.integer.value[0];
191 corgi_ext_control(codec);
192 return 1;
193 }
194
195 static int corgi_get_spk(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
197 {
198 ucontrol->value.integer.value[0] = corgi_spk_func;
199 return 0;
200 }
201
202 static int corgi_set_spk(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
204 {
205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
206
207 if (corgi_spk_func == ucontrol->value.integer.value[0])
208 return 0;
209
210 corgi_spk_func = ucontrol->value.integer.value[0];
211 corgi_ext_control(codec);
212 return 1;
213 }
214
215 static int corgi_amp_event(struct snd_soc_dapm_widget *w,
216 struct snd_kcontrol *k, int event)
217 {
218 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
219 return 0;
220 }
221
222 static int corgi_mic_event(struct snd_soc_dapm_widget *w,
223 struct snd_kcontrol *k, int event)
224 {
225 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
226 return 0;
227 }
228
229 /* corgi machine dapm widgets */
230 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
231 SND_SOC_DAPM_HP("Headphone Jack", NULL),
232 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
233 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
234 SND_SOC_DAPM_LINE("Line Jack", NULL),
235 SND_SOC_DAPM_HP("Headset Jack", NULL),
236 };
237
238 /* Corgi machine audio map (connections to the codec pins) */
239 static const struct snd_soc_dapm_route audio_map[] = {
240
241 /* headset Jack - in = micin, out = LHPOUT*/
242 {"Headset Jack", NULL, "LHPOUT"},
243
244 /* headphone connected to LHPOUT1, RHPOUT1 */
245 {"Headphone Jack", NULL, "LHPOUT"},
246 {"Headphone Jack", NULL, "RHPOUT"},
247
248 /* speaker connected to LOUT, ROUT */
249 {"Ext Spk", NULL, "ROUT"},
250 {"Ext Spk", NULL, "LOUT"},
251
252 /* mic is connected to MICIN (via right channel of headphone jack) */
253 {"MICIN", NULL, "Mic Jack"},
254
255 /* Same as the above but no mic bias for line signals */
256 {"MICIN", NULL, "Line Jack"},
257 };
258
259 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
260 "Off"};
261 static const char *spk_function[] = {"On", "Off"};
262 static const struct soc_enum corgi_enum[] = {
263 SOC_ENUM_SINGLE_EXT(5, jack_function),
264 SOC_ENUM_SINGLE_EXT(2, spk_function),
265 };
266
267 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
268 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
269 corgi_set_jack),
270 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
271 corgi_set_spk),
272 };
273
274 /*
275 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
276 */
277 static int corgi_wm8731_init(struct snd_soc_codec *codec)
278 {
279 int err;
280
281 snd_soc_dapm_nc_pin(codec, "LLINEIN");
282 snd_soc_dapm_nc_pin(codec, "RLINEIN");
283
284 /* Add corgi specific controls */
285 err = snd_soc_add_controls(codec, wm8731_corgi_controls,
286 ARRAY_SIZE(wm8731_corgi_controls));
287 if (err < 0)
288 return err;
289
290 /* Add corgi specific widgets */
291 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
292 ARRAY_SIZE(wm8731_dapm_widgets));
293
294 /* Set up corgi specific audio path audio_map */
295 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
296
297 snd_soc_dapm_sync(codec);
298 return 0;
299 }
300
301 /* corgi digital audio interface glue - connects codec <--> CPU */
302 static struct snd_soc_dai_link corgi_dai = {
303 .name = "WM8731",
304 .stream_name = "WM8731",
305 .cpu_dai = &pxa_i2s_dai,
306 .codec_dai = &wm8731_dai,
307 .init = corgi_wm8731_init,
308 .ops = &corgi_ops,
309 };
310
311 /* corgi audio machine driver */
312 static struct snd_soc_card snd_soc_corgi = {
313 .name = "Corgi",
314 .platform = &pxa2xx_soc_platform,
315 .dai_link = &corgi_dai,
316 .num_links = 1,
317 };
318
319 /* corgi audio subsystem */
320 static struct snd_soc_device corgi_snd_devdata = {
321 .card = &snd_soc_corgi,
322 .codec_dev = &soc_codec_dev_wm8731,
323 };
324
325 /*
326 * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
327 * New drivers should register the wm8731 I2C device in the machine
328 * setup code (under arch/arm for ARM systems).
329 */
330 static int wm8731_i2c_register(void)
331 {
332 struct i2c_board_info info;
333 struct i2c_adapter *adapter;
334 struct i2c_client *client;
335
336 memset(&info, 0, sizeof(struct i2c_board_info));
337 info.addr = 0x1b;
338 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
339
340 adapter = i2c_get_adapter(0);
341 if (!adapter) {
342 printk(KERN_ERR "can't get i2c adapter 0\n");
343 return -ENODEV;
344 }
345
346 client = i2c_new_device(adapter, &info);
347 i2c_put_adapter(adapter);
348 if (!client) {
349 printk(KERN_ERR "can't add i2c device at 0x%x\n",
350 (unsigned int)info.addr);
351 return -ENODEV;
352 }
353
354 return 0;
355 }
356
357 static struct platform_device *corgi_snd_device;
358
359 static int __init corgi_init(void)
360 {
361 int ret;
362
363 if (!(machine_is_corgi() || machine_is_shepherd() ||
364 machine_is_husky()))
365 return -ENODEV;
366
367 ret = wm8731_i2c_register();
368 if (ret != 0)
369 return ret;
370
371 corgi_snd_device = platform_device_alloc("soc-audio", -1);
372 if (!corgi_snd_device)
373 return -ENOMEM;
374
375 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
376 corgi_snd_devdata.dev = &corgi_snd_device->dev;
377 ret = platform_device_add(corgi_snd_device);
378
379 if (ret)
380 platform_device_put(corgi_snd_device);
381
382 return ret;
383 }
384
385 static void __exit corgi_exit(void)
386 {
387 platform_device_unregister(corgi_snd_device);
388 }
389
390 module_init(corgi_init);
391 module_exit(corgi_exit);
392
393 /* Module information */
394 MODULE_AUTHOR("Richard Purdie");
395 MODULE_DESCRIPTION("ALSA SoC Corgi");
396 MODULE_LICENSE("GPL");