2 * mfld_machine.c - ASoc Machine driver for Intel Medfield MID platform
4 * Copyright (C) 2010 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/slab.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"
40 #define MFLD_JACK_INSERT 0x04
42 enum soc_mic_bias_zones
{
44 /* mic bias volutage range for Headphones*/
46 /* mic bias volutage range for American Headset*/
48 /* mic bias volutage range for Headset*/
53 static unsigned int hs_switch
;
54 static unsigned int lo_dac
;
56 struct mfld_mc_private
{
57 struct platform_device
*socdev
;
58 void __iomem
*int_base
;
59 struct snd_soc_codec
*codec
;
63 struct snd_soc_jack mfld_jack
;
65 /*Headset jack detection DAPM pins */
66 static struct snd_soc_jack_pin mfld_jack_pins
[] = {
69 .mask
= SND_JACK_HEADPHONE
,
73 .mask
= SND_JACK_MICROPHONE
,
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
},
83 /* sound card controls */
84 static const char *headset_switch_text
[] = {"Earpiece", "Headset"};
86 static const char *lo_text
[] = {"Vibra", "Headset", "IHF", "None"};
88 static const struct soc_enum headset_enum
=
89 SOC_ENUM_SINGLE_EXT(2, headset_switch_text
);
91 static const struct soc_enum lo_enum
=
92 SOC_ENUM_SINGLE_EXT(4, lo_text
);
94 static int headset_get_switch(struct snd_kcontrol
*kcontrol
,
95 struct snd_ctl_elem_value
*ucontrol
)
97 ucontrol
->value
.integer
.value
[0] = hs_switch
;
101 static int headset_set_switch(struct snd_kcontrol
*kcontrol
,
102 struct snd_ctl_elem_value
*ucontrol
)
104 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
106 if (ucontrol
->value
.integer
.value
[0] == hs_switch
)
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");
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");
118 snd_soc_dapm_sync(&codec
->dapm
);
119 hs_switch
= ucontrol
->value
.integer
.value
[0];
124 static void lo_enable_out_pins(struct snd_soc_codec
*codec
)
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");
133 snd_soc_dapm_enable_pin(&codec
->dapm
, "Headphones");
134 snd_soc_dapm_disable_pin(&codec
->dapm
, "EPOUT");
136 snd_soc_dapm_disable_pin(&codec
->dapm
, "Headphones");
137 snd_soc_dapm_enable_pin(&codec
->dapm
, "EPOUT");
141 static int lo_get_switch(struct snd_kcontrol
*kcontrol
,
142 struct snd_ctl_elem_value
*ucontrol
)
144 ucontrol
->value
.integer
.value
[0] = lo_dac
;
148 static int lo_set_switch(struct snd_kcontrol
*kcontrol
,
149 struct snd_ctl_elem_value
*ucontrol
)
151 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
153 if (ucontrol
->value
.integer
.value
[0] == lo_dac
)
156 /* we dont want to work with last state of lineout so just enable all
157 * pins and then disable pins not required
159 lo_enable_out_pins(codec
);
160 switch (ucontrol
->value
.integer
.value
[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);
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);
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);
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);
189 snd_soc_dapm_sync(&codec
->dapm
);
190 lo_dac
= ucontrol
->value
.integer
.value
[0];
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
),
201 static const struct snd_soc_dapm_widget mfld_widgets
[] = {
202 SND_SOC_DAPM_HP("Headphones", NULL
),
203 SND_SOC_DAPM_MIC("Mic", NULL
),
206 static const struct snd_soc_dapm_route mfld_map
[] = {
207 {"Headphones", NULL
, "HPOUTR"},
208 {"Headphones", NULL
, "HPOUTL"},
209 {"Mic", NULL
, "AMIC1"},
212 static void mfld_jack_check(unsigned int intr_status
)
214 struct mfld_jack_data jack_data
;
216 jack_data
.mfld_jack
= &mfld_jack
;
217 jack_data
.intr_id
= intr_status
;
219 sn95031_jack_detection(&jack_data
);
220 /* TODO: add american headset detection post gpiolib support */
223 static int mfld_init(struct snd_soc_pcm_runtime
*runtime
)
225 struct snd_soc_codec
*codec
= runtime
->codec
;
226 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
229 /* Add jack sense widgets */
230 snd_soc_dapm_new_controls(dapm
, mfld_widgets
, ARRAY_SIZE(mfld_widgets
));
233 snd_soc_dapm_add_routes(dapm
, mfld_map
, ARRAY_SIZE(mfld_map
));
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
);
240 ret_val
= snd_soc_add_controls(codec
, mfld_snd_controls
,
241 ARRAY_SIZE(mfld_snd_controls
));
243 pr_err("soc_add_controls failed %d", ret_val
);
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");
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
);
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
);
263 pr_err("jack creation failed\n");
267 ret_val
= snd_soc_jack_add_pins(&mfld_jack
,
268 ARRAY_SIZE(mfld_jack_pins
), mfld_jack_pins
);
270 pr_err("adding jack pins failed\n");
273 ret_val
= snd_soc_jack_add_zones(&mfld_jack
,
274 ARRAY_SIZE(mfld_zones
), mfld_zones
);
276 pr_err("adding jack zones failed\n");
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
);
287 struct snd_soc_dai_link mfld_msic_dailink
[] = {
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",
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",
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",
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",
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
),
333 static irqreturn_t
snd_mfld_jack_intr_handler(int irq
, void *dev
)
335 struct mfld_mc_private
*mc_private
= (struct mfld_mc_private
*) dev
;
337 memcpy_fromio(&mc_private
->interrupt_status
,
338 ((void *)(mc_private
->int_base
)),
340 return IRQ_WAKE_THREAD
;
343 static irqreturn_t
snd_mfld_jack_detection(int irq
, void *data
)
345 struct mfld_mc_private
*mc_drv_ctx
= (struct mfld_mc_private
*) data
;
347 if (mfld_jack
.codec
== NULL
)
349 mfld_jack_check(mc_drv_ctx
->interrupt_status
);
354 static int __devinit
snd_mfld_mc_probe(struct platform_device
*pdev
)
356 int ret_val
= 0, irq
;
357 struct mfld_mc_private
*mc_drv_ctx
;
358 struct resource
*irq_mem
;
360 pr_debug("snd_mfld_mc_probe called\n");
362 /* retrive the irq number */
363 irq
= platform_get_irq(pdev
, 0);
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
);
369 pr_err("allocation failed\n");
373 irq_mem
= platform_get_resource_byname(
374 pdev
, IORESOURCE_MEM
, "IRQ_BASE");
376 pr_err("no mem resource given\n");
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");
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
);
392 pr_err("cannot register IRQ\n");
395 /* register the soc card */
396 snd_soc_card_mfld
.dev
= &pdev
->dev
;
397 ret_val
= snd_soc_register_card(&snd_soc_card_mfld
);
399 pr_debug("snd_soc_register_card failed %d\n", ret_val
);
402 platform_set_drvdata(pdev
, mc_drv_ctx
);
403 pr_debug("successfully exited probe\n");
407 free_irq(irq
, mc_drv_ctx
);
413 static int __devexit
snd_mfld_mc_remove(struct platform_device
*pdev
)
415 struct mfld_mc_private
*mc_drv_ctx
= platform_get_drvdata(pdev
);
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
);
421 platform_set_drvdata(pdev
, NULL
);
425 static struct platform_driver snd_mfld_mc_driver
= {
427 .owner
= THIS_MODULE
,
428 .name
= "msic_audio",
430 .probe
= snd_mfld_mc_probe
,
431 .remove
= __devexit_p(snd_mfld_mc_remove
),
434 static int __init
snd_mfld_driver_init(void)
436 pr_debug("snd_mfld_driver_init called\n");
437 return platform_driver_register(&snd_mfld_mc_driver
);
439 module_init(snd_mfld_driver_init
);
441 static void __exit
snd_mfld_driver_exit(void)
443 pr_debug("snd_mfld_driver_exit called\n");
444 platform_driver_unregister(&snd_mfld_mc_driver
);
446 module_exit(snd_mfld_driver_exit
);
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");