blob: 0a2c4f22303811b55921452f783ae485fefb3bdd [file] [log] [blame]
Jassi Brar5033f432010-11-22 15:37:25 +09001/* sound/soc/samsung/smartq_wm8987.c
Maurus Cuelenaerece93a372010-07-03 02:46:12 +02002 *
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
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020016#include <linux/gpio.h>
17
Jarkko Nikula1c8f2c42010-11-21 19:48:44 +020018#include <sound/soc.h>
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020019#include <sound/jack.h>
20
21#include <asm/mach-types.h>
22
Jassi Brarb9493d62010-11-22 15:37:02 +090023#include "i2s.h"
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020024#include "../codecs/wm8750.h"
25
26/*
27 * WM8987 is register compatible with WM8750, so using that as base driver.
28 */
29
30static struct snd_soc_card snd_soc_smartq;
31
32static int smartq_hifi_hw_params(struct snd_pcm_substream *substream,
33 struct snd_pcm_hw_params *params)
34{
35 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Axel Lin74bd21e2010-11-25 13:43:49 +080036 struct snd_soc_dai *codec_dai = rtd->codec_dai;
37 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020038 unsigned int clk = 0;
39 int ret;
40
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020041 switch (params_rate(params)) {
42 case 8000:
43 case 16000:
44 case 32000:
45 case 48000:
46 case 96000:
47 clk = 12288000;
48 break;
49 case 11025:
50 case 22050:
51 case 44100:
52 case 88200:
53 clk = 11289600;
54 break;
55 }
56
57 /* set codec DAI configuration */
58 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
59 SND_SOC_DAIFMT_NB_NF |
60 SND_SOC_DAIFMT_CBS_CFS);
61 if (ret < 0)
62 return ret;
63
64 /* set cpu DAI configuration */
65 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
66 SND_SOC_DAIFMT_NB_NF |
67 SND_SOC_DAIFMT_CBS_CFS);
68 if (ret < 0)
69 return ret;
70
Jassi Brarb9493d62010-11-22 15:37:02 +090071 /* Use PCLK for I2S signal generation */
72 ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
73 0, SND_SOC_CLOCK_IN);
74 if (ret < 0)
75 return ret;
76
77 /* Gate the RCLK output on PAD */
78 ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
79 0, SND_SOC_CLOCK_IN);
80 if (ret < 0)
81 return ret;
82
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020083 /* set the codec system clock for DAC and ADC */
84 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
85 SND_SOC_CLOCK_IN);
86 if (ret < 0)
87 return ret;
88
Maurus Cuelenaerece93a372010-07-03 02:46:12 +020089 return 0;
90}
91
92/*
93 * SmartQ WM8987 HiFi DAI operations.
94 */
95static struct snd_soc_ops smartq_hifi_ops = {
96 .hw_params = smartq_hifi_hw_params,
97};
98
99static struct snd_soc_jack smartq_jack;
100
101static struct snd_soc_jack_pin smartq_jack_pins[] = {
102 /* Disable speaker when headphone is plugged in */
103 {
104 .pin = "Internal Speaker",
105 .mask = SND_JACK_HEADPHONE,
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200106 },
107};
108
109static struct snd_soc_jack_gpio smartq_jack_gpios[] = {
110 {
111 .gpio = S3C64XX_GPL(12),
112 .name = "headphone detect",
113 .report = SND_JACK_HEADPHONE,
114 .debounce_time = 200,
115 },
116};
117
118static const struct snd_kcontrol_new wm8987_smartq_controls[] = {
119 SOC_DAPM_PIN_SWITCH("Internal Speaker"),
120 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
121 SOC_DAPM_PIN_SWITCH("Internal Mic"),
122};
123
124static int smartq_speaker_event(struct snd_soc_dapm_widget *w,
125 struct snd_kcontrol *k,
126 int event)
127{
128 gpio_set_value(S3C64XX_GPK(12), SND_SOC_DAPM_EVENT_OFF(event));
129
130 return 0;
131}
132
133static const struct snd_soc_dapm_widget wm8987_dapm_widgets[] = {
134 SND_SOC_DAPM_SPK("Internal Speaker", smartq_speaker_event),
135 SND_SOC_DAPM_HP("Headphone Jack", NULL),
136 SND_SOC_DAPM_MIC("Internal Mic", NULL),
137};
138
139static const struct snd_soc_dapm_route audio_map[] = {
140 {"Headphone Jack", NULL, "LOUT2"},
141 {"Headphone Jack", NULL, "ROUT2"},
142
143 {"Internal Speaker", NULL, "LOUT2"},
144 {"Internal Speaker", NULL, "ROUT2"},
145
146 {"Mic Bias", NULL, "Internal Mic"},
147 {"LINPUT2", NULL, "Mic Bias"},
148};
149
Axel Lin74bd21e2010-11-25 13:43:49 +0800150static int smartq_wm8987_init(struct snd_soc_pcm_runtime *rtd)
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200151{
Axel Lin74bd21e2010-11-25 13:43:49 +0800152 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 struct snd_soc_dapm_context *dapm = &codec->dapm;
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200154 int err = 0;
155
156 /* Add SmartQ specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200157 snd_soc_dapm_new_controls(dapm, wm8987_dapm_widgets,
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200158 ARRAY_SIZE(wm8987_dapm_widgets));
159
160 /* add SmartQ specific controls */
161 err = snd_soc_add_controls(codec, wm8987_smartq_controls,
162 ARRAY_SIZE(wm8987_smartq_controls));
163
164 if (err < 0)
165 return err;
166
167 /* setup SmartQ specific audio path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200169
170 /* set endpoints to not connected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200171 snd_soc_dapm_nc_pin(dapm, "LINPUT1");
172 snd_soc_dapm_nc_pin(dapm, "RINPUT1");
173 snd_soc_dapm_nc_pin(dapm, "OUT3");
174 snd_soc_dapm_nc_pin(dapm, "ROUT1");
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200175
176 /* set endpoints to default off mode */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200177 snd_soc_dapm_enable_pin(dapm, "Internal Speaker");
178 snd_soc_dapm_enable_pin(dapm, "Internal Mic");
179 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200180
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200181 err = snd_soc_dapm_sync(dapm);
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200182 if (err)
183 return err;
184
185 /* Headphone jack detection */
Axel Lin74bd21e2010-11-25 13:43:49 +0800186 err = snd_soc_jack_new(codec, "Headphone Jack",
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200187 SND_JACK_HEADPHONE, &smartq_jack);
188 if (err)
189 return err;
190
191 err = snd_soc_jack_add_pins(&smartq_jack, ARRAY_SIZE(smartq_jack_pins),
192 smartq_jack_pins);
193 if (err)
194 return err;
195
196 err = snd_soc_jack_add_gpios(&smartq_jack,
197 ARRAY_SIZE(smartq_jack_gpios),
198 smartq_jack_gpios);
199
200 return err;
201}
202
203static struct snd_soc_dai_link smartq_dai[] = {
204 {
205 .name = "wm8987",
206 .stream_name = "SmartQ Hi-Fi",
Jassi Brarb9493d62010-11-22 15:37:02 +0900207 .cpu_dai_name = "samsung-i2s.0",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000208 .codec_dai_name = "wm8750-hifi",
Jassi Brar58bb4072010-11-22 15:35:50 +0900209 .platform_name = "samsung-audio",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000210 .codec_name = "wm8750-codec.0-0x1a",
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200211 .init = smartq_wm8987_init,
212 .ops = &smartq_hifi_ops,
213 },
214};
215
216static struct snd_soc_card snd_soc_smartq = {
217 .name = "SmartQ",
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200218 .dai_link = smartq_dai,
219 .num_links = ARRAY_SIZE(smartq_dai),
220};
221
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200222static struct platform_device *smartq_snd_device;
223
224static int __init smartq_init(void)
225{
226 int ret;
227
228 if (!machine_is_smartq7() && !machine_is_smartq5()) {
229 pr_info("Only SmartQ is supported by this ASoC driver\n");
230 return -ENODEV;
231 }
232
233 smartq_snd_device = platform_device_alloc("soc-audio", -1);
234 if (!smartq_snd_device)
235 return -ENOMEM;
236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000237 platform_set_drvdata(smartq_snd_device, &snd_soc_smartq);
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200238
239 ret = platform_device_add(smartq_snd_device);
240 if (ret) {
241 platform_device_put(smartq_snd_device);
242 return ret;
243 }
244
245 /* Initialise GPIOs used by amplifiers */
246 ret = gpio_request(S3C64XX_GPK(12), "amplifiers shutdown");
247 if (ret) {
248 dev_err(&smartq_snd_device->dev, "Failed to register GPK12\n");
249 goto err_unregister_device;
250 }
251
252 /* Disable amplifiers */
253 ret = gpio_direction_output(S3C64XX_GPK(12), 1);
254 if (ret) {
255 dev_err(&smartq_snd_device->dev, "Failed to configure GPK12\n");
256 goto err_free_gpio_amp_shut;
257 }
258
259 return 0;
260
261err_free_gpio_amp_shut:
262 gpio_free(S3C64XX_GPK(12));
263err_unregister_device:
264 platform_device_unregister(smartq_snd_device);
265
266 return ret;
267}
268
269static void __exit smartq_exit(void)
270{
Axel Lin3790f202010-11-26 14:53:38 +0800271 gpio_free(S3C64XX_GPK(12));
Maurus Cuelenaerece93a372010-07-03 02:46:12 +0200272 snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios),
273 smartq_jack_gpios);
274
275 platform_device_unregister(smartq_snd_device);
276}
277
278module_init(smartq_init);
279module_exit(smartq_exit);
280
281/* Module information */
282MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>");
283MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987");
284MODULE_LICENSE("GPL");