2 * intelmid.c - Intel Sound card driver for MID
4 * Copyright (C) 2008-10 Intel Corp
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 * ALSA driver for Intel MID sound card chipset
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/slab.h>
32 #include <linux/platform_device.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35 #include <sound/control.h>
37 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/initval.h>
41 #include "intel_sst.h"
42 #include "intel_sst_ioctl.h"
43 #include "intelmid_snd_control.h"
50 MODULE_DESCRIPTION("Intel MAD Sound card driver");
51 MODULE_LICENSE("GPL v2");
52 MODULE_SUPPORTED_DEVICE("{Intel,Intel_MAD}");
55 static int card_index = SNDRV_DEFAULT_IDX1;/* Index 0-MAX */
56 static char *card_id = SNDRV_DEFAULT_STR1; /* ID for this card */
58 module_param(card_index, int, 0444);
59 MODULE_PARM_DESC(card_index, "Index value for INTELMAD soundcard.");
60 module_param(card_id, charp, 0444);
61 MODULE_PARM_DESC(card_id, "ID string for INTELMAD soundcard.");
63 int sst_card_vendor_id;
64 int intelmid_audio_interrupt_enable;/*checkpatch fix*/
66 /* Data path functionalities */
67 static struct snd_pcm_hardware snd_intelmad_stream = {
68 .info = (SNDRV_PCM_INFO_INTERLEAVED |
69 SNDRV_PCM_INFO_DOUBLE |
70 SNDRV_PCM_INFO_PAUSE |
71 SNDRV_PCM_INFO_RESUME |
73 SNDRV_PCM_INFO_MMAP_VALID |
74 SNDRV_PCM_INFO_BLOCK_TRANSFER |
75 SNDRV_PCM_INFO_SYNC_START),
76 .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
77 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
78 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
79 .rates = (SNDRV_PCM_RATE_8000|
80 SNDRV_PCM_RATE_44100 |
81 SNDRV_PCM_RATE_48000),
85 .channels_min = MIN_CHANNEL,
86 .channels_max = MAX_CHANNEL_AMIC,
87 .buffer_bytes_max = MAX_BUFFER,
88 .period_bytes_min = MIN_PERIOD_BYTES,
89 .period_bytes_max = MAX_PERIOD_BYTES,
90 .periods_min = MIN_PERIODS,
91 .periods_max = MAX_PERIODS,
92 .fifo_size = FIFO_SIZE,
97 * snd_intelmad_pcm_trigger - stream activities are handled here
99 * @substream:substream for which the stream function is called
100 * @cmd:the stream commamd that requested from upper layer
102 * This function is called whenever an a stream activity is invoked
104 static int snd_intelmad_pcm_trigger(struct snd_pcm_substream *substream,
107 int ret_val = 0, str_id;
108 struct snd_intelmad *intelmaddata;
109 struct mad_stream_pvt *stream;
110 struct intel_sst_pcm_control *sst_ops;
114 intelmaddata = snd_pcm_substream_chip(substream);
115 stream = substream->runtime->private_data;
117 WARN_ON(!intelmaddata->sstdrv_ops);
118 WARN_ON(!intelmaddata->sstdrv_ops->scard_ops);
119 sst_ops = intelmaddata->sstdrv_ops->pcm_control;
120 str_id = stream->stream_info.str_id;
123 case SNDRV_PCM_TRIGGER_START:
124 pr_debug("Trigger Start\n");
125 ret_val = sst_ops->device_control(SST_SND_START, &str_id);
128 stream->stream_status = RUNNING;
129 stream->substream = substream;
131 case SNDRV_PCM_TRIGGER_STOP:
132 pr_debug("in stop\n");
133 ret_val = sst_ops->device_control(SST_SND_DROP, &str_id);
136 stream->stream_status = DROPPED;
138 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
139 pr_debug("in pause\n");
140 ret_val = sst_ops->device_control(SST_SND_PAUSE, &str_id);
143 stream->stream_status = PAUSED;
145 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
146 pr_debug("in pause release\n");
147 ret_val = sst_ops->device_control(SST_SND_RESUME, &str_id);
150 stream->stream_status = RUNNING;
159 * snd_intelmad_pcm_prepare- internal preparation before starting a stream
161 * @substream: substream for which the function is called
163 * This function is called when a stream is started for internal preparation.
165 static int snd_intelmad_pcm_prepare(struct snd_pcm_substream *substream)
167 struct mad_stream_pvt *stream;
169 struct snd_intelmad *intelmaddata;
171 pr_debug("pcm_prepare called\n");
174 stream = substream->runtime->private_data;
175 intelmaddata = snd_pcm_substream_chip(substream);
176 pr_debug("pb cnt = %d cap cnt = %d\n",\
177 intelmaddata->playback_cnt,
178 intelmaddata->capture_cnt);
180 if (stream->stream_info.str_id) {
181 pr_debug("Prepare called for already set stream\n");
182 ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
183 SST_SND_DROP, &stream->stream_info.str_id);
187 ret_val = snd_intelmad_alloc_stream(substream);
190 stream->dbg_cum_bytes = 0;
191 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
192 intelmaddata->playback_cnt++;
194 intelmaddata->capture_cnt++;
195 /* return back the stream id */
196 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
197 "%d", stream->stream_info.str_id);
198 pr_debug("stream id to user = %s\n",
201 ret_val = snd_intelmad_init_stream(substream);
204 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
208 static int snd_intelmad_hw_params(struct snd_pcm_substream *substream,
209 struct snd_pcm_hw_params *hw_params)
213 pr_debug("snd_intelmad_hw_params called\n");
214 ret_val = snd_pcm_lib_malloc_pages(substream,
215 params_buffer_bytes(hw_params));
216 memset(substream->runtime->dma_area, 0,
217 params_buffer_bytes(hw_params));
222 static int snd_intelmad_hw_free(struct snd_pcm_substream *substream)
224 pr_debug("snd_intelmad_hw_free called\n");
225 return snd_pcm_lib_free_pages(substream);
229 * snd_intelmad_pcm_pointer- to send the current buffer pointer processed by hw
231 * @substream: substream for which the function is called
233 * This function is called by ALSA framework to get the current hw buffer ptr
234 * when a period is elapsed
236 static snd_pcm_uframes_t snd_intelmad_pcm_pointer
237 (struct snd_pcm_substream *substream)
239 /* struct snd_pcm_runtime *runtime = substream->runtime; */
240 struct mad_stream_pvt *stream;
241 struct snd_intelmad *intelmaddata;
246 intelmaddata = snd_pcm_substream_chip(substream);
247 stream = substream->runtime->private_data;
248 if (stream->stream_status == INIT)
251 ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
252 SST_SND_BUFFER_POINTER, &stream->stream_info);
254 pr_err("error code = 0x%x\n", ret_val);
257 pr_debug("samples reported out 0x%llx\n",
258 stream->stream_info.buffer_ptr);
259 pr_debug("Frame bits:: %d period_count :: %d\n",
260 (int)substream->runtime->frame_bits,
261 (int)substream->runtime->period_size);
263 return stream->stream_info.buffer_ptr;
268 * snd_intelmad_close- to free parameteres when stream is stopped
270 * @substream: substream for which the function is called
272 * This function is called by ALSA framework when stream is stopped
274 static int snd_intelmad_close(struct snd_pcm_substream *substream)
276 struct snd_intelmad *intelmaddata;
277 struct mad_stream_pvt *stream;
278 int ret_val = 0, str_id;
282 stream = substream->runtime->private_data;
283 str_id = stream->stream_info.str_id;
285 pr_debug("sst: snd_intelmad_close called for %d\n", str_id);
286 intelmaddata = snd_pcm_substream_chip(substream);
288 pr_debug("str id = %d\n", stream->stream_info.str_id);
289 if (stream->stream_info.str_id) {
290 /* SST API to actually stop/free the stream */
291 ret_val = intelmaddata->sstdrv_ops->pcm_control->close(str_id);
292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
293 intelmaddata->playback_cnt--;
295 intelmaddata->capture_cnt--;
297 pr_debug("snd_intelmad_close : pb cnt = %d cap cnt = %d\n",
298 intelmaddata->playback_cnt, intelmaddata->capture_cnt);
299 kfree(substream->runtime->private_data);
304 * snd_intelmad_open- to set runtime parameters during stream start
306 * @substream: substream for which the function is called
307 * @type: audio device type
309 * This function is called by ALSA framework when stream is started
311 static int snd_intelmad_open(struct snd_pcm_substream *substream,
312 enum snd_sst_audio_device_type type)
314 struct snd_intelmad *intelmaddata;
315 struct snd_pcm_runtime *runtime;
316 struct mad_stream_pvt *stream;
320 pr_debug("snd_intelmad_open called\n");
322 intelmaddata = snd_pcm_substream_chip(substream);
323 runtime = substream->runtime;
324 /* set the runtime hw parameter with local snd_pcm_hardware struct */
325 runtime->hw = snd_intelmad_stream;
326 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
327 runtime->hw = snd_intelmad_stream;
328 runtime->hw.rates = SNDRV_PCM_RATE_48000;
329 runtime->hw.rate_min = MAX_RATE;
330 runtime->hw.formats = (SNDRV_PCM_FMTBIT_S24 |
331 SNDRV_PCM_FMTBIT_U24);
332 if (intelmaddata->sstdrv_ops->scard_ops->input_dev_id == AMIC)
333 runtime->hw.channels_max = MAX_CHANNEL_AMIC;
335 runtime->hw.channels_max = MAX_CHANNEL_DMIC;
338 /* setup the internal datastruture stream pointers based on it being
339 playback or capture stream */
340 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
343 stream->stream_info.str_id = 0;
344 stream->device = type;
345 stream->stream_status = INIT;
346 runtime->private_data = stream;
347 return snd_pcm_hw_constraint_integer(runtime,
348 SNDRV_PCM_HW_PARAM_PERIODS);
351 static int snd_intelmad_headset_open(struct snd_pcm_substream *substream)
353 return snd_intelmad_open(substream, SND_SST_DEVICE_HEADSET);
356 static int snd_intelmad_ihf_open(struct snd_pcm_substream *substream)
358 return snd_intelmad_open(substream, SND_SST_DEVICE_IHF);
361 static int snd_intelmad_vibra_open(struct snd_pcm_substream *substream)
363 return snd_intelmad_open(substream, SND_SST_DEVICE_VIBRA);
366 static int snd_intelmad_haptic_open(struct snd_pcm_substream *substream)
368 return snd_intelmad_open(substream, SND_SST_DEVICE_HAPTIC);
371 static struct snd_pcm_ops snd_intelmad_headset_ops = {
372 .open = snd_intelmad_headset_open,
373 .close = snd_intelmad_close,
374 .ioctl = snd_pcm_lib_ioctl,
375 .hw_params = snd_intelmad_hw_params,
376 .hw_free = snd_intelmad_hw_free,
377 .prepare = snd_intelmad_pcm_prepare,
378 .trigger = snd_intelmad_pcm_trigger,
379 .pointer = snd_intelmad_pcm_pointer,
382 static struct snd_pcm_ops snd_intelmad_ihf_ops = {
383 .open = snd_intelmad_ihf_open,
384 .close = snd_intelmad_close,
385 .ioctl = snd_pcm_lib_ioctl,
386 .hw_params = snd_intelmad_hw_params,
387 .hw_free = snd_intelmad_hw_free,
388 .prepare = snd_intelmad_pcm_prepare,
389 .trigger = snd_intelmad_pcm_trigger,
390 .pointer = snd_intelmad_pcm_pointer,
393 static struct snd_pcm_ops snd_intelmad_vibra_ops = {
394 .open = snd_intelmad_vibra_open,
395 .close = snd_intelmad_close,
396 .ioctl = snd_pcm_lib_ioctl,
397 .hw_params = snd_intelmad_hw_params,
398 .hw_free = snd_intelmad_hw_free,
399 .prepare = snd_intelmad_pcm_prepare,
400 .trigger = snd_intelmad_pcm_trigger,
401 .pointer = snd_intelmad_pcm_pointer,
404 static struct snd_pcm_ops snd_intelmad_haptic_ops = {
405 .open = snd_intelmad_haptic_open,
406 .close = snd_intelmad_close,
407 .ioctl = snd_pcm_lib_ioctl,
408 .hw_params = snd_intelmad_hw_params,
409 .hw_free = snd_intelmad_hw_free,
410 .prepare = snd_intelmad_pcm_prepare,
411 .trigger = snd_intelmad_pcm_trigger,
412 .pointer = snd_intelmad_pcm_pointer,
415 static struct snd_pcm_ops snd_intelmad_capture_ops = {
416 .open = snd_intelmad_headset_open,
417 .close = snd_intelmad_close,
418 .ioctl = snd_pcm_lib_ioctl,
419 .hw_params = snd_intelmad_hw_params,
420 .hw_free = snd_intelmad_hw_free,
421 .prepare = snd_intelmad_pcm_prepare,
422 .trigger = snd_intelmad_pcm_trigger,
423 .pointer = snd_intelmad_pcm_pointer,
428 * snd_intelmad_intr_handler- interrupt handler
430 * @irq : irq number of the interrupt received
431 * @dev: device context
433 * This function is called when an interrupt is raised at the sound card
435 static irqreturn_t snd_intelmad_intr_handler(int irq, void *dev)
437 struct snd_intelmad *intelmaddata =
438 (struct snd_intelmad *)dev;
441 memcpy_fromio(&intsts,
442 ((void *)(intelmaddata->int_base)),
444 intelmaddata->mad_jack_msg.intsts = intsts;
445 intelmaddata->mad_jack_msg.intelmaddata = intelmaddata;
447 queue_work(intelmaddata->mad_jack_wq, &intelmaddata->mad_jack_msg.wq);
452 void sst_mad_send_jack_report(struct snd_jack *jack,
453 int buttonpressevent , int status)
457 pr_debug("MAD error jack empty\n");
460 pr_debug("MAD send jack report for = %d!!!\n", status);
461 pr_debug("MAD send jack report %d\n", jack->type);
462 snd_jack_report(jack, status);
464 /*button pressed and released */
465 if (buttonpressevent)
466 snd_jack_report(jack, 0);
467 pr_debug("MAD sending jack report Done !!!\n");
474 void sst_mad_jackdetection_fs(u8 intsts , struct snd_intelmad *intelmaddata)
476 struct snd_jack *jack = NULL;
477 unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
478 struct sc_reg_access sc_access[] = {
479 {0x187, 0x00, MASK7},
480 {0x188, 0x10, MASK4},
481 {0x18b, 0x10, MASK4},
484 struct sc_reg_access sc_access_write[] = {
490 if (!(intelmid_audio_interrupt_enable)) {
491 pr_debug("Audio interrupt enable\n");
492 sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 3);
494 sst_sc_reg_access(sc_access_write, PMIC_WRITE, 1);
495 intelmid_audio_interrupt_enable = 1;
496 intelmaddata->jack[0].jack_status = 0;
497 intelmaddata->jack[1].jack_status = 0;
500 /* send headphone detect */
501 pr_debug("MAD headphone %d\n", intsts & 0x4);
502 jack = &intelmaddata->jack[0].jack;
503 present = !(intelmaddata->jack[0].jack_status);
504 intelmaddata->jack[0].jack_status = present;
510 /* send short push */
511 pr_debug("MAD short push %d\n", intsts & 0x2);
512 jack = &intelmaddata->jack[2].jack;
519 pr_debug("MAD long push %d\n", intsts & 0x1);
520 jack = &intelmaddata->jack[3].jack;
526 if (!(intelmid_audio_interrupt_enable)) {
527 pr_debug("Audio interrupt enable\n");
528 sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 3);
530 sst_sc_reg_access(sc_access_write, PMIC_WRITE, 1);
531 intelmid_audio_interrupt_enable = 1;
532 intelmaddata->jack[0].jack_status = 0;
533 intelmaddata->jack[1].jack_status = 0;
535 /* send headset detect */
536 pr_debug("MAD headset = %d\n", intsts & 0x8);
537 jack = &intelmaddata->jack[1].jack;
538 present = !(intelmaddata->jack[1].jack_status);
539 intelmaddata->jack[1].jack_status = present;
544 sst_mad_send_jack_report(jack, buttonpressflag, present);
548 void sst_mad_jackdetection_mx(u8 intsts, struct snd_intelmad *intelmaddata)
550 u8 value = 0, jack_prev_state = 0;
551 struct snd_jack *jack = NULL;
552 unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
554 struct sc_reg_access sc_access_read = {0,};
555 struct snd_pmic_ops *scard_ops;
557 scard_ops = intelmaddata->sstdrv_ops->scard_ops;
559 pr_debug("previous value: %x\n", intelmaddata->jack_prev_state);
561 if (!(intelmid_audio_interrupt_enable)) {
562 pr_debug("Audio interrupt enable\n");
563 intelmaddata->jack_prev_state = 0xC0;
564 intelmid_audio_interrupt_enable = 1;
568 jack_prev_state = intelmaddata->jack_prev_state;
569 if (intelmaddata->pmic_status == PMIC_INIT) {
570 sc_access_read.reg_addr = 0x201;
571 sst_sc_reg_access(&sc_access_read, PMIC_READ, 1);
572 value = (sc_access_read.value);
573 pr_debug("value returned = 0x%x\n", value);
576 if (jack_prev_state == 0xc0 && value == 0x40) {
577 /*headset detected. */
578 pr_debug("MAD headset inserted\n");
579 jack = &intelmaddata->jack[1].jack;
582 intelmaddata->jack[1].jack_status = 1;
586 if (jack_prev_state == 0xc0 && value == 0x00) {
587 /* headphone detected. */
588 pr_debug("MAD headphone inserted\n");
589 jack = &intelmaddata->jack[0].jack;
595 if (jack_prev_state == 0x40 && value == 0xc0) {
597 pr_debug("Jack headset status %d\n",
598 intelmaddata->jack[1].jack_status);
599 pr_debug("MAD headset removed\n");
600 jack = &intelmaddata->jack[1].jack;
603 intelmaddata->jack[1].jack_status = 0;
606 if (jack_prev_state == 0x00 && value == 0xc0) {
607 /* headphone detected. */
608 pr_debug("Jack headphone status %d\n",
609 intelmaddata->jack[0].jack_status);
610 pr_debug("headphone removed\n");
611 jack = &intelmaddata->jack[0].jack;
616 if (jack_prev_state == 0x40 && value == 0x00) {
618 do_gettimeofday(&intelmaddata->jack[1].buttonpressed);
619 pr_debug("MAD button press detected\n");
623 if (jack_prev_state == 0x00 && value == 0x40) {
624 if (intelmaddata->jack[1].jack_status) {
627 &intelmaddata->jack[1].buttonreleased);
629 pr_debug("Button Released detected\n");
630 timediff = intelmaddata->jack[1].
631 buttonreleased.tv_sec - intelmaddata->
632 jack[1].buttonpressed.tv_sec;
635 pr_debug("long press detected\n");
636 /* send headphone detect/undetect */
637 jack = &intelmaddata->jack[3].jack;
641 pr_debug("short press detected\n");
642 /* send headphone detect/undetect */
643 jack = &intelmaddata->jack[2].jack;
650 intelmaddata->jack_prev_state = value;
653 sst_mad_send_jack_report(jack, buttonpressflag, present);
657 void sst_mad_jackdetection_nec(u8 intsts, struct snd_intelmad *intelmaddata)
660 struct snd_jack *jack = NULL;
661 unsigned int present = 0, jack_event_flag = 0, buttonpressflag = 0;
662 struct sc_reg_access sc_access_read = {0,};
664 if (intelmaddata->pmic_status == PMIC_INIT) {
665 sc_access_read.reg_addr = 0x132;
666 sst_sc_reg_access(&sc_access_read, PMIC_READ, 1);
667 value = (sc_access_read.value);
668 pr_debug("value returned = 0x%x\n", value);
671 pr_debug("headset detected\n");
672 /* send headset detect/undetect */
673 jack = &intelmaddata->jack[1].jack;
674 present = (value == 0x1) ? 1 : 0;
678 pr_debug("headphone detected\n");
679 /* send headphone detect/undetect */
680 jack = &intelmaddata->jack[0].jack;
681 present = (value == 0x2) ? 1 : 0;
685 pr_debug("short push detected\n");
686 /* send short push */
687 jack = &intelmaddata->jack[2].jack;
693 pr_debug("long push detected\n");
695 jack = &intelmaddata->jack[3].jack;
702 sst_mad_send_jack_report(jack, buttonpressflag, present);
707 void sst_process_mad_jack_detection(struct work_struct *work)
710 struct mad_jack_msg_wq *mad_jack_detect =
711 container_of(work, struct mad_jack_msg_wq, wq);
713 struct snd_intelmad *intelmaddata =
714 mad_jack_detect->intelmaddata;
716 intsts = mad_jack_detect->intsts;
718 switch (intelmaddata->sstdrv_ops->vendor_id) {
720 sst_mad_jackdetection_fs(intsts , intelmaddata);
723 sst_mad_jackdetection_mx(intsts , intelmaddata);
726 sst_mad_jackdetection_nec(intsts , intelmaddata);
732 static int __devinit snd_intelmad_register_irq(
733 struct snd_intelmad *intelmaddata)
736 u32 regbase = AUDINT_BASE, regsize = 8;
739 pr_debug("irq reg done, regbase 0x%x, regsize 0x%x\n",
741 intelmaddata->int_base = ioremap_nocache(regbase, regsize);
742 if (!intelmaddata->int_base)
743 pr_err("Mapping of cache failed\n");
744 pr_debug("irq = 0x%x\n", intelmaddata->irq);
745 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL)
746 drv_name = DRIVER_NAME_MFLD;
748 drv_name = DRIVER_NAME_MRST;
749 ret_val = request_irq(intelmaddata->irq,
750 snd_intelmad_intr_handler,
751 IRQF_SHARED, drv_name,
754 pr_err("cannot register IRQ\n");
758 static int __devinit snd_intelmad_sst_register(
759 struct snd_intelmad *intelmaddata)
762 struct snd_pmic_ops *intelmad_vendor_ops[MAX_VENDORS] = {
769 struct sc_reg_access vendor_addr = {0x00, 0x00, 0x00};
771 if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
772 ret_val = sst_sc_reg_access(&vendor_addr, PMIC_READ, 1);
775 sst_card_vendor_id = (vendor_addr.value & (MASK2|MASK1|MASK0));
776 pr_debug("orginal n extrated vendor id = 0x%x %d\n",
777 vendor_addr.value, sst_card_vendor_id);
778 if (sst_card_vendor_id < 0 || sst_card_vendor_id > 2) {
779 pr_err("vendor card not supported!!\n");
783 sst_card_vendor_id = 0x3;
785 intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
786 intelmaddata->sstdrv_ops->vendor_id = sst_card_vendor_id;
787 BUG_ON(!intelmad_vendor_ops[sst_card_vendor_id]);
788 intelmaddata->sstdrv_ops->scard_ops =
789 intelmad_vendor_ops[sst_card_vendor_id];
791 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
792 intelmaddata->sstdrv_ops->scard_ops->pb_on = 0;
793 intelmaddata->sstdrv_ops->scard_ops->cap_on = 0;
794 intelmaddata->sstdrv_ops->scard_ops->input_dev_id = DMIC;
795 intelmaddata->sstdrv_ops->scard_ops->output_dev_id =
799 /* registering with SST driver to get access to SST APIs to use */
800 ret_val = register_sst_card(intelmaddata->sstdrv_ops);
802 pr_err("sst card registration failed\n");
806 sst_card_vendor_id = intelmaddata->sstdrv_ops->vendor_id;
807 intelmaddata->pmic_status = PMIC_UNINIT;
811 /* Driver Init/exit functionalities */
813 * snd_intelmad_pcm_new - to setup pcm for the card
815 * @card: pointer to the sound card structure
816 * @intelmaddata: pointer to internal context
817 * @pb: playback count for this card
818 * @cap: capture count for this card
819 * @index: device index
821 * This function is called from probe function to set up pcm params
824 static int __devinit snd_intelmad_pcm_new(struct snd_card *card,
825 struct snd_intelmad *intelmaddata,
826 unsigned int pb, unsigned int cap, unsigned int index)
830 char name[32] = INTEL_MAD;
831 struct snd_pcm_ops *pb_ops = NULL, *cap_ops = NULL;
833 pr_debug("called for pb %d, cp %d, idx %d\n", pb, cap, index);
834 ret_val = snd_pcm_new(card, name, index, pb, cap, &pcm);
837 /* setup the ops for playback and capture streams */
840 pb_ops = &snd_intelmad_headset_ops;
841 cap_ops = &snd_intelmad_capture_ops;
844 pb_ops = &snd_intelmad_ihf_ops;
845 cap_ops = &snd_intelmad_capture_ops;
848 pb_ops = &snd_intelmad_vibra_ops;
849 cap_ops = &snd_intelmad_capture_ops;
852 pb_ops = &snd_intelmad_haptic_ops;
853 cap_ops = &snd_intelmad_capture_ops;
857 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, pb_ops);
859 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, cap_ops);
860 /* setup private data which can be retrieved when required */
861 pcm->private_data = intelmaddata;
863 strncpy(pcm->name, card->shortname, strlen(card->shortname));
864 /* allocate dma pages for ALSA stream operations */
865 snd_pcm_lib_preallocate_pages_for_all(pcm,
866 SNDRV_DMA_TYPE_CONTINUOUS,
867 snd_dma_continuous_data(GFP_KERNEL),
868 MIN_BUFFER, MAX_BUFFER);
872 static int __devinit snd_intelmad_pcm(struct snd_card *card,
873 struct snd_intelmad *intelmaddata)
878 WARN_ON(!intelmaddata);
879 pr_debug("snd_intelmad_pcm called\n");
880 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 1, 0);
881 if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT)
883 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 1);
886 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 2);
889 return snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 3);
893 * snd_intelmad_jack- to setup jack settings of the card
895 * @intelmaddata: pointer to internal context
897 * This function is called send jack events
899 static int snd_intelmad_jack(struct snd_intelmad *intelmaddata)
901 struct snd_jack *jack;
904 pr_debug("snd_intelmad_jack called\n");
905 jack = &intelmaddata->jack[0].jack;
906 retval = snd_jack_new(intelmaddata->card, "Headphone",
907 SND_JACK_HEADPHONE, &jack);
910 snd_jack_report(jack, 0);
912 jack->private_data = jack;
913 intelmaddata->jack[0].jack = *jack;
916 jack = &intelmaddata->jack[1].jack;
917 retval = snd_jack_new(intelmaddata->card, "Headset",
918 SND_JACK_HEADSET, &jack);
924 jack->private_data = jack;
925 intelmaddata->jack[1].jack = *jack;
928 jack = &intelmaddata->jack[2].jack;
929 retval = snd_jack_new(intelmaddata->card, "Short Press",
930 SND_JACK_HS_SHORT_PRESS, &jack);
935 jack->private_data = jack;
936 intelmaddata->jack[2].jack = *jack;
939 jack = &intelmaddata->jack[3].jack;
940 retval = snd_jack_new(intelmaddata->card, "Long Press",
941 SND_JACK_HS_LONG_PRESS, &jack);
946 jack->private_data = jack;
947 intelmaddata->jack[3].jack = *jack;
953 * snd_intelmad_mixer- to setup mixer settings of the card
955 * @intelmaddata: pointer to internal context
957 * This function is called from probe function to set up mixer controls
959 static int __devinit snd_intelmad_mixer(struct snd_intelmad *intelmaddata)
961 struct snd_card *card;
963 int ret_val = 0, max_controls = 0;
964 char *mixername = "IntelMAD Controls";
965 struct snd_kcontrol_new *controls;
967 WARN_ON(!intelmaddata);
969 card = intelmaddata->card;
970 strncpy(card->mixername, mixername, sizeof(card->mixername)-1);
971 /* add all widget controls and expose the same */
972 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
973 max_controls = MAX_CTRL_MFLD;
974 controls = snd_intelmad_controls_mfld;
976 max_controls = MAX_CTRL_MRST;
977 controls = snd_intelmad_controls_mrst;
979 for (idx = 0; idx < max_controls; idx++) {
980 ret_val = snd_ctl_add(card,
981 snd_ctl_new1(&controls[idx],
983 pr_debug("mixer[idx]=%d added\n", idx);
985 pr_err("in adding of control index = %d\n", idx);
992 static int snd_intelmad_dev_free(struct snd_device *device)
994 struct snd_intelmad *intelmaddata;
998 intelmaddata = device->device_data;
1000 pr_debug("snd_intelmad_dev_free called\n");
1001 snd_card_free(intelmaddata->card);
1002 /*genl_unregister_family(&audio_event_genl_family);*/
1003 unregister_sst_card(intelmaddata->sstdrv_ops);
1005 /* free allocated memory for internal context */
1006 destroy_workqueue(intelmaddata->mad_jack_wq);
1007 kfree(intelmaddata->sstdrv_ops);
1008 kfree(intelmaddata);
1012 static int __devinit snd_intelmad_create(
1013 struct snd_intelmad *intelmaddata,
1014 struct snd_card *card)
1017 static struct snd_device_ops ops = {
1018 .dev_free = snd_intelmad_dev_free,
1021 WARN_ON(!intelmaddata);
1023 /* ALSA api to register for the device */
1024 ret_val = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelmaddata, &ops);
1029 * snd_intelmad_probe- function registred for init
1030 * @pdev : pointer to the device struture
1031 * This function is called when the device is initialized
1033 int __devinit snd_intelmad_probe(struct platform_device *pdev)
1035 struct snd_card *card;
1037 struct snd_intelmad *intelmaddata;
1038 const struct platform_device_id *id = platform_get_device_id(pdev);
1039 unsigned int cpu_id = (unsigned int)id->driver_data;
1041 pr_debug("probe for %s cpu_id %d\n", pdev->name, cpu_id);
1042 if (!strcmp(pdev->name, DRIVER_NAME_MRST))
1043 pr_debug("detected MRST\n");
1044 else if (!strcmp(pdev->name, DRIVER_NAME_MFLD))
1045 pr_debug("detected MFLD\n");
1047 pr_err("detected unknown device abort!!\n");
1050 if ((cpu_id < CPU_CHIP_LINCROFT) || (cpu_id > CPU_CHIP_PENWELL)) {
1051 pr_err("detected unknown cpu_id abort!!\n");
1054 /* allocate memory for saving internal context and working */
1055 intelmaddata = kzalloc(sizeof(*intelmaddata), GFP_KERNEL);
1056 if (!intelmaddata) {
1057 pr_debug("mem alloctn fail\n");
1061 /* allocate memory for LPE API set */
1062 intelmaddata->sstdrv_ops = kzalloc(sizeof(struct intel_sst_card_ops),
1064 if (!intelmaddata->sstdrv_ops) {
1065 pr_err("mem allocation for ops fail\n");
1066 kfree(intelmaddata);
1070 intelmaddata->cpu_id = cpu_id;
1071 /* create a card instance with ALSA framework */
1072 ret_val = snd_card_create(card_index, card_id, THIS_MODULE, 0, &card);
1074 pr_err("snd_card_create fail\n");
1078 intelmaddata->pdev = pdev;
1079 intelmaddata->irq = platform_get_irq(pdev, 0);
1080 platform_set_drvdata(pdev, intelmaddata);
1081 intelmaddata->card = card;
1082 intelmaddata->card_id = card_id;
1083 intelmaddata->card_index = card_index;
1084 intelmaddata->master_mute = UNMUTE;
1085 intelmaddata->playback_cnt = intelmaddata->capture_cnt = 0;
1086 strncpy(card->driver, INTEL_MAD, strlen(INTEL_MAD));
1087 strncpy(card->shortname, INTEL_MAD, strlen(INTEL_MAD));
1089 intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
1090 /* registering with LPE driver to get access to SST APIs to use */
1091 ret_val = snd_intelmad_sst_register(intelmaddata);
1093 pr_err("snd_intelmad_sst_register failed\n");
1097 intelmaddata->pmic_status = PMIC_INIT;
1099 ret_val = snd_intelmad_pcm(card, intelmaddata);
1101 pr_err("snd_intelmad_pcm failed\n");
1105 ret_val = snd_intelmad_mixer(intelmaddata);
1107 pr_err("snd_intelmad_mixer failed\n");
1111 ret_val = snd_intelmad_jack(intelmaddata);
1113 pr_err("snd_intelmad_jack failed\n");
1117 /*create work queue for jack interrupt*/
1118 INIT_WORK(&intelmaddata->mad_jack_msg.wq,
1119 sst_process_mad_jack_detection);
1121 intelmaddata->mad_jack_wq = create_workqueue("sst_mad_jack_wq");
1122 if (!intelmaddata->mad_jack_wq)
1123 goto free_mad_jack_wq;
1125 ret_val = snd_intelmad_register_irq(intelmaddata);
1127 pr_err("snd_intelmad_register_irq fail\n");
1131 /* internal function call to register device with ALSA */
1132 ret_val = snd_intelmad_create(intelmaddata, card);
1134 pr_err("snd_intelmad_create failed\n");
1137 card->private_data = &intelmaddata;
1138 snd_card_set_dev(card, &pdev->dev);
1139 ret_val = snd_card_register(card);
1141 pr_err("snd_card_register failed\n");
1145 pr_debug("snd_intelmad_probe complete\n");
1149 destroy_workqueue(intelmaddata->mad_jack_wq);
1151 pr_err("probe failed\n");
1152 snd_card_free(card);
1153 kfree(intelmaddata->sstdrv_ops);
1154 kfree(intelmaddata);
1159 static int snd_intelmad_remove(struct platform_device *pdev)
1161 struct snd_intelmad *intelmaddata = platform_get_drvdata(pdev);
1164 snd_card_free(intelmaddata->card);
1165 unregister_sst_card(intelmaddata->sstdrv_ops);
1166 /* free allocated memory for internal context */
1167 destroy_workqueue(intelmaddata->mad_jack_wq);
1168 kfree(intelmaddata->sstdrv_ops);
1169 kfree(intelmaddata);
1174 /*********************************************************************
1175 * Driver initialization and exit
1176 *********************************************************************/
1177 static const struct platform_device_id snd_intelmad_ids[] = {
1178 {DRIVER_NAME_MRST, CPU_CHIP_LINCROFT},
1179 {DRIVER_NAME_MFLD, CPU_CHIP_PENWELL},
1184 static struct platform_driver snd_intelmad_driver = {
1186 .owner = THIS_MODULE,
1187 .name = "intel_mid_sound_card",
1189 .id_table = snd_intelmad_ids,
1190 .probe = snd_intelmad_probe,
1191 .remove = __devexit_p(snd_intelmad_remove),
1195 * alsa_card_intelmad_init- driver init function
1197 * This function is called when driver module is inserted
1199 static int __init alsa_card_intelmad_init(void)
1201 pr_debug("mad_init called\n");
1202 return platform_driver_register(&snd_intelmad_driver);
1206 * alsa_card_intelmad_exit- driver exit function
1208 * This function is called when driver module is removed
1210 static void __exit alsa_card_intelmad_exit(void)
1212 pr_debug("mad_exit called\n");
1213 return platform_driver_unregister(&snd_intelmad_driver);
1216 module_init(alsa_card_intelmad_init)
1217 module_exit(alsa_card_intelmad_exit)