blob: 70b4f02da2c0a0780a0627e16a4ab2a734cbd7e4 [file] [log] [blame]
Ian Minett95c6e9c2011-06-15 15:35:17 -07001/*
2 * HD audio interface patch for Creative CA0132 chip
3 *
4 * Copyright (c) 2011, Creative Technology Ltd.
5 *
6 * Based on patch_ca0110.c
7 * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
8 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/pci.h>
28#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040029#include <linux/module.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070030#include <sound/core.h>
31#include "hda_codec.h"
32#include "hda_local.h"
33
34#define WIDGET_CHIP_CTRL 0x15
35#define WIDGET_DSP_CTRL 0x16
36
37#define WUH_MEM_CONNID 10
38#define DSP_MEM_CONNID 16
39
40enum hda_cmd_vendor_io {
41 /* for DspIO node */
42 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
43 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
44
45 VENDOR_DSPIO_STATUS = 0xF01,
46 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
47 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
48 VENDOR_DSPIO_DSP_INIT = 0x703,
49 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
50 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
51
52 /* for ChipIO node */
53 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
54 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
55 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
56 VENDOR_CHIPIO_DATA_LOW = 0x300,
57 VENDOR_CHIPIO_DATA_HIGH = 0x400,
58
59 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
60 VENDOR_CHIPIO_STATUS = 0xF01,
61 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
62 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
63
64 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
65
66 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
67 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
68 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
69 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
70 VENDOR_CHIPIO_FLAG_SET = 0x70F,
71 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
72 VENDOR_CHIPIO_PARAMETER_SET = 0x710,
73 VENDOR_CHIPIO_PARAMETER_GET = 0xF10,
74
75 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
76 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
77 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
78 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
79
80 VENDOR_CHIPIO_PARAMETER_EX_ID_GET = 0xF17,
81 VENDOR_CHIPIO_PARAMETER_EX_ID_SET = 0x717,
82 VENDOR_CHIPIO_PARAMETER_EX_VALUE_GET = 0xF18,
83 VENDOR_CHIPIO_PARAMETER_EX_VALUE_SET = 0x718
84};
85
86/*
87 * Control flag IDs
88 */
89enum control_flag_id {
90 /* Connection manager stream setup is bypassed/enabled */
91 CONTROL_FLAG_C_MGR = 0,
92 /* DSP DMA is bypassed/enabled */
93 CONTROL_FLAG_DMA = 1,
94 /* 8051 'idle' mode is disabled/enabled */
95 CONTROL_FLAG_IDLE_ENABLE = 2,
96 /* Tracker for the SPDIF-in path is bypassed/enabled */
97 CONTROL_FLAG_TRACKER = 3,
98 /* DigitalOut to Spdif2Out connection is disabled/enabled */
99 CONTROL_FLAG_SPDIF2OUT = 4,
100 /* Digital Microphone is disabled/enabled */
101 CONTROL_FLAG_DMIC = 5,
102 /* ADC_B rate is 48 kHz/96 kHz */
103 CONTROL_FLAG_ADC_B_96KHZ = 6,
104 /* ADC_C rate is 48 kHz/96 kHz */
105 CONTROL_FLAG_ADC_C_96KHZ = 7,
106 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
107 CONTROL_FLAG_DAC_96KHZ = 8,
108 /* DSP rate is 48 kHz/96 kHz */
109 CONTROL_FLAG_DSP_96KHZ = 9,
110 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
111 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
112 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
113 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
114 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
115 CONTROL_FLAG_DECODE_LOOP = 12,
116 /* De-emphasis filter on DAC-1 disabled/enabled */
117 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
118 /* De-emphasis filter on DAC-2 disabled/enabled */
119 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
120 /* De-emphasis filter on DAC-3 disabled/enabled */
121 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
122 /* High-pass filter on ADC_B disabled/enabled */
123 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
124 /* High-pass filter on ADC_C disabled/enabled */
125 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
126 /* Common mode on Port_A disabled/enabled */
127 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
128 /* Common mode on Port_D disabled/enabled */
129 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
130 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
131 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
132 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
133 CONTROL_FLAG_PORT_D_10K0HM_LOAD = 21,
134 /* ASI rate is 48kHz/96kHz */
135 CONTROL_FLAG_ASI_96KHZ = 22,
136 /* DAC power settings able to control attached ports no/yes */
137 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
138 /* Clock Stop OK reporting is disabled/enabled */
139 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
140 /* Number of control flags */
141 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
142};
143
144/*
145 * Control parameter IDs
146 */
147enum control_parameter_id {
148 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
149 CONTROL_PARAM_SPDIF1_SOURCE = 2,
150
151 /* Stream Control */
152
153 /* Select stream with the given ID */
154 CONTROL_PARAM_STREAM_ID = 24,
155 /* Source connection point for the selected stream */
156 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
157 /* Destination connection point for the selected stream */
158 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
159 /* Number of audio channels in the selected stream */
160 CONTROL_PARAM_STREAMS_CHANNELS = 27,
161 /*Enable control for the selected stream */
162 CONTROL_PARAM_STREAM_CONTROL = 28,
163
164 /* Connection Point Control */
165
166 /* Select connection point with the given ID */
167 CONTROL_PARAM_CONN_POINT_ID = 29,
168 /* Connection point sample rate */
169 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
170
171 /* Node Control */
172
173 /* Select HDA node with the given ID */
174 CONTROL_PARAM_NODE_ID = 31
175};
176
177/*
178 * Dsp Io Status codes
179 */
180enum hda_vendor_status_dspio {
181 /* Success */
182 VENDOR_STATUS_DSPIO_OK = 0x00,
183 /* Busy, unable to accept new command, the host must retry */
184 VENDOR_STATUS_DSPIO_BUSY = 0x01,
185 /* SCP command queue is full */
186 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
187 /* SCP response queue is empty */
188 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
189};
190
191/*
192 * Chip Io Status codes
193 */
194enum hda_vendor_status_chipio {
195 /* Success */
196 VENDOR_STATUS_CHIPIO_OK = 0x00,
197 /* Busy, unable to accept new command, the host must retry */
198 VENDOR_STATUS_CHIPIO_BUSY = 0x01
199};
200
201/*
202 * CA0132 sample rate
203 */
204enum ca0132_sample_rate {
205 SR_6_000 = 0x00,
206 SR_8_000 = 0x01,
207 SR_9_600 = 0x02,
208 SR_11_025 = 0x03,
209 SR_16_000 = 0x04,
210 SR_22_050 = 0x05,
211 SR_24_000 = 0x06,
212 SR_32_000 = 0x07,
213 SR_44_100 = 0x08,
214 SR_48_000 = 0x09,
215 SR_88_200 = 0x0A,
216 SR_96_000 = 0x0B,
217 SR_144_000 = 0x0C,
218 SR_176_400 = 0x0D,
219 SR_192_000 = 0x0E,
220 SR_384_000 = 0x0F,
221
222 SR_COUNT = 0x10,
223
224 SR_RATE_UNKNOWN = 0x1F
225};
226
227/*
228 * Scp Helper function
229 */
230enum get_set {
231 IS_SET = 0,
232 IS_GET = 1,
233};
234
235/*
236 * Duplicated from ca0110 codec
237 */
238
239static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
240{
241 if (pin) {
242 snd_hda_codec_write(codec, pin, 0,
243 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
244 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
245 snd_hda_codec_write(codec, pin, 0,
246 AC_VERB_SET_AMP_GAIN_MUTE,
247 AMP_OUT_UNMUTE);
248 }
249 if (dac)
250 snd_hda_codec_write(codec, dac, 0,
251 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
252}
253
254static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
255{
256 if (pin) {
257 snd_hda_codec_write(codec, pin, 0,
258 AC_VERB_SET_PIN_WIDGET_CONTROL,
259 PIN_VREF80);
260 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
261 snd_hda_codec_write(codec, pin, 0,
262 AC_VERB_SET_AMP_GAIN_MUTE,
263 AMP_IN_UNMUTE(0));
264 }
265 if (adc)
266 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
267 AMP_IN_UNMUTE(0));
268}
269
270static char *dirstr[2] = { "Playback", "Capture" };
271
272static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
273 int chan, int dir)
274{
275 char namestr[44];
276 int type = dir ? HDA_INPUT : HDA_OUTPUT;
277 struct snd_kcontrol_new knew =
278 HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
David Henningsson69b9b6d2012-08-20 11:17:00 +0200279 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_MUTE) == 0) {
280 snd_printdd("Skipping '%s %s Switch' (no mute on node 0x%x)\n", pfx, dirstr[dir], nid);
281 return 0;
282 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700283 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
284 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
285}
286
287static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
288 int chan, int dir)
289{
290 char namestr[44];
291 int type = dir ? HDA_INPUT : HDA_OUTPUT;
292 struct snd_kcontrol_new knew =
293 HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
David Henningsson69b9b6d2012-08-20 11:17:00 +0200294 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_NUM_STEPS) == 0) {
295 snd_printdd("Skipping '%s %s Volume' (no amp on node 0x%x)\n", pfx, dirstr[dir], nid);
296 return 0;
297 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700298 sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
299 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
300}
301
302#define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
303#define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
304#define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
305#define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
306#define add_mono_switch(codec, nid, pfx, chan) \
307 _add_switch(codec, nid, pfx, chan, 0)
308#define add_mono_volume(codec, nid, pfx, chan) \
309 _add_volume(codec, nid, pfx, chan, 0)
310#define add_in_mono_switch(codec, nid, pfx, chan) \
311 _add_switch(codec, nid, pfx, chan, 1)
312#define add_in_mono_volume(codec, nid, pfx, chan) \
313 _add_volume(codec, nid, pfx, chan, 1)
314
315
316/*
317 * CA0132 specific
318 */
319
320struct ca0132_spec {
321 struct auto_pin_cfg autocfg;
322 struct hda_multi_out multiout;
323 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
324 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
325 hda_nid_t hp_dac;
326 hda_nid_t input_pins[AUTO_PIN_LAST];
327 hda_nid_t adcs[AUTO_PIN_LAST];
328 hda_nid_t dig_out;
329 hda_nid_t dig_in;
330 unsigned int num_inputs;
331 long curr_hp_switch;
332 long curr_hp_volume[2];
333 long curr_speaker_switch;
334 struct mutex chipio_mutex;
335 const char *input_labels[AUTO_PIN_LAST];
336 struct hda_pcm pcm_rec[2]; /* PCM information */
337};
338
339/* Chip access helper function */
340static int chipio_send(struct hda_codec *codec,
341 unsigned int reg,
342 unsigned int data)
343{
344 unsigned int res;
345 int retry = 50;
346
347 /* send bits of data specified by reg */
348 do {
349 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
350 reg, data);
351 if (res == VENDOR_STATUS_CHIPIO_OK)
352 return 0;
353 } while (--retry);
354 return -EIO;
355}
356
357/*
358 * Write chip address through the vendor widget -- NOT protected by the Mutex!
359 */
360static int chipio_write_address(struct hda_codec *codec,
361 unsigned int chip_addx)
362{
363 int res;
364
365 /* send low 16 bits of the address */
366 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
367 chip_addx & 0xffff);
368
369 if (res != -EIO) {
370 /* send high 16 bits of the address */
371 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
372 chip_addx >> 16);
373 }
374
375 return res;
376}
377
378/*
379 * Write data through the vendor widget -- NOT protected by the Mutex!
380 */
381
382static int chipio_write_data(struct hda_codec *codec, unsigned int data)
383{
384 int res;
385
386 /* send low 16 bits of the data */
387 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
388
389 if (res != -EIO) {
390 /* send high 16 bits of the data */
391 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
392 data >> 16);
393 }
394
395 return res;
396}
397
398/*
399 * Read data through the vendor widget -- NOT protected by the Mutex!
400 */
401static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
402{
403 int res;
404
405 /* post read */
406 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
407
408 if (res != -EIO) {
409 /* read status */
410 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
411 }
412
413 if (res != -EIO) {
414 /* read data */
415 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
416 VENDOR_CHIPIO_HIC_READ_DATA,
417 0);
418 }
419
420 return res;
421}
422
423/*
424 * Write given value to the given address through the chip I/O widget.
425 * protected by the Mutex
426 */
427static int chipio_write(struct hda_codec *codec,
428 unsigned int chip_addx, const unsigned int data)
429{
430 struct ca0132_spec *spec = codec->spec;
431 int err;
432
433 mutex_lock(&spec->chipio_mutex);
434
435 /* write the address, and if successful proceed to write data */
436 err = chipio_write_address(codec, chip_addx);
437 if (err < 0)
438 goto exit;
439
440 err = chipio_write_data(codec, data);
441 if (err < 0)
442 goto exit;
443
444exit:
445 mutex_unlock(&spec->chipio_mutex);
446 return err;
447}
448
449/*
450 * Read the given address through the chip I/O widget
451 * protected by the Mutex
452 */
453static int chipio_read(struct hda_codec *codec,
454 unsigned int chip_addx, unsigned int *data)
455{
456 struct ca0132_spec *spec = codec->spec;
457 int err;
458
459 mutex_lock(&spec->chipio_mutex);
460
461 /* write the address, and if successful proceed to write data */
462 err = chipio_write_address(codec, chip_addx);
463 if (err < 0)
464 goto exit;
465
466 err = chipio_read_data(codec, data);
467 if (err < 0)
468 goto exit;
469
470exit:
471 mutex_unlock(&spec->chipio_mutex);
472 return err;
473}
474
475/*
476 * PCM stuffs
477 */
478static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
479 u32 stream_tag,
480 int channel_id, int format)
481{
482 unsigned int oldval, newval;
483
484 if (!nid)
485 return;
486
487 snd_printdd("ca0132_setup_stream: "
488 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
489 nid, stream_tag, channel_id, format);
490
491 /* update the format-id if changed */
492 oldval = snd_hda_codec_read(codec, nid, 0,
493 AC_VERB_GET_STREAM_FORMAT,
494 0);
495 if (oldval != format) {
496 msleep(20);
497 snd_hda_codec_write(codec, nid, 0,
498 AC_VERB_SET_STREAM_FORMAT,
499 format);
500 }
501
502 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
503 newval = (stream_tag << 4) | channel_id;
504 if (oldval != newval) {
505 snd_hda_codec_write(codec, nid, 0,
506 AC_VERB_SET_CHANNEL_STREAMID,
507 newval);
508 }
509}
510
511static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
512{
513 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
514 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
515}
516
517/*
518 * PCM callbacks
519 */
520static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
521 struct hda_codec *codec,
522 unsigned int stream_tag,
523 unsigned int format,
524 struct snd_pcm_substream *substream)
525{
526 struct ca0132_spec *spec = codec->spec;
527
528 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
529
530 return 0;
531}
532
533static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
534 struct hda_codec *codec,
535 struct snd_pcm_substream *substream)
536{
537 struct ca0132_spec *spec = codec->spec;
538
539 ca0132_cleanup_stream(codec, spec->dacs[0]);
540
541 return 0;
542}
543
544/*
545 * Digital out
546 */
547static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
548 struct hda_codec *codec,
549 unsigned int stream_tag,
550 unsigned int format,
551 struct snd_pcm_substream *substream)
552{
553 struct ca0132_spec *spec = codec->spec;
554
555 ca0132_setup_stream(codec, spec->dig_out, stream_tag, 0, format);
556
557 return 0;
558}
559
560static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
561 struct hda_codec *codec,
562 struct snd_pcm_substream *substream)
563{
564 struct ca0132_spec *spec = codec->spec;
565
566 ca0132_cleanup_stream(codec, spec->dig_out);
567
568 return 0;
569}
570
571/*
572 * Analog capture
573 */
574static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
575 struct hda_codec *codec,
576 unsigned int stream_tag,
577 unsigned int format,
578 struct snd_pcm_substream *substream)
579{
580 struct ca0132_spec *spec = codec->spec;
581
582 ca0132_setup_stream(codec, spec->adcs[substream->number],
583 stream_tag, 0, format);
584
585 return 0;
586}
587
588static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
589 struct hda_codec *codec,
590 struct snd_pcm_substream *substream)
591{
592 struct ca0132_spec *spec = codec->spec;
593
594 ca0132_cleanup_stream(codec, spec->adcs[substream->number]);
595
596 return 0;
597}
598
599/*
600 * Digital capture
601 */
602static int ca0132_dig_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
603 struct hda_codec *codec,
604 unsigned int stream_tag,
605 unsigned int format,
606 struct snd_pcm_substream *substream)
607{
608 struct ca0132_spec *spec = codec->spec;
609
610 ca0132_setup_stream(codec, spec->dig_in, stream_tag, 0, format);
611
612 return 0;
613}
614
615static int ca0132_dig_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
616 struct hda_codec *codec,
617 struct snd_pcm_substream *substream)
618{
619 struct ca0132_spec *spec = codec->spec;
620
621 ca0132_cleanup_stream(codec, spec->dig_in);
622
623 return 0;
624}
625
626/*
627 */
628static struct hda_pcm_stream ca0132_pcm_analog_playback = {
629 .substreams = 1,
630 .channels_min = 2,
631 .channels_max = 2,
632 .ops = {
633 .prepare = ca0132_playback_pcm_prepare,
634 .cleanup = ca0132_playback_pcm_cleanup
635 },
636};
637
638static struct hda_pcm_stream ca0132_pcm_analog_capture = {
639 .substreams = 1,
640 .channels_min = 2,
641 .channels_max = 2,
642 .ops = {
643 .prepare = ca0132_capture_pcm_prepare,
644 .cleanup = ca0132_capture_pcm_cleanup
645 },
646};
647
648static struct hda_pcm_stream ca0132_pcm_digital_playback = {
649 .substreams = 1,
650 .channels_min = 2,
651 .channels_max = 2,
652 .ops = {
653 .prepare = ca0132_dig_playback_pcm_prepare,
654 .cleanup = ca0132_dig_playback_pcm_cleanup
655 },
656};
657
658static struct hda_pcm_stream ca0132_pcm_digital_capture = {
659 .substreams = 1,
660 .channels_min = 2,
661 .channels_max = 2,
662 .ops = {
663 .prepare = ca0132_dig_capture_pcm_prepare,
664 .cleanup = ca0132_dig_capture_pcm_cleanup
665 },
666};
667
668static int ca0132_build_pcms(struct hda_codec *codec)
669{
670 struct ca0132_spec *spec = codec->spec;
671 struct hda_pcm *info = spec->pcm_rec;
672
673 codec->pcm_info = info;
674 codec->num_pcms = 0;
675
676 info->name = "CA0132 Analog";
677 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
678 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
679 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
680 spec->multiout.max_channels;
681 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
682 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
683 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
684 codec->num_pcms++;
685
686 if (!spec->dig_out && !spec->dig_in)
687 return 0;
688
689 info++;
690 info->name = "CA0132 Digital";
691 info->pcm_type = HDA_PCM_TYPE_SPDIF;
692 if (spec->dig_out) {
693 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
694 ca0132_pcm_digital_playback;
695 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
696 }
697 if (spec->dig_in) {
698 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
699 ca0132_pcm_digital_capture;
700 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
701 }
702 codec->num_pcms++;
703
704 return 0;
705}
706
707#define REG_CODEC_MUTE 0x18b014
708#define REG_CODEC_HP_VOL_L 0x18b070
709#define REG_CODEC_HP_VOL_R 0x18b074
710
711static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
712 struct snd_ctl_elem_value *ucontrol)
713{
714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
715 struct ca0132_spec *spec = codec->spec;
716 long *valp = ucontrol->value.integer.value;
717
718 *valp = spec->curr_hp_switch;
719 return 0;
720}
721
722static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_value *ucontrol)
724{
725 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
726 struct ca0132_spec *spec = codec->spec;
727 long *valp = ucontrol->value.integer.value;
728 unsigned int data;
729 int err;
730
731 /* any change? */
732 if (spec->curr_hp_switch == *valp)
733 return 0;
734
735 snd_hda_power_up(codec);
736
737 err = chipio_read(codec, REG_CODEC_MUTE, &data);
738 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100739 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700740
741 /* *valp 0 is mute, 1 is unmute */
742 data = (data & 0x7f) | (*valp ? 0 : 0x80);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100743 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700744 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100745 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700746
747 spec->curr_hp_switch = *valp;
748
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100749 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -0700750 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100751 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700752}
753
754static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
755 struct snd_ctl_elem_value *ucontrol)
756{
757 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
758 struct ca0132_spec *spec = codec->spec;
759 long *valp = ucontrol->value.integer.value;
760
761 *valp = spec->curr_speaker_switch;
762 return 0;
763}
764
765static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_value *ucontrol)
767{
768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
769 struct ca0132_spec *spec = codec->spec;
770 long *valp = ucontrol->value.integer.value;
771 unsigned int data;
772 int err;
773
774 /* any change? */
775 if (spec->curr_speaker_switch == *valp)
776 return 0;
777
778 snd_hda_power_up(codec);
779
780 err = chipio_read(codec, REG_CODEC_MUTE, &data);
781 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100782 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700783
784 /* *valp 0 is mute, 1 is unmute */
785 data = (data & 0xef) | (*valp ? 0 : 0x10);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100786 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700787 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100788 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700789
790 spec->curr_speaker_switch = *valp;
791
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100792 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -0700793 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100794 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700795}
796
797static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_value *ucontrol)
799{
800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
801 struct ca0132_spec *spec = codec->spec;
802 long *valp = ucontrol->value.integer.value;
803
804 *valp++ = spec->curr_hp_volume[0];
805 *valp = spec->curr_hp_volume[1];
806 return 0;
807}
808
809static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
810 struct snd_ctl_elem_value *ucontrol)
811{
812 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
813 struct ca0132_spec *spec = codec->spec;
814 long *valp = ucontrol->value.integer.value;
815 long left_vol, right_vol;
816 unsigned int data;
817 int val;
818 int err;
819
820 left_vol = *valp++;
821 right_vol = *valp;
822
823 /* any change? */
824 if ((spec->curr_hp_volume[0] == left_vol) &&
825 (spec->curr_hp_volume[1] == right_vol))
826 return 0;
827
828 snd_hda_power_up(codec);
829
830 err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
831 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100832 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700833
834 val = 31 - left_vol;
835 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100836 err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700837 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100838 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700839
840 val = 31 - right_vol;
841 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100842 err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700843 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100844 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700845
846 spec->curr_hp_volume[0] = left_vol;
847 spec->curr_hp_volume[1] = right_vol;
848
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100849 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -0700850 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100851 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700852}
853
854static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
855{
856 struct snd_kcontrol_new knew =
857 HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
858 nid, 1, 0, HDA_OUTPUT);
859 knew.get = ca0132_hp_switch_get;
860 knew.put = ca0132_hp_switch_put;
861 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
862}
863
864static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
865{
866 struct snd_kcontrol_new knew =
867 HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
868 nid, 3, 0, HDA_OUTPUT);
869 knew.get = ca0132_hp_volume_get;
870 knew.put = ca0132_hp_volume_put;
871 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
872}
873
874static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
875{
876 struct snd_kcontrol_new knew =
877 HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
878 nid, 1, 0, HDA_OUTPUT);
879 knew.get = ca0132_speaker_switch_get;
880 knew.put = ca0132_speaker_switch_put;
881 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
882}
883
884static void ca0132_fix_hp_caps(struct hda_codec *codec)
885{
886 struct ca0132_spec *spec = codec->spec;
887 struct auto_pin_cfg *cfg = &spec->autocfg;
888 unsigned int caps;
889
890 /* set mute-capable, 1db step, 32 steps, ofs 6 */
891 caps = 0x80031f06;
892 snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
893}
894
895static int ca0132_build_controls(struct hda_codec *codec)
896{
897 struct ca0132_spec *spec = codec->spec;
898 struct auto_pin_cfg *cfg = &spec->autocfg;
899 int i, err;
900
901 if (spec->multiout.num_dacs) {
902 err = add_speaker_switch(codec, spec->out_pins[0]);
903 if (err < 0)
904 return err;
905 }
906
907 if (cfg->hp_outs) {
908 ca0132_fix_hp_caps(codec);
909 err = add_hp_switch(codec, cfg->hp_pins[0]);
910 if (err < 0)
911 return err;
912 err = add_hp_volume(codec, cfg->hp_pins[0]);
913 if (err < 0)
914 return err;
915 }
916
917 for (i = 0; i < spec->num_inputs; i++) {
918 const char *label = spec->input_labels[i];
919
920 err = add_in_switch(codec, spec->adcs[i], label);
921 if (err < 0)
922 return err;
923 err = add_in_volume(codec, spec->adcs[i], label);
924 if (err < 0)
925 return err;
926 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
927 /* add Mic-Boost */
928 err = add_in_mono_volume(codec, spec->input_pins[i],
929 "Mic Boost", 1);
930 if (err < 0)
931 return err;
932 }
933 }
934
935 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +0200936 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
937 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700938 if (err < 0)
939 return err;
940 err = add_out_volume(codec, spec->dig_out, "IEC958");
941 if (err < 0)
942 return err;
943 }
944
945 if (spec->dig_in) {
946 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
947 if (err < 0)
948 return err;
949 err = add_in_volume(codec, spec->dig_in, "IEC958");
Takashi Iwaib97f6bf2012-02-07 11:00:53 +0100950 if (err < 0)
951 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700952 }
953 return 0;
954}
955
956
957static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
958{
959 /* Set Creative extension */
960 snd_printdd("SET CREATIVE EXTENSION\n");
961 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
962 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
963 enable);
964 msleep(20);
965}
966
967
968static void ca0132_config(struct hda_codec *codec)
969{
970 struct ca0132_spec *spec = codec->spec;
971 struct auto_pin_cfg *cfg = &spec->autocfg;
972
973 /* line-outs */
974 cfg->line_outs = 1;
975 cfg->line_out_pins[0] = 0x0b; /* front */
976 cfg->line_out_type = AUTO_PIN_LINE_OUT;
977
978 spec->dacs[0] = 0x02;
979 spec->out_pins[0] = 0x0b;
980 spec->multiout.dac_nids = spec->dacs;
981 spec->multiout.num_dacs = 1;
982 spec->multiout.max_channels = 2;
983
984 /* headphone */
985 cfg->hp_outs = 1;
986 cfg->hp_pins[0] = 0x0f;
987
988 spec->hp_dac = 0;
989 spec->multiout.hp_nid = 0;
990
991 /* inputs */
992 cfg->num_inputs = 2; /* Mic-in and line-in */
993 cfg->inputs[0].pin = 0x12;
994 cfg->inputs[0].type = AUTO_PIN_MIC;
995 cfg->inputs[1].pin = 0x11;
996 cfg->inputs[1].type = AUTO_PIN_LINE_IN;
997
998 /* Mic-in */
999 spec->input_pins[0] = 0x12;
1000 spec->input_labels[0] = "Mic-In";
1001 spec->adcs[0] = 0x07;
1002
1003 /* Line-In */
1004 spec->input_pins[1] = 0x11;
1005 spec->input_labels[1] = "Line-In";
1006 spec->adcs[1] = 0x08;
1007 spec->num_inputs = 2;
1008}
1009
1010static void ca0132_init_chip(struct hda_codec *codec)
1011{
1012 struct ca0132_spec *spec = codec->spec;
1013
1014 mutex_init(&spec->chipio_mutex);
1015}
1016
1017static void ca0132_exit_chip(struct hda_codec *codec)
1018{
1019 /* put any chip cleanup stuffs here. */
1020}
1021
1022static int ca0132_init(struct hda_codec *codec)
1023{
1024 struct ca0132_spec *spec = codec->spec;
1025 struct auto_pin_cfg *cfg = &spec->autocfg;
1026 int i;
1027
1028 for (i = 0; i < spec->multiout.num_dacs; i++) {
1029 init_output(codec, spec->out_pins[i],
1030 spec->multiout.dac_nids[i]);
1031 }
1032 init_output(codec, cfg->hp_pins[0], spec->hp_dac);
1033 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
1034
1035 for (i = 0; i < spec->num_inputs; i++)
1036 init_input(codec, spec->input_pins[i], spec->adcs[i]);
1037
1038 init_input(codec, cfg->dig_in_pin, spec->dig_in);
1039
1040 ca0132_set_ct_ext(codec, 1);
1041
1042 return 0;
1043}
1044
1045
1046static void ca0132_free(struct hda_codec *codec)
1047{
1048 ca0132_set_ct_ext(codec, 0);
1049 ca0132_exit_chip(codec);
1050 kfree(codec->spec);
1051}
1052
1053static struct hda_codec_ops ca0132_patch_ops = {
1054 .build_controls = ca0132_build_controls,
1055 .build_pcms = ca0132_build_pcms,
1056 .init = ca0132_init,
1057 .free = ca0132_free,
1058};
1059
1060
1061
1062static int patch_ca0132(struct hda_codec *codec)
1063{
1064 struct ca0132_spec *spec;
1065
1066 snd_printdd("patch_ca0132\n");
1067
1068 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1069 if (!spec)
1070 return -ENOMEM;
1071 codec->spec = spec;
1072
1073 ca0132_init_chip(codec);
1074
1075 ca0132_config(codec);
1076
1077 codec->patch_ops = ca0132_patch_ops;
1078
1079 return 0;
1080}
1081
1082/*
1083 * patch entries
1084 */
1085static struct hda_codec_preset snd_hda_preset_ca0132[] = {
1086 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
1087 {} /* terminator */
1088};
1089
1090MODULE_ALIAS("snd-hda-codec-id:11020011");
1091
1092MODULE_LICENSE("GPL");
1093MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
1094
1095static struct hda_codec_preset_list ca0132_list = {
1096 .preset = snd_hda_preset_ca0132,
1097 .owner = THIS_MODULE,
1098};
1099
1100static int __init patch_ca0132_init(void)
1101{
1102 return snd_hda_add_codec_preset(&ca0132_list);
1103}
1104
1105static void __exit patch_ca0132_exit(void)
1106{
1107 snd_hda_delete_codec_preset(&ca0132_list);
1108}
1109
1110module_init(patch_ca0132_init)
1111module_exit(patch_ca0132_exit)