Merge branch 'for-3.1' into for-3.2
[linux-block.git] / sound / soc / mid-x86 / mfld_machine.c
CommitLineData
55c72036
VK
1/*
2 * mfld_machine.c - ASoc Machine driver for Intel Medfield MID platform
3 *
4 * Copyright (C) 2010 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/slab.h>
42aee9b4 30#include <linux/io.h>
55c72036
VK
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
42aee9b4 34#include <sound/jack.h>
55c72036
VK
35#include "../codecs/sn95031.h"
36
37#define MID_MONO 1
38#define MID_STEREO 2
39#define MID_MAX_CAP 5
42aee9b4
VK
40#define MFLD_JACK_INSERT 0x04
41
42enum soc_mic_bias_zones {
43 MFLD_MV_START = 0,
44 /* mic bias volutage range for Headphones*/
45 MFLD_MV_HP = 400,
46 /* mic bias volutage range for American Headset*/
47 MFLD_MV_AM_HS = 650,
48 /* mic bias volutage range for Headset*/
49 MFLD_MV_HS = 2000,
50 MFLD_MV_UNDEFINED,
51};
55c72036
VK
52
53static unsigned int hs_switch;
54static unsigned int lo_dac;
55
42aee9b4 56struct mfld_mc_private {
42aee9b4 57 void __iomem *int_base;
42aee9b4
VK
58 u8 interrupt_status;
59};
60
61struct snd_soc_jack mfld_jack;
62
63/*Headset jack detection DAPM pins */
64static struct snd_soc_jack_pin mfld_jack_pins[] = {
65 {
66 .pin = "Headphones",
67 .mask = SND_JACK_HEADPHONE,
68 },
69 {
70 .pin = "AMIC1",
71 .mask = SND_JACK_MICROPHONE,
72 },
73};
74
7ae74340
VK
75/* jack detection voltage zones */
76static struct snd_soc_jack_zone mfld_zones[] = {
77 {MFLD_MV_START, MFLD_MV_AM_HS, SND_JACK_HEADPHONE},
78 {MFLD_MV_AM_HS, MFLD_MV_HS, SND_JACK_HEADSET},
79};
80
55c72036
VK
81/* sound card controls */
82static const char *headset_switch_text[] = {"Earpiece", "Headset"};
83
84static const char *lo_text[] = {"Vibra", "Headset", "IHF", "None"};
85
86static const struct soc_enum headset_enum =
87 SOC_ENUM_SINGLE_EXT(2, headset_switch_text);
88
89static const struct soc_enum lo_enum =
90 SOC_ENUM_SINGLE_EXT(4, lo_text);
91
92static int headset_get_switch(struct snd_kcontrol *kcontrol,
93 struct snd_ctl_elem_value *ucontrol)
94{
95 ucontrol->value.integer.value[0] = hs_switch;
96 return 0;
97}
98
99static int headset_set_switch(struct snd_kcontrol *kcontrol,
100 struct snd_ctl_elem_value *ucontrol)
101{
102 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
103
104 if (ucontrol->value.integer.value[0] == hs_switch)
105 return 0;
106
107 if (ucontrol->value.integer.value[0]) {
108 pr_debug("hs_set HS path\n");
42aee9b4 109 snd_soc_dapm_enable_pin(&codec->dapm, "Headphones");
55c72036
VK
110 snd_soc_dapm_disable_pin(&codec->dapm, "EPOUT");
111 } else {
112 pr_debug("hs_set EP path\n");
42aee9b4 113 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
55c72036
VK
114 snd_soc_dapm_enable_pin(&codec->dapm, "EPOUT");
115 }
116 snd_soc_dapm_sync(&codec->dapm);
117 hs_switch = ucontrol->value.integer.value[0];
118
119 return 0;
120}
121
122static void lo_enable_out_pins(struct snd_soc_codec *codec)
123{
124 snd_soc_dapm_enable_pin(&codec->dapm, "IHFOUTL");
125 snd_soc_dapm_enable_pin(&codec->dapm, "IHFOUTR");
126 snd_soc_dapm_enable_pin(&codec->dapm, "LINEOUTL");
127 snd_soc_dapm_enable_pin(&codec->dapm, "LINEOUTR");
128 snd_soc_dapm_enable_pin(&codec->dapm, "VIB1OUT");
129 snd_soc_dapm_enable_pin(&codec->dapm, "VIB2OUT");
130 if (hs_switch) {
42aee9b4 131 snd_soc_dapm_enable_pin(&codec->dapm, "Headphones");
55c72036
VK
132 snd_soc_dapm_disable_pin(&codec->dapm, "EPOUT");
133 } else {
42aee9b4 134 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
55c72036
VK
135 snd_soc_dapm_enable_pin(&codec->dapm, "EPOUT");
136 }
137}
138
139static int lo_get_switch(struct snd_kcontrol *kcontrol,
140 struct snd_ctl_elem_value *ucontrol)
141{
142 ucontrol->value.integer.value[0] = lo_dac;
143 return 0;
144}
145
146static int lo_set_switch(struct snd_kcontrol *kcontrol,
147 struct snd_ctl_elem_value *ucontrol)
148{
149 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
150
151 if (ucontrol->value.integer.value[0] == lo_dac)
152 return 0;
153
154 /* we dont want to work with last state of lineout so just enable all
155 * pins and then disable pins not required
156 */
157 lo_enable_out_pins(codec);
158 switch (ucontrol->value.integer.value[0]) {
159 case 0:
160 pr_debug("set vibra path\n");
161 snd_soc_dapm_disable_pin(&codec->dapm, "VIB1OUT");
162 snd_soc_dapm_disable_pin(&codec->dapm, "VIB2OUT");
163 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0);
164 break;
165
166 case 1:
167 pr_debug("set hs path\n");
42aee9b4 168 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
55c72036
VK
169 snd_soc_dapm_disable_pin(&codec->dapm, "EPOUT");
170 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x22);
171 break;
172
173 case 2:
174 pr_debug("set spkr path\n");
175 snd_soc_dapm_disable_pin(&codec->dapm, "IHFOUTL");
176 snd_soc_dapm_disable_pin(&codec->dapm, "IHFOUTR");
177 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x44);
178 break;
179
180 case 3:
181 pr_debug("set null path\n");
182 snd_soc_dapm_disable_pin(&codec->dapm, "LINEOUTL");
183 snd_soc_dapm_disable_pin(&codec->dapm, "LINEOUTR");
184 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x66);
185 break;
186 }
187 snd_soc_dapm_sync(&codec->dapm);
188 lo_dac = ucontrol->value.integer.value[0];
189 return 0;
190}
191
192static const struct snd_kcontrol_new mfld_snd_controls[] = {
193 SOC_ENUM_EXT("Playback Switch", headset_enum,
194 headset_get_switch, headset_set_switch),
195 SOC_ENUM_EXT("Lineout Mux", lo_enum,
196 lo_get_switch, lo_set_switch),
197};
198
42aee9b4
VK
199static const struct snd_soc_dapm_widget mfld_widgets[] = {
200 SND_SOC_DAPM_HP("Headphones", NULL),
201 SND_SOC_DAPM_MIC("Mic", NULL),
202};
203
204static const struct snd_soc_dapm_route mfld_map[] = {
205 {"Headphones", NULL, "HPOUTR"},
206 {"Headphones", NULL, "HPOUTL"},
207 {"Mic", NULL, "AMIC1"},
208};
209
210static void mfld_jack_check(unsigned int intr_status)
211{
212 struct mfld_jack_data jack_data;
213
214 jack_data.mfld_jack = &mfld_jack;
215 jack_data.intr_id = intr_status;
216
217 sn95031_jack_detection(&jack_data);
218 /* TODO: add american headset detection post gpiolib support */
219}
220
55c72036
VK
221static int mfld_init(struct snd_soc_pcm_runtime *runtime)
222{
223 struct snd_soc_codec *codec = runtime->codec;
224 struct snd_soc_dapm_context *dapm = &codec->dapm;
225 int ret_val;
226
42aee9b4
VK
227 /* Add jack sense widgets */
228 snd_soc_dapm_new_controls(dapm, mfld_widgets, ARRAY_SIZE(mfld_widgets));
229
230 /* Set up the map */
231 snd_soc_dapm_add_routes(dapm, mfld_map, ARRAY_SIZE(mfld_map));
232
233 /* always connected */
234 snd_soc_dapm_enable_pin(dapm, "Headphones");
235 snd_soc_dapm_enable_pin(dapm, "Mic");
236 snd_soc_dapm_sync(dapm);
237
55c72036
VK
238 ret_val = snd_soc_add_controls(codec, mfld_snd_controls,
239 ARRAY_SIZE(mfld_snd_controls));
240 if (ret_val) {
241 pr_err("soc_add_controls failed %d", ret_val);
242 return ret_val;
243 }
244 /* default is earpiece pin, userspace sets it explcitly */
42aee9b4 245 snd_soc_dapm_disable_pin(dapm, "Headphones");
55c72036
VK
246 /* default is lineout NC, userspace sets it explcitly */
247 snd_soc_dapm_disable_pin(dapm, "LINEOUTL");
248 snd_soc_dapm_disable_pin(dapm, "LINEOUTR");
249 lo_dac = 3;
250 hs_switch = 0;
d316553a
HP
251 /* we dont use linein in this so set to NC */
252 snd_soc_dapm_disable_pin(dapm, "LINEINL");
253 snd_soc_dapm_disable_pin(dapm, "LINEINR");
42aee9b4
VK
254 snd_soc_dapm_sync(dapm);
255
256 /* Headset and button jack detection */
257 ret_val = snd_soc_jack_new(codec, "Intel(R) MID Audio Jack",
258 SND_JACK_HEADSET | SND_JACK_BTN_0 |
259 SND_JACK_BTN_1, &mfld_jack);
260 if (ret_val) {
261 pr_err("jack creation failed\n");
262 return ret_val;
263 }
264
265 ret_val = snd_soc_jack_add_pins(&mfld_jack,
266 ARRAY_SIZE(mfld_jack_pins), mfld_jack_pins);
267 if (ret_val) {
268 pr_err("adding jack pins failed\n");
269 return ret_val;
270 }
7ae74340
VK
271 ret_val = snd_soc_jack_add_zones(&mfld_jack,
272 ARRAY_SIZE(mfld_zones), mfld_zones);
273 if (ret_val) {
274 pr_err("adding jack zones failed\n");
275 return ret_val;
276 }
42aee9b4
VK
277
278 /* we want to check if anything is inserted at boot,
279 * so send a fake event to codec and it will read adc
280 * to find if anything is there or not */
281 mfld_jack_check(MFLD_JACK_INSERT);
282 return ret_val;
55c72036
VK
283}
284
285struct snd_soc_dai_link mfld_msic_dailink[] = {
286 {
287 .name = "Medfield Headset",
288 .stream_name = "Headset",
289 .cpu_dai_name = "Headset-cpu-dai",
290 .codec_dai_name = "SN95031 Headset",
291 .codec_name = "sn95031",
292 .platform_name = "sst-platform",
293 .init = mfld_init,
294 },
295 {
296 .name = "Medfield Speaker",
297 .stream_name = "Speaker",
298 .cpu_dai_name = "Speaker-cpu-dai",
299 .codec_dai_name = "SN95031 Speaker",
300 .codec_name = "sn95031",
301 .platform_name = "sst-platform",
302 .init = NULL,
303 },
304 {
305 .name = "Medfield Vibra",
306 .stream_name = "Vibra1",
307 .cpu_dai_name = "Vibra1-cpu-dai",
308 .codec_dai_name = "SN95031 Vibra1",
309 .codec_name = "sn95031",
310 .platform_name = "sst-platform",
311 .init = NULL,
312 },
313 {
314 .name = "Medfield Haptics",
315 .stream_name = "Vibra2",
316 .cpu_dai_name = "Vibra2-cpu-dai",
317 .codec_dai_name = "SN95031 Vibra2",
318 .codec_name = "sn95031",
319 .platform_name = "sst-platform",
320 .init = NULL,
321 },
322};
323
324/* SoC card */
325static struct snd_soc_card snd_soc_card_mfld = {
326 .name = "medfield_audio",
327 .dai_link = mfld_msic_dailink,
328 .num_links = ARRAY_SIZE(mfld_msic_dailink),
329};
330
42aee9b4
VK
331static irqreturn_t snd_mfld_jack_intr_handler(int irq, void *dev)
332{
333 struct mfld_mc_private *mc_private = (struct mfld_mc_private *) dev;
334
335 memcpy_fromio(&mc_private->interrupt_status,
336 ((void *)(mc_private->int_base)),
337 sizeof(u8));
338 return IRQ_WAKE_THREAD;
339}
340
341static irqreturn_t snd_mfld_jack_detection(int irq, void *data)
342{
343 struct mfld_mc_private *mc_drv_ctx = (struct mfld_mc_private *) data;
344
345 if (mfld_jack.codec == NULL)
346 return IRQ_HANDLED;
347 mfld_jack_check(mc_drv_ctx->interrupt_status);
348
349 return IRQ_HANDLED;
350}
351
55c72036
VK
352static int __devinit snd_mfld_mc_probe(struct platform_device *pdev)
353{
42aee9b4
VK
354 int ret_val = 0, irq;
355 struct mfld_mc_private *mc_drv_ctx;
356 struct resource *irq_mem;
55c72036
VK
357
358 pr_debug("snd_mfld_mc_probe called\n");
359
42aee9b4
VK
360 /* retrive the irq number */
361 irq = platform_get_irq(pdev, 0);
362
363 /* audio interrupt base of SRAM location where
364 * interrupts are stored by System FW */
365 mc_drv_ctx = kzalloc(sizeof(*mc_drv_ctx), GFP_ATOMIC);
366 if (!mc_drv_ctx) {
367 pr_err("allocation failed\n");
55c72036
VK
368 return -ENOMEM;
369 }
42aee9b4
VK
370
371 irq_mem = platform_get_resource_byname(
372 pdev, IORESOURCE_MEM, "IRQ_BASE");
373 if (!irq_mem) {
374 pr_err("no mem resource given\n");
375 ret_val = -ENODEV;
376 goto unalloc;
377 }
378 mc_drv_ctx->int_base = ioremap_nocache(irq_mem->start,
379 resource_size(irq_mem));
380 if (!mc_drv_ctx->int_base) {
381 pr_err("Mapping of cache failed\n");
382 ret_val = -ENOMEM;
383 goto unalloc;
384 }
385 /* register for interrupt */
386 ret_val = request_threaded_irq(irq, snd_mfld_jack_intr_handler,
387 snd_mfld_jack_detection,
388 IRQF_SHARED, pdev->dev.driver->name, mc_drv_ctx);
389 if (ret_val) {
390 pr_err("cannot register IRQ\n");
391 goto unalloc;
392 }
d58198b9
VK
393 /* register the soc card */
394 snd_soc_card_mfld.dev = &pdev->dev;
395 ret_val = snd_soc_register_card(&snd_soc_card_mfld);
55c72036 396 if (ret_val) {
d58198b9
VK
397 pr_debug("snd_soc_register_card failed %d\n", ret_val);
398 goto freeirq;
55c72036 399 }
42aee9b4 400 platform_set_drvdata(pdev, mc_drv_ctx);
55c72036
VK
401 pr_debug("successfully exited probe\n");
402 return ret_val;
42aee9b4 403
42aee9b4
VK
404freeirq:
405 free_irq(irq, mc_drv_ctx);
406unalloc:
407 kfree(mc_drv_ctx);
408 return ret_val;
55c72036
VK
409}
410
411static int __devexit snd_mfld_mc_remove(struct platform_device *pdev)
412{
42aee9b4 413 struct mfld_mc_private *mc_drv_ctx = platform_get_drvdata(pdev);
55c72036 414
42aee9b4
VK
415 pr_debug("snd_mfld_mc_remove called\n");
416 free_irq(platform_get_irq(pdev, 0), mc_drv_ctx);
d58198b9 417 snd_soc_unregister_card(&snd_soc_card_mfld);
42aee9b4 418 kfree(mc_drv_ctx);
55c72036
VK
419 platform_set_drvdata(pdev, NULL);
420 return 0;
421}
422
423static struct platform_driver snd_mfld_mc_driver = {
424 .driver = {
425 .owner = THIS_MODULE,
426 .name = "msic_audio",
427 },
428 .probe = snd_mfld_mc_probe,
429 .remove = __devexit_p(snd_mfld_mc_remove),
430};
431
432static int __init snd_mfld_driver_init(void)
433{
434 pr_debug("snd_mfld_driver_init called\n");
435 return platform_driver_register(&snd_mfld_mc_driver);
436}
437module_init(snd_mfld_driver_init);
438
439static void __exit snd_mfld_driver_exit(void)
440{
441 pr_debug("snd_mfld_driver_exit called\n");
442 platform_driver_unregister(&snd_mfld_mc_driver);
443}
444module_exit(snd_mfld_driver_exit);
445
446MODULE_DESCRIPTION("ASoC Intel(R) MID Machine driver");
447MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
448MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
449MODULE_LICENSE("GPL v2");
450MODULE_ALIAS("platform:msic-audio");