Staging: Merge Ben Collins solo6x10 tree with upstream
[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/slab.h>
31 #include <linux/io.h>
32 #include <linux/platform_device.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35 #include <sound/control.h>
36 #include <asm/mrst.h>
37 #include <sound/pcm.h>
38 #include "jack.h"
39 #include <sound/pcm_params.h>
40 #include <sound/initval.h>
41 #include "intel_sst.h"
42 #include "intel_sst_ioctl.h"
43 #include "intelmid_snd_control.h"
44 #include "intelmid.h"
45
46 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
47 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
48 MODULE_AUTHOR("Dharageswari R <dharageswari.r@intel.com>");
49 MODULE_AUTHOR("KP Jeeja <jeeja.kp@intel.com>");
50 MODULE_DESCRIPTION("Intel MAD Sound card driver");
51 MODULE_LICENSE("GPL v2");
52 MODULE_SUPPORTED_DEVICE("{Intel,Intel_MAD}");
53
54
55 static int card_index = SNDRV_DEFAULT_IDX1;/* Index 0-MAX */
56 static char *card_id = SNDRV_DEFAULT_STR1;      /* ID for this card */
57
58 module_param(card_index, int, 0444);
59 MODULE_PARM_DESC(card_index, "Index value for INTELMAD soundcard.");
60 module_param(card_id, charp, 0444);
61 MODULE_PARM_DESC(card_id, "ID string for INTELMAD soundcard.");
62
63 int     sst_card_vendor_id;
64 int intelmid_audio_interrupt_enable;/*checkpatch fix*/
65
66 /* Data path functionalities */
67 static struct snd_pcm_hardware snd_intelmad_stream = {
68         .info = (SNDRV_PCM_INFO_INTERLEAVED |
69                         SNDRV_PCM_INFO_DOUBLE |
70                         SNDRV_PCM_INFO_PAUSE |
71                         SNDRV_PCM_INFO_RESUME |
72                         SNDRV_PCM_INFO_MMAP|
73                         SNDRV_PCM_INFO_MMAP_VALID |
74                         SNDRV_PCM_INFO_BLOCK_TRANSFER |
75                         SNDRV_PCM_INFO_SYNC_START),
76         .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
77                         SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
78                         SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
79         .rates = (SNDRV_PCM_RATE_8000|
80                         SNDRV_PCM_RATE_44100 |
81                         SNDRV_PCM_RATE_48000),
82         .rate_min = MIN_RATE,
83
84         .rate_max = MAX_RATE,
85         .channels_min = MIN_CHANNEL,
86         .channels_max = MAX_CHANNEL_AMIC,
87         .buffer_bytes_max = MAX_BUFFER,
88         .period_bytes_min = MIN_PERIOD_BYTES,
89         .period_bytes_max = MAX_PERIOD_BYTES,
90         .periods_min = MIN_PERIODS,
91         .periods_max = MAX_PERIODS,
92         .fifo_size = FIFO_SIZE,
93 };
94
95
96 /**
97  * snd_intelmad_pcm_trigger - stream activities are handled here
98  *
99  * @substream:substream for which the stream function is called
100  * @cmd:the stream commamd that requested from upper layer
101  *
102  * This function is called whenever an a stream activity is invoked
103  */
104 static int snd_intelmad_pcm_trigger(struct snd_pcm_substream *substream,
105                                         int cmd)
106 {
107         int ret_val = 0;
108         struct snd_intelmad *intelmaddata;
109         struct mad_stream_pvt *stream;
110         /*struct stream_buffer buffer_to_sst;*/
111
112
113
114         WARN_ON(!substream);
115
116         intelmaddata = snd_pcm_substream_chip(substream);
117         stream = substream->runtime->private_data;
118
119         WARN_ON(!intelmaddata->sstdrv_ops);
120         WARN_ON(!intelmaddata->sstdrv_ops->scard_ops);
121
122         switch (cmd) {
123         case SNDRV_PCM_TRIGGER_START:
124                 pr_debug("Trigger Start\n");
125                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_START,
126                                 &stream->stream_info.str_id);
127                 if (ret_val)
128                         return ret_val;
129                 stream->stream_status = RUNNING;
130                 stream->substream = substream;
131                 stream->stream_status = RUNNING;
132                 break;
133         case SNDRV_PCM_TRIGGER_STOP:
134                 pr_debug("in stop\n");
135                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_DROP,
136                                 &stream->stream_info.str_id);
137                 if (ret_val)
138                         return ret_val;
139                 stream->stream_status = DROPPED;
140                 break;
141         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
142                 pr_debug("in pause\n");
143                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_PAUSE,
144                                 &stream->stream_info.str_id);
145                 if (ret_val)
146                         return ret_val;
147                 stream->stream_status = PAUSED;
148                 break;
149         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
150                 pr_debug("in pause release\n");
151                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_RESUME,
152                                                 &stream->stream_info.str_id);
153                 if (ret_val)
154                         return ret_val;
155                 stream->stream_status = RUNNING;
156                 break;
157         default:
158                 return -EINVAL;
159         }
160         return ret_val;
161 }
162
163 /**
164 * snd_intelmad_pcm_prepare- internal preparation before starting a stream
165 *
166 * @substream:  substream for which the function is called
167 *
168 * This function is called when a stream is started for internal preparation.
169 */
170 static int snd_intelmad_pcm_prepare(struct snd_pcm_substream *substream)
171 {
172         struct mad_stream_pvt *stream;
173         int ret_val = 0;
174         struct snd_intelmad *intelmaddata;
175
176         pr_debug("pcm_prepare called\n");
177
178         WARN_ON(!substream);
179         stream = substream->runtime->private_data;
180         intelmaddata = snd_pcm_substream_chip(substream);
181         pr_debug("pb cnt = %d cap cnt = %d\n",\
182                 intelmaddata->playback_cnt,
183                 intelmaddata->capture_cnt);
184
185         if (stream->stream_info.str_id) {
186                 pr_debug("Prepare called for already set stream\n");
187                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_DROP,
188                                         &stream->stream_info.str_id);
189                 return ret_val;
190         }
191
192          ret_val = snd_intelmad_alloc_stream(substream);
193         if (ret_val < 0)
194                 return ret_val;
195         stream->dbg_cum_bytes = 0;
196         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
197                         intelmaddata->playback_cnt++;
198         else
199                 intelmaddata->capture_cnt++;
200         /* return back the stream id */
201         snprintf(substream->pcm->id, sizeof(substream->pcm->id),
202                         "%d", stream->stream_info.str_id);
203         pr_debug("stream id to user = %s\n",
204                         substream->pcm->id);
205
206         ret_val = snd_intelmad_init_stream(substream);
207         if (ret_val)
208                 return ret_val;
209         substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
210         return ret_val;
211 }
212
213 static int snd_intelmad_hw_params(struct snd_pcm_substream *substream,
214                                     struct snd_pcm_hw_params *hw_params)
215 {
216         int ret_val;
217
218         pr_debug("snd_intelmad_hw_params called\n");
219         ret_val = snd_pcm_lib_malloc_pages(substream,
220                         params_buffer_bytes(hw_params));
221         memset(substream->runtime->dma_area, 0,
222                         params_buffer_bytes(hw_params));
223
224         return ret_val;
225 }
226
227 static int snd_intelmad_hw_free(struct snd_pcm_substream *substream)
228 {
229         pr_debug("snd_intelmad_hw_free called\n");
230         return snd_pcm_lib_free_pages(substream);
231 }
232
233 /**
234  * snd_intelmad_pcm_pointer- to send the current buffer pointer processed by hw
235  *
236  * @substream:  substream for which the function is called
237  *
238  * This function is called by ALSA framework to get the current hw buffer ptr
239  * when a period is elapsed
240  */
241 static snd_pcm_uframes_t snd_intelmad_pcm_pointer
242                         (struct snd_pcm_substream *substream)
243 {
244         /* struct snd_pcm_runtime *runtime = substream->runtime; */
245         struct mad_stream_pvt *stream;
246         struct snd_intelmad *intelmaddata;
247         int ret_val;
248
249         WARN_ON(!substream);
250
251         intelmaddata = snd_pcm_substream_chip(substream);
252         stream = substream->runtime->private_data;
253         if (stream->stream_status == INIT)
254                 return 0;
255
256         ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_BUFFER_POINTER,
257                                 &stream->stream_info);
258         if (ret_val) {
259                 pr_err("error code = 0x%x\n", ret_val);
260                 return ret_val;
261         }
262         pr_debug("samples reported out 0x%llx\n",
263                         stream->stream_info.buffer_ptr);
264         pr_debug("Frame bits:: %d period_count :: %d\n",
265                         (int)substream->runtime->frame_bits,
266                         (int)substream->runtime->period_size);
267
268         return stream->stream_info.buffer_ptr;
269
270 }
271
272 /**
273  * snd_intelmad_close- to free parameteres when stream is stopped
274  *
275  * @substream:  substream for which the function is called
276  *
277  * This function is called by ALSA framework when stream is stopped
278  */
279 static int snd_intelmad_close(struct snd_pcm_substream *substream)
280 {
281         struct snd_intelmad *intelmaddata;
282         struct mad_stream_pvt *stream;
283         int ret_val = 0;
284
285         WARN_ON(!substream);
286
287         stream = substream->runtime->private_data;
288
289         pr_debug("snd_intelmad_close called\n");
290         intelmaddata = snd_pcm_substream_chip(substream);
291
292         pr_debug("str id = %d\n", stream->stream_info.str_id);
293         if (stream->stream_info.str_id) {
294                 /* SST API to actually stop/free the stream */
295                 ret_val = intelmaddata->sstdrv_ops->control_set(SST_SND_FREE,
296                                 &stream->stream_info.str_id);
297                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
298                         intelmaddata->playback_cnt--;
299                 else
300                         intelmaddata->capture_cnt--;
301         }
302         pr_debug("snd_intelmad_close : pb cnt = %d cap cnt = %d\n",
303                 intelmaddata->playback_cnt, intelmaddata->capture_cnt);
304         kfree(substream->runtime->private_data);
305         return ret_val;
306 }
307
308 /**
309  * snd_intelmad_open- to set runtime parameters during stream start
310  *
311  * @substream:  substream for which the function is called
312  * @type: audio device type
313  *
314  * This function is called by ALSA framework when stream is started
315  */
316 static int snd_intelmad_open(struct snd_pcm_substream *substream,
317                         enum snd_sst_audio_device_type type)
318 {
319         struct snd_intelmad *intelmaddata;
320         struct snd_pcm_runtime *runtime;
321         struct mad_stream_pvt *stream;
322
323         WARN_ON(!substream);
324
325         pr_debug("snd_intelmad_open called\n");
326
327         intelmaddata = snd_pcm_substream_chip(substream);
328         runtime = substream->runtime;
329         /* set the runtime hw parameter with local snd_pcm_hardware struct */
330         runtime->hw = snd_intelmad_stream;
331         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
332                 runtime->hw = snd_intelmad_stream;
333                 runtime->hw.rates = SNDRV_PCM_RATE_48000;
334                 runtime->hw.rate_min = MAX_RATE;
335                 runtime->hw.formats = (SNDRV_PCM_FMTBIT_S24 |
336                                                 SNDRV_PCM_FMTBIT_U24);
337                 if (intelmaddata->sstdrv_ops->scard_ops->input_dev_id == AMIC)
338                         runtime->hw.channels_max = MAX_CHANNEL_AMIC;
339                 else
340                         runtime->hw.channels_max = MAX_CHANNEL_DMIC;
341
342         }
343         /* setup the internal datastruture stream pointers based on it being
344         playback or capture stream */
345         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
346         if (!stream)
347                 return -ENOMEM;
348         stream->stream_info.str_id = 0;
349         stream->device = type;
350         stream->stream_status = INIT;
351         runtime->private_data = stream;
352         return snd_pcm_hw_constraint_integer(runtime,
353                          SNDRV_PCM_HW_PARAM_PERIODS);
354 }
355
356 static int snd_intelmad_headset_open(struct snd_pcm_substream *substream)
357 {
358         return snd_intelmad_open(substream, SND_SST_DEVICE_HEADSET);
359 }
360
361 static int snd_intelmad_ihf_open(struct snd_pcm_substream *substream)
362 {
363         return snd_intelmad_open(substream, SND_SST_DEVICE_IHF);
364 }
365
366 static int snd_intelmad_vibra_open(struct snd_pcm_substream *substream)
367 {
368         return snd_intelmad_open(substream, SND_SST_DEVICE_VIBRA);
369 }
370
371 static int snd_intelmad_haptic_open(struct snd_pcm_substream *substream)
372 {
373         return snd_intelmad_open(substream, SND_SST_DEVICE_HAPTIC);
374 }
375
376 static struct snd_pcm_ops snd_intelmad_headset_ops = {
377         .open = snd_intelmad_headset_open,
378         .close = snd_intelmad_close,
379         .ioctl = snd_pcm_lib_ioctl,
380         .hw_params = snd_intelmad_hw_params,
381         .hw_free = snd_intelmad_hw_free,
382         .prepare = snd_intelmad_pcm_prepare,
383         .trigger = snd_intelmad_pcm_trigger,
384         .pointer = snd_intelmad_pcm_pointer,
385 };
386
387 static struct snd_pcm_ops snd_intelmad_ihf_ops = {
388         .open = snd_intelmad_ihf_open,
389         .close = snd_intelmad_close,
390         .ioctl = snd_pcm_lib_ioctl,
391         .hw_params = snd_intelmad_hw_params,
392         .hw_free = snd_intelmad_hw_free,
393         .prepare = snd_intelmad_pcm_prepare,
394         .trigger = snd_intelmad_pcm_trigger,
395         .pointer = snd_intelmad_pcm_pointer,
396 };
397
398 static struct snd_pcm_ops snd_intelmad_vibra_ops = {
399         .open = snd_intelmad_vibra_open,
400         .close = snd_intelmad_close,
401         .ioctl = snd_pcm_lib_ioctl,
402         .hw_params = snd_intelmad_hw_params,
403         .hw_free = snd_intelmad_hw_free,
404         .prepare = snd_intelmad_pcm_prepare,
405         .trigger = snd_intelmad_pcm_trigger,
406         .pointer = snd_intelmad_pcm_pointer,
407 };
408
409 static struct snd_pcm_ops snd_intelmad_haptic_ops = {
410         .open = snd_intelmad_haptic_open,
411         .close = snd_intelmad_close,
412         .ioctl = snd_pcm_lib_ioctl,
413         .hw_params = snd_intelmad_hw_params,
414         .hw_free = snd_intelmad_hw_free,
415         .prepare = snd_intelmad_pcm_prepare,
416         .trigger = snd_intelmad_pcm_trigger,
417         .pointer = snd_intelmad_pcm_pointer,
418 };
419
420 static struct snd_pcm_ops snd_intelmad_capture_ops = {
421         .open = snd_intelmad_headset_open,
422         .close = snd_intelmad_close,
423         .ioctl = snd_pcm_lib_ioctl,
424         .hw_params = snd_intelmad_hw_params,
425         .hw_free = snd_intelmad_hw_free,
426         .prepare = snd_intelmad_pcm_prepare,
427         .trigger = snd_intelmad_pcm_trigger,
428         .pointer = snd_intelmad_pcm_pointer,
429 };
430
431
432 /**
433  * snd_intelmad_intr_handler- interrupt handler
434  *
435  * @irq :  irq number of the interrupt received
436  * @dev: device context
437  *
438  * This function is called when an interrupt is raised at the sound card
439  */
440 static irqreturn_t snd_intelmad_intr_handler(int irq, void *dev)
441 {
442         struct snd_intelmad *intelmaddata =
443                         (struct snd_intelmad *)dev;
444         u8 intsts;
445
446         memcpy_fromio(&intsts,
447                         ((void *)(intelmaddata->int_base)),
448                         sizeof(u8));
449         intelmaddata->mad_jack_msg.intsts = intsts;
450         intelmaddata->mad_jack_msg.intelmaddata = intelmaddata;
451
452         queue_work(intelmaddata->mad_jack_wq, &intelmaddata->mad_jack_msg.wq);
453
454         return IRQ_HANDLED;
455 }
456
457 void sst_mad_send_jack_report(struct snd_jack *jack,
458                                 int buttonpressevent , int status)
459 {
460
461         if (!jack) {
462                 pr_debug("MAD error jack empty\n");
463
464         } else {
465                 pr_debug("MAD send jack report for = %d!!!\n", status);
466                 pr_debug("MAD send jack report %d\n", jack->type);
467                 snd_jack_report(jack, status);
468
469                 /*button pressed and released */
470                 if (buttonpressevent)
471                         snd_jack_report(jack, 0);
472                 pr_debug("MAD sending jack report Done !!!\n");
473         }
474
475
476
477 }
478
479 void sst_mad_jackdetection_fs(u8 intsts , struct snd_intelmad *intelmaddata)
480 {
481         struct snd_jack *jack = NULL;
482         unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
483         struct sc_reg_access sc_access[] = {
484                                 {0x187, 0x00, MASK7},
485                                 {0x188, 0x10, MASK4},
486                                 {0x18b, 0x10, MASK4},
487         };
488
489         struct sc_reg_access sc_access_write[] = {
490                                 {0x198, 0x00, 0x0},
491         };
492
493         if (intsts & 0x4) {
494
495                 if (!(intelmid_audio_interrupt_enable)) {
496                         pr_debug("Audio interrupt enable\n");
497                         sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 3);
498
499                         sst_sc_reg_access(sc_access_write, PMIC_WRITE, 1);
500                         intelmid_audio_interrupt_enable = 1;
501                         intelmaddata->jack[0].jack_status = 0;
502                         intelmaddata->jack[1].jack_status = 0;
503
504                 }
505                 /* send headphone detect */
506                 pr_debug("MAD headphone %d\n", intsts & 0x4);
507                 jack = &intelmaddata->jack[0].jack;
508                 present = !(intelmaddata->jack[0].jack_status);
509                 intelmaddata->jack[0].jack_status = present;
510                 jack_event_flag = 1;
511
512         }
513
514         if (intsts & 0x2) {
515                 /* send short push */
516                 pr_debug("MAD short push %d\n", intsts & 0x2);
517                 jack = &intelmaddata->jack[2].jack;
518                 present = 1;
519                 jack_event_flag = 1;
520                 buttonpressflag = 1;
521         }
522         if (intsts & 0x1) {
523                 /* send long push */
524                 pr_debug("MAD long push %d\n", intsts & 0x1);
525                 jack = &intelmaddata->jack[3].jack;
526                 present = 1;
527                 jack_event_flag = 1;
528                 buttonpressflag = 1;
529         }
530         if (intsts & 0x8) {
531                 if (!(intelmid_audio_interrupt_enable)) {
532                         pr_debug("Audio interrupt enable\n");
533                         sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 3);
534
535                         sst_sc_reg_access(sc_access_write, PMIC_WRITE, 1);
536                         intelmid_audio_interrupt_enable = 1;
537                         intelmaddata->jack[0].jack_status = 0;
538                         intelmaddata->jack[1].jack_status = 0;
539                 }
540                 /* send headset detect */
541                 pr_debug("MAD headset = %d\n", intsts & 0x8);
542                 jack = &intelmaddata->jack[1].jack;
543                 present = !(intelmaddata->jack[1].jack_status);
544                 intelmaddata->jack[1].jack_status = present;
545                 jack_event_flag = 1;
546         }
547
548         if (jack_event_flag)
549                 sst_mad_send_jack_report(jack, buttonpressflag, present);
550 }
551
552
553 void sst_mad_jackdetection_mx(u8 intsts, struct snd_intelmad *intelmaddata)
554 {
555         u8 value = 0, jack_prev_state = 0;
556         struct snd_jack *jack = NULL;
557         unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
558         time_t  timediff;
559         struct sc_reg_access sc_access_read = {0,};
560         struct snd_pmic_ops *scard_ops;
561
562         scard_ops = intelmaddata->sstdrv_ops->scard_ops;
563
564         pr_debug("previous value: %x\n", intelmaddata->jack_prev_state);
565
566         if (!(intelmid_audio_interrupt_enable)) {
567                 pr_debug("Audio interrupt enable\n");
568                 intelmaddata->jack_prev_state = 0xC0;
569                 intelmid_audio_interrupt_enable = 1;
570         }
571
572         if (intsts & 0x2) {
573                 jack_prev_state = intelmaddata->jack_prev_state;
574                 if (intelmaddata->pmic_status == PMIC_INIT) {
575                         sc_access_read.reg_addr = 0x201;
576                         sst_sc_reg_access(&sc_access_read, PMIC_READ, 1);
577                         value = (sc_access_read.value);
578                         pr_debug("value returned = 0x%x\n", value);
579                 }
580
581                 if (jack_prev_state == 0xc0 && value == 0x40) {
582                         /*headset detected. */
583                         pr_debug("MAD headset inserted\n");
584                         jack = &intelmaddata->jack[1].jack;
585                         present = 1;
586                         jack_event_flag = 1;
587                         intelmaddata->jack[1].jack_status = 1;
588
589                 }
590
591                 if (jack_prev_state == 0xc0 && value == 0x00) {
592                         /* headphone  detected. */
593                         pr_debug("MAD headphone inserted\n");
594                         jack = &intelmaddata->jack[0].jack;
595                         present = 1;
596                         jack_event_flag = 1;
597
598                 }
599
600                 if (jack_prev_state == 0x40 && value == 0xc0) {
601                         /*headset  removed*/
602                         pr_debug("Jack headset status %d\n",
603                                 intelmaddata->jack[1].jack_status);
604                         pr_debug("MAD headset removed\n");
605                         jack = &intelmaddata->jack[1].jack;
606                         present = 0;
607                         jack_event_flag = 1;
608                         intelmaddata->jack[1].jack_status = 0;
609                 }
610
611                 if (jack_prev_state == 0x00 && value == 0xc0) {
612                         /* headphone  detected. */
613                         pr_debug("Jack headphone status %d\n",
614                                         intelmaddata->jack[0].jack_status);
615                         pr_debug("headphone removed\n");
616                         jack = &intelmaddata->jack[0].jack;
617                         present = 0;
618                         jack_event_flag = 1;
619                 }
620
621                 if (jack_prev_state == 0x40 && value == 0x00) {
622                         /*button pressed*/
623                         do_gettimeofday(&intelmaddata->jack[1].buttonpressed);
624                         pr_debug("MAD button press detected\n");
625                 }
626
627
628                 if (jack_prev_state == 0x00 && value == 0x40) {
629                         if (intelmaddata->jack[1].jack_status) {
630                                 /*button pressed*/
631                                 do_gettimeofday(
632                                         &intelmaddata->jack[1].buttonreleased);
633                                 /*button pressed */
634                                 pr_debug("Button Released detected\n");
635                                 timediff = intelmaddata->jack[1].
636                                         buttonreleased.tv_sec - intelmaddata->
637                                         jack[1].buttonpressed.tv_sec;
638                                 buttonpressflag = 1;
639                                 if (timediff > 1) {
640                                         pr_debug("long press detected\n");
641                                         /* send headphone detect/undetect */
642                                         jack = &intelmaddata->jack[3].jack;
643                                         present = 1;
644                                         jack_event_flag = 1;
645                                 } else {
646                                         pr_debug("short press detected\n");
647                                         /* send headphone detect/undetect */
648                                         jack = &intelmaddata->jack[2].jack;
649                                         present = 1;
650                                         jack_event_flag = 1;
651                                 }
652                         }
653
654                 }
655                 intelmaddata->jack_prev_state = value;
656         }
657         if (jack_event_flag)
658                 sst_mad_send_jack_report(jack, buttonpressflag, present);
659 }
660
661
662 void sst_mad_jackdetection_nec(u8 intsts, struct snd_intelmad *intelmaddata)
663 {
664         u8 value = 0;
665         struct snd_jack *jack = NULL;
666         unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
667         struct sc_reg_access sc_access_read = {0,};
668
669         if (intelmaddata->pmic_status == PMIC_INIT) {
670                 sc_access_read.reg_addr = 0x132;
671                 sst_sc_reg_access(&sc_access_read, PMIC_READ, 1);
672                 value = (sc_access_read.value);
673                 pr_debug("value returned = 0x%x\n", value);
674         }
675         if (intsts & 0x1) {
676                 pr_debug("headset detected\n");
677                 /* send headset detect/undetect */
678                 jack = &intelmaddata->jack[1].jack;
679                 present = (value == 0x1) ? 1 : 0;
680                 jack_event_flag = 1;
681         }
682         if (intsts & 0x2) {
683                 pr_debug("headphone detected\n");
684                 /* send headphone detect/undetect */
685                 jack = &intelmaddata->jack[0].jack;
686                 present = (value == 0x2) ? 1 : 0;
687                 jack_event_flag = 1;
688         }
689         if (intsts & 0x4) {
690                 pr_debug("short push detected\n");
691                 /* send short push */
692                 jack = &intelmaddata->jack[2].jack;
693                 present = 1;
694                 jack_event_flag = 1;
695                 buttonpressflag = 1;
696         }
697         if (intsts & 0x8) {
698                 pr_debug("long push detected\n");
699                 /* send long push */
700                 jack = &intelmaddata->jack[3].jack;
701                 present = 1;
702                 jack_event_flag = 1;
703                 buttonpressflag = 1;
704         }
705
706         if (jack_event_flag)
707                 sst_mad_send_jack_report(jack, buttonpressflag, present);
708
709
710 }
711
712 void sst_process_mad_jack_detection(struct work_struct *work)
713 {
714         u8 intsts;
715         struct mad_jack_msg_wq *mad_jack_detect =
716                         container_of(work, struct mad_jack_msg_wq, wq);
717
718         struct snd_intelmad *intelmaddata =
719                         mad_jack_detect->intelmaddata;
720
721         intsts = mad_jack_detect->intsts;
722
723         switch (intelmaddata->sstdrv_ops->vendor_id) {
724         case SND_FS:
725                 sst_mad_jackdetection_fs(intsts , intelmaddata);
726                 break;
727         case SND_MX:
728                 sst_mad_jackdetection_mx(intsts , intelmaddata);
729                 break;
730         case SND_NC:
731                 sst_mad_jackdetection_nec(intsts , intelmaddata);
732                 break;
733         }
734 }
735
736
737 static int __devinit snd_intelmad_register_irq(
738                                         struct snd_intelmad *intelmaddata)
739 {
740         int ret_val;
741         u32 regbase = AUDINT_BASE, regsize = 8;
742         char *drv_name;
743
744         pr_debug("irq reg done, regbase 0x%x, regsize 0x%x\n",
745                                         regbase, regsize);
746         intelmaddata->int_base = ioremap_nocache(regbase, regsize);
747         if (!intelmaddata->int_base)
748                 pr_err("Mapping of cache failed\n");
749         pr_debug("irq = 0x%x\n", intelmaddata->irq);
750         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL)
751                 drv_name = DRIVER_NAME_MFLD;
752         else
753                 drv_name = DRIVER_NAME_MRST;
754         ret_val = request_irq(intelmaddata->irq,
755                                 snd_intelmad_intr_handler,
756                                 IRQF_SHARED, drv_name,
757                                 intelmaddata);
758         if (ret_val)
759                 pr_err("cannot register IRQ\n");
760         return ret_val;
761 }
762
763 static int __devinit snd_intelmad_sst_register(
764                         struct snd_intelmad *intelmaddata)
765 {
766         int ret_val = 0;
767         struct snd_pmic_ops *intelmad_vendor_ops[MAX_VENDORS] = {
768                 &snd_pmic_ops_fs,
769                 &snd_pmic_ops_mx,
770                 &snd_pmic_ops_nc,
771                 &snd_msic_ops
772         };
773
774         struct sc_reg_access vendor_addr = {0x00, 0x00, 0x00};
775
776         if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
777                 ret_val = sst_sc_reg_access(&vendor_addr, PMIC_READ, 1);
778                 if (ret_val)
779                         return ret_val;
780                 sst_card_vendor_id = (vendor_addr.value & (MASK2|MASK1|MASK0));
781                 pr_debug("orginal n extrated vendor id = 0x%x %d\n",
782                                 vendor_addr.value, sst_card_vendor_id);
783                 if (sst_card_vendor_id < 0 || sst_card_vendor_id > 2) {
784                         pr_err("vendor card not supported!!\n");
785                         return -EIO;
786                 }
787         } else
788                 sst_card_vendor_id = 0x3;
789
790         intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
791         intelmaddata->sstdrv_ops->vendor_id = sst_card_vendor_id;
792         BUG_ON(!intelmad_vendor_ops[sst_card_vendor_id]);
793         intelmaddata->sstdrv_ops->scard_ops =
794                         intelmad_vendor_ops[sst_card_vendor_id];
795
796         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
797                 intelmaddata->sstdrv_ops->scard_ops->pb_on = 0;
798                 intelmaddata->sstdrv_ops->scard_ops->cap_on = 0;
799                 intelmaddata->sstdrv_ops->scard_ops->input_dev_id = DMIC;
800                 intelmaddata->sstdrv_ops->scard_ops->output_dev_id =
801                                                         STEREO_HEADPHONE;
802         }
803
804         /* registering with SST driver to get access to SST APIs to use */
805         ret_val = register_sst_card(intelmaddata->sstdrv_ops);
806         if (ret_val) {
807                 pr_err("sst card registration failed\n");
808                 return ret_val;
809         }
810
811         sst_card_vendor_id = intelmaddata->sstdrv_ops->vendor_id;
812         intelmaddata->pmic_status = PMIC_UNINIT;
813         return ret_val;
814 }
815
816 /* Driver Init/exit functionalities */
817 /**
818  * snd_intelmad_pcm_new - to setup pcm for the card
819  *
820  * @card:  pointer to the sound card structure
821  * @intelmaddata: pointer to internal context
822  * @pb: playback count for this card
823  * @cap: capture count for this card
824  * @index: device index
825  *
826  * This function is called from probe function to set up pcm params
827  * and functions
828  */
829 static int __devinit snd_intelmad_pcm_new(struct snd_card *card,
830                         struct snd_intelmad *intelmaddata,
831                         unsigned int pb, unsigned int cap, unsigned int index)
832 {
833         int ret_val = 0;
834         struct snd_pcm *pcm;
835         char name[32] = INTEL_MAD;
836         struct snd_pcm_ops *pb_ops = NULL, *cap_ops = NULL;
837
838         pr_debug("called for pb %d, cp %d, idx %d\n", pb, cap, index);
839         ret_val = snd_pcm_new(card, name, index, pb, cap, &pcm);
840         if (ret_val)
841                 return ret_val;
842         /* setup the ops for playback and capture streams */
843         switch (index) {
844         case 0:
845                 pb_ops = &snd_intelmad_headset_ops;
846                 cap_ops = &snd_intelmad_capture_ops;
847                 break;
848         case 1:
849                 pb_ops = &snd_intelmad_ihf_ops;
850                 cap_ops = &snd_intelmad_capture_ops;
851                 break;
852         case 2:
853                 pb_ops = &snd_intelmad_vibra_ops;
854                 cap_ops = &snd_intelmad_capture_ops;
855                 break;
856         case 3:
857                 pb_ops = &snd_intelmad_haptic_ops;
858                 cap_ops = &snd_intelmad_capture_ops;
859                 break;
860         }
861         if (pb)
862                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, pb_ops);
863         if (cap)
864                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, cap_ops);
865         /* setup private data which can be retrieved when required */
866         pcm->private_data = intelmaddata;
867         pcm->info_flags = 0;
868         strncpy(pcm->name, card->shortname, strlen(card->shortname));
869         /* allocate dma pages for ALSA stream operations */
870         snd_pcm_lib_preallocate_pages_for_all(pcm,
871                         SNDRV_DMA_TYPE_CONTINUOUS,
872                         snd_dma_continuous_data(GFP_KERNEL),
873                         MIN_BUFFER, MAX_BUFFER);
874         return ret_val;
875 }
876
877 static int __devinit snd_intelmad_pcm(struct snd_card *card,
878                                 struct snd_intelmad *intelmaddata)
879 {
880         int ret_val = 0;
881
882         WARN_ON(!card);
883         WARN_ON(!intelmaddata);
884         pr_debug("snd_intelmad_pcm called\n");
885         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 1, 0);
886         if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT)
887                 return ret_val;
888         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 1);
889         if (ret_val)
890                 return ret_val;
891         ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 2);
892         if (ret_val)
893                 return ret_val;
894         return snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 3);
895 }
896
897 /**
898  * snd_intelmad_jack- to setup jack settings of the card
899  *
900  * @intelmaddata: pointer to internal context
901  *
902  * This function is called send jack events
903  */
904 static int snd_intelmad_jack(struct snd_intelmad *intelmaddata)
905 {
906         struct snd_jack *jack;
907         int retval;
908
909         pr_debug("snd_intelmad_jack called\n");
910         jack = &intelmaddata->jack[0].jack;
911         retval = snd_jack_new(intelmaddata->card, "Headphone",
912                                 SND_JACK_HEADPHONE, &jack);
913         if (retval < 0)
914                 return retval;
915         snd_jack_report(jack, 0);
916
917         jack->private_data = jack;
918         intelmaddata->jack[0].jack = *jack;
919
920
921         jack = &intelmaddata->jack[1].jack;
922         retval = snd_jack_new(intelmaddata->card, "Headset",
923                                 SND_JACK_HEADSET, &jack);
924         if (retval < 0)
925                 return retval;
926
927
928
929         jack->private_data = jack;
930         intelmaddata->jack[1].jack = *jack;
931
932
933         jack = &intelmaddata->jack[2].jack;
934         retval = snd_jack_new(intelmaddata->card, "Short Press",
935                                 SND_JACK_HS_SHORT_PRESS, &jack);
936         if (retval < 0)
937                 return retval;
938
939
940         jack->private_data = jack;
941         intelmaddata->jack[2].jack = *jack;
942
943
944         jack = &intelmaddata->jack[3].jack;
945         retval = snd_jack_new(intelmaddata->card, "Long Press",
946                                 SND_JACK_HS_LONG_PRESS, &jack);
947         if (retval < 0)
948                 return retval;
949
950
951         jack->private_data = jack;
952         intelmaddata->jack[3].jack = *jack;
953
954         return retval;
955 }
956
957 /**
958  * snd_intelmad_mixer- to setup mixer settings of the card
959  *
960  * @intelmaddata: pointer to internal context
961  *
962  * This function is called from probe function to set up mixer controls
963  */
964 static int __devinit snd_intelmad_mixer(struct snd_intelmad *intelmaddata)
965 {
966         struct snd_card *card;
967         unsigned int idx;
968         int ret_val = 0, max_controls = 0;
969         char *mixername = "IntelMAD Controls";
970         struct snd_kcontrol_new *controls;
971
972         WARN_ON(!intelmaddata);
973
974         card = intelmaddata->card;
975         strncpy(card->mixername, mixername, sizeof(card->mixername)-1);
976         /* add all widget controls and expose the same */
977         if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
978                 max_controls = MAX_CTRL_MFLD;
979                 controls = snd_intelmad_controls_mfld;
980         } else {
981                 max_controls = MAX_CTRL_MRST;
982                 controls = snd_intelmad_controls_mrst;
983         }
984         for (idx = 0; idx < max_controls; idx++) {
985                 ret_val = snd_ctl_add(card,
986                                 snd_ctl_new1(&controls[idx],
987                                 intelmaddata));
988                 pr_debug("mixer[idx]=%d added\n", idx);
989                 if (ret_val) {
990                         pr_err("in adding of control index = %d\n", idx);
991                         break;
992                 }
993         }
994         return ret_val;
995 }
996
997 static int snd_intelmad_dev_free(struct snd_device *device)
998 {
999         struct snd_intelmad *intelmaddata;
1000
1001         WARN_ON(!device);
1002
1003         intelmaddata = device->device_data;
1004
1005         pr_debug("snd_intelmad_dev_free called\n");
1006         snd_card_free(intelmaddata->card);
1007         /*genl_unregister_family(&audio_event_genl_family);*/
1008         unregister_sst_card(intelmaddata->sstdrv_ops);
1009
1010         /* free allocated memory for internal context */
1011         destroy_workqueue(intelmaddata->mad_jack_wq);
1012         kfree(intelmaddata->sstdrv_ops);
1013         kfree(intelmaddata);
1014         return 0;
1015 }
1016
1017 static int __devinit snd_intelmad_create(
1018                 struct snd_intelmad *intelmaddata,
1019                 struct snd_card *card)
1020 {
1021         int ret_val;
1022         static struct snd_device_ops ops = {
1023                 .dev_free =     snd_intelmad_dev_free,
1024         };
1025
1026         WARN_ON(!intelmaddata);
1027         WARN_ON(!card);
1028         /* ALSA api to register for the device */
1029         ret_val = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelmaddata, &ops);
1030         return ret_val;
1031 }
1032
1033 /**
1034 * snd_intelmad_probe- function registred for init
1035 * @pdev :  pointer to the device struture
1036 * This function is called when the device is initialized
1037 */
1038 int __devinit snd_intelmad_probe(struct platform_device *pdev)
1039 {
1040         struct snd_card *card;
1041         int ret_val;
1042         struct snd_intelmad *intelmaddata;
1043         const struct platform_device_id *id = platform_get_device_id(pdev);
1044         unsigned int cpu_id = (unsigned int)id->driver_data;
1045
1046         pr_debug("probe for %s cpu_id %d\n", pdev->name, cpu_id);
1047         if (!strcmp(pdev->name, DRIVER_NAME_MRST))
1048                 pr_debug("detected MRST\n");
1049         else if (!strcmp(pdev->name, DRIVER_NAME_MFLD))
1050                 pr_debug("detected MFLD\n");
1051         else {
1052                 pr_err("detected unknown device abort!!\n");
1053                 return -EIO;
1054         }
1055         if ((cpu_id < CPU_CHIP_LINCROFT) || (cpu_id > CPU_CHIP_PENWELL)) {
1056                 pr_err("detected unknown cpu_id abort!!\n");
1057                 return -EIO;
1058         }
1059         /* allocate memory for saving internal context and working */
1060         intelmaddata = kzalloc(sizeof(*intelmaddata), GFP_KERNEL);
1061         if (!intelmaddata) {
1062                 pr_debug("mem alloctn fail\n");
1063                 return -ENOMEM;
1064         }
1065
1066         /* allocate memory for LPE API set */
1067         intelmaddata->sstdrv_ops = kzalloc(sizeof(struct intel_sst_card_ops),
1068                                         GFP_KERNEL);
1069         if (!intelmaddata->sstdrv_ops) {
1070                 pr_err("mem allocation for ops fail\n");
1071                 kfree(intelmaddata);
1072                 return -ENOMEM;
1073         }
1074
1075         intelmaddata->cpu_id = cpu_id;
1076         /* create a card instance with ALSA framework */
1077         ret_val = snd_card_create(card_index, card_id, THIS_MODULE, 0, &card);
1078         if (ret_val) {
1079                 pr_err("snd_card_create fail\n");
1080                 goto free_allocs;
1081         }
1082
1083         intelmaddata->pdev = pdev;
1084         intelmaddata->irq = platform_get_irq(pdev, 0);
1085         platform_set_drvdata(pdev, intelmaddata);
1086         intelmaddata->card = card;
1087         intelmaddata->card_id = card_id;
1088         intelmaddata->card_index = card_index;
1089         intelmaddata->master_mute = UNMUTE;
1090         intelmaddata->playback_cnt =  intelmaddata->capture_cnt = 0;
1091         strncpy(card->driver, INTEL_MAD, strlen(INTEL_MAD));
1092         strncpy(card->shortname, INTEL_MAD, strlen(INTEL_MAD));
1093
1094         intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
1095         /* registering with LPE driver to get access to SST APIs to use */
1096         ret_val = snd_intelmad_sst_register(intelmaddata);
1097         if (ret_val) {
1098                 pr_err("snd_intelmad_sst_register failed\n");
1099                 goto free_allocs;
1100         }
1101
1102         intelmaddata->pmic_status = PMIC_INIT;
1103
1104         ret_val = snd_intelmad_pcm(card, intelmaddata);
1105         if (ret_val) {
1106                 pr_err("snd_intelmad_pcm failed\n");
1107                 goto free_allocs;
1108         }
1109
1110         ret_val = snd_intelmad_mixer(intelmaddata);
1111         if (ret_val) {
1112                 pr_err("snd_intelmad_mixer failed\n");
1113                 goto free_allocs;
1114         }
1115
1116         ret_val = snd_intelmad_jack(intelmaddata);
1117         if (ret_val) {
1118                 pr_err("snd_intelmad_jack failed\n");
1119                 goto free_allocs;
1120         }
1121
1122         /*create work queue for jack interrupt*/
1123         INIT_WORK(&intelmaddata->mad_jack_msg.wq,
1124                 sst_process_mad_jack_detection);
1125
1126         intelmaddata->mad_jack_wq = create_workqueue("sst_mad_jack_wq");
1127         if (!intelmaddata->mad_jack_wq)
1128                 goto free_mad_jack_wq;
1129
1130         ret_val = snd_intelmad_register_irq(intelmaddata);
1131         if (ret_val) {
1132                 pr_err("snd_intelmad_register_irq fail\n");
1133                 goto free_allocs;
1134         }
1135
1136         /* internal function call to register device with ALSA */
1137         ret_val = snd_intelmad_create(intelmaddata, card);
1138         if (ret_val) {
1139                 pr_err("snd_intelmad_create failed\n");
1140                 goto free_allocs;
1141         }
1142         card->private_data = &intelmaddata;
1143         snd_card_set_dev(card, &pdev->dev);
1144         ret_val = snd_card_register(card);
1145         if (ret_val) {
1146                 pr_err("snd_card_register failed\n");
1147                 goto free_allocs;
1148         }
1149
1150         pr_debug("snd_intelmad_probe complete\n");
1151         return ret_val;
1152
1153 free_mad_jack_wq:
1154         destroy_workqueue(intelmaddata->mad_jack_wq);
1155 free_allocs:
1156         pr_err("probe failed\n");
1157         snd_card_free(card);
1158         kfree(intelmaddata->sstdrv_ops);
1159         kfree(intelmaddata);
1160         return ret_val;
1161 }
1162
1163
1164 static int snd_intelmad_remove(struct platform_device *pdev)
1165 {
1166         struct snd_intelmad *intelmaddata = platform_get_drvdata(pdev);
1167
1168         if (intelmaddata) {
1169                 snd_card_free(intelmaddata->card);
1170                 unregister_sst_card(intelmaddata->sstdrv_ops);
1171                 /* free allocated memory for internal context */
1172                 destroy_workqueue(intelmaddata->mad_jack_wq);
1173                 kfree(intelmaddata->sstdrv_ops);
1174                 kfree(intelmaddata);
1175         }
1176         return 0;
1177 }
1178
1179 /*********************************************************************
1180  *              Driver initialization and exit
1181  *********************************************************************/
1182 static const struct platform_device_id snd_intelmad_ids[] = {
1183         {DRIVER_NAME_MRST, CPU_CHIP_LINCROFT},
1184         {DRIVER_NAME_MFLD, CPU_CHIP_PENWELL},
1185         {"", 0},
1186
1187 };
1188
1189 static struct platform_driver snd_intelmad_driver = {
1190         .driver = {
1191                 .owner = THIS_MODULE,
1192                 .name = "intel_mid_sound_card",
1193         },
1194         .id_table = snd_intelmad_ids,
1195         .probe = snd_intelmad_probe,
1196         .remove = __devexit_p(snd_intelmad_remove),
1197 };
1198
1199 /*
1200  * alsa_card_intelmad_init- driver init function
1201  *
1202  * This function is called when driver module is inserted
1203  */
1204 static int __init alsa_card_intelmad_init(void)
1205 {
1206         pr_debug("mad_init called\n");
1207         return platform_driver_register(&snd_intelmad_driver);
1208 }
1209
1210 /**
1211  * alsa_card_intelmad_exit- driver exit function
1212  *
1213  * This function is called when driver module is removed
1214  */
1215 static void __exit alsa_card_intelmad_exit(void)
1216 {
1217         pr_debug("mad_exit called\n");
1218         return platform_driver_unregister(&snd_intelmad_driver);
1219 }
1220
1221 module_init(alsa_card_intelmad_init)
1222 module_exit(alsa_card_intelmad_exit)
1223