Merge branch 'for-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound...
[pandora-kernel.git] / drivers / staging / intel_sst / intelmid.c
1 /*
2  *   intelmid.c - Intel Sound card driver for MID
3  *
4  *  Copyright (C) 2008-10 Intel Corp
5  *  Authors:    Harsha Priya <priya.harsha@intel.com>
6  *              Vinod Koul <vinod.koul@intel.com>
7  *              Dharageswari R <dharageswari.r@intel.com>
8  *              KP Jeeja <jeeja.kp@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  * ALSA driver for Intel MID sound card chipset
26  */
27
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/io.h>
33 #include <linux/platform_device.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/firmware.h>
37 #include <linux/input.h>
38 #include <sound/control.h>
39 #include <asm/mrst.h>
40 #include <sound/pcm.h>
41 #include <sound/jack.h>
42 #include <sound/pcm_params.h>
43 #include <sound/initval.h>
44 #include <linux/gpio.h>
45 #include "intel_sst.h"
46 #include "intel_sst_ioctl.h"
47 #include "intel_sst_fw_ipc.h"
48 #include "intel_sst_common.h"
49 #include "intelmid_snd_control.h"
50 #include "intelmid_adc_control.h"
51 #include "intelmid.h"
52
53 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
54 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
55 MODULE_AUTHOR("Dharageswari R <dharageswari.r@intel.com>");
56 MODULE_AUTHOR("KP Jeeja <jeeja.kp@intel.com>");
57 MODULE_DESCRIPTION("Intel MAD Sound card driver");
58 MODULE_LICENSE("GPL v2");
59 MODULE_SUPPORTED_DEVICE("{Intel,Intel_MAD}");
60
61
62 static int card_index = SNDRV_DEFAULT_IDX1;/* Index 0-MAX */
63 static char *card_id = SNDRV_DEFAULT_STR1;      /* ID for this card */
64
65 module_param(card_index, int, 0444);
66 MODULE_PARM_DESC(card_index, "Index value for INTELMAD soundcard.");
67 module_param(card_id, charp, 0444);
68 MODULE_PARM_DESC(card_id, "ID string for INTELMAD soundcard.");
69
70 int     sst_card_vendor_id;
71 int intelmid_audio_interrupt_enable;/*checkpatch fix*/
72 struct snd_intelmad *intelmad_drv;
73
74 #define INFO(_cpu_id, _irq_cache, _size) \
75         ((kernel_ulong_t)&(struct snd_intelmad_probe_info) {    \
76                 .cpu_id = (_cpu_id),                    \
77                 .irq_cache = (_irq_cache),                      \
78                 .size = (_size),                                \
79         })
80 /* Data path functionalities */
81 static struct snd_pcm_hardware snd_intelmad_stream = {
82         .info = (SNDRV_PCM_INFO_INTERLEAVED |
83                         SNDRV_PCM_INFO_DOUBLE |
84                         SNDRV_PCM_INFO_PAUSE |
85                         SNDRV_PCM_INFO_RESUME |
86                         SNDRV_PCM_INFO_MMAP|
87                         SNDRV_PCM_INFO_MMAP_VALID |
88                         SNDRV_PCM_INFO_BLOCK_TRANSFER |
89                         SNDRV_PCM_INFO_SYNC_START),
90         .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
91                         SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
92                         SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
93         .rates = (SNDRV_PCM_RATE_8000|
94                         SNDRV_PCM_RATE_44100 |
95                         SNDRV_PCM_RATE_48000),
96         .rate_min = MIN_RATE,
97
98         .rate_max = MAX_RATE,
99         .channels_min = MIN_CHANNEL,
100         .channels_max = MAX_CHANNEL_AMIC,
101         .buffer_bytes_max = MAX_BUFFER,
102         .period_bytes_min = MIN_PERIOD_BYTES,
103         .period_bytes_max = MAX_PERIOD_BYTES,
104         .periods_min = MIN_PERIODS,
105         .periods_max = MAX_PERIODS,
106         .fifo_size = FIFO_SIZE,
107 };
108
109
110 /**
111  * snd_intelmad_pcm_trigger - stream activities are handled here
112  *
113  * @substream:substream for which the stream function is called
114  * @cmd:the stream commamd that requested from upper layer
115  *
116  * This function is called whenever an a stream activity is invoked
117  */
118 static int snd_intelmad_pcm_trigger(struct snd_pcm_substream *substream,
119                                         int cmd)
120 {
121         int ret_val = 0, str_id;
122         struct snd_intelmad *intelmaddata;
123         struct mad_stream_pvt *stream;
124         struct intel_sst_pcm_control *sst_ops;
125
126         WARN_ON(!substream);
127
128         intelmaddata = snd_pcm_substream_chip(substream);
129         stream = substream->runtime->private_data;
130
131         WARN_ON(!intelmaddata->sstdrv_ops);
132         WARN_ON(!intelmaddata->sstdrv_ops->scard_ops);
133         sst_ops  = intelmaddata->sstdrv_ops->pcm_control;
134         str_id = stream->stream_info.str_id;
135
136         switch (cmd) {
137         case SNDRV_PCM_TRIGGER_START:
138                 pr_debug("Trigger Start\n");
139                 ret_val = sst_ops->device_control(SST_SND_START, &str_id);
140                 if (ret_val)
141                         return ret_val;
142                 stream->stream_status = RUNNING;
143                 stream->substream = substream;
144                 break;
145         case SNDRV_PCM_TRIGGER_STOP:
146                 pr_debug("in stop\n");
147                 ret_val = sst_ops->device_control(SST_SND_DROP, &str_id);
148                 if (ret_val)
149                         return ret_val;
150                 stream->stream_status = DROPPED;
151                 break;
152         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
153                 pr_debug("in pause\n");
154                 ret_val = sst_ops->device_control(SST_SND_PAUSE, &str_id);
155                 if (ret_val)
156                         return ret_val;
157                 stream->stream_status = PAUSED;
158                 break;
159         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
160                 pr_debug("in pause release\n");
161                 ret_val = sst_ops->device_control(SST_SND_RESUME, &str_id);
162                 if (ret_val)
163                         return ret_val;
164                 stream->stream_status = RUNNING;
165                 break;
166         default:
167                 return -EINVAL;
168         }
169         return ret_val;
170 }
171
172 /**
173 * snd_intelmad_pcm_prepare- internal preparation before starting a stream
174 *
175 * @substream:  substream for which the function is called
176 *
177 * This function is called when a stream is started for internal preparation.
178 */
179 static int snd_intelmad_pcm_prepare(struct snd_pcm_substream *substream)
180 {
181         struct mad_stream_pvt *stream;
182         int ret_val = 0;
183         struct snd_intelmad *intelmaddata;
184
185         pr_debug("pcm_prepare called\n");
186
187         WARN_ON(!substream);
188         stream = substream->runtime->private_data;
189         intelmaddata = snd_pcm_substream_chip(substream);
190         pr_debug("pb cnt = %d cap cnt = %d\n",\
191                 intelmaddata->playback_cnt,
192                 intelmaddata->capture_cnt);
193
194         if (stream->stream_info.str_id) {
195                 pr_debug("Prepare called for already set stream\n");
196                 ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
197                                 SST_SND_DROP, &stream->stream_info.str_id);
198                 return ret_val;
199         }
200
201         ret_val = snd_intelmad_alloc_stream(substream);
202         if (ret_val < 0)
203                 return ret_val;
204         stream->dbg_cum_bytes = 0;
205         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
206                         intelmaddata->playback_cnt++;
207         else
208                 intelmaddata->capture_cnt++;
209         /* return back the stream id */
210         snprintf(substream->pcm->id, sizeof(substream->pcm->id),
211                         "%d", stream->stream_info.str_id);
212         pr_debug("stream id to user = %s\n",
213                         substream->pcm->id);
214
215         ret_val = snd_intelmad_init_stream(substream);
216         if (ret_val)
217                 return ret_val;
218         substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
219         return ret_val;
220 }
221
222 static int snd_intelmad_hw_params(struct snd_pcm_substream *substream,
223                                     struct snd_pcm_hw_params *hw_params)
224 {
225         int ret_val;
226
227         pr_debug("snd_intelmad_hw_params called\n");
228         ret_val = snd_pcm_lib_malloc_pages(substream,
229                         params_buffer_bytes(hw_params));
230         memset(substream->runtime->dma_area, 0,
231                         params_buffer_bytes(hw_params));
232
233         return ret_val;
234 }
235
236 static int snd_intelmad_hw_free(struct snd_pcm_substream *substream)
237 {
238         pr_debug("snd_intelmad_hw_free called\n");
239         return snd_pcm_lib_free_pages(substream);
240 }
241
242 /**
243  * snd_intelmad_pcm_pointer- to send the current buffer pointer processed by hw
244  *
245  * @substream:  substream for which the function is called
246  *
247  * This function is called by ALSA framework to get the current hw buffer ptr
248  * when a period is elapsed
249  */
250 static snd_pcm_uframes_t snd_intelmad_pcm_pointer
251                         (struct snd_pcm_substream *substream)
252 {
253         /* struct snd_pcm_runtime *runtime = substream->runtime; */
254         struct mad_stream_pvt *stream;
255         struct snd_intelmad *intelmaddata;
256         int ret_val;
257
258         WARN_ON(!substream);
259
260         intelmaddata = snd_pcm_substream_chip(substream);
261         stream = substream->runtime->private_data;
262         if (stream->stream_status == INIT)
263                 return 0;
264
265         ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
266                         SST_SND_BUFFER_POINTER, &stream->stream_info);
267         if (ret_val) {
268                 pr_err("error code = 0x%x\n", ret_val);
269                 return ret_val;
270         }
271         pr_debug("samples reported out 0x%llx\n",
272                         stream->stream_info.buffer_ptr);
273         pr_debug("Frame bits:: %d period_count :: %d\n",
274                         (int)substream->runtime->frame_bits,
275                         (int)substream->runtime->period_size);
276
277         return stream->stream_info.buffer_ptr;
278
279 }
280
281 /**
282  * snd_intelmad_close- to free parameteres when stream is stopped
283  *
284  * @substream:  substream for which the function is called
285  *
286  * This function is called by ALSA framework when stream is stopped
287  */
288 static int snd_intelmad_close(struct snd_pcm_substream *substream)
289 {
290         struct snd_intelmad *intelmaddata;
291         struct mad_stream_pvt *stream;
292         int ret_val = 0, str_id;
293
294         WARN_ON(!substream);
295
296         stream = substream->runtime->private_data;
297         str_id = stream->stream_info.str_id;
298
299         pr_debug("sst: snd_intelmad_close called for %d\n", str_id);
300         intelmaddata = snd_pcm_substream_chip(substream);
301
302         pr_debug("str id = %d\n", stream->stream_info.str_id);
303         if (stream->stream_info.str_id) {
304                 /* SST API to actually stop/free the stream */
305                 ret_val = intelmaddata->sstdrv_ops->pcm_control->close(str_id);
306                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
307                         intelmaddata->playback_cnt--;
308                 else
309                         intelmaddata->capture_cnt--;
310         }
311         pr_debug("snd_intelmad_close : pb cnt = %d cap cnt = %d\n",
312                 intelmaddata->playback_cnt, intelmaddata->capture_cnt);
313         kfree(substream->runtime->private_data);
314         return ret_val;
315 }
316
317 /**
318  * snd_intelmad_open- to set runtime parameters during stream start
319  *
320  * @substream:  substream for which the function is called
321  * @type: audio device type
322  *
323  * This function is called by ALSA framework when stream is started
324  */
325 static int snd_intelmad_open(struct snd_pcm_substream *substream,
326                         enum snd_sst_audio_device_type type)
327 {
328         struct snd_intelmad *intelmaddata;
329         struct snd_pcm_runtime *runtime;
330         struct mad_stream_pvt *stream;
331
332         WARN_ON(!substream);
333
334         pr_debug("snd_intelmad_open called\n");
335
336         intelmaddata = snd_pcm_substream_chip(substream);
337         runtime = substream->runtime;
338         /* set the runtime hw parameter with local snd_pcm_hardware struct */
339         runtime->hw = snd_intelmad_stream;
340         if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
341                 /*
342                  * MRST firmware currently denies stereo recording requests.
343                  */
344                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
345                         runtime->hw.formats = (SNDRV_PCM_FMTBIT_S16 |
346                                                SNDRV_PCM_FMTBIT_U16);
347                         runtime->hw.channels_max = 1;
348                 }
349         }
350         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
351                 runtime->hw = snd_intelmad_stream;
352                 runtime->hw.rates = SNDRV_PCM_RATE_48000;
353                 runtime->hw.rate_min = MAX_RATE;
354                 runtime->hw.formats = (SNDRV_PCM_FMTBIT_S24 |
355                                                 SNDRV_PCM_FMTBIT_U24);
356                 if (intelmaddata->sstdrv_ops->scard_ops->input_dev_id == AMIC)
357                         runtime->hw.channels_max = MAX_CHANNEL_AMIC;
358                 else
359                         runtime->hw.channels_max = MAX_CHANNEL_DMIC;
360
361         }
362         /* setup the internal datastruture stream pointers based on it being
363         playback or capture stream */
364         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
365         if (!stream)
366                 return -ENOMEM;
367         stream->stream_info.str_id = 0;
368         stream->device = type;
369         stream->stream_status = INIT;
370         runtime->private_data = stream;
371         return snd_pcm_hw_constraint_integer(runtime,
372                          SNDRV_PCM_HW_PARAM_PERIODS);
373 }
374
375 static int snd_intelmad_headset_open(struct snd_pcm_substream *substream)
376 {
377         return snd_intelmad_open(substream, SND_SST_DEVICE_HEADSET);
378 }
379
380 static int snd_intelmad_ihf_open(struct snd_pcm_substream *substream)
381 {
382         return snd_intelmad_open(substream, SND_SST_DEVICE_IHF);
383 }
384
385 static int snd_intelmad_vibra_open(struct snd_pcm_substream *substream)
386 {
387         return snd_intelmad_open(substream, SND_SST_DEVICE_VIBRA);
388 }
389
390 static int snd_intelmad_haptic_open(struct snd_pcm_substream *substream)
391 {
392         return snd_intelmad_open(substream, SND_SST_DEVICE_HAPTIC);
393 }
394
395 static struct snd_pcm_ops snd_intelmad_headset_ops = {
396         .open = snd_intelmad_headset_open,
397         .close = snd_intelmad_close,
398         .ioctl = snd_pcm_lib_ioctl,
399         .hw_params = snd_intelmad_hw_params,
400         .hw_free = snd_intelmad_hw_free,
401         .prepare = snd_intelmad_pcm_prepare,
402         .trigger = snd_intelmad_pcm_trigger,
403         .pointer = snd_intelmad_pcm_pointer,
404 };
405
406 static struct snd_pcm_ops snd_intelmad_ihf_ops = {
407         .open = snd_intelmad_ihf_open,
408         .close = snd_intelmad_close,
409         .ioctl = snd_pcm_lib_ioctl,
410         .hw_params = snd_intelmad_hw_params,
411         .hw_free = snd_intelmad_hw_free,
412         .prepare = snd_intelmad_pcm_prepare,
413         .trigger = snd_intelmad_pcm_trigger,
414         .pointer = snd_intelmad_pcm_pointer,
415 };
416
417 static struct snd_pcm_ops snd_intelmad_vibra_ops = {
418         .open = snd_intelmad_vibra_open,
419         .close = snd_intelmad_close,
420         .ioctl = snd_pcm_lib_ioctl,
421         .hw_params = snd_intelmad_hw_params,
422         .hw_free = snd_intelmad_hw_free,
423         .prepare = snd_intelmad_pcm_prepare,
424         .trigger = snd_intelmad_pcm_trigger,
425         .pointer = snd_intelmad_pcm_pointer,
426 };
427
428 static struct snd_pcm_ops snd_intelmad_haptic_ops = {
429         .open = snd_intelmad_haptic_open,
430         .close = snd_intelmad_close,
431         .ioctl = snd_pcm_lib_ioctl,
432         .hw_params = snd_intelmad_hw_params,
433         .hw_free = snd_intelmad_hw_free,
434         .prepare = snd_intelmad_pcm_prepare,
435         .trigger = snd_intelmad_pcm_trigger,
436         .pointer = snd_intelmad_pcm_pointer,
437 };
438
439 static struct snd_pcm_ops snd_intelmad_capture_ops = {
440         .open = snd_intelmad_headset_open,
441         .close = snd_intelmad_close,
442         .ioctl = snd_pcm_lib_ioctl,
443         .hw_params = snd_intelmad_hw_params,
444         .hw_free = snd_intelmad_hw_free,
445         .prepare = snd_intelmad_pcm_prepare,
446         .trigger = snd_intelmad_pcm_trigger,
447         .pointer = snd_intelmad_pcm_pointer,
448 };
449
450 int intelmad_get_mic_bias(void)
451 {
452         struct snd_pmic_ops *pmic_ops;
453
454         if (!intelmad_drv || !intelmad_drv->sstdrv_ops)
455                 return -ENODEV;
456         pmic_ops = intelmad_drv->sstdrv_ops->scard_ops;
457         if (pmic_ops && pmic_ops->pmic_get_mic_bias)
458                 return pmic_ops->pmic_get_mic_bias(intelmad_drv);
459         else
460                 return -ENODEV;
461 }
462 EXPORT_SYMBOL_GPL(intelmad_get_mic_bias);
463
464 int intelmad_set_headset_state(int state)
465 {
466         struct snd_pmic_ops *pmic_ops;
467
468         if (!intelmad_drv || !intelmad_drv->sstdrv_ops)
469                 return -ENODEV;
470         pmic_ops = intelmad_drv->sstdrv_ops->scard_ops;
471         if (pmic_ops && pmic_ops->pmic_set_headset_state)
472                 return pmic_ops->pmic_set_headset_state(state);
473         else
474                 return -ENODEV;
475 }
476 EXPORT_SYMBOL_GPL(intelmad_set_headset_state);
477
478 void sst_process_mad_jack_detection(struct work_struct *work)
479 {
480         u8 interrupt_status;
481         struct mad_jack_msg_wq *mad_jack_detect =
482                         container_of(work, struct mad_jack_msg_wq, wq);
483
484         struct snd_intelmad *intelmaddata =
485                         mad_jack_detect->intelmaddata;
486
487         if (!intelmaddata)
488                 return;
489
490         interrupt_status = mad_jack_detect->intsts;
491         if (intelmaddata->sstdrv_ops && intelmaddata->sstdrv_ops->scard_ops
492                         && intelmaddata->sstdrv_ops->scard_ops->pmic_irq_cb) {
493                 intelmaddata->sstdrv_ops->scard_ops->pmic_irq_cb(
494                         (void *)intelmaddata, interrupt_status);
495                 intelmaddata->sstdrv_ops->scard_ops->pmic_jack_enable();
496         }
497         kfree(mad_jack_detect);
498 }
499 /**
500  * snd_intelmad_intr_handler- interrupt handler
501  *
502  * @irq :  irq number of the interrupt received
503  * @dev: device context
504  *
505  * This function is called when an interrupt is raised at the sound card
506  */
507 static irqreturn_t snd_intelmad_intr_handler(int irq, void *dev)
508 {
509         struct snd_intelmad *intelmaddata =
510                         (struct snd_intelmad *)dev;
511         u8 interrupt_status;
512         struct mad_jack_msg_wq  *mad_jack_msg;
513         memcpy_fromio(&interrupt_status,
514                         ((void *)(intelmaddata->int_base)),
515                         sizeof(u8));
516
517         mad_jack_msg = kzalloc(sizeof(*mad_jack_msg), GFP_ATOMIC);
518         mad_jack_msg->intsts = interrupt_status;
519         mad_jack_msg->intelmaddata = intelmaddata;
520         INIT_WORK(&mad_jack_msg->wq, sst_process_mad_jack_detection);
521         queue_work(intelmaddata->mad_jack_wq, &mad_jack_msg->wq);
522
523         return IRQ_HANDLED;
524 }
525
526 void sst_mad_send_jack_report(struct snd_jack *jack,
527                                 int buttonpressevent , int status)
528 {
529
530         if (!jack) {
531                 pr_debug("MAD error jack empty\n");
532
533         } else {
534                 snd_jack_report(jack, status);
535                 /* button pressed and released */
536                 if (buttonpressevent)
537                         snd_jack_report(jack, 0);
538                 pr_debug("MAD sending jack report Done !!!\n");
539         }
540 }
541
542 static int __devinit snd_intelmad_register_irq(
543                 struct snd_intelmad *intelmaddata, unsigned int regbase,
544                 unsigned int regsize)
545 {
546         int ret_val;
547         char *drv_name;
548
549         pr_debug("irq reg regbase 0x%x, regsize 0x%x\n",
550                                         regbase, regsize);
551         intelmaddata->int_base = ioremap_nocache(regbase, regsize);
552         if (!intelmaddata->int_base)
553                 pr_err("Mapping of cache failed\n");
554         pr_debug("irq = 0x%x\n", intelmaddata->irq);
555         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL)
556                 drv_name = DRIVER_NAME_MFLD;
557         else
558                 drv_name = DRIVER_NAME_MRST;
559         ret_val = request_irq(intelmaddata->irq,
560                                 snd_intelmad_intr_handler,
561                                 IRQF_SHARED, drv_name,
562                                 intelmaddata);
563         if (ret_val)
564                 pr_err("cannot register IRQ\n");
565         return ret_val;
566 }
567
568 static int __devinit snd_intelmad_sst_register(
569                         struct snd_intelmad *intelmaddata)
570 {
571         int ret_val = 0;
572         struct snd_pmic_ops *intelmad_vendor_ops[MAX_VENDORS] = {
573                 &snd_pmic_ops_fs,
574                 &snd_pmic_ops_mx,
575                 &snd_pmic_ops_nc,
576                 &snd_msic_ops
577         };
578
579         struct sc_reg_access vendor_addr = {0x00, 0x00, 0x00};
580
581         if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
582                 ret_val = sst_sc_reg_access(&vendor_addr, PMIC_READ, 1);
583                 if (ret_val)
584                         return ret_val;
585                 sst_card_vendor_id = (vendor_addr.value & (MASK2|MASK1|MASK0));
586                 pr_debug("original n extrated vendor id = 0x%x %d\n",
587                                 vendor_addr.value, sst_card_vendor_id);
588                 if (sst_card_vendor_id < 0 || sst_card_vendor_id > 2) {
589                         pr_err("vendor card not supported!!\n");
590                         return -EIO;
591                 }
592         } else
593                 sst_card_vendor_id = 0x3;
594
595         intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
596         intelmaddata->sstdrv_ops->vendor_id = sst_card_vendor_id;
597         BUG_ON(!intelmad_vendor_ops[sst_card_vendor_id]);
598         intelmaddata->sstdrv_ops->scard_ops =
599                         intelmad_vendor_ops[sst_card_vendor_id];
600
601         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
602                 intelmaddata->sstdrv_ops->scard_ops->pb_on = 0;
603                 intelmaddata->sstdrv_ops->scard_ops->cap_on = 0;
604                 intelmaddata->sstdrv_ops->scard_ops->input_dev_id = DMIC;
605                 intelmaddata->sstdrv_ops->scard_ops->output_dev_id =
606                                                         STEREO_HEADPHONE;
607                 intelmaddata->sstdrv_ops->scard_ops->lineout_dev_id = NONE;
608         }
609
610         /* registering with SST driver to get access to SST APIs to use */
611         ret_val = register_sst_card(intelmaddata->sstdrv_ops);
612         if (ret_val) {
613                 pr_err("sst card registration failed\n");
614                 return ret_val;
615         }
616         sst_card_vendor_id = intelmaddata->sstdrv_ops->vendor_id;
617         intelmaddata->pmic_status = PMIC_UNINIT;
618         return ret_val;
619 }
620
621 static void snd_intelmad_page_free(struct snd_pcm *pcm)
622 {
623         snd_pcm_lib_preallocate_free_for_all(pcm);
624 }
625 /* Driver Init/exit functionalities */
626 /**
627  * snd_intelmad_pcm_new - to setup pcm for the card
628  *
629  * @card:  pointer to the sound card structure
630  * @intelmaddata: pointer to internal context
631  * @pb: playback count for this card
632  * @cap: capture count for this card
633  * @index: device index
634  *
635  * This function is called from probe function to set up pcm params
636  * and functions
637  */
638 static int __devinit snd_intelmad_pcm_new(struct snd_card *card,
639                         struct snd_intelmad *intelmaddata,
640                         unsigned int pb, unsigned int cap, unsigned int index)
641 {
642         int ret_val = 0;
643         struct snd_pcm *pcm;
644         char name[32] = INTEL_MAD;
645         struct snd_pcm_ops *pb_ops = NULL, *cap_ops = NULL;
646
647         pr_debug("called for pb %d, cp %d, idx %d\n", pb, cap, index);
648         ret_val = snd_pcm_new(card, name, index, pb, cap, &pcm);
649         if (ret_val)
650                 return ret_val;
651         /* setup the ops for playback and capture streams */
652         switch (index) {
653         case 0:
654                 pb_ops = &snd_intelmad_headset_ops;
655                 cap_ops = &snd_intelmad_capture_ops;
656                 break;
657         case 1:
658                 pb_ops = &snd_intelmad_ihf_ops;
659                 cap_ops = &snd_intelmad_capture_ops;
660                 break;
661         case 2:
662                 pb_ops = &snd_intelmad_vibra_ops;
663                 cap_ops = &snd_intelmad_capture_ops;
664                 break;
665         case 3:
666                 pb_ops = &snd_intelmad_haptic_ops;
667                 cap_ops = &snd_intelmad_capture_ops;
668                 break;
669         }
670         if (pb)
671                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, pb_ops);
672         if (cap)
673                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, cap_ops);
674         /* setup private data which can be retrieved when required */
675         pcm->private_data = intelmaddata;
676         pcm->private_free = snd_intelmad_page_free;
677         pcm->info_flags = 0;
678         strncpy(pcm->name, card->shortname, strlen(card->shortname));
679         /* allocate dma pages for ALSA stream operations */
680         snd_pcm_lib_preallocate_pages_for_all(pcm,
681                         SNDRV_DMA_TYPE_CONTINUOUS,
682                         snd_dma_continuous_data(GFP_KERNEL),
683                         MIN_BUFFER, MAX_BUFFER);
684         return ret_val;
685 }
686
687 static int __devinit snd_intelmad_pcm(struct snd_card *card,
688                                 struct snd_intelmad *intelmaddata)
689 {
690         int ret_val = 0;
691
692         WARN_ON(!card);
693         WARN_ON(!intelmaddata);
694         pr_debug("snd_intelmad_pcm called\n");
695         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 1, 0);
696         if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT)
697                 return ret_val;
698         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 1);
699         if (ret_val)
700                 return ret_val;
701         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 2);
702         if (ret_val)
703                 return ret_val;
704         return snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 3);
705 }
706
707 /**
708  * snd_intelmad_jack- to setup jack settings of the card
709  *
710  * @intelmaddata: pointer to internal context
711  *
712  * This function is called send jack events
713  */
714 static int snd_intelmad_jack(struct snd_intelmad *intelmaddata)
715 {
716         struct snd_jack *jack;
717         int retval;
718
719         pr_debug("snd_intelmad_jack called\n");
720         jack = &intelmaddata->jack[0].jack;
721         snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PHONE);
722         retval = snd_jack_new(intelmaddata->card, "Intel(R) MID Audio Jack",
723                 SND_JACK_HEADPHONE | SND_JACK_HEADSET |
724                 SW_JACK_PHYSICAL_INSERT | SND_JACK_BTN_0
725                 | SND_JACK_BTN_1, &jack);
726         pr_debug("snd_intelmad_jack called\n");
727         if (retval < 0)
728                 return retval;
729         snd_jack_report(jack, 0);
730
731         jack->private_data = jack;
732         intelmaddata->jack[0].jack = *jack;
733
734         return retval;
735 }
736
737 /**
738  * snd_intelmad_mixer- to setup mixer settings of the card
739  *
740  * @intelmaddata: pointer to internal context
741  *
742  * This function is called from probe function to set up mixer controls
743  */
744 static int __devinit snd_intelmad_mixer(struct snd_intelmad *intelmaddata)
745 {
746         struct snd_card *card;
747         unsigned int idx;
748         int ret_val = 0, max_controls = 0;
749         char *mixername = "IntelMAD Controls";
750         struct snd_kcontrol_new *controls;
751
752         WARN_ON(!intelmaddata);
753
754         card = intelmaddata->card;
755         strncpy(card->mixername, mixername, sizeof(card->mixername)-1);
756         /* add all widget controls and expose the same */
757         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
758                 max_controls = MAX_CTRL_MFLD;
759                 controls = snd_intelmad_controls_mfld;
760         } else {
761                 max_controls = MAX_CTRL_MRST;
762                 controls = snd_intelmad_controls_mrst;
763         }
764         for (idx = 0; idx < max_controls; idx++) {
765                 ret_val = snd_ctl_add(card,
766                                 snd_ctl_new1(&controls[idx],
767                                 intelmaddata));
768                 pr_debug("mixer[idx]=%d added\n", idx);
769                 if (ret_val) {
770                         pr_err("in adding of control index = %d\n", idx);
771                         break;
772                 }
773         }
774         return ret_val;
775 }
776
777 static int snd_intelmad_dev_free(struct snd_device *device)
778 {
779         struct snd_intelmad *intelmaddata;
780
781         WARN_ON(!device);
782
783         intelmaddata = device->device_data;
784
785         pr_debug("snd_intelmad_dev_free called\n");
786         unregister_sst_card(intelmaddata->sstdrv_ops);
787
788         /* free allocated memory for internal context */
789         destroy_workqueue(intelmaddata->mad_jack_wq);
790         device->device_data = NULL;
791         kfree(intelmaddata->sstdrv_ops);
792         kfree(intelmaddata);
793
794         return 0;
795 }
796
797 static int __devinit snd_intelmad_create(
798                 struct snd_intelmad *intelmaddata,
799                 struct snd_card *card)
800 {
801         int ret_val;
802         static struct snd_device_ops ops = {
803                 .dev_free =     snd_intelmad_dev_free,
804         };
805
806         WARN_ON(!intelmaddata);
807         WARN_ON(!card);
808         /* ALSA api to register for the device */
809         ret_val = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelmaddata, &ops);
810         return ret_val;
811 }
812
813 /**
814 * snd_intelmad_probe- function registred for init
815 * @pdev :  pointer to the device struture
816 * This function is called when the device is initialized
817 */
818 int __devinit snd_intelmad_probe(struct platform_device *pdev)
819 {
820         struct snd_card *card;
821         int ret_val;
822         struct snd_intelmad *intelmaddata;
823         const struct platform_device_id *id = platform_get_device_id(pdev);
824         struct snd_intelmad_probe_info *info = (void *)id->driver_data;
825
826         pr_debug("probe for %s cpu_id %d\n", pdev->name, info->cpu_id);
827         pr_debug("rq_chache %x of size %x\n", info->irq_cache, info->size);
828         if (!strcmp(pdev->name, DRIVER_NAME_MRST))
829                 pr_debug("detected MRST\n");
830         else if (!strcmp(pdev->name, DRIVER_NAME_MFLD))
831                 pr_debug("detected MFLD\n");
832         else {
833                 pr_err("detected unknown device abort!!\n");
834                 return -EIO;
835         }
836         if ((info->cpu_id < CPU_CHIP_LINCROFT) ||
837                                 (info->cpu_id > CPU_CHIP_PENWELL)) {
838                 pr_err("detected unknown cpu_id abort!!\n");
839                 return -EIO;
840         }
841         /* allocate memory for saving internal context and working */
842         intelmaddata = kzalloc(sizeof(*intelmaddata), GFP_KERNEL);
843         if (!intelmaddata) {
844                 pr_debug("mem alloctn fail\n");
845                 return -ENOMEM;
846         }
847         intelmad_drv = intelmaddata;
848
849         /* allocate memory for LPE API set */
850         intelmaddata->sstdrv_ops = kzalloc(sizeof(struct intel_sst_card_ops),
851                                         GFP_KERNEL);
852         if (!intelmaddata->sstdrv_ops) {
853                 pr_err("mem allocation for ops fail\n");
854                 kfree(intelmaddata);
855                 return -ENOMEM;
856         }
857
858         intelmaddata->cpu_id = info->cpu_id;
859         /* create a card instance with ALSA framework */
860         ret_val = snd_card_create(card_index, card_id, THIS_MODULE, 0, &card);
861         if (ret_val) {
862                 pr_err("snd_card_create fail\n");
863                 goto free_allocs;
864         }
865
866         intelmaddata->pdev = pdev;
867         intelmaddata->irq = platform_get_irq(pdev, 0);
868         platform_set_drvdata(pdev, intelmaddata);
869         intelmaddata->card = card;
870         intelmaddata->card_id = card_id;
871         intelmaddata->card_index = card_index;
872         intelmaddata->master_mute = UNMUTE;
873         intelmaddata->playback_cnt =  intelmaddata->capture_cnt = 0;
874         strncpy(card->driver, INTEL_MAD, strlen(INTEL_MAD));
875         strncpy(card->shortname, INTEL_MAD, strlen(INTEL_MAD));
876
877         intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
878         /* registering with LPE driver to get access to SST APIs to use */
879         ret_val = snd_intelmad_sst_register(intelmaddata);
880         if (ret_val) {
881                 pr_err("snd_intelmad_sst_register failed\n");
882                 goto set_null_data;
883         }
884
885         intelmaddata->pmic_status = PMIC_INIT;
886
887         ret_val = snd_intelmad_pcm(card, intelmaddata);
888         if (ret_val) {
889                 pr_err("snd_intelmad_pcm failed\n");
890                 goto free_sst;
891         }
892
893         ret_val = snd_intelmad_mixer(intelmaddata);
894         if (ret_val) {
895                 pr_err("snd_intelmad_mixer failed\n");
896                 goto free_card;
897         }
898
899         ret_val = snd_intelmad_jack(intelmaddata);
900         if (ret_val) {
901                 pr_err("snd_intelmad_jack failed\n");
902                 goto free_card;
903         }
904         intelmaddata->adc_address = mid_initialize_adc();
905
906         /*create work queue for jack interrupt*/
907         INIT_WORK(&intelmaddata->mad_jack_msg.wq,
908                 sst_process_mad_jack_detection);
909
910         intelmaddata->mad_jack_wq = create_workqueue("sst_mad_jack_wq");
911         if (!intelmaddata->mad_jack_wq)
912                 goto free_card;
913
914         ret_val = snd_intelmad_register_irq(intelmaddata,
915                                         info->irq_cache, info->size);
916         if (ret_val) {
917                 pr_err("snd_intelmad_register_irq fail\n");
918                 goto free_mad_jack_wq;
919         }
920
921         /* internal function call to register device with ALSA */
922         ret_val = snd_intelmad_create(intelmaddata, card);
923         if (ret_val) {
924                 pr_err("snd_intelmad_create failed\n");
925                 goto set_pvt_data;
926         }
927         card->private_data = &intelmaddata;
928         snd_card_set_dev(card, &pdev->dev);
929         ret_val = snd_card_register(card);
930         if (ret_val) {
931                 pr_err("snd_card_register failed\n");
932                 goto set_pvt_data;
933         }
934         if (pdev->dev.platform_data) {
935                 int gpio_amp = *(int *)pdev->dev.platform_data;
936                 if (gpio_request_one(gpio_amp, GPIOF_OUT_INIT_LOW, "amp power"))
937                         gpio_amp = 0;
938                 intelmaddata->sstdrv_ops->scard_ops->gpio_amp = gpio_amp;
939         }
940
941         pr_debug("snd_intelmad_probe complete\n");
942         return ret_val;
943
944 set_pvt_data:
945         card->private_data = NULL;
946 free_mad_jack_wq:
947         destroy_workqueue(intelmaddata->mad_jack_wq);
948 free_card:
949         snd_card_free(intelmaddata->card);
950 free_sst:
951         unregister_sst_card(intelmaddata->sstdrv_ops);
952 set_null_data:
953         platform_set_drvdata(pdev, NULL);
954 free_allocs:
955         pr_err("probe failed\n");
956         snd_card_free(card);
957         kfree(intelmaddata->sstdrv_ops);
958         kfree(intelmaddata);
959         return ret_val;
960 }
961
962
963 static int snd_intelmad_remove(struct platform_device *pdev)
964 {
965         struct snd_intelmad *intelmaddata = platform_get_drvdata(pdev);
966
967         if (intelmaddata) {
968                 if (intelmaddata->sstdrv_ops->scard_ops->gpio_amp)
969                         gpio_free(intelmaddata->sstdrv_ops->scard_ops->gpio_amp);
970                 free_irq(intelmaddata->irq, intelmaddata);
971                 snd_card_free(intelmaddata->card);
972         }
973         intelmad_drv = NULL;
974         platform_set_drvdata(pdev, NULL);
975         return 0;
976 }
977
978 /*********************************************************************
979  *              Driver initialization and exit
980  *********************************************************************/
981 static const struct platform_device_id snd_intelmad_ids[] = {
982         {DRIVER_NAME_MRST, INFO(CPU_CHIP_LINCROFT, AUDINT_BASE, 1)},
983         {DRIVER_NAME_MFLD, INFO(CPU_CHIP_PENWELL, 0xFFFF7FCD, 1)},
984         {"", 0},
985
986 };
987
988 static struct platform_driver snd_intelmad_driver = {
989         .driver = {
990                 .owner = THIS_MODULE,
991                 .name = "intel_mid_sound_card",
992         },
993         .id_table = snd_intelmad_ids,
994         .probe = snd_intelmad_probe,
995         .remove = __devexit_p(snd_intelmad_remove),
996 };
997
998 /*
999  * alsa_card_intelmad_init- driver init function
1000  *
1001  * This function is called when driver module is inserted
1002  */
1003 static int __init alsa_card_intelmad_init(void)
1004 {
1005         pr_debug("mad_init called\n");
1006         return platform_driver_register(&snd_intelmad_driver);
1007 }
1008
1009 /**
1010  * alsa_card_intelmad_exit- driver exit function
1011  *
1012  * This function is called when driver module is removed
1013  */
1014 static void __exit alsa_card_intelmad_exit(void)
1015 {
1016         pr_debug("mad_exit called\n");
1017         return platform_driver_unregister(&snd_intelmad_driver);
1018 }
1019
1020 module_init(alsa_card_intelmad_init)
1021 module_exit(alsa_card_intelmad_exit)
1022