blob: ca83e1878666b413ef29867908feeba01df18e9c [file] [log] [blame]
Maurus Cuelenaerece93a372010-07-03 02:46:12 +02001/* sound/soc/s3c24xx/smartq_wm8987.c
2 *
3 * Copyright 2010 Maurus Cuelenaere <mcuelenaere@gmail.com>
4 *
5 * Based on smdk6410_wm8987.c
6 * Copyright 2007 Wolfson Microelectronics PLC. - linux@wolfsonmicro.com
7 * Graeme Gregory - graeme.gregory@wolfsonmicro.com
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
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/gpio.h>
19
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
Jarkko Nikula1c8f2c42010-11-21 19:48:44 +020022#include <sound/soc.h>
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020023#include <sound/jack.h>
24
25#include <asm/mach-types.h>
26
27#include "s3c-dma.h"
28#include "s3c64xx-i2s.h"
29
30#include "../codecs/wm8750.h"
31
32/*
33 * WM8987 is register compatible with WM8750, so using that as base driver.
34 */
35
36static struct snd_soc_card snd_soc_smartq;
37
38static int smartq_hifi_hw_params(struct snd_pcm_substream *substream,
39 struct snd_pcm_hw_params *params)
40{
41 struct snd_soc_pcm_runtime *rtd = substream->private_data;
42 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
43 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
44 struct s3c_i2sv2_rate_calc div;
45 unsigned int clk = 0;
46 int ret;
47
48 s3c_i2sv2_iis_calc_rate(&div, NULL, params_rate(params),
49 s3c_i2sv2_get_clock(cpu_dai));
50
51 switch (params_rate(params)) {
52 case 8000:
53 case 16000:
54 case 32000:
55 case 48000:
56 case 96000:
57 clk = 12288000;
58 break;
59 case 11025:
60 case 22050:
61 case 44100:
62 case 88200:
63 clk = 11289600;
64 break;
65 }
66
67 /* set codec DAI configuration */
68 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
69 SND_SOC_DAIFMT_NB_NF |
70 SND_SOC_DAIFMT_CBS_CFS);
71 if (ret < 0)
72 return ret;
73
74 /* set cpu DAI configuration */
75 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
76 SND_SOC_DAIFMT_NB_NF |
77 SND_SOC_DAIFMT_CBS_CFS);
78 if (ret < 0)
79 return ret;
80
81 /* set the codec system clock for DAC and ADC */
82 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
83 SND_SOC_CLOCK_IN);
84 if (ret < 0)
85 return ret;
86
87 /* set MCLK division for sample rate */
88 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C_I2SV2_DIV_RCLK, div.fs_div);
89 if (ret < 0)
90 return ret;
91
92 /* set prescaler division for sample rate */
93 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C_I2SV2_DIV_PRESCALER,
94 div.clk_div - 1);
95 if (ret < 0)
96 return ret;
97
98 return 0;
99}
100
101/*
102 * SmartQ WM8987 HiFi DAI operations.
103 */
104static struct snd_soc_ops smartq_hifi_ops = {
105 .hw_params = smartq_hifi_hw_params,
106};
107
108static struct snd_soc_jack smartq_jack;
109
110static struct snd_soc_jack_pin smartq_jack_pins[] = {
111 /* Disable speaker when headphone is plugged in */
112 {
113 .pin = "Internal Speaker",
114 .mask = SND_JACK_HEADPHONE,
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200115 },
116};
117
118static struct snd_soc_jack_gpio smartq_jack_gpios[] = {
119 {
120 .gpio = S3C64XX_GPL(12),
121 .name = "headphone detect",
122 .report = SND_JACK_HEADPHONE,
123 .debounce_time = 200,
124 },
125};
126
127static const struct snd_kcontrol_new wm8987_smartq_controls[] = {
128 SOC_DAPM_PIN_SWITCH("Internal Speaker"),
129 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
130 SOC_DAPM_PIN_SWITCH("Internal Mic"),
131};
132
133static int smartq_speaker_event(struct snd_soc_dapm_widget *w,
134 struct snd_kcontrol *k,
135 int event)
136{
137 gpio_set_value(S3C64XX_GPK(12), SND_SOC_DAPM_EVENT_OFF(event));
138
139 return 0;
140}
141
142static const struct snd_soc_dapm_widget wm8987_dapm_widgets[] = {
143 SND_SOC_DAPM_SPK("Internal Speaker", smartq_speaker_event),
144 SND_SOC_DAPM_HP("Headphone Jack", NULL),
145 SND_SOC_DAPM_MIC("Internal Mic", NULL),
146};
147
148static const struct snd_soc_dapm_route audio_map[] = {
149 {"Headphone Jack", NULL, "LOUT2"},
150 {"Headphone Jack", NULL, "ROUT2"},
151
152 {"Internal Speaker", NULL, "LOUT2"},
153 {"Internal Speaker", NULL, "ROUT2"},
154
155 {"Mic Bias", NULL, "Internal Mic"},
156 {"LINPUT2", NULL, "Mic Bias"},
157};
158
159static int smartq_wm8987_init(struct snd_soc_codec *codec)
160{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200161 struct snd_soc_dapm_context *dapm = &codec->dapm;
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200162 int err = 0;
163
164 /* Add SmartQ specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 snd_soc_dapm_new_controls(dapm, wm8987_dapm_widgets,
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200166 ARRAY_SIZE(wm8987_dapm_widgets));
167
168 /* add SmartQ specific controls */
169 err = snd_soc_add_controls(codec, wm8987_smartq_controls,
170 ARRAY_SIZE(wm8987_smartq_controls));
171
172 if (err < 0)
173 return err;
174
175 /* setup SmartQ specific audio path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200176 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200177
178 /* set endpoints to not connected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200179 snd_soc_dapm_nc_pin(dapm, "LINPUT1");
180 snd_soc_dapm_nc_pin(dapm, "RINPUT1");
181 snd_soc_dapm_nc_pin(dapm, "OUT3");
182 snd_soc_dapm_nc_pin(dapm, "ROUT1");
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200183
184 /* set endpoints to default off mode */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200185 snd_soc_dapm_enable_pin(dapm, "Internal Speaker");
186 snd_soc_dapm_enable_pin(dapm, "Internal Mic");
187 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200188
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200189 err = snd_soc_dapm_sync(dapm);
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200190 if (err)
191 return err;
192
193 /* Headphone jack detection */
194 err = snd_soc_jack_new(&snd_soc_smartq, "Headphone Jack",
195 SND_JACK_HEADPHONE, &smartq_jack);
196 if (err)
197 return err;
198
199 err = snd_soc_jack_add_pins(&smartq_jack, ARRAY_SIZE(smartq_jack_pins),
200 smartq_jack_pins);
201 if (err)
202 return err;
203
204 err = snd_soc_jack_add_gpios(&smartq_jack,
205 ARRAY_SIZE(smartq_jack_gpios),
206 smartq_jack_gpios);
207
208 return err;
209}
210
211static struct snd_soc_dai_link smartq_dai[] = {
212 {
213 .name = "wm8987",
214 .stream_name = "SmartQ Hi-Fi",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000215 .cpu_dai_name = "s3c64xx-i2s.0",
216 .codec_dai_name = "wm8750-hifi",
Jassi Brar58bb4072010-11-22 15:35:50 +0900217 .platform_name = "samsung-audio",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000218 .codec_name = "wm8750-codec.0-0x1a",
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200219 .init = smartq_wm8987_init,
220 .ops = &smartq_hifi_ops,
221 },
222};
223
224static struct snd_soc_card snd_soc_smartq = {
225 .name = "SmartQ",
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200226 .dai_link = smartq_dai,
227 .num_links = ARRAY_SIZE(smartq_dai),
228};
229
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200230static struct platform_device *smartq_snd_device;
231
232static int __init smartq_init(void)
233{
234 int ret;
235
236 if (!machine_is_smartq7() && !machine_is_smartq5()) {
237 pr_info("Only SmartQ is supported by this ASoC driver\n");
238 return -ENODEV;
239 }
240
241 smartq_snd_device = platform_device_alloc("soc-audio", -1);
242 if (!smartq_snd_device)
243 return -ENOMEM;
244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000245 platform_set_drvdata(smartq_snd_device, &snd_soc_smartq);
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200246
247 ret = platform_device_add(smartq_snd_device);
248 if (ret) {
249 platform_device_put(smartq_snd_device);
250 return ret;
251 }
252
253 /* Initialise GPIOs used by amplifiers */
254 ret = gpio_request(S3C64XX_GPK(12), "amplifiers shutdown");
255 if (ret) {
256 dev_err(&smartq_snd_device->dev, "Failed to register GPK12\n");
257 goto err_unregister_device;
258 }
259
260 /* Disable amplifiers */
261 ret = gpio_direction_output(S3C64XX_GPK(12), 1);
262 if (ret) {
263 dev_err(&smartq_snd_device->dev, "Failed to configure GPK12\n");
264 goto err_free_gpio_amp_shut;
265 }
266
267 return 0;
268
269err_free_gpio_amp_shut:
270 gpio_free(S3C64XX_GPK(12));
271err_unregister_device:
272 platform_device_unregister(smartq_snd_device);
273
274 return ret;
275}
276
277static void __exit smartq_exit(void)
278{
279 snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios),
280 smartq_jack_gpios);
281
282 platform_device_unregister(smartq_snd_device);
283}
284
285module_init(smartq_init);
286module_exit(smartq_exit);
287
288/* Module information */
289MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>");
290MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987");
291MODULE_LICENSE("GPL");