tty/serial: atmel_serial: whitespace and braces modifications
[zen-stable.git] / drivers / staging / intel_sst / intelmid.c
blob25656ad2802e0c02003ac1452d0ad80e4d72e7ea
1 /*
2 * intelmid.c - Intel Sound card driver for MID
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
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 <linux/input.h>
37 #include <sound/control.h>
38 #include <asm/mrst.h>
39 #include <sound/pcm.h>
40 #include <sound/jack.h>
41 #include <sound/pcm_params.h>
42 #include <sound/initval.h>
43 #include <linux/gpio.h>
44 #include "intel_sst.h"
45 #include "intel_sst_ioctl.h"
46 #include "intel_sst_fw_ipc.h"
47 #include "intel_sst_common.h"
48 #include "intelmid_snd_control.h"
49 #include "intelmid_adc_control.h"
50 #include "intelmid.h"
52 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
53 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
54 MODULE_AUTHOR("Dharageswari R <dharageswari.r@intel.com>");
55 MODULE_AUTHOR("KP Jeeja <jeeja.kp@intel.com>");
56 MODULE_DESCRIPTION("Intel MAD Sound card driver");
57 MODULE_LICENSE("GPL v2");
58 MODULE_SUPPORTED_DEVICE("{Intel,Intel_MAD}");
61 static int card_index = SNDRV_DEFAULT_IDX1;/* Index 0-MAX */
62 static char *card_id = SNDRV_DEFAULT_STR1; /* ID for this card */
64 module_param(card_index, int, 0444);
65 MODULE_PARM_DESC(card_index, "Index value for INTELMAD soundcard.");
66 module_param(card_id, charp, 0444);
67 MODULE_PARM_DESC(card_id, "ID string for INTELMAD soundcard.");
69 int sst_card_vendor_id;
70 int intelmid_audio_interrupt_enable;/*checkpatch fix*/
71 struct snd_intelmad *intelmad_drv;
73 #define INFO(_cpu_id, _irq_cache, _size) \
74 ((kernel_ulong_t)&(struct snd_intelmad_probe_info) { \
75 .cpu_id = (_cpu_id), \
76 .irq_cache = (_irq_cache), \
77 .size = (_size), \
79 /* Data path functionalities */
80 static struct snd_pcm_hardware snd_intelmad_stream = {
81 .info = (SNDRV_PCM_INFO_INTERLEAVED |
82 SNDRV_PCM_INFO_DOUBLE |
83 SNDRV_PCM_INFO_PAUSE |
84 SNDRV_PCM_INFO_RESUME |
85 SNDRV_PCM_INFO_MMAP|
86 SNDRV_PCM_INFO_MMAP_VALID |
87 SNDRV_PCM_INFO_BLOCK_TRANSFER |
88 SNDRV_PCM_INFO_SYNC_START),
89 .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
90 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
91 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
92 .rates = (SNDRV_PCM_RATE_8000|
93 SNDRV_PCM_RATE_44100 |
94 SNDRV_PCM_RATE_48000),
95 .rate_min = MIN_RATE,
97 .rate_max = MAX_RATE,
98 .channels_min = MIN_CHANNEL,
99 .channels_max = MAX_CHANNEL_AMIC,
100 .buffer_bytes_max = MAX_BUFFER,
101 .period_bytes_min = MIN_PERIOD_BYTES,
102 .period_bytes_max = MAX_PERIOD_BYTES,
103 .periods_min = MIN_PERIODS,
104 .periods_max = MAX_PERIODS,
105 .fifo_size = FIFO_SIZE,
110 * snd_intelmad_pcm_trigger - stream activities are handled here
112 * @substream:substream for which the stream function is called
113 * @cmd:the stream commamd that requested from upper layer
115 * This function is called whenever an a stream activity is invoked
117 static int snd_intelmad_pcm_trigger(struct snd_pcm_substream *substream,
118 int cmd)
120 int ret_val = 0, str_id;
121 struct snd_intelmad *intelmaddata;
122 struct mad_stream_pvt *stream;
123 struct intel_sst_pcm_control *sst_ops;
125 WARN_ON(!substream);
127 intelmaddata = snd_pcm_substream_chip(substream);
128 stream = substream->runtime->private_data;
130 WARN_ON(!intelmaddata->sstdrv_ops);
131 WARN_ON(!intelmaddata->sstdrv_ops->scard_ops);
132 sst_ops = intelmaddata->sstdrv_ops->pcm_control;
133 str_id = stream->stream_info.str_id;
135 switch (cmd) {
136 case SNDRV_PCM_TRIGGER_START:
137 pr_debug("Trigger Start\n");
138 ret_val = sst_ops->device_control(SST_SND_START, &str_id);
139 if (ret_val)
140 return ret_val;
141 stream->stream_status = RUNNING;
142 stream->substream = substream;
143 break;
144 case SNDRV_PCM_TRIGGER_STOP:
145 pr_debug("in stop\n");
146 ret_val = sst_ops->device_control(SST_SND_DROP, &str_id);
147 if (ret_val)
148 return ret_val;
149 stream->stream_status = DROPPED;
150 break;
151 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
152 pr_debug("in pause\n");
153 ret_val = sst_ops->device_control(SST_SND_PAUSE, &str_id);
154 if (ret_val)
155 return ret_val;
156 stream->stream_status = PAUSED;
157 break;
158 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
159 pr_debug("in pause release\n");
160 ret_val = sst_ops->device_control(SST_SND_RESUME, &str_id);
161 if (ret_val)
162 return ret_val;
163 stream->stream_status = RUNNING;
164 break;
165 default:
166 return -EINVAL;
168 return ret_val;
172 * snd_intelmad_pcm_prepare- internal preparation before starting a stream
174 * @substream: substream for which the function is called
176 * This function is called when a stream is started for internal preparation.
178 static int snd_intelmad_pcm_prepare(struct snd_pcm_substream *substream)
180 struct mad_stream_pvt *stream;
181 int ret_val = 0;
182 struct snd_intelmad *intelmaddata;
184 pr_debug("pcm_prepare called\n");
186 WARN_ON(!substream);
187 stream = substream->runtime->private_data;
188 intelmaddata = snd_pcm_substream_chip(substream);
189 pr_debug("pb cnt = %d cap cnt = %d\n",\
190 intelmaddata->playback_cnt,
191 intelmaddata->capture_cnt);
193 if (stream->stream_info.str_id) {
194 pr_debug("Prepare called for already set stream\n");
195 ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
196 SST_SND_DROP, &stream->stream_info.str_id);
197 return ret_val;
200 ret_val = snd_intelmad_alloc_stream(substream);
201 if (ret_val < 0)
202 return ret_val;
203 stream->dbg_cum_bytes = 0;
204 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
205 intelmaddata->playback_cnt++;
206 else
207 intelmaddata->capture_cnt++;
208 /* return back the stream id */
209 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
210 "%d", stream->stream_info.str_id);
211 pr_debug("stream id to user = %s\n",
212 substream->pcm->id);
214 ret_val = snd_intelmad_init_stream(substream);
215 if (ret_val)
216 return ret_val;
217 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
218 return ret_val;
221 static int snd_intelmad_hw_params(struct snd_pcm_substream *substream,
222 struct snd_pcm_hw_params *hw_params)
224 int ret_val;
226 pr_debug("snd_intelmad_hw_params called\n");
227 ret_val = snd_pcm_lib_malloc_pages(substream,
228 params_buffer_bytes(hw_params));
229 memset(substream->runtime->dma_area, 0,
230 params_buffer_bytes(hw_params));
232 return ret_val;
235 static int snd_intelmad_hw_free(struct snd_pcm_substream *substream)
237 pr_debug("snd_intelmad_hw_free called\n");
238 return snd_pcm_lib_free_pages(substream);
242 * snd_intelmad_pcm_pointer- to send the current buffer pointer processed by hw
244 * @substream: substream for which the function is called
246 * This function is called by ALSA framework to get the current hw buffer ptr
247 * when a period is elapsed
249 static snd_pcm_uframes_t snd_intelmad_pcm_pointer
250 (struct snd_pcm_substream *substream)
252 /* struct snd_pcm_runtime *runtime = substream->runtime; */
253 struct mad_stream_pvt *stream;
254 struct snd_intelmad *intelmaddata;
255 int ret_val;
257 WARN_ON(!substream);
259 intelmaddata = snd_pcm_substream_chip(substream);
260 stream = substream->runtime->private_data;
261 if (stream->stream_status == INIT)
262 return 0;
264 ret_val = intelmaddata->sstdrv_ops->pcm_control->device_control(
265 SST_SND_BUFFER_POINTER, &stream->stream_info);
266 if (ret_val) {
267 pr_err("error code = 0x%x\n", ret_val);
268 return ret_val;
270 pr_debug("samples reported out 0x%llx\n",
271 stream->stream_info.buffer_ptr);
272 pr_debug("Frame bits:: %d period_count :: %d\n",
273 (int)substream->runtime->frame_bits,
274 (int)substream->runtime->period_size);
276 return stream->stream_info.buffer_ptr;
281 * snd_intelmad_close- to free parameteres when stream is stopped
283 * @substream: substream for which the function is called
285 * This function is called by ALSA framework when stream is stopped
287 static int snd_intelmad_close(struct snd_pcm_substream *substream)
289 struct snd_intelmad *intelmaddata;
290 struct mad_stream_pvt *stream;
291 int ret_val = 0, str_id;
293 WARN_ON(!substream);
295 stream = substream->runtime->private_data;
296 str_id = stream->stream_info.str_id;
298 pr_debug("sst: snd_intelmad_close called for %d\n", str_id);
299 intelmaddata = snd_pcm_substream_chip(substream);
301 pr_debug("str id = %d\n", stream->stream_info.str_id);
302 if (stream->stream_info.str_id) {
303 /* SST API to actually stop/free the stream */
304 ret_val = intelmaddata->sstdrv_ops->pcm_control->close(str_id);
305 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
306 intelmaddata->playback_cnt--;
307 else
308 intelmaddata->capture_cnt--;
310 pr_debug("snd_intelmad_close : pb cnt = %d cap cnt = %d\n",
311 intelmaddata->playback_cnt, intelmaddata->capture_cnt);
312 kfree(substream->runtime->private_data);
313 return ret_val;
317 * snd_intelmad_open- to set runtime parameters during stream start
319 * @substream: substream for which the function is called
320 * @type: audio device type
322 * This function is called by ALSA framework when stream is started
324 static int snd_intelmad_open(struct snd_pcm_substream *substream,
325 enum snd_sst_audio_device_type type)
327 struct snd_intelmad *intelmaddata;
328 struct snd_pcm_runtime *runtime;
329 struct mad_stream_pvt *stream;
331 WARN_ON(!substream);
333 pr_debug("snd_intelmad_open called\n");
335 intelmaddata = snd_pcm_substream_chip(substream);
336 runtime = substream->runtime;
337 /* set the runtime hw parameter with local snd_pcm_hardware struct */
338 runtime->hw = snd_intelmad_stream;
339 if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
341 * MRST firmware currently denies stereo recording requests.
343 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
344 runtime->hw.formats = (SNDRV_PCM_FMTBIT_S16 |
345 SNDRV_PCM_FMTBIT_U16);
346 runtime->hw.channels_max = 1;
349 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
350 runtime->hw = snd_intelmad_stream;
351 runtime->hw.rates = SNDRV_PCM_RATE_48000;
352 runtime->hw.rate_min = MAX_RATE;
353 runtime->hw.formats = (SNDRV_PCM_FMTBIT_S24 |
354 SNDRV_PCM_FMTBIT_U24);
355 if (intelmaddata->sstdrv_ops->scard_ops->input_dev_id == AMIC)
356 runtime->hw.channels_max = MAX_CHANNEL_AMIC;
357 else
358 runtime->hw.channels_max = MAX_CHANNEL_DMIC;
361 /* setup the internal datastruture stream pointers based on it being
362 playback or capture stream */
363 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
364 if (!stream)
365 return -ENOMEM;
366 stream->stream_info.str_id = 0;
367 stream->device = type;
368 stream->stream_status = INIT;
369 runtime->private_data = stream;
370 return snd_pcm_hw_constraint_integer(runtime,
371 SNDRV_PCM_HW_PARAM_PERIODS);
374 static int snd_intelmad_headset_open(struct snd_pcm_substream *substream)
376 return snd_intelmad_open(substream, SND_SST_DEVICE_HEADSET);
379 static int snd_intelmad_ihf_open(struct snd_pcm_substream *substream)
381 return snd_intelmad_open(substream, SND_SST_DEVICE_IHF);
384 static int snd_intelmad_vibra_open(struct snd_pcm_substream *substream)
386 return snd_intelmad_open(substream, SND_SST_DEVICE_VIBRA);
389 static int snd_intelmad_haptic_open(struct snd_pcm_substream *substream)
391 return snd_intelmad_open(substream, SND_SST_DEVICE_HAPTIC);
394 static struct snd_pcm_ops snd_intelmad_headset_ops = {
395 .open = snd_intelmad_headset_open,
396 .close = snd_intelmad_close,
397 .ioctl = snd_pcm_lib_ioctl,
398 .hw_params = snd_intelmad_hw_params,
399 .hw_free = snd_intelmad_hw_free,
400 .prepare = snd_intelmad_pcm_prepare,
401 .trigger = snd_intelmad_pcm_trigger,
402 .pointer = snd_intelmad_pcm_pointer,
405 static struct snd_pcm_ops snd_intelmad_ihf_ops = {
406 .open = snd_intelmad_ihf_open,
407 .close = snd_intelmad_close,
408 .ioctl = snd_pcm_lib_ioctl,
409 .hw_params = snd_intelmad_hw_params,
410 .hw_free = snd_intelmad_hw_free,
411 .prepare = snd_intelmad_pcm_prepare,
412 .trigger = snd_intelmad_pcm_trigger,
413 .pointer = snd_intelmad_pcm_pointer,
416 static struct snd_pcm_ops snd_intelmad_vibra_ops = {
417 .open = snd_intelmad_vibra_open,
418 .close = snd_intelmad_close,
419 .ioctl = snd_pcm_lib_ioctl,
420 .hw_params = snd_intelmad_hw_params,
421 .hw_free = snd_intelmad_hw_free,
422 .prepare = snd_intelmad_pcm_prepare,
423 .trigger = snd_intelmad_pcm_trigger,
424 .pointer = snd_intelmad_pcm_pointer,
427 static struct snd_pcm_ops snd_intelmad_haptic_ops = {
428 .open = snd_intelmad_haptic_open,
429 .close = snd_intelmad_close,
430 .ioctl = snd_pcm_lib_ioctl,
431 .hw_params = snd_intelmad_hw_params,
432 .hw_free = snd_intelmad_hw_free,
433 .prepare = snd_intelmad_pcm_prepare,
434 .trigger = snd_intelmad_pcm_trigger,
435 .pointer = snd_intelmad_pcm_pointer,
438 static struct snd_pcm_ops snd_intelmad_capture_ops = {
439 .open = snd_intelmad_headset_open,
440 .close = snd_intelmad_close,
441 .ioctl = snd_pcm_lib_ioctl,
442 .hw_params = snd_intelmad_hw_params,
443 .hw_free = snd_intelmad_hw_free,
444 .prepare = snd_intelmad_pcm_prepare,
445 .trigger = snd_intelmad_pcm_trigger,
446 .pointer = snd_intelmad_pcm_pointer,
449 int intelmad_get_mic_bias(void)
451 struct snd_pmic_ops *pmic_ops;
453 if (!intelmad_drv || !intelmad_drv->sstdrv_ops)
454 return -ENODEV;
455 pmic_ops = intelmad_drv->sstdrv_ops->scard_ops;
456 if (pmic_ops && pmic_ops->pmic_get_mic_bias)
457 return pmic_ops->pmic_get_mic_bias(intelmad_drv);
458 else
459 return -ENODEV;
461 EXPORT_SYMBOL_GPL(intelmad_get_mic_bias);
463 int intelmad_set_headset_state(int state)
465 struct snd_pmic_ops *pmic_ops;
467 if (!intelmad_drv || !intelmad_drv->sstdrv_ops)
468 return -ENODEV;
469 pmic_ops = intelmad_drv->sstdrv_ops->scard_ops;
470 if (pmic_ops && pmic_ops->pmic_set_headset_state)
471 return pmic_ops->pmic_set_headset_state(state);
472 else
473 return -ENODEV;
475 EXPORT_SYMBOL_GPL(intelmad_set_headset_state);
477 void sst_process_mad_jack_detection(struct work_struct *work)
479 u8 interrupt_status;
480 struct mad_jack_msg_wq *mad_jack_detect =
481 container_of(work, struct mad_jack_msg_wq, wq);
483 struct snd_intelmad *intelmaddata =
484 mad_jack_detect->intelmaddata;
486 if (!intelmaddata)
487 return;
489 interrupt_status = mad_jack_detect->intsts;
490 if (intelmaddata->sstdrv_ops && intelmaddata->sstdrv_ops->scard_ops
491 && intelmaddata->sstdrv_ops->scard_ops->pmic_irq_cb) {
492 intelmaddata->sstdrv_ops->scard_ops->pmic_irq_cb(
493 (void *)intelmaddata, interrupt_status);
494 intelmaddata->sstdrv_ops->scard_ops->pmic_jack_enable();
496 kfree(mad_jack_detect);
499 * snd_intelmad_intr_handler- interrupt handler
501 * @irq : irq number of the interrupt received
502 * @dev: device context
504 * This function is called when an interrupt is raised at the sound card
506 static irqreturn_t snd_intelmad_intr_handler(int irq, void *dev)
508 struct snd_intelmad *intelmaddata =
509 (struct snd_intelmad *)dev;
510 u8 interrupt_status;
511 struct mad_jack_msg_wq *mad_jack_msg;
512 memcpy_fromio(&interrupt_status,
513 ((void *)(intelmaddata->int_base)),
514 sizeof(u8));
516 mad_jack_msg = kzalloc(sizeof(*mad_jack_msg), GFP_ATOMIC);
517 mad_jack_msg->intsts = interrupt_status;
518 mad_jack_msg->intelmaddata = intelmaddata;
519 INIT_WORK(&mad_jack_msg->wq, sst_process_mad_jack_detection);
520 queue_work(intelmaddata->mad_jack_wq, &mad_jack_msg->wq);
522 return IRQ_HANDLED;
525 void sst_mad_send_jack_report(struct snd_jack *jack,
526 int buttonpressevent , int status)
529 if (!jack) {
530 pr_debug("MAD error jack empty\n");
532 } else {
533 snd_jack_report(jack, status);
534 /* button pressed and released */
535 if (buttonpressevent)
536 snd_jack_report(jack, 0);
537 pr_debug("MAD sending jack report Done !!!\n");
541 static int __devinit snd_intelmad_register_irq(
542 struct snd_intelmad *intelmaddata, unsigned int regbase,
543 unsigned int regsize)
545 int ret_val;
546 char *drv_name;
548 pr_debug("irq reg regbase 0x%x, regsize 0x%x\n",
549 regbase, regsize);
550 intelmaddata->int_base = ioremap_nocache(regbase, regsize);
551 if (!intelmaddata->int_base)
552 pr_err("Mapping of cache failed\n");
553 pr_debug("irq = 0x%x\n", intelmaddata->irq);
554 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL)
555 drv_name = DRIVER_NAME_MFLD;
556 else
557 drv_name = DRIVER_NAME_MRST;
558 ret_val = request_irq(intelmaddata->irq,
559 snd_intelmad_intr_handler,
560 IRQF_SHARED, drv_name,
561 intelmaddata);
562 if (ret_val)
563 pr_err("cannot register IRQ\n");
564 return ret_val;
567 static int __devinit snd_intelmad_sst_register(
568 struct snd_intelmad *intelmaddata)
570 int ret_val = 0;
571 struct snd_pmic_ops *intelmad_vendor_ops[MAX_VENDORS] = {
572 &snd_pmic_ops_fs,
573 &snd_pmic_ops_mx,
574 &snd_pmic_ops_nc,
575 &snd_msic_ops
578 struct sc_reg_access vendor_addr = {0x00, 0x00, 0x00};
580 if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT) {
581 ret_val = sst_sc_reg_access(&vendor_addr, PMIC_READ, 1);
582 if (ret_val)
583 return ret_val;
584 sst_card_vendor_id = (vendor_addr.value & (MASK2|MASK1|MASK0));
585 pr_debug("original n extrated vendor id = 0x%x %d\n",
586 vendor_addr.value, sst_card_vendor_id);
587 if (sst_card_vendor_id < 0 || sst_card_vendor_id > 2) {
588 pr_err("vendor card not supported!!\n");
589 return -EIO;
591 } else
592 sst_card_vendor_id = 0x3;
594 intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
595 intelmaddata->sstdrv_ops->vendor_id = sst_card_vendor_id;
596 BUG_ON(!intelmad_vendor_ops[sst_card_vendor_id]);
597 intelmaddata->sstdrv_ops->scard_ops =
598 intelmad_vendor_ops[sst_card_vendor_id];
600 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
601 intelmaddata->sstdrv_ops->scard_ops->pb_on = 0;
602 intelmaddata->sstdrv_ops->scard_ops->cap_on = 0;
603 intelmaddata->sstdrv_ops->scard_ops->input_dev_id = DMIC;
604 intelmaddata->sstdrv_ops->scard_ops->output_dev_id =
605 STEREO_HEADPHONE;
606 intelmaddata->sstdrv_ops->scard_ops->lineout_dev_id = NONE;
609 /* registering with SST driver to get access to SST APIs to use */
610 ret_val = register_sst_card(intelmaddata->sstdrv_ops);
611 if (ret_val) {
612 pr_err("sst card registration failed\n");
613 return ret_val;
615 sst_card_vendor_id = intelmaddata->sstdrv_ops->vendor_id;
616 intelmaddata->pmic_status = PMIC_UNINIT;
617 return ret_val;
620 static void snd_intelmad_page_free(struct snd_pcm *pcm)
622 snd_pcm_lib_preallocate_free_for_all(pcm);
624 /* Driver Init/exit functionalities */
626 * snd_intelmad_pcm_new - to setup pcm for the card
628 * @card: pointer to the sound card structure
629 * @intelmaddata: pointer to internal context
630 * @pb: playback count for this card
631 * @cap: capture count for this card
632 * @index: device index
634 * This function is called from probe function to set up pcm params
635 * and functions
637 static int __devinit snd_intelmad_pcm_new(struct snd_card *card,
638 struct snd_intelmad *intelmaddata,
639 unsigned int pb, unsigned int cap, unsigned int index)
641 int ret_val = 0;
642 struct snd_pcm *pcm;
643 char name[32] = INTEL_MAD;
644 struct snd_pcm_ops *pb_ops = NULL, *cap_ops = NULL;
646 pr_debug("called for pb %d, cp %d, idx %d\n", pb, cap, index);
647 ret_val = snd_pcm_new(card, name, index, pb, cap, &pcm);
648 if (ret_val)
649 return ret_val;
650 /* setup the ops for playback and capture streams */
651 switch (index) {
652 case 0:
653 pb_ops = &snd_intelmad_headset_ops;
654 cap_ops = &snd_intelmad_capture_ops;
655 break;
656 case 1:
657 pb_ops = &snd_intelmad_ihf_ops;
658 cap_ops = &snd_intelmad_capture_ops;
659 break;
660 case 2:
661 pb_ops = &snd_intelmad_vibra_ops;
662 cap_ops = &snd_intelmad_capture_ops;
663 break;
664 case 3:
665 pb_ops = &snd_intelmad_haptic_ops;
666 cap_ops = &snd_intelmad_capture_ops;
667 break;
669 if (pb)
670 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, pb_ops);
671 if (cap)
672 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, cap_ops);
673 /* setup private data which can be retrieved when required */
674 pcm->private_data = intelmaddata;
675 pcm->private_free = snd_intelmad_page_free;
676 pcm->info_flags = 0;
677 strncpy(pcm->name, card->shortname, strlen(card->shortname));
678 /* allocate dma pages for ALSA stream operations */
679 snd_pcm_lib_preallocate_pages_for_all(pcm,
680 SNDRV_DMA_TYPE_CONTINUOUS,
681 snd_dma_continuous_data(GFP_KERNEL),
682 MIN_BUFFER, MAX_BUFFER);
683 return ret_val;
686 static int __devinit snd_intelmad_pcm(struct snd_card *card,
687 struct snd_intelmad *intelmaddata)
689 int ret_val = 0;
691 WARN_ON(!card);
692 WARN_ON(!intelmaddata);
693 pr_debug("snd_intelmad_pcm called\n");
694 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 1, 0);
695 if (intelmaddata->cpu_id == CPU_CHIP_LINCROFT)
696 return ret_val;
697 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 1);
698 if (ret_val)
699 return ret_val;
700 ret_val = snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 2);
701 if (ret_val)
702 return ret_val;
703 return snd_intelmad_pcm_new(card, intelmaddata, 1, 0, 3);
707 * snd_intelmad_jack- to setup jack settings of the card
709 * @intelmaddata: pointer to internal context
711 * This function is called send jack events
713 static int snd_intelmad_jack(struct snd_intelmad *intelmaddata)
715 struct snd_jack *jack;
716 int retval;
718 pr_debug("snd_intelmad_jack called\n");
719 jack = &intelmaddata->jack[0].jack;
720 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PHONE);
721 retval = snd_jack_new(intelmaddata->card, "Intel(R) MID Audio Jack",
722 SND_JACK_HEADPHONE | SND_JACK_HEADSET |
723 SW_JACK_PHYSICAL_INSERT | SND_JACK_BTN_0
724 | SND_JACK_BTN_1, &jack);
725 pr_debug("snd_intelmad_jack called\n");
726 if (retval < 0)
727 return retval;
728 snd_jack_report(jack, 0);
730 jack->private_data = jack;
731 intelmaddata->jack[0].jack = *jack;
733 return retval;
737 * snd_intelmad_mixer- to setup mixer settings of the card
739 * @intelmaddata: pointer to internal context
741 * This function is called from probe function to set up mixer controls
743 static int __devinit snd_intelmad_mixer(struct snd_intelmad *intelmaddata)
745 struct snd_card *card;
746 unsigned int idx;
747 int ret_val = 0, max_controls = 0;
748 char *mixername = "IntelMAD Controls";
749 struct snd_kcontrol_new *controls;
751 WARN_ON(!intelmaddata);
753 card = intelmaddata->card;
754 strncpy(card->mixername, mixername, sizeof(card->mixername)-1);
755 /* add all widget controls and expose the same */
756 if (intelmaddata->cpu_id == CPU_CHIP_PENWELL) {
757 max_controls = MAX_CTRL_MFLD;
758 controls = snd_intelmad_controls_mfld;
759 } else {
760 max_controls = MAX_CTRL_MRST;
761 controls = snd_intelmad_controls_mrst;
763 for (idx = 0; idx < max_controls; idx++) {
764 ret_val = snd_ctl_add(card,
765 snd_ctl_new1(&controls[idx],
766 intelmaddata));
767 pr_debug("mixer[idx]=%d added\n", idx);
768 if (ret_val) {
769 pr_err("in adding of control index = %d\n", idx);
770 break;
773 return ret_val;
776 static int snd_intelmad_dev_free(struct snd_device *device)
778 struct snd_intelmad *intelmaddata;
780 WARN_ON(!device);
782 intelmaddata = device->device_data;
784 pr_debug("snd_intelmad_dev_free called\n");
785 unregister_sst_card(intelmaddata->sstdrv_ops);
787 /* free allocated memory for internal context */
788 destroy_workqueue(intelmaddata->mad_jack_wq);
789 device->device_data = NULL;
790 kfree(intelmaddata->sstdrv_ops);
791 kfree(intelmaddata);
793 return 0;
796 static int __devinit snd_intelmad_create(
797 struct snd_intelmad *intelmaddata,
798 struct snd_card *card)
800 int ret_val;
801 static struct snd_device_ops ops = {
802 .dev_free = snd_intelmad_dev_free,
805 WARN_ON(!intelmaddata);
806 WARN_ON(!card);
807 /* ALSA api to register for the device */
808 ret_val = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelmaddata, &ops);
809 return ret_val;
813 * snd_intelmad_probe- function registred for init
814 * @pdev : pointer to the device struture
815 * This function is called when the device is initialized
817 int __devinit snd_intelmad_probe(struct platform_device *pdev)
819 struct snd_card *card;
820 int ret_val;
821 struct snd_intelmad *intelmaddata;
822 const struct platform_device_id *id = platform_get_device_id(pdev);
823 struct snd_intelmad_probe_info *info = (void *)id->driver_data;
825 pr_debug("probe for %s cpu_id %d\n", pdev->name, info->cpu_id);
826 pr_debug("rq_chache %x of size %x\n", info->irq_cache, info->size);
827 if (!strcmp(pdev->name, DRIVER_NAME_MRST))
828 pr_debug("detected MRST\n");
829 else if (!strcmp(pdev->name, DRIVER_NAME_MFLD))
830 pr_debug("detected MFLD\n");
831 else {
832 pr_err("detected unknown device abort!!\n");
833 return -EIO;
835 if ((info->cpu_id < CPU_CHIP_LINCROFT) ||
836 (info->cpu_id > CPU_CHIP_PENWELL)) {
837 pr_err("detected unknown cpu_id abort!!\n");
838 return -EIO;
840 /* allocate memory for saving internal context and working */
841 intelmaddata = kzalloc(sizeof(*intelmaddata), GFP_KERNEL);
842 if (!intelmaddata) {
843 pr_debug("mem alloctn fail\n");
844 return -ENOMEM;
846 intelmad_drv = intelmaddata;
848 /* allocate memory for LPE API set */
849 intelmaddata->sstdrv_ops = kzalloc(sizeof(struct intel_sst_card_ops),
850 GFP_KERNEL);
851 if (!intelmaddata->sstdrv_ops) {
852 pr_err("mem allocation for ops fail\n");
853 kfree(intelmaddata);
854 return -ENOMEM;
857 intelmaddata->cpu_id = info->cpu_id;
858 /* create a card instance with ALSA framework */
859 ret_val = snd_card_create(card_index, card_id, THIS_MODULE, 0, &card);
860 if (ret_val) {
861 pr_err("snd_card_create fail\n");
862 goto free_allocs;
865 intelmaddata->pdev = pdev;
866 intelmaddata->irq = platform_get_irq(pdev, 0);
867 platform_set_drvdata(pdev, intelmaddata);
868 intelmaddata->card = card;
869 intelmaddata->card_id = card_id;
870 intelmaddata->card_index = card_index;
871 intelmaddata->master_mute = UNMUTE;
872 intelmaddata->playback_cnt = intelmaddata->capture_cnt = 0;
873 strncpy(card->driver, INTEL_MAD, strlen(INTEL_MAD));
874 strncpy(card->shortname, INTEL_MAD, strlen(INTEL_MAD));
876 intelmaddata->sstdrv_ops->module_name = SST_CARD_NAMES;
877 /* registering with LPE driver to get access to SST APIs to use */
878 ret_val = snd_intelmad_sst_register(intelmaddata);
879 if (ret_val) {
880 pr_err("snd_intelmad_sst_register failed\n");
881 goto set_null_data;
884 intelmaddata->pmic_status = PMIC_INIT;
886 ret_val = snd_intelmad_pcm(card, intelmaddata);
887 if (ret_val) {
888 pr_err("snd_intelmad_pcm failed\n");
889 goto free_sst;
892 ret_val = snd_intelmad_mixer(intelmaddata);
893 if (ret_val) {
894 pr_err("snd_intelmad_mixer failed\n");
895 goto free_card;
898 ret_val = snd_intelmad_jack(intelmaddata);
899 if (ret_val) {
900 pr_err("snd_intelmad_jack failed\n");
901 goto free_card;
903 intelmaddata->adc_address = mid_initialize_adc();
905 /*create work queue for jack interrupt*/
906 INIT_WORK(&intelmaddata->mad_jack_msg.wq,
907 sst_process_mad_jack_detection);
909 intelmaddata->mad_jack_wq = create_workqueue("sst_mad_jack_wq");
910 if (!intelmaddata->mad_jack_wq)
911 goto free_card;
913 ret_val = snd_intelmad_register_irq(intelmaddata,
914 info->irq_cache, info->size);
915 if (ret_val) {
916 pr_err("snd_intelmad_register_irq fail\n");
917 goto free_mad_jack_wq;
920 /* internal function call to register device with ALSA */
921 ret_val = snd_intelmad_create(intelmaddata, card);
922 if (ret_val) {
923 pr_err("snd_intelmad_create failed\n");
924 goto set_pvt_data;
926 card->private_data = &intelmaddata;
927 snd_card_set_dev(card, &pdev->dev);
928 ret_val = snd_card_register(card);
929 if (ret_val) {
930 pr_err("snd_card_register failed\n");
931 goto set_pvt_data;
933 if (pdev->dev.platform_data) {
934 int gpio_amp = *(int *)pdev->dev.platform_data;
935 if (gpio_request_one(gpio_amp, GPIOF_OUT_INIT_LOW, "amp power"))
936 gpio_amp = 0;
937 intelmaddata->sstdrv_ops->scard_ops->gpio_amp = gpio_amp;
940 pr_debug("snd_intelmad_probe complete\n");
941 return ret_val;
943 set_pvt_data:
944 card->private_data = NULL;
945 free_mad_jack_wq:
946 destroy_workqueue(intelmaddata->mad_jack_wq);
947 free_card:
948 snd_card_free(intelmaddata->card);
949 free_sst:
950 unregister_sst_card(intelmaddata->sstdrv_ops);
951 set_null_data:
952 platform_set_drvdata(pdev, NULL);
953 free_allocs:
954 pr_err("probe failed\n");
955 snd_card_free(card);
956 kfree(intelmaddata->sstdrv_ops);
957 kfree(intelmaddata);
958 return ret_val;
962 static int snd_intelmad_remove(struct platform_device *pdev)
964 struct snd_intelmad *intelmaddata = platform_get_drvdata(pdev);
966 if (intelmaddata) {
967 if (intelmaddata->sstdrv_ops->scard_ops->gpio_amp)
968 gpio_free(intelmaddata->sstdrv_ops->scard_ops->gpio_amp);
969 free_irq(intelmaddata->irq, intelmaddata);
970 snd_card_free(intelmaddata->card);
972 intelmad_drv = NULL;
973 platform_set_drvdata(pdev, NULL);
974 return 0;
977 /*********************************************************************
978 * Driver initialization and exit
979 *********************************************************************/
980 static const struct platform_device_id snd_intelmad_ids[] = {
981 {DRIVER_NAME_MRST, INFO(CPU_CHIP_LINCROFT, AUDINT_BASE, 1)},
982 {DRIVER_NAME_MFLD, INFO(CPU_CHIP_PENWELL, 0xFFFF7FCD, 1)},
983 {"", 0},
987 static struct platform_driver snd_intelmad_driver = {
988 .driver = {
989 .owner = THIS_MODULE,
990 .name = "intel_mid_sound_card",
992 .id_table = snd_intelmad_ids,
993 .probe = snd_intelmad_probe,
994 .remove = __devexit_p(snd_intelmad_remove),
998 * alsa_card_intelmad_init- driver init function
1000 * This function is called when driver module is inserted
1002 static int __init alsa_card_intelmad_init(void)
1004 pr_debug("mad_init called\n");
1005 return platform_driver_register(&snd_intelmad_driver);
1009 * alsa_card_intelmad_exit- driver exit function
1011 * This function is called when driver module is removed
1013 static void __exit alsa_card_intelmad_exit(void)
1015 pr_debug("mad_exit called\n");
1016 return platform_driver_unregister(&snd_intelmad_driver);
1019 module_init(alsa_card_intelmad_init)
1020 module_exit(alsa_card_intelmad_exit)