blob: 0cef32e9d4023ff2116cd0886b4be9e382e55b2e [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040031#include <linux/module.h>
Vinod Koul55c72032011-01-04 20:16:50 +053032#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
Vinod Koul42aee9b2011-02-09 21:44:33 +053035#include <sound/jack.h>
Vinod Koul55c72032011-01-04 20:16:50 +053036#include "../codecs/sn95031.h"
37
38#define MID_MONO 1
39#define MID_STEREO 2
40#define MID_MAX_CAP 5
Vinod Koul42aee9b2011-02-09 21:44:33 +053041#define MFLD_JACK_INSERT 0x04
42
43enum soc_mic_bias_zones {
44 MFLD_MV_START = 0,
45 /* mic bias volutage range for Headphones*/
46 MFLD_MV_HP = 400,
47 /* mic bias volutage range for American Headset*/
48 MFLD_MV_AM_HS = 650,
49 /* mic bias volutage range for Headset*/
50 MFLD_MV_HS = 2000,
51 MFLD_MV_UNDEFINED,
52};
Vinod Koul55c72032011-01-04 20:16:50 +053053
54static unsigned int hs_switch;
55static unsigned int lo_dac;
56
Vinod Koul42aee9b2011-02-09 21:44:33 +053057struct mfld_mc_private {
Vinod Koul42aee9b2011-02-09 21:44:33 +053058 void __iomem *int_base;
Vinod Koul42aee9b2011-02-09 21:44:33 +053059 u8 interrupt_status;
60};
61
62struct snd_soc_jack mfld_jack;
63
64/*Headset jack detection DAPM pins */
65static struct snd_soc_jack_pin mfld_jack_pins[] = {
66 {
67 .pin = "Headphones",
68 .mask = SND_JACK_HEADPHONE,
69 },
70 {
71 .pin = "AMIC1",
72 .mask = SND_JACK_MICROPHONE,
73 },
74};
75
Vinod Koul7ae74342011-02-10 12:58:01 +053076/* jack detection voltage zones */
77static struct snd_soc_jack_zone mfld_zones[] = {
78 {MFLD_MV_START, MFLD_MV_AM_HS, SND_JACK_HEADPHONE},
79 {MFLD_MV_AM_HS, MFLD_MV_HS, SND_JACK_HEADSET},
80};
81
Vinod Koul55c72032011-01-04 20:16:50 +053082/* sound card controls */
83static const char *headset_switch_text[] = {"Earpiece", "Headset"};
84
85static const char *lo_text[] = {"Vibra", "Headset", "IHF", "None"};
86
87static const struct soc_enum headset_enum =
88 SOC_ENUM_SINGLE_EXT(2, headset_switch_text);
89
90static const struct soc_enum lo_enum =
91 SOC_ENUM_SINGLE_EXT(4, lo_text);
92
93static int headset_get_switch(struct snd_kcontrol *kcontrol,
94 struct snd_ctl_elem_value *ucontrol)
95{
96 ucontrol->value.integer.value[0] = hs_switch;
97 return 0;
98}
99
100static int headset_set_switch(struct snd_kcontrol *kcontrol,
101 struct snd_ctl_elem_value *ucontrol)
102{
103 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Charles Keepax121df682014-02-18 15:22:21 +0000104 struct snd_soc_dapm_context *dapm = &codec->dapm;
Vinod Koul55c72032011-01-04 20:16:50 +0530105
106 if (ucontrol->value.integer.value[0] == hs_switch)
107 return 0;
108
Charles Keepax121df682014-02-18 15:22:21 +0000109 snd_soc_dapm_mutex_lock(dapm);
110
Vinod Koul55c72032011-01-04 20:16:50 +0530111 if (ucontrol->value.integer.value[0]) {
112 pr_debug("hs_set HS path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000113 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphones");
114 snd_soc_dapm_disable_pin_unlocked(dapm, "EPOUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530115 } else {
116 pr_debug("hs_set EP path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000117 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphones");
118 snd_soc_dapm_enable_pin_unlocked(dapm, "EPOUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530119 }
Charles Keepax121df682014-02-18 15:22:21 +0000120
121 snd_soc_dapm_sync_unlocked(dapm);
122
123 snd_soc_dapm_mutex_unlock(dapm);
124
Vinod Koul55c72032011-01-04 20:16:50 +0530125 hs_switch = ucontrol->value.integer.value[0];
126
127 return 0;
128}
129
130static void lo_enable_out_pins(struct snd_soc_codec *codec)
131{
Charles Keepax121df682014-02-18 15:22:21 +0000132 struct snd_soc_dapm_context *dapm = &codec->dapm;
133
134 snd_soc_dapm_enable_pin_unlocked(dapm, "IHFOUTL");
135 snd_soc_dapm_enable_pin_unlocked(dapm, "IHFOUTR");
136 snd_soc_dapm_enable_pin_unlocked(dapm, "LINEOUTL");
137 snd_soc_dapm_enable_pin_unlocked(dapm, "LINEOUTR");
138 snd_soc_dapm_enable_pin_unlocked(dapm, "VIB1OUT");
139 snd_soc_dapm_enable_pin_unlocked(dapm, "VIB2OUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530140 if (hs_switch) {
Charles Keepax121df682014-02-18 15:22:21 +0000141 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphones");
142 snd_soc_dapm_disable_pin_unlocked(dapm, "EPOUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530143 } else {
Charles Keepax121df682014-02-18 15:22:21 +0000144 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphones");
145 snd_soc_dapm_enable_pin_unlocked(dapm, "EPOUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530146 }
147}
148
149static int lo_get_switch(struct snd_kcontrol *kcontrol,
150 struct snd_ctl_elem_value *ucontrol)
151{
152 ucontrol->value.integer.value[0] = lo_dac;
153 return 0;
154}
155
156static int lo_set_switch(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
158{
159 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Charles Keepax121df682014-02-18 15:22:21 +0000160 struct snd_soc_dapm_context *dapm = &codec->dapm;
Vinod Koul55c72032011-01-04 20:16:50 +0530161
162 if (ucontrol->value.integer.value[0] == lo_dac)
163 return 0;
164
Charles Keepax121df682014-02-18 15:22:21 +0000165 snd_soc_dapm_mutex_lock(dapm);
166
Vinod Koul55c72032011-01-04 20:16:50 +0530167 /* we dont want to work with last state of lineout so just enable all
168 * pins and then disable pins not required
169 */
170 lo_enable_out_pins(codec);
Charles Keepax121df682014-02-18 15:22:21 +0000171
Vinod Koul55c72032011-01-04 20:16:50 +0530172 switch (ucontrol->value.integer.value[0]) {
173 case 0:
174 pr_debug("set vibra path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000175 snd_soc_dapm_disable_pin_unlocked(dapm, "VIB1OUT");
176 snd_soc_dapm_disable_pin_unlocked(dapm, "VIB2OUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530177 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0);
178 break;
179
180 case 1:
181 pr_debug("set hs path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000182 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphones");
183 snd_soc_dapm_disable_pin_unlocked(dapm, "EPOUT");
Vinod Koul55c72032011-01-04 20:16:50 +0530184 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x22);
185 break;
186
187 case 2:
188 pr_debug("set spkr path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000189 snd_soc_dapm_disable_pin_unlocked(dapm, "IHFOUTL");
190 snd_soc_dapm_disable_pin_unlocked(dapm, "IHFOUTR");
Vinod Koul55c72032011-01-04 20:16:50 +0530191 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x44);
192 break;
193
194 case 3:
195 pr_debug("set null path\n");
Charles Keepax121df682014-02-18 15:22:21 +0000196 snd_soc_dapm_disable_pin_unlocked(dapm, "LINEOUTL");
197 snd_soc_dapm_disable_pin_unlocked(dapm, "LINEOUTR");
Vinod Koul55c72032011-01-04 20:16:50 +0530198 snd_soc_update_bits(codec, SN95031_LOCTL, 0x66, 0x66);
199 break;
200 }
Charles Keepax121df682014-02-18 15:22:21 +0000201
202 snd_soc_dapm_sync_unlocked(dapm);
203
204 snd_soc_dapm_mutex_unlock(dapm);
205
Vinod Koul55c72032011-01-04 20:16:50 +0530206 lo_dac = ucontrol->value.integer.value[0];
207 return 0;
208}
209
210static const struct snd_kcontrol_new mfld_snd_controls[] = {
211 SOC_ENUM_EXT("Playback Switch", headset_enum,
212 headset_get_switch, headset_set_switch),
213 SOC_ENUM_EXT("Lineout Mux", lo_enum,
214 lo_get_switch, lo_set_switch),
215};
216
Vinod Koul42aee9b2011-02-09 21:44:33 +0530217static const struct snd_soc_dapm_widget mfld_widgets[] = {
218 SND_SOC_DAPM_HP("Headphones", NULL),
219 SND_SOC_DAPM_MIC("Mic", NULL),
220};
221
222static const struct snd_soc_dapm_route mfld_map[] = {
223 {"Headphones", NULL, "HPOUTR"},
224 {"Headphones", NULL, "HPOUTL"},
225 {"Mic", NULL, "AMIC1"},
226};
227
228static void mfld_jack_check(unsigned int intr_status)
229{
230 struct mfld_jack_data jack_data;
231
232 jack_data.mfld_jack = &mfld_jack;
233 jack_data.intr_id = intr_status;
234
235 sn95031_jack_detection(&jack_data);
236 /* TODO: add american headset detection post gpiolib support */
237}
238
Vinod Koul55c72032011-01-04 20:16:50 +0530239static int mfld_init(struct snd_soc_pcm_runtime *runtime)
240{
241 struct snd_soc_codec *codec = runtime->codec;
242 struct snd_soc_dapm_context *dapm = &codec->dapm;
243 int ret_val;
244
Vinod Koul42aee9b2011-02-09 21:44:33 +0530245 /* Add jack sense widgets */
246 snd_soc_dapm_new_controls(dapm, mfld_widgets, ARRAY_SIZE(mfld_widgets));
247
248 /* Set up the map */
249 snd_soc_dapm_add_routes(dapm, mfld_map, ARRAY_SIZE(mfld_map));
250
251 /* always connected */
252 snd_soc_dapm_enable_pin(dapm, "Headphones");
253 snd_soc_dapm_enable_pin(dapm, "Mic");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530254
Liam Girdwood022658b2012-02-03 17:43:09 +0000255 ret_val = snd_soc_add_codec_controls(codec, mfld_snd_controls,
Vinod Koul55c72032011-01-04 20:16:50 +0530256 ARRAY_SIZE(mfld_snd_controls));
257 if (ret_val) {
258 pr_err("soc_add_controls failed %d", ret_val);
259 return ret_val;
260 }
261 /* default is earpiece pin, userspace sets it explcitly */
Vinod Koul42aee9b2011-02-09 21:44:33 +0530262 snd_soc_dapm_disable_pin(dapm, "Headphones");
Vinod Koul55c72032011-01-04 20:16:50 +0530263 /* default is lineout NC, userspace sets it explcitly */
264 snd_soc_dapm_disable_pin(dapm, "LINEOUTL");
265 snd_soc_dapm_disable_pin(dapm, "LINEOUTR");
266 lo_dac = 3;
267 hs_switch = 0;
Harsha Priyad316553a2011-01-28 22:28:32 +0530268 /* we dont use linein in this so set to NC */
269 snd_soc_dapm_disable_pin(dapm, "LINEINL");
270 snd_soc_dapm_disable_pin(dapm, "LINEINR");
Vinod Koul42aee9b2011-02-09 21:44:33 +0530271
272 /* Headset and button jack detection */
273 ret_val = snd_soc_jack_new(codec, "Intel(R) MID Audio Jack",
274 SND_JACK_HEADSET | SND_JACK_BTN_0 |
275 SND_JACK_BTN_1, &mfld_jack);
276 if (ret_val) {
277 pr_err("jack creation failed\n");
278 return ret_val;
279 }
280
281 ret_val = snd_soc_jack_add_pins(&mfld_jack,
282 ARRAY_SIZE(mfld_jack_pins), mfld_jack_pins);
283 if (ret_val) {
284 pr_err("adding jack pins failed\n");
285 return ret_val;
286 }
Vinod Koul7ae74342011-02-10 12:58:01 +0530287 ret_val = snd_soc_jack_add_zones(&mfld_jack,
288 ARRAY_SIZE(mfld_zones), mfld_zones);
289 if (ret_val) {
290 pr_err("adding jack zones failed\n");
291 return ret_val;
292 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530293
294 /* we want to check if anything is inserted at boot,
295 * so send a fake event to codec and it will read adc
296 * to find if anything is there or not */
297 mfld_jack_check(MFLD_JACK_INSERT);
298 return ret_val;
Vinod Koul55c72032011-01-04 20:16:50 +0530299}
300
Axel Lin00c65162011-12-14 19:23:01 +0800301static struct snd_soc_dai_link mfld_msic_dailink[] = {
Vinod Koul55c72032011-01-04 20:16:50 +0530302 {
303 .name = "Medfield Headset",
304 .stream_name = "Headset",
305 .cpu_dai_name = "Headset-cpu-dai",
306 .codec_dai_name = "SN95031 Headset",
307 .codec_name = "sn95031",
308 .platform_name = "sst-platform",
309 .init = mfld_init,
310 },
311 {
312 .name = "Medfield Speaker",
313 .stream_name = "Speaker",
314 .cpu_dai_name = "Speaker-cpu-dai",
315 .codec_dai_name = "SN95031 Speaker",
316 .codec_name = "sn95031",
317 .platform_name = "sst-platform",
318 .init = NULL,
319 },
320 {
321 .name = "Medfield Vibra",
322 .stream_name = "Vibra1",
323 .cpu_dai_name = "Vibra1-cpu-dai",
324 .codec_dai_name = "SN95031 Vibra1",
325 .codec_name = "sn95031",
326 .platform_name = "sst-platform",
327 .init = NULL,
328 },
329 {
330 .name = "Medfield Haptics",
331 .stream_name = "Vibra2",
332 .cpu_dai_name = "Vibra2-cpu-dai",
333 .codec_dai_name = "SN95031 Vibra2",
334 .codec_name = "sn95031",
335 .platform_name = "sst-platform",
336 .init = NULL,
337 },
Vinod Koulc514a912012-08-16 17:10:42 +0530338 {
339 .name = "Medfield Compress",
340 .stream_name = "Speaker",
341 .cpu_dai_name = "Compress-cpu-dai",
342 .codec_dai_name = "SN95031 Speaker",
343 .codec_name = "sn95031",
344 .platform_name = "sst-platform",
345 .init = NULL,
346 },
Vinod Koul55c72032011-01-04 20:16:50 +0530347};
348
349/* SoC card */
350static struct snd_soc_card snd_soc_card_mfld = {
351 .name = "medfield_audio",
Axel Lin8270ba02011-12-23 14:50:17 +0800352 .owner = THIS_MODULE,
Vinod Koul55c72032011-01-04 20:16:50 +0530353 .dai_link = mfld_msic_dailink,
354 .num_links = ARRAY_SIZE(mfld_msic_dailink),
355};
356
Vinod Koul42aee9b2011-02-09 21:44:33 +0530357static irqreturn_t snd_mfld_jack_intr_handler(int irq, void *dev)
358{
359 struct mfld_mc_private *mc_private = (struct mfld_mc_private *) dev;
360
361 memcpy_fromio(&mc_private->interrupt_status,
362 ((void *)(mc_private->int_base)),
363 sizeof(u8));
364 return IRQ_WAKE_THREAD;
365}
366
367static irqreturn_t snd_mfld_jack_detection(int irq, void *data)
368{
369 struct mfld_mc_private *mc_drv_ctx = (struct mfld_mc_private *) data;
370
371 if (mfld_jack.codec == NULL)
372 return IRQ_HANDLED;
373 mfld_jack_check(mc_drv_ctx->interrupt_status);
374
375 return IRQ_HANDLED;
376}
377
Bill Pemberton7759f2e2012-12-07 09:26:25 -0500378static int snd_mfld_mc_probe(struct platform_device *pdev)
Vinod Koul55c72032011-01-04 20:16:50 +0530379{
Vinod Koul42aee9b2011-02-09 21:44:33 +0530380 int ret_val = 0, irq;
381 struct mfld_mc_private *mc_drv_ctx;
382 struct resource *irq_mem;
Vinod Koul55c72032011-01-04 20:16:50 +0530383
384 pr_debug("snd_mfld_mc_probe called\n");
385
Vinod Koul42aee9b2011-02-09 21:44:33 +0530386 /* retrive the irq number */
387 irq = platform_get_irq(pdev, 0);
388
389 /* audio interrupt base of SRAM location where
390 * interrupts are stored by System FW */
Wei Yongjunde693002013-06-25 10:16:56 +0800391 mc_drv_ctx = devm_kzalloc(&pdev->dev, sizeof(*mc_drv_ctx), GFP_ATOMIC);
Vinod Koul42aee9b2011-02-09 21:44:33 +0530392 if (!mc_drv_ctx) {
393 pr_err("allocation failed\n");
Vinod Koul55c72032011-01-04 20:16:50 +0530394 return -ENOMEM;
395 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530396
397 irq_mem = platform_get_resource_byname(
398 pdev, IORESOURCE_MEM, "IRQ_BASE");
399 if (!irq_mem) {
400 pr_err("no mem resource given\n");
Wei Yongjunde693002013-06-25 10:16:56 +0800401 return -ENODEV;
Vinod Koul42aee9b2011-02-09 21:44:33 +0530402 }
Wei Yongjunde693002013-06-25 10:16:56 +0800403 mc_drv_ctx->int_base = devm_ioremap_nocache(&pdev->dev, irq_mem->start,
404 resource_size(irq_mem));
Vinod Koul42aee9b2011-02-09 21:44:33 +0530405 if (!mc_drv_ctx->int_base) {
406 pr_err("Mapping of cache failed\n");
Wei Yongjunde693002013-06-25 10:16:56 +0800407 return -ENOMEM;
Vinod Koul42aee9b2011-02-09 21:44:33 +0530408 }
409 /* register for interrupt */
Wei Yongjunde693002013-06-25 10:16:56 +0800410 ret_val = devm_request_threaded_irq(&pdev->dev, irq,
411 snd_mfld_jack_intr_handler,
Vinod Koul42aee9b2011-02-09 21:44:33 +0530412 snd_mfld_jack_detection,
413 IRQF_SHARED, pdev->dev.driver->name, mc_drv_ctx);
414 if (ret_val) {
415 pr_err("cannot register IRQ\n");
Wei Yongjunde693002013-06-25 10:16:56 +0800416 return ret_val;
Vinod Koul42aee9b2011-02-09 21:44:33 +0530417 }
Vinod Kould58198b2011-02-15 18:28:55 +0530418 /* register the soc card */
419 snd_soc_card_mfld.dev = &pdev->dev;
Sachin Kamat33c89c32013-09-17 09:49:04 +0530420 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_mfld);
Vinod Koul55c72032011-01-04 20:16:50 +0530421 if (ret_val) {
Vinod Kould58198b2011-02-15 18:28:55 +0530422 pr_debug("snd_soc_register_card failed %d\n", ret_val);
Wei Yongjunde693002013-06-25 10:16:56 +0800423 return ret_val;
Vinod Koul55c72032011-01-04 20:16:50 +0530424 }
Vinod Koul42aee9b2011-02-09 21:44:33 +0530425 platform_set_drvdata(pdev, mc_drv_ctx);
Vinod Koul55c72032011-01-04 20:16:50 +0530426 pr_debug("successfully exited probe\n");
Wei Yongjunde693002013-06-25 10:16:56 +0800427 return 0;
Vinod Koul55c72032011-01-04 20:16:50 +0530428}
429
Vinod Koul55c72032011-01-04 20:16:50 +0530430static struct platform_driver snd_mfld_mc_driver = {
431 .driver = {
432 .owner = THIS_MODULE,
433 .name = "msic_audio",
434 },
435 .probe = snd_mfld_mc_probe,
Vinod Koul55c72032011-01-04 20:16:50 +0530436};
437
Axel Lin29515d62011-11-24 11:51:56 +0800438module_platform_driver(snd_mfld_mc_driver);
Vinod Koul55c72032011-01-04 20:16:50 +0530439
440MODULE_DESCRIPTION("ASoC Intel(R) MID Machine driver");
441MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
442MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
443MODULE_LICENSE("GPL v2");
444MODULE_ALIAS("platform:msic-audio");