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