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