blob: 96487fb8d265ee05a3d8d452e0c16de855dd27b0 [file] [log] [blame]
Vinod Koul55c72032011-01-04 20:16:50 +05301/*
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>
Vinod Koul42aee9b2011-02-09 21:44:33 +053030#include <linux/io.h>
Vinod Koul55c72032011-01-04 20:16:50 +053031#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
Vinod Koul42aee9b2011-02-09 21:44:33 +053034#include <sound/jack.h>
Vinod Koul55c72032011-01-04 20:16:50 +053035#include "../codecs/sn95031.h"
36
37#define MID_MONO 1
38#define MID_STEREO 2
39#define MID_MAX_CAP 5
Vinod Koul42aee9b2011-02-09 21:44:33 +053040#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};
Vinod Koul55c72032011-01-04 20:16:50 +053052
53static unsigned int hs_switch;
54static unsigned int lo_dac;
55
Vinod Koul42aee9b2011-02-09 21:44:33 +053056struct 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
Vinod Koul7ae74342011-02-10 12:58:01 +053077/* jack detection voltage zones */
78static 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
Vinod Koul55c72032011-01-04 20:16:50 +053083/* sound card controls */
84static const char *headset_switch_text[] = {"Earpiece", "Headset"};
85
86static const char *lo_text[] = {"Vibra", "Headset", "IHF", "None"};
87
88static const struct soc_enum headset_enum =
89 SOC_ENUM_SINGLE_EXT(2, headset_switch_text);
90
91static const struct soc_enum lo_enum =
92 SOC_ENUM_SINGLE_EXT(4, lo_text);
93
94static 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
101static 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");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530111 snd_soc_dapm_enable_pin(&codec->dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530112 snd_soc_dapm_disable_pin(&codec->dapm, "EPOUT");
113 } else {
114 pr_debug("hs_set EP path\n");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530115 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530116 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
124static 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) {
Vinod Koul42aee9b2011-02-09 21:44:33 +0530133 snd_soc_dapm_enable_pin(&codec->dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530134 snd_soc_dapm_disable_pin(&codec->dapm, "EPOUT");
135 } else {
Vinod Koul42aee9b2011-02-09 21:44:33 +0530136 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530137 snd_soc_dapm_enable_pin(&codec->dapm, "EPOUT");
138 }
139}
140
141static 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
148static 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");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530170 snd_soc_dapm_disable_pin(&codec->dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530171 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
194static 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
Vinod Koul42aee9b2011-02-09 21:44:33 +0530201static const struct snd_soc_dapm_widget mfld_widgets[] = {
202 SND_SOC_DAPM_HP("Headphones", NULL),
203 SND_SOC_DAPM_MIC("Mic", NULL),
204};
205
206static const struct snd_soc_dapm_route mfld_map[] = {
207 {"Headphones", NULL, "HPOUTR"},
208 {"Headphones", NULL, "HPOUTL"},
209 {"Mic", NULL, "AMIC1"},
210};
211
212static 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
Vinod Koul55c72032011-01-04 20:16:50 +0530223static 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
Vinod Koul42aee9b2011-02-09 21:44:33 +0530229 /* 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
Vinod Koul55c72032011-01-04 20:16:50 +0530240 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 */
Vinod Koul42aee9b2011-02-09 21:44:33 +0530247 snd_soc_dapm_disable_pin(dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530248 /* 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;
Harsha Priyad316553a2011-01-28 22:28:32 +0530253 /* 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");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530256 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 }
Vinod Koul7ae74342011-02-10 12:58:01 +0530273 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 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530279
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;
Vinod Koul55c72032011-01-04 20:16:50 +0530285}
286
287struct 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 */
327static 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
Vinod Koul42aee9b2011-02-09 21:44:33 +0530333static 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
343static 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
Vinod Koul55c72032011-01-04 20:16:50 +0530354static int __devinit snd_mfld_mc_probe(struct platform_device *pdev)
355{
Vinod Koul42aee9b2011-02-09 21:44:33 +0530356 int ret_val = 0, irq;
357 struct mfld_mc_private *mc_drv_ctx;
358 struct resource *irq_mem;
Vinod Koul55c72032011-01-04 20:16:50 +0530359
360 pr_debug("snd_mfld_mc_probe called\n");
361
Vinod Koul42aee9b2011-02-09 21:44:33 +0530362 /* 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");
Vinod Koul55c72032011-01-04 20:16:50 +0530370 return -ENOMEM;
371 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530372
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 /* create soc device */
396 mc_drv_ctx->socdev = platform_device_alloc("soc-audio", -1);
397 if (!mc_drv_ctx->socdev) {
398 pr_err("soc-audio device allocation failed\n");
399 ret_val = -ENOMEM;
400 goto freeirq;
401 }
402 platform_set_drvdata(mc_drv_ctx->socdev, &snd_soc_card_mfld);
403 ret_val = platform_device_add(mc_drv_ctx->socdev);
Vinod Koul55c72032011-01-04 20:16:50 +0530404 if (ret_val) {
405 pr_err("Unable to add soc-audio device, err %d\n", ret_val);
Vinod Koul42aee9b2011-02-09 21:44:33 +0530406 goto unregister;
Vinod Koul55c72032011-01-04 20:16:50 +0530407 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530408 platform_set_drvdata(pdev, mc_drv_ctx);
Vinod Koul55c72032011-01-04 20:16:50 +0530409 pr_debug("successfully exited probe\n");
410 return ret_val;
Vinod Koul42aee9b2011-02-09 21:44:33 +0530411
412unregister:
413 platform_device_put(mc_drv_ctx->socdev);
414freeirq:
415 free_irq(irq, mc_drv_ctx);
416unalloc:
417 kfree(mc_drv_ctx);
418 return ret_val;
Vinod Koul55c72032011-01-04 20:16:50 +0530419}
420
421static int __devexit snd_mfld_mc_remove(struct platform_device *pdev)
422{
Vinod Koul42aee9b2011-02-09 21:44:33 +0530423 struct mfld_mc_private *mc_drv_ctx = platform_get_drvdata(pdev);
Vinod Koul55c72032011-01-04 20:16:50 +0530424
Vinod Koul42aee9b2011-02-09 21:44:33 +0530425 pr_debug("snd_mfld_mc_remove called\n");
426 free_irq(platform_get_irq(pdev, 0), mc_drv_ctx);
427 platform_device_unregister(mc_drv_ctx->socdev);
428 kfree(mc_drv_ctx);
Vinod Koul55c72032011-01-04 20:16:50 +0530429 platform_set_drvdata(pdev, NULL);
430 return 0;
431}
432
433static struct platform_driver snd_mfld_mc_driver = {
434 .driver = {
435 .owner = THIS_MODULE,
436 .name = "msic_audio",
437 },
438 .probe = snd_mfld_mc_probe,
439 .remove = __devexit_p(snd_mfld_mc_remove),
440};
441
442static int __init snd_mfld_driver_init(void)
443{
444 pr_debug("snd_mfld_driver_init called\n");
445 return platform_driver_register(&snd_mfld_mc_driver);
446}
447module_init(snd_mfld_driver_init);
448
449static void __exit snd_mfld_driver_exit(void)
450{
451 pr_debug("snd_mfld_driver_exit called\n");
452 platform_driver_unregister(&snd_mfld_mc_driver);
453}
454module_exit(snd_mfld_driver_exit);
455
456MODULE_DESCRIPTION("ASoC Intel(R) MID Machine driver");
457MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
458MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
459MODULE_LICENSE("GPL v2");
460MODULE_ALIAS("platform:msic-audio");