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