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 void __iomem
*int_base
;
61 struct snd_soc_jack mfld_jack
;
63 /*Headset jack detection DAPM pins */
64 static struct snd_soc_jack_pin mfld_jack_pins
[] = {
67 .mask
= SND_JACK_HEADPHONE
,
71 .mask
= SND_JACK_MICROPHONE
,
75 /* jack detection voltage zones */
76 static 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
},
81 /* sound card controls */
82 static const char *headset_switch_text
[] = {"Earpiece", "Headset"};
84 static const char *lo_text
[] = {"Vibra", "Headset", "IHF", "None"};
86 static const struct soc_enum headset_enum
=
87 SOC_ENUM_SINGLE_EXT(2, headset_switch_text
);
89 static const struct soc_enum lo_enum
=
90 SOC_ENUM_SINGLE_EXT(4, lo_text
);
92 static int headset_get_switch(struct snd_kcontrol
*kcontrol
,
93 struct snd_ctl_elem_value
*ucontrol
)
95 ucontrol
->value
.integer
.value
[0] = hs_switch
;
99 static int headset_set_switch(struct snd_kcontrol
*kcontrol
,
100 struct snd_ctl_elem_value
*ucontrol
)
102 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
104 if (ucontrol
->value
.integer
.value
[0] == hs_switch
)
107 if (ucontrol
->value
.integer
.value
[0]) {
108 pr_debug("hs_set HS path\n");
109 snd_soc_dapm_enable_pin(&codec
->dapm
, "Headphones");
110 snd_soc_dapm_disable_pin(&codec
->dapm
, "EPOUT");
112 pr_debug("hs_set EP path\n");
113 snd_soc_dapm_disable_pin(&codec
->dapm
, "Headphones");
114 snd_soc_dapm_enable_pin(&codec
->dapm
, "EPOUT");
116 snd_soc_dapm_sync(&codec
->dapm
);
117 hs_switch
= ucontrol
->value
.integer
.value
[0];
122 static void lo_enable_out_pins(struct snd_soc_codec
*codec
)
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");
131 snd_soc_dapm_enable_pin(&codec
->dapm
, "Headphones");
132 snd_soc_dapm_disable_pin(&codec
->dapm
, "EPOUT");
134 snd_soc_dapm_disable_pin(&codec
->dapm
, "Headphones");
135 snd_soc_dapm_enable_pin(&codec
->dapm
, "EPOUT");
139 static int lo_get_switch(struct snd_kcontrol
*kcontrol
,
140 struct snd_ctl_elem_value
*ucontrol
)
142 ucontrol
->value
.integer
.value
[0] = lo_dac
;
146 static int lo_set_switch(struct snd_kcontrol
*kcontrol
,
147 struct snd_ctl_elem_value
*ucontrol
)
149 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
151 if (ucontrol
->value
.integer
.value
[0] == lo_dac
)
154 /* we dont want to work with last state of lineout so just enable all
155 * pins and then disable pins not required
157 lo_enable_out_pins(codec
);
158 switch (ucontrol
->value
.integer
.value
[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);
167 pr_debug("set hs path\n");
168 snd_soc_dapm_disable_pin(&codec
->dapm
, "Headphones");
169 snd_soc_dapm_disable_pin(&codec
->dapm
, "EPOUT");
170 snd_soc_update_bits(codec
, SN95031_LOCTL
, 0x66, 0x22);
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);
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);
187 snd_soc_dapm_sync(&codec
->dapm
);
188 lo_dac
= ucontrol
->value
.integer
.value
[0];
192 static 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
),
199 static const struct snd_soc_dapm_widget mfld_widgets
[] = {
200 SND_SOC_DAPM_HP("Headphones", NULL
),
201 SND_SOC_DAPM_MIC("Mic", NULL
),
204 static const struct snd_soc_dapm_route mfld_map
[] = {
205 {"Headphones", NULL
, "HPOUTR"},
206 {"Headphones", NULL
, "HPOUTL"},
207 {"Mic", NULL
, "AMIC1"},
210 static void mfld_jack_check(unsigned int intr_status
)
212 struct mfld_jack_data jack_data
;
214 jack_data
.mfld_jack
= &mfld_jack
;
215 jack_data
.intr_id
= intr_status
;
217 sn95031_jack_detection(&jack_data
);
218 /* TODO: add american headset detection post gpiolib support */
221 static int mfld_init(struct snd_soc_pcm_runtime
*runtime
)
223 struct snd_soc_codec
*codec
= runtime
->codec
;
224 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
227 /* Add jack sense widgets */
228 snd_soc_dapm_new_controls(dapm
, mfld_widgets
, ARRAY_SIZE(mfld_widgets
));
231 snd_soc_dapm_add_routes(dapm
, mfld_map
, ARRAY_SIZE(mfld_map
));
233 /* always connected */
234 snd_soc_dapm_enable_pin(dapm
, "Headphones");
235 snd_soc_dapm_enable_pin(dapm
, "Mic");
237 ret_val
= snd_soc_add_controls(codec
, mfld_snd_controls
,
238 ARRAY_SIZE(mfld_snd_controls
));
240 pr_err("soc_add_controls failed %d", ret_val
);
243 /* default is earpiece pin, userspace sets it explcitly */
244 snd_soc_dapm_disable_pin(dapm
, "Headphones");
245 /* default is lineout NC, userspace sets it explcitly */
246 snd_soc_dapm_disable_pin(dapm
, "LINEOUTL");
247 snd_soc_dapm_disable_pin(dapm
, "LINEOUTR");
250 /* we dont use linein in this so set to NC */
251 snd_soc_dapm_disable_pin(dapm
, "LINEINL");
252 snd_soc_dapm_disable_pin(dapm
, "LINEINR");
254 /* Headset and button jack detection */
255 ret_val
= snd_soc_jack_new(codec
, "Intel(R) MID Audio Jack",
256 SND_JACK_HEADSET
| SND_JACK_BTN_0
|
257 SND_JACK_BTN_1
, &mfld_jack
);
259 pr_err("jack creation failed\n");
263 ret_val
= snd_soc_jack_add_pins(&mfld_jack
,
264 ARRAY_SIZE(mfld_jack_pins
), mfld_jack_pins
);
266 pr_err("adding jack pins failed\n");
269 ret_val
= snd_soc_jack_add_zones(&mfld_jack
,
270 ARRAY_SIZE(mfld_zones
), mfld_zones
);
272 pr_err("adding jack zones failed\n");
276 /* we want to check if anything is inserted at boot,
277 * so send a fake event to codec and it will read adc
278 * to find if anything is there or not */
279 mfld_jack_check(MFLD_JACK_INSERT
);
283 struct snd_soc_dai_link mfld_msic_dailink
[] = {
285 .name
= "Medfield Headset",
286 .stream_name
= "Headset",
287 .cpu_dai_name
= "Headset-cpu-dai",
288 .codec_dai_name
= "SN95031 Headset",
289 .codec_name
= "sn95031",
290 .platform_name
= "sst-platform",
294 .name
= "Medfield Speaker",
295 .stream_name
= "Speaker",
296 .cpu_dai_name
= "Speaker-cpu-dai",
297 .codec_dai_name
= "SN95031 Speaker",
298 .codec_name
= "sn95031",
299 .platform_name
= "sst-platform",
303 .name
= "Medfield Vibra",
304 .stream_name
= "Vibra1",
305 .cpu_dai_name
= "Vibra1-cpu-dai",
306 .codec_dai_name
= "SN95031 Vibra1",
307 .codec_name
= "sn95031",
308 .platform_name
= "sst-platform",
312 .name
= "Medfield Haptics",
313 .stream_name
= "Vibra2",
314 .cpu_dai_name
= "Vibra2-cpu-dai",
315 .codec_dai_name
= "SN95031 Vibra2",
316 .codec_name
= "sn95031",
317 .platform_name
= "sst-platform",
323 static struct snd_soc_card snd_soc_card_mfld
= {
324 .name
= "medfield_audio",
325 .dai_link
= mfld_msic_dailink
,
326 .num_links
= ARRAY_SIZE(mfld_msic_dailink
),
329 static irqreturn_t
snd_mfld_jack_intr_handler(int irq
, void *dev
)
331 struct mfld_mc_private
*mc_private
= (struct mfld_mc_private
*) dev
;
333 memcpy_fromio(&mc_private
->interrupt_status
,
334 ((void *)(mc_private
->int_base
)),
336 return IRQ_WAKE_THREAD
;
339 static irqreturn_t
snd_mfld_jack_detection(int irq
, void *data
)
341 struct mfld_mc_private
*mc_drv_ctx
= (struct mfld_mc_private
*) data
;
343 if (mfld_jack
.codec
== NULL
)
345 mfld_jack_check(mc_drv_ctx
->interrupt_status
);
350 static int __devinit
snd_mfld_mc_probe(struct platform_device
*pdev
)
352 int ret_val
= 0, irq
;
353 struct mfld_mc_private
*mc_drv_ctx
;
354 struct resource
*irq_mem
;
356 pr_debug("snd_mfld_mc_probe called\n");
358 /* retrive the irq number */
359 irq
= platform_get_irq(pdev
, 0);
361 /* audio interrupt base of SRAM location where
362 * interrupts are stored by System FW */
363 mc_drv_ctx
= kzalloc(sizeof(*mc_drv_ctx
), GFP_ATOMIC
);
365 pr_err("allocation failed\n");
369 irq_mem
= platform_get_resource_byname(
370 pdev
, IORESOURCE_MEM
, "IRQ_BASE");
372 pr_err("no mem resource given\n");
376 mc_drv_ctx
->int_base
= ioremap_nocache(irq_mem
->start
,
377 resource_size(irq_mem
));
378 if (!mc_drv_ctx
->int_base
) {
379 pr_err("Mapping of cache failed\n");
383 /* register for interrupt */
384 ret_val
= request_threaded_irq(irq
, snd_mfld_jack_intr_handler
,
385 snd_mfld_jack_detection
,
386 IRQF_SHARED
, pdev
->dev
.driver
->name
, mc_drv_ctx
);
388 pr_err("cannot register IRQ\n");
391 /* register the soc card */
392 snd_soc_card_mfld
.dev
= &pdev
->dev
;
393 ret_val
= snd_soc_register_card(&snd_soc_card_mfld
);
395 pr_debug("snd_soc_register_card failed %d\n", ret_val
);
398 platform_set_drvdata(pdev
, mc_drv_ctx
);
399 pr_debug("successfully exited probe\n");
403 free_irq(irq
, mc_drv_ctx
);
409 static int __devexit
snd_mfld_mc_remove(struct platform_device
*pdev
)
411 struct mfld_mc_private
*mc_drv_ctx
= platform_get_drvdata(pdev
);
413 pr_debug("snd_mfld_mc_remove called\n");
414 free_irq(platform_get_irq(pdev
, 0), mc_drv_ctx
);
415 snd_soc_unregister_card(&snd_soc_card_mfld
);
417 platform_set_drvdata(pdev
, NULL
);
421 static struct platform_driver snd_mfld_mc_driver
= {
423 .owner
= THIS_MODULE
,
424 .name
= "msic_audio",
426 .probe
= snd_mfld_mc_probe
,
427 .remove
= __devexit_p(snd_mfld_mc_remove
),
430 static int __init
snd_mfld_driver_init(void)
432 pr_debug("snd_mfld_driver_init called\n");
433 return platform_driver_register(&snd_mfld_mc_driver
);
435 module_init(snd_mfld_driver_init
);
437 static void __exit
snd_mfld_driver_exit(void)
439 pr_debug("snd_mfld_driver_exit called\n");
440 platform_driver_unregister(&snd_mfld_mc_driver
);
442 module_exit(snd_mfld_driver_exit
);
444 MODULE_DESCRIPTION("ASoC Intel(R) MID Machine driver");
445 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
446 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
447 MODULE_LICENSE("GPL v2");
448 MODULE_ALIAS("platform:msic-audio");