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