blob: 77903a398289c203f802df61db7695bf9b97a19e [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 Minett4aa3bb02012-09-20 20:29:15 -070030#include <linux/firmware.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020034#include "hda_auto_parser.h"
Ian Minett5aaca442012-12-20 18:53:34 -080035#include "hda_jack.h"
Ian Minett95c6e9c2011-06-15 15:35:17 -070036
Ian Minettbcd109c2012-09-20 20:29:14 -070037#include "ca0132_regs.h"
38
Ian Minettef6b2ea2012-12-20 18:53:33 -080039/* Enable this to see controls for tuning purpose. */
40/*#define ENABLE_TUNING_CONTROLS*/
41
42#define FLOAT_ZERO 0x00000000
43#define FLOAT_ONE 0x3f800000
44#define FLOAT_TWO 0x40000000
45#define FLOAT_MINUS_5 0xc0a00000
46
47#define UNSOL_TAG_HP 0x10
48#define UNSOL_TAG_AMIC1 0x12
49#define UNSOL_TAG_DSP 0x16
50
Ian Minett4aa3bb02012-09-20 20:29:15 -070051#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
52#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
53
54#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
55#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
56#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
57
58#define MASTERCONTROL 0x80
Ian Minettef6b2ea2012-12-20 18:53:33 -080059#define MASTERCONTROL_ALLOC_DMA_CHAN 10
60#define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
Ian Minett4aa3bb02012-09-20 20:29:15 -070061
Ian Minett95c6e9c2011-06-15 15:35:17 -070062#define WIDGET_CHIP_CTRL 0x15
63#define WIDGET_DSP_CTRL 0x16
64
Ian Minett4aa3bb02012-09-20 20:29:15 -070065#define MEM_CONNID_MICIN1 3
66#define MEM_CONNID_MICIN2 5
67#define MEM_CONNID_MICOUT1 12
68#define MEM_CONNID_MICOUT2 14
69#define MEM_CONNID_WUH 10
70#define MEM_CONNID_DSP 16
71#define MEM_CONNID_DMIC 100
72
73#define SCP_SET 0
74#define SCP_GET 1
75
Ian Minett01ef7db2012-09-20 20:29:16 -070076#define EFX_FILE "ctefx.bin"
77
78MODULE_FIRMWARE(EFX_FILE);
79
Ian Minettef6b2ea2012-12-20 18:53:33 -080080static char *dirstr[2] = { "Playback", "Capture" };
81
82enum {
83 SPEAKER_OUT,
84 HEADPHONE_OUT
85};
86
87enum {
88 DIGITAL_MIC,
89 LINE_MIC_IN
90};
91
92enum {
93#define VNODE_START_NID 0x80
94 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
95 VNID_MIC,
96 VNID_HP_SEL,
97 VNID_AMIC1_SEL,
98 VNID_HP_ASEL,
99 VNID_AMIC1_ASEL,
100 VNODE_END_NID,
101#define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
102
103#define EFFECT_START_NID 0x90
104#define OUT_EFFECT_START_NID EFFECT_START_NID
105 SURROUND = OUT_EFFECT_START_NID,
106 CRYSTALIZER,
107 DIALOG_PLUS,
108 SMART_VOLUME,
109 X_BASS,
110 EQUALIZER,
111 OUT_EFFECT_END_NID,
112#define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
113
114#define IN_EFFECT_START_NID OUT_EFFECT_END_NID
115 ECHO_CANCELLATION = IN_EFFECT_START_NID,
116 VOICE_FOCUS,
117 MIC_SVM,
118 NOISE_REDUCTION,
119 IN_EFFECT_END_NID,
120#define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
121
122 VOICEFX = IN_EFFECT_END_NID,
123 PLAY_ENHANCEMENT,
124 CRYSTAL_VOICE,
125 EFFECT_END_NID
126#define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
127};
128
129/* Effects values size*/
130#define EFFECT_VALS_MAX_COUNT 12
131
132struct ct_effect {
133 char name[44];
134 hda_nid_t nid;
135 int mid; /*effect module ID*/
136 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
137 int direct; /* 0:output; 1:input*/
138 int params; /* number of default non-on/off params */
139 /*effect default values, 1st is on/off. */
140 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
141};
142
143#define EFX_DIR_OUT 0
144#define EFX_DIR_IN 1
145
146static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
147 { .name = "Surround",
148 .nid = SURROUND,
149 .mid = 0x96,
150 .reqs = {0, 1},
151 .direct = EFX_DIR_OUT,
152 .params = 1,
153 .def_vals = {0x3F800000, 0x3F2B851F}
154 },
155 { .name = "Crystalizer",
156 .nid = CRYSTALIZER,
157 .mid = 0x96,
158 .reqs = {7, 8},
159 .direct = EFX_DIR_OUT,
160 .params = 1,
161 .def_vals = {0x3F800000, 0x3F266666}
162 },
163 { .name = "Dialog Plus",
164 .nid = DIALOG_PLUS,
165 .mid = 0x96,
166 .reqs = {2, 3},
167 .direct = EFX_DIR_OUT,
168 .params = 1,
169 .def_vals = {0x00000000, 0x3F000000}
170 },
171 { .name = "Smart Volume",
172 .nid = SMART_VOLUME,
173 .mid = 0x96,
174 .reqs = {4, 5, 6},
175 .direct = EFX_DIR_OUT,
176 .params = 2,
177 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
178 },
179 { .name = "X-Bass",
180 .nid = X_BASS,
181 .mid = 0x96,
182 .reqs = {24, 23, 25},
183 .direct = EFX_DIR_OUT,
184 .params = 2,
185 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
186 },
187 { .name = "Equalizer",
188 .nid = EQUALIZER,
189 .mid = 0x96,
190 .reqs = {9, 10, 11, 12, 13, 14,
191 15, 16, 17, 18, 19, 20},
192 .direct = EFX_DIR_OUT,
193 .params = 11,
194 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
195 0x00000000, 0x00000000, 0x00000000, 0x00000000,
196 0x00000000, 0x00000000, 0x00000000, 0x00000000}
197 },
198 { .name = "Echo Cancellation",
199 .nid = ECHO_CANCELLATION,
200 .mid = 0x95,
201 .reqs = {0, 1, 2, 3},
202 .direct = EFX_DIR_IN,
203 .params = 3,
204 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
205 },
206 { .name = "Voice Focus",
207 .nid = VOICE_FOCUS,
208 .mid = 0x95,
209 .reqs = {6, 7, 8, 9},
210 .direct = EFX_DIR_IN,
211 .params = 3,
212 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
213 },
214 { .name = "Mic SVM",
215 .nid = MIC_SVM,
216 .mid = 0x95,
217 .reqs = {44, 45},
218 .direct = EFX_DIR_IN,
219 .params = 1,
220 .def_vals = {0x00000000, 0x3F3D70A4}
221 },
222 { .name = "Noise Reduction",
223 .nid = NOISE_REDUCTION,
224 .mid = 0x95,
225 .reqs = {4, 5},
226 .direct = EFX_DIR_IN,
227 .params = 1,
228 .def_vals = {0x3F800000, 0x3F000000}
229 },
230 { .name = "VoiceFX",
231 .nid = VOICEFX,
232 .mid = 0x95,
233 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
234 .direct = EFX_DIR_IN,
235 .params = 8,
236 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
237 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
238 0x00000000}
239 }
240};
241
242/* Tuning controls */
243#ifdef ENABLE_TUNING_CONTROLS
244
245enum {
246#define TUNING_CTL_START_NID 0xC0
247 WEDGE_ANGLE = TUNING_CTL_START_NID,
248 SVM_LEVEL,
249 EQUALIZER_BAND_0,
250 EQUALIZER_BAND_1,
251 EQUALIZER_BAND_2,
252 EQUALIZER_BAND_3,
253 EQUALIZER_BAND_4,
254 EQUALIZER_BAND_5,
255 EQUALIZER_BAND_6,
256 EQUALIZER_BAND_7,
257 EQUALIZER_BAND_8,
258 EQUALIZER_BAND_9,
259 TUNING_CTL_END_NID
260#define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
261};
262
263struct ct_tuning_ctl {
264 char name[44];
265 hda_nid_t parent_nid;
266 hda_nid_t nid;
267 int mid; /*effect module ID*/
268 int req; /*effect module request*/
269 int direct; /* 0:output; 1:input*/
270 unsigned int def_val;/*effect default values*/
271};
272
273static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
274 { .name = "Wedge Angle",
275 .parent_nid = VOICE_FOCUS,
276 .nid = WEDGE_ANGLE,
277 .mid = 0x95,
278 .req = 8,
279 .direct = EFX_DIR_IN,
280 .def_val = 0x41F00000
281 },
282 { .name = "SVM Level",
283 .parent_nid = MIC_SVM,
284 .nid = SVM_LEVEL,
285 .mid = 0x95,
286 .req = 45,
287 .direct = EFX_DIR_IN,
288 .def_val = 0x3F3D70A4
289 },
290 { .name = "EQ Band0",
291 .parent_nid = EQUALIZER,
292 .nid = EQUALIZER_BAND_0,
293 .mid = 0x96,
294 .req = 11,
295 .direct = EFX_DIR_OUT,
296 .def_val = 0x00000000
297 },
298 { .name = "EQ Band1",
299 .parent_nid = EQUALIZER,
300 .nid = EQUALIZER_BAND_1,
301 .mid = 0x96,
302 .req = 12,
303 .direct = EFX_DIR_OUT,
304 .def_val = 0x00000000
305 },
306 { .name = "EQ Band2",
307 .parent_nid = EQUALIZER,
308 .nid = EQUALIZER_BAND_2,
309 .mid = 0x96,
310 .req = 13,
311 .direct = EFX_DIR_OUT,
312 .def_val = 0x00000000
313 },
314 { .name = "EQ Band3",
315 .parent_nid = EQUALIZER,
316 .nid = EQUALIZER_BAND_3,
317 .mid = 0x96,
318 .req = 14,
319 .direct = EFX_DIR_OUT,
320 .def_val = 0x00000000
321 },
322 { .name = "EQ Band4",
323 .parent_nid = EQUALIZER,
324 .nid = EQUALIZER_BAND_4,
325 .mid = 0x96,
326 .req = 15,
327 .direct = EFX_DIR_OUT,
328 .def_val = 0x00000000
329 },
330 { .name = "EQ Band5",
331 .parent_nid = EQUALIZER,
332 .nid = EQUALIZER_BAND_5,
333 .mid = 0x96,
334 .req = 16,
335 .direct = EFX_DIR_OUT,
336 .def_val = 0x00000000
337 },
338 { .name = "EQ Band6",
339 .parent_nid = EQUALIZER,
340 .nid = EQUALIZER_BAND_6,
341 .mid = 0x96,
342 .req = 17,
343 .direct = EFX_DIR_OUT,
344 .def_val = 0x00000000
345 },
346 { .name = "EQ Band7",
347 .parent_nid = EQUALIZER,
348 .nid = EQUALIZER_BAND_7,
349 .mid = 0x96,
350 .req = 18,
351 .direct = EFX_DIR_OUT,
352 .def_val = 0x00000000
353 },
354 { .name = "EQ Band8",
355 .parent_nid = EQUALIZER,
356 .nid = EQUALIZER_BAND_8,
357 .mid = 0x96,
358 .req = 19,
359 .direct = EFX_DIR_OUT,
360 .def_val = 0x00000000
361 },
362 { .name = "EQ Band9",
363 .parent_nid = EQUALIZER,
364 .nid = EQUALIZER_BAND_9,
365 .mid = 0x96,
366 .req = 20,
367 .direct = EFX_DIR_OUT,
368 .def_val = 0x00000000
369 }
370};
371#endif
372
373/* Voice FX Presets */
374#define VOICEFX_MAX_PARAM_COUNT 9
375
376struct ct_voicefx {
377 char *name;
378 hda_nid_t nid;
379 int mid;
380 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
381};
382
383struct ct_voicefx_preset {
384 char *name; /*preset name*/
385 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
386};
387
388struct ct_voicefx ca0132_voicefx = {
389 .name = "VoiceFX Capture Switch",
390 .nid = VOICEFX,
391 .mid = 0x95,
392 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
393};
394
395struct ct_voicefx_preset ca0132_voicefx_presets[] = {
396 { .name = "Neutral",
397 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
398 0x44FA0000, 0x3F800000, 0x3F800000,
399 0x3F800000, 0x00000000, 0x00000000 }
400 },
401 { .name = "Female2Male",
402 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
403 0x44FA0000, 0x3F19999A, 0x3F866666,
404 0x3F800000, 0x00000000, 0x00000000 }
405 },
406 { .name = "Male2Female",
407 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
408 0x450AC000, 0x4017AE14, 0x3F6B851F,
409 0x3F800000, 0x00000000, 0x00000000 }
410 },
411 { .name = "ScrappyKid",
412 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
413 0x44FA0000, 0x40400000, 0x3F28F5C3,
414 0x3F800000, 0x00000000, 0x00000000 }
415 },
416 { .name = "Elderly",
417 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
418 0x44E10000, 0x3FB33333, 0x3FB9999A,
419 0x3F800000, 0x3E3A2E43, 0x00000000 }
420 },
421 { .name = "Orc",
422 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
423 0x45098000, 0x3F266666, 0x3FC00000,
424 0x3F800000, 0x00000000, 0x00000000 }
425 },
426 { .name = "Elf",
427 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
428 0x45193000, 0x3F8E147B, 0x3F75C28F,
429 0x3F800000, 0x00000000, 0x00000000 }
430 },
431 { .name = "Dwarf",
432 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
433 0x45007000, 0x3F451EB8, 0x3F7851EC,
434 0x3F800000, 0x00000000, 0x00000000 }
435 },
436 { .name = "AlienBrute",
437 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
438 0x451F6000, 0x3F266666, 0x3FA7D945,
439 0x3F800000, 0x3CF5C28F, 0x00000000 }
440 },
441 { .name = "Robot",
442 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
443 0x44FA0000, 0x3FB2718B, 0x3F800000,
444 0xBC07010E, 0x00000000, 0x00000000 }
445 },
446 { .name = "Marine",
447 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
448 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
449 0x3F0A3D71, 0x00000000, 0x00000000 }
450 },
451 { .name = "Emo",
452 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
453 0x44FA0000, 0x3F800000, 0x3F800000,
454 0x3E4CCCCD, 0x00000000, 0x00000000 }
455 },
456 { .name = "DeepVoice",
457 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
458 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
459 0x3F800000, 0x00000000, 0x00000000 }
460 },
461 { .name = "Munchkin",
462 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
463 0x44FA0000, 0x3F800000, 0x3F1A043C,
464 0x3F800000, 0x00000000, 0x00000000 }
465 }
466};
467
Ian Minett95c6e9c2011-06-15 15:35:17 -0700468enum hda_cmd_vendor_io {
469 /* for DspIO node */
470 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
471 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
472
473 VENDOR_DSPIO_STATUS = 0xF01,
474 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
475 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
476 VENDOR_DSPIO_DSP_INIT = 0x703,
477 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
478 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
479
480 /* for ChipIO node */
481 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
482 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
483 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
484 VENDOR_CHIPIO_DATA_LOW = 0x300,
485 VENDOR_CHIPIO_DATA_HIGH = 0x400,
486
487 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
488 VENDOR_CHIPIO_STATUS = 0xF01,
489 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
490 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
491
Ian Minett4aa3bb02012-09-20 20:29:15 -0700492 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
493 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
494
Ian Minett95c6e9c2011-06-15 15:35:17 -0700495 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700496 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700497
498 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
499 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
500 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
501 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
502 VENDOR_CHIPIO_FLAG_SET = 0x70F,
503 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700504 VENDOR_CHIPIO_PARAM_SET = 0x710,
505 VENDOR_CHIPIO_PARAM_GET = 0xF10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700506
507 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
508 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
509 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
510 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
511
Ian Minett4aa3bb02012-09-20 20:29:15 -0700512 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
513 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
514 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
515 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
516
517 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
518 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
519 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
520 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
521 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
522 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
523
524 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
Ian Minett95c6e9c2011-06-15 15:35:17 -0700525};
526
527/*
528 * Control flag IDs
529 */
530enum control_flag_id {
531 /* Connection manager stream setup is bypassed/enabled */
532 CONTROL_FLAG_C_MGR = 0,
533 /* DSP DMA is bypassed/enabled */
534 CONTROL_FLAG_DMA = 1,
535 /* 8051 'idle' mode is disabled/enabled */
536 CONTROL_FLAG_IDLE_ENABLE = 2,
537 /* Tracker for the SPDIF-in path is bypassed/enabled */
538 CONTROL_FLAG_TRACKER = 3,
539 /* DigitalOut to Spdif2Out connection is disabled/enabled */
540 CONTROL_FLAG_SPDIF2OUT = 4,
541 /* Digital Microphone is disabled/enabled */
542 CONTROL_FLAG_DMIC = 5,
543 /* ADC_B rate is 48 kHz/96 kHz */
544 CONTROL_FLAG_ADC_B_96KHZ = 6,
545 /* ADC_C rate is 48 kHz/96 kHz */
546 CONTROL_FLAG_ADC_C_96KHZ = 7,
547 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
548 CONTROL_FLAG_DAC_96KHZ = 8,
549 /* DSP rate is 48 kHz/96 kHz */
550 CONTROL_FLAG_DSP_96KHZ = 9,
551 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
552 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
553 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
554 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
555 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
556 CONTROL_FLAG_DECODE_LOOP = 12,
557 /* De-emphasis filter on DAC-1 disabled/enabled */
558 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
559 /* De-emphasis filter on DAC-2 disabled/enabled */
560 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
561 /* De-emphasis filter on DAC-3 disabled/enabled */
562 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
563 /* High-pass filter on ADC_B disabled/enabled */
564 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
565 /* High-pass filter on ADC_C disabled/enabled */
566 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
567 /* Common mode on Port_A disabled/enabled */
568 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
569 /* Common mode on Port_D disabled/enabled */
570 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
571 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
572 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
573 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700574 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700575 /* ASI rate is 48kHz/96kHz */
576 CONTROL_FLAG_ASI_96KHZ = 22,
577 /* DAC power settings able to control attached ports no/yes */
578 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
579 /* Clock Stop OK reporting is disabled/enabled */
580 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
581 /* Number of control flags */
582 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
583};
584
585/*
586 * Control parameter IDs
587 */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700588enum control_param_id {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800589 /* 0: None, 1: Mic1In*/
590 CONTROL_PARAM_VIP_SOURCE = 1,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700591 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
592 CONTROL_PARAM_SPDIF1_SOURCE = 2,
Ian Minettef6b2ea2012-12-20 18:53:33 -0800593 /* Port A output stage gain setting to use when 16 Ohm output
594 * impedance is selected*/
595 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
596 /* Port D output stage gain setting to use when 16 Ohm output
597 * impedance is selected*/
598 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700599
600 /* Stream Control */
601
602 /* Select stream with the given ID */
603 CONTROL_PARAM_STREAM_ID = 24,
604 /* Source connection point for the selected stream */
605 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
606 /* Destination connection point for the selected stream */
607 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
608 /* Number of audio channels in the selected stream */
609 CONTROL_PARAM_STREAMS_CHANNELS = 27,
610 /*Enable control for the selected stream */
611 CONTROL_PARAM_STREAM_CONTROL = 28,
612
613 /* Connection Point Control */
614
615 /* Select connection point with the given ID */
616 CONTROL_PARAM_CONN_POINT_ID = 29,
617 /* Connection point sample rate */
618 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
619
620 /* Node Control */
621
622 /* Select HDA node with the given ID */
623 CONTROL_PARAM_NODE_ID = 31
624};
625
626/*
627 * Dsp Io Status codes
628 */
629enum hda_vendor_status_dspio {
630 /* Success */
631 VENDOR_STATUS_DSPIO_OK = 0x00,
632 /* Busy, unable to accept new command, the host must retry */
633 VENDOR_STATUS_DSPIO_BUSY = 0x01,
634 /* SCP command queue is full */
635 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
636 /* SCP response queue is empty */
637 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
638};
639
640/*
641 * Chip Io Status codes
642 */
643enum hda_vendor_status_chipio {
644 /* Success */
645 VENDOR_STATUS_CHIPIO_OK = 0x00,
646 /* Busy, unable to accept new command, the host must retry */
647 VENDOR_STATUS_CHIPIO_BUSY = 0x01
648};
649
650/*
651 * CA0132 sample rate
652 */
653enum ca0132_sample_rate {
654 SR_6_000 = 0x00,
655 SR_8_000 = 0x01,
656 SR_9_600 = 0x02,
657 SR_11_025 = 0x03,
658 SR_16_000 = 0x04,
659 SR_22_050 = 0x05,
660 SR_24_000 = 0x06,
661 SR_32_000 = 0x07,
662 SR_44_100 = 0x08,
663 SR_48_000 = 0x09,
664 SR_88_200 = 0x0A,
665 SR_96_000 = 0x0B,
666 SR_144_000 = 0x0C,
667 SR_176_400 = 0x0D,
668 SR_192_000 = 0x0E,
669 SR_384_000 = 0x0F,
670
671 SR_COUNT = 0x10,
672
673 SR_RATE_UNKNOWN = 0x1F
674};
675
Ian Minett95c6e9c2011-06-15 15:35:17 -0700676static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
677{
678 if (pin) {
Ian Minett5aaca442012-12-20 18:53:34 -0800679 snd_hda_codec_write(codec, pin, 0,
680 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700681 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
682 snd_hda_codec_write(codec, pin, 0,
683 AC_VERB_SET_AMP_GAIN_MUTE,
684 AMP_OUT_UNMUTE);
685 }
Takashi Iwai8e13fc12012-08-08 17:26:54 +0200686 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
Ian Minett95c6e9c2011-06-15 15:35:17 -0700687 snd_hda_codec_write(codec, dac, 0,
688 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
689}
690
691static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
692{
693 if (pin) {
Ian Minett5aaca442012-12-20 18:53:34 -0800694 snd_hda_codec_write(codec, pin, 0,
695 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700696 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
697 snd_hda_codec_write(codec, pin, 0,
698 AC_VERB_SET_AMP_GAIN_MUTE,
699 AMP_IN_UNMUTE(0));
700 }
Ian Minett5aaca442012-12-20 18:53:34 -0800701 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
Ian Minett95c6e9c2011-06-15 15:35:17 -0700702 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
703 AMP_IN_UNMUTE(0));
Ian Minett5aaca442012-12-20 18:53:34 -0800704
705 /* init to 0 dB and unmute. */
706 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
707 HDA_AMP_VOLMASK, 0x5a);
708 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
709 HDA_AMP_MUTE, 0);
710 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700711}
712
Ian Minett01ef7db2012-09-20 20:29:16 -0700713enum dsp_download_state {
714 DSP_DOWNLOAD_FAILED = -1,
715 DSP_DOWNLOAD_INIT = 0,
716 DSP_DOWNLOADING = 1,
717 DSP_DOWNLOADED = 2
718};
719
Ian Minett01ef7db2012-09-20 20:29:16 -0700720/* retrieve parameters from hda format */
721#define get_hdafmt_chs(fmt) (fmt & 0xf)
722#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
723#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
724#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700725
726/*
727 * CA0132 specific
728 */
729
730struct ca0132_spec {
Ian Minetta7e76272012-12-20 18:53:35 -0800731 struct snd_kcontrol_new *mixers[5];
732 unsigned int num_mixers;
Ian Minett5aaca442012-12-20 18:53:34 -0800733 const struct hda_verb *base_init_verbs;
734 const struct hda_verb *base_exit_verbs;
735 const struct hda_verb *init_verbs[5];
736 unsigned int num_init_verbs; /* exclude base init verbs */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700737 struct auto_pin_cfg autocfg;
Ian Minett5aaca442012-12-20 18:53:34 -0800738
739 /* Nodes configurations */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700740 struct hda_multi_out multiout;
741 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
742 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
Ian Minett5aaca442012-12-20 18:53:34 -0800743 unsigned int num_outputs;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700744 hda_nid_t input_pins[AUTO_PIN_LAST];
745 hda_nid_t adcs[AUTO_PIN_LAST];
746 hda_nid_t dig_out;
747 hda_nid_t dig_in;
748 unsigned int num_inputs;
Ian Minetta7e76272012-12-20 18:53:35 -0800749 hda_nid_t shared_mic_nid;
750 hda_nid_t shared_out_nid;
Ian Minett5aaca442012-12-20 18:53:34 -0800751 struct hda_pcm pcm_rec[5]; /* PCM information */
Ian Minett01ef7db2012-09-20 20:29:16 -0700752
753 /* chip access */
754 struct mutex chipio_mutex; /* chip access mutex */
755 u32 curr_chip_addx;
756
757 /* DSP download related */
758 enum dsp_download_state dsp_state;
759 unsigned int dsp_stream_id;
760 unsigned int wait_scp;
761 unsigned int wait_scp_header;
762 unsigned int wait_num_data;
763 unsigned int scp_resp_header;
764 unsigned int scp_resp_data[4];
765 unsigned int scp_resp_count;
Ian Minett5aaca442012-12-20 18:53:34 -0800766
767 /* mixer and effects related */
768 unsigned char dmic_ctl;
769 int cur_out_type;
770 int cur_mic_type;
771 long vnode_lvol[VNODES_COUNT];
772 long vnode_rvol[VNODES_COUNT];
773 long vnode_lswitch[VNODES_COUNT];
774 long vnode_rswitch[VNODES_COUNT];
775 long effects_switch[EFFECTS_COUNT];
776 long voicefx_val;
777 long cur_mic_boost;
Ian Minett44f0c972012-12-20 18:53:38 -0800778
779#ifdef ENABLE_TUNING_CONTROLS
780 long cur_ctl_vals[TUNING_CTLS_COUNT];
781#endif
Ian Minett95c6e9c2011-06-15 15:35:17 -0700782};
783
Ian Minett01ef7db2012-09-20 20:29:16 -0700784/*
785 * CA0132 codec access
786 */
787unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
788 unsigned int verb, unsigned int parm, unsigned int *res)
789{
790 unsigned int response;
791 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
792 *res = response;
793
794 return ((response == -1) ? -1 : 0);
795}
796
797static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
798 unsigned short converter_format, unsigned int *res)
799{
800 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
801 converter_format & 0xffff, res);
802}
803
804static int codec_set_converter_stream_channel(struct hda_codec *codec,
805 hda_nid_t nid, unsigned char stream,
806 unsigned char channel, unsigned int *res)
807{
808 unsigned char converter_stream_channel = 0;
809
810 converter_stream_channel = (stream << 4) | (channel & 0x0f);
811 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
812 converter_stream_channel, res);
813}
814
Ian Minett95c6e9c2011-06-15 15:35:17 -0700815/* Chip access helper function */
816static int chipio_send(struct hda_codec *codec,
817 unsigned int reg,
818 unsigned int data)
819{
820 unsigned int res;
821 int retry = 50;
822
823 /* send bits of data specified by reg */
824 do {
825 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
826 reg, data);
827 if (res == VENDOR_STATUS_CHIPIO_OK)
828 return 0;
829 } while (--retry);
830 return -EIO;
831}
832
833/*
834 * Write chip address through the vendor widget -- NOT protected by the Mutex!
835 */
836static int chipio_write_address(struct hda_codec *codec,
837 unsigned int chip_addx)
838{
Ian Minett4861af82012-09-20 20:29:20 -0700839 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700840 int res;
841
Ian Minett4861af82012-09-20 20:29:20 -0700842 if (spec->curr_chip_addx == chip_addx)
843 return 0;
844
Ian Minett95c6e9c2011-06-15 15:35:17 -0700845 /* send low 16 bits of the address */
846 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
847 chip_addx & 0xffff);
848
849 if (res != -EIO) {
850 /* send high 16 bits of the address */
851 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
852 chip_addx >> 16);
853 }
854
Ian Minett4861af82012-09-20 20:29:20 -0700855 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
856
Ian Minett95c6e9c2011-06-15 15:35:17 -0700857 return res;
858}
859
860/*
861 * Write data through the vendor widget -- NOT protected by the Mutex!
862 */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700863static int chipio_write_data(struct hda_codec *codec, unsigned int data)
864{
Ian Minett5aaca442012-12-20 18:53:34 -0800865 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700866 int res;
867
868 /* send low 16 bits of the data */
869 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
870
871 if (res != -EIO) {
872 /* send high 16 bits of the data */
873 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
874 data >> 16);
875 }
876
Ian Minett5aaca442012-12-20 18:53:34 -0800877 /*If no error encountered, automatically increment the address
878 as per chip behaviour*/
879 spec->curr_chip_addx = (res != -EIO) ?
880 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700881 return res;
882}
883
Ian Minettd5c21b82012-09-20 20:29:18 -0700884/*
885 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
886 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700887static int chipio_write_data_multiple(struct hda_codec *codec,
888 const u32 *data,
889 unsigned int count)
890{
891 int status = 0;
892
893 if (data == NULL) {
894 snd_printdd(KERN_ERR "chipio_write_data null ptr");
895 return -EINVAL;
896 }
897
898 while ((count-- != 0) && (status == 0))
899 status = chipio_write_data(codec, *data++);
900
901 return status;
902}
903
904
Ian Minett95c6e9c2011-06-15 15:35:17 -0700905/*
906 * Read data through the vendor widget -- NOT protected by the Mutex!
907 */
908static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
909{
Ian Minett5aaca442012-12-20 18:53:34 -0800910 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700911 int res;
912
913 /* post read */
914 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
915
916 if (res != -EIO) {
917 /* read status */
918 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
919 }
920
921 if (res != -EIO) {
922 /* read data */
923 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
924 VENDOR_CHIPIO_HIC_READ_DATA,
925 0);
926 }
927
Ian Minett5aaca442012-12-20 18:53:34 -0800928 /*If no error encountered, automatically increment the address
929 as per chip behaviour*/
930 spec->curr_chip_addx = (res != -EIO) ?
931 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700932 return res;
933}
934
935/*
936 * Write given value to the given address through the chip I/O widget.
937 * protected by the Mutex
938 */
939static int chipio_write(struct hda_codec *codec,
940 unsigned int chip_addx, const unsigned int data)
941{
942 struct ca0132_spec *spec = codec->spec;
943 int err;
944
945 mutex_lock(&spec->chipio_mutex);
946
947 /* write the address, and if successful proceed to write data */
948 err = chipio_write_address(codec, chip_addx);
949 if (err < 0)
950 goto exit;
951
952 err = chipio_write_data(codec, data);
953 if (err < 0)
954 goto exit;
955
956exit:
957 mutex_unlock(&spec->chipio_mutex);
958 return err;
959}
960
Ian Minettd5c21b82012-09-20 20:29:18 -0700961/*
962 * Write multiple values to the given address through the chip I/O widget.
963 * protected by the Mutex
964 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700965static int chipio_write_multiple(struct hda_codec *codec,
966 u32 chip_addx,
967 const u32 *data,
968 unsigned int count)
969{
970 struct ca0132_spec *spec = codec->spec;
971 int status;
972
973 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -0700974 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -0700975 if (status < 0)
976 goto error;
977
978 status = chipio_write_data_multiple(codec, data, count);
979error:
980 mutex_unlock(&spec->chipio_mutex);
981
982 return status;
983}
984
Ian Minett95c6e9c2011-06-15 15:35:17 -0700985/*
986 * Read the given address through the chip I/O widget
987 * protected by the Mutex
988 */
989static int chipio_read(struct hda_codec *codec,
990 unsigned int chip_addx, unsigned int *data)
991{
992 struct ca0132_spec *spec = codec->spec;
993 int err;
994
995 mutex_lock(&spec->chipio_mutex);
996
997 /* write the address, and if successful proceed to write data */
998 err = chipio_write_address(codec, chip_addx);
999 if (err < 0)
1000 goto exit;
1001
1002 err = chipio_read_data(codec, data);
1003 if (err < 0)
1004 goto exit;
1005
1006exit:
1007 mutex_unlock(&spec->chipio_mutex);
1008 return err;
1009}
1010
Ian Minettd5c21b82012-09-20 20:29:18 -07001011/*
1012 * Set chip control flags through the chip I/O widget.
1013 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001014static void chipio_set_control_flag(struct hda_codec *codec,
1015 enum control_flag_id flag_id,
1016 bool flag_state)
1017{
1018 unsigned int val;
1019 unsigned int flag_bit;
1020
1021 flag_bit = (flag_state ? 1 : 0);
1022 val = (flag_bit << 7) | (flag_id);
1023 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1024 VENDOR_CHIPIO_FLAG_SET, val);
1025}
1026
Ian Minettd5c21b82012-09-20 20:29:18 -07001027/*
1028 * Set chip parameters through the chip I/O widget.
1029 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001030static void chipio_set_control_param(struct hda_codec *codec,
1031 enum control_param_id param_id, int param_val)
1032{
1033 struct ca0132_spec *spec = codec->spec;
1034 int val;
1035
1036 if ((param_id < 32) && (param_val < 8)) {
1037 val = (param_val << 5) | (param_id);
1038 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1039 VENDOR_CHIPIO_PARAM_SET, val);
1040 } else {
1041 mutex_lock(&spec->chipio_mutex);
1042 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1043 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1044 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1045 param_id);
1046 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1047 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1048 param_val);
1049 }
1050 mutex_unlock(&spec->chipio_mutex);
1051 }
1052}
1053
Ian Minettd5c21b82012-09-20 20:29:18 -07001054/*
1055 * Set sampling rate of the connection point.
1056 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001057static void chipio_set_conn_rate(struct hda_codec *codec,
1058 int connid, enum ca0132_sample_rate rate)
1059{
1060 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1061 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1062 rate);
1063}
1064
Ian Minettd5c21b82012-09-20 20:29:18 -07001065/*
1066 * Enable clocks.
1067 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001068static void chipio_enable_clocks(struct hda_codec *codec)
1069{
1070 struct ca0132_spec *spec = codec->spec;
1071
1072 mutex_lock(&spec->chipio_mutex);
1073 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1074 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1075 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1076 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1077 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1078 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1079 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1080 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1081 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1082 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1083 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1084 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1085 mutex_unlock(&spec->chipio_mutex);
1086}
1087
1088/*
1089 * CA0132 DSP IO stuffs
1090 */
1091static int dspio_send(struct hda_codec *codec, unsigned int reg,
1092 unsigned int data)
1093{
1094 unsigned int res;
1095 int retry = 50;
1096
1097 /* send bits of data specified by reg to dsp */
1098 do {
1099 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1100 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1101 return res;
1102 } while (--retry);
1103
1104 return -EIO;
1105}
1106
Ian Minettd5c21b82012-09-20 20:29:18 -07001107/*
1108 * Wait for DSP to be ready for commands
1109 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001110static void dspio_write_wait(struct hda_codec *codec)
1111{
Ian Minett4861af82012-09-20 20:29:20 -07001112 int status;
1113 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001114
Ian Minett01ef7db2012-09-20 20:29:16 -07001115 do {
Ian Minett4861af82012-09-20 20:29:20 -07001116 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1117 VENDOR_DSPIO_STATUS, 0);
1118 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1119 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1120 break;
1121 msleep(1);
1122 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001123}
1124
Ian Minettd5c21b82012-09-20 20:29:18 -07001125/*
1126 * Write SCP data to DSP
1127 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001128static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1129{
1130 struct ca0132_spec *spec = codec->spec;
1131 int status;
1132
1133 dspio_write_wait(codec);
1134
1135 mutex_lock(&spec->chipio_mutex);
1136 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1137 scp_data & 0xffff);
1138 if (status < 0)
1139 goto error;
1140
1141 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1142 scp_data >> 16);
1143 if (status < 0)
1144 goto error;
1145
1146 /* OK, now check if the write itself has executed*/
1147 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1148 VENDOR_DSPIO_STATUS, 0);
1149error:
1150 mutex_unlock(&spec->chipio_mutex);
1151
1152 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1153 -EIO : 0;
1154}
1155
Ian Minettd5c21b82012-09-20 20:29:18 -07001156/*
1157 * Write multiple SCP data to DSP
1158 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001159static int dspio_write_multiple(struct hda_codec *codec,
1160 unsigned int *buffer, unsigned int size)
1161{
1162 int status = 0;
1163 unsigned int count;
1164
1165 if ((buffer == NULL))
1166 return -EINVAL;
1167
1168 count = 0;
1169 while (count < size) {
1170 status = dspio_write(codec, *buffer++);
1171 if (status != 0)
1172 break;
1173 count++;
1174 }
1175
1176 return status;
1177}
1178
Ian Minetta73d5112012-12-20 18:53:37 -08001179static int dspio_read(struct hda_codec *codec, unsigned int *data)
1180{
1181 int status;
1182
1183 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1184 if (status == -EIO)
1185 return status;
1186
1187 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1188 if (status == -EIO ||
1189 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1190 return -EIO;
1191
1192 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1193 VENDOR_DSPIO_SCP_READ_DATA, 0);
1194
1195 return 0;
1196}
1197
1198static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1199 unsigned int *buf_size, unsigned int size_count)
1200{
1201 int status = 0;
1202 unsigned int size = *buf_size;
1203 unsigned int count;
1204 unsigned int skip_count;
1205 unsigned int dummy;
1206
1207 if ((buffer == NULL))
1208 return -1;
1209
1210 count = 0;
1211 while (count < size && count < size_count) {
1212 status = dspio_read(codec, buffer++);
1213 if (status != 0)
1214 break;
1215 count++;
1216 }
1217
1218 skip_count = count;
1219 if (status == 0) {
1220 while (skip_count < size) {
1221 status = dspio_read(codec, &dummy);
1222 if (status != 0)
1223 break;
1224 skip_count++;
1225 }
1226 }
1227 *buf_size = count;
1228
1229 return status;
1230}
1231
Ian Minettd5c21b82012-09-20 20:29:18 -07001232/*
1233 * Construct the SCP header using corresponding fields
1234 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001235static inline unsigned int
1236make_scp_header(unsigned int target_id, unsigned int source_id,
1237 unsigned int get_flag, unsigned int req,
1238 unsigned int device_flag, unsigned int resp_flag,
1239 unsigned int error_flag, unsigned int data_size)
1240{
1241 unsigned int header = 0;
1242
1243 header = (data_size & 0x1f) << 27;
1244 header |= (error_flag & 0x01) << 26;
1245 header |= (resp_flag & 0x01) << 25;
1246 header |= (device_flag & 0x01) << 24;
1247 header |= (req & 0x7f) << 17;
1248 header |= (get_flag & 0x01) << 16;
1249 header |= (source_id & 0xff) << 8;
1250 header |= target_id & 0xff;
1251
1252 return header;
1253}
1254
Ian Minettd5c21b82012-09-20 20:29:18 -07001255/*
1256 * Extract corresponding fields from SCP header
1257 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001258static inline void
1259extract_scp_header(unsigned int header,
1260 unsigned int *target_id, unsigned int *source_id,
1261 unsigned int *get_flag, unsigned int *req,
1262 unsigned int *device_flag, unsigned int *resp_flag,
1263 unsigned int *error_flag, unsigned int *data_size)
1264{
1265 if (data_size)
1266 *data_size = (header >> 27) & 0x1f;
1267 if (error_flag)
1268 *error_flag = (header >> 26) & 0x01;
1269 if (resp_flag)
1270 *resp_flag = (header >> 25) & 0x01;
1271 if (device_flag)
1272 *device_flag = (header >> 24) & 0x01;
1273 if (req)
1274 *req = (header >> 17) & 0x7f;
1275 if (get_flag)
1276 *get_flag = (header >> 16) & 0x01;
1277 if (source_id)
1278 *source_id = (header >> 8) & 0xff;
1279 if (target_id)
1280 *target_id = header & 0xff;
1281}
1282
1283#define SCP_MAX_DATA_WORDS (16)
1284
1285/* Structure to contain any SCP message */
1286struct scp_msg {
1287 unsigned int hdr;
1288 unsigned int data[SCP_MAX_DATA_WORDS];
1289};
1290
Ian Minetta73d5112012-12-20 18:53:37 -08001291static void dspio_clear_response_queue(struct hda_codec *codec)
1292{
1293 unsigned int dummy = 0;
1294 int status = -1;
1295
1296 /* clear all from the response queue */
1297 do {
1298 status = dspio_read(codec, &dummy);
1299 } while (status == 0);
1300}
1301
1302static int dspio_get_response_data(struct hda_codec *codec)
1303{
1304 struct ca0132_spec *spec = codec->spec;
1305 unsigned int data = 0;
1306 unsigned int count;
1307
1308 if (dspio_read(codec, &data) < 0)
1309 return -EIO;
1310
1311 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1312 spec->scp_resp_header = data;
1313 spec->scp_resp_count = data >> 27;
1314 count = spec->wait_num_data;
1315 dspio_read_multiple(codec, spec->scp_resp_data,
1316 &spec->scp_resp_count, count);
1317 return 0;
1318 }
1319
1320 return -EIO;
1321}
1322
Ian Minettd5c21b82012-09-20 20:29:18 -07001323/*
1324 * Send SCP message to DSP
1325 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001326static int dspio_send_scp_message(struct hda_codec *codec,
1327 unsigned char *send_buf,
1328 unsigned int send_buf_size,
1329 unsigned char *return_buf,
1330 unsigned int return_buf_size,
1331 unsigned int *bytes_returned)
1332{
1333 struct ca0132_spec *spec = codec->spec;
1334 int retry;
1335 int status = -1;
1336 unsigned int scp_send_size = 0;
1337 unsigned int total_size;
1338 bool waiting_for_resp = false;
1339 unsigned int header;
1340 struct scp_msg *ret_msg;
1341 unsigned int resp_src_id, resp_target_id;
1342 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1343
1344 if (bytes_returned)
1345 *bytes_returned = 0;
1346
1347 /* get scp header from buffer */
1348 header = *((unsigned int *)send_buf);
1349 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1350 &device_flag, NULL, NULL, &data_size);
1351 scp_send_size = data_size + 1;
1352 total_size = (scp_send_size * 4);
1353
1354 if (send_buf_size < total_size)
1355 return -EINVAL;
1356
1357 if (get_flag || device_flag) {
1358 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1359 return -EINVAL;
1360
1361 spec->wait_scp_header = *((unsigned int *)send_buf);
1362
1363 /* swap source id with target id */
1364 resp_target_id = src_id;
1365 resp_src_id = target_id;
1366 spec->wait_scp_header &= 0xffff0000;
1367 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1368 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1369 spec->wait_scp = 1;
1370 waiting_for_resp = true;
1371 }
1372
1373 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1374 scp_send_size);
1375 if (status < 0) {
1376 spec->wait_scp = 0;
1377 return status;
1378 }
1379
1380 if (waiting_for_resp) {
1381 memset(return_buf, 0, return_buf_size);
1382 retry = 50;
1383 do {
1384 msleep(20);
1385 } while (spec->wait_scp && (--retry != 0));
1386 waiting_for_resp = false;
1387 if (retry != 0) {
1388 ret_msg = (struct scp_msg *)return_buf;
1389 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1390 memcpy(&ret_msg->data, spec->scp_resp_data,
1391 spec->wait_num_data);
1392 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1393 status = 0;
1394 } else {
1395 status = -EIO;
1396 }
1397 spec->wait_scp = 0;
1398 }
1399
1400 return status;
1401}
1402
Ian Minettd5c21b82012-09-20 20:29:18 -07001403/**
1404 * Prepare and send the SCP message to DSP
1405 * @codec: the HDA codec
1406 * @mod_id: ID of the DSP module to send the command
1407 * @req: ID of request to send to the DSP module
1408 * @dir: SET or GET
1409 * @data: pointer to the data to send with the request, request specific
1410 * @len: length of the data, in bytes
1411 * @reply: point to the buffer to hold data returned for a reply
1412 * @reply_len: length of the reply buffer returned from GET
1413 *
1414 * Returns zero or a negative error code.
1415 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001416static int dspio_scp(struct hda_codec *codec,
1417 int mod_id, int req, int dir, void *data, unsigned int len,
1418 void *reply, unsigned int *reply_len)
1419{
1420 int status = 0;
1421 struct scp_msg scp_send, scp_reply;
1422 unsigned int ret_bytes, send_size, ret_size;
1423 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1424 unsigned int reply_data_size;
1425
1426 memset(&scp_send, 0, sizeof(scp_send));
1427 memset(&scp_reply, 0, sizeof(scp_reply));
1428
1429 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1430 return -EINVAL;
1431
1432 if (dir == SCP_GET && reply == NULL) {
1433 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
1434 return -EINVAL;
1435 }
1436
1437 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1438 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
1439 return -EINVAL;
1440 }
1441
1442 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1443 0, 0, 0, len/sizeof(unsigned int));
1444 if (data != NULL && len > 0) {
1445 len = min((unsigned int)(sizeof(scp_send.data)), len);
1446 memcpy(scp_send.data, data, len);
1447 }
1448
1449 ret_bytes = 0;
1450 send_size = sizeof(unsigned int) + len;
1451 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1452 send_size, (unsigned char *)&scp_reply,
1453 sizeof(scp_reply), &ret_bytes);
1454
1455 if (status < 0) {
1456 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1457 return status;
1458 }
1459
1460 /* extract send and reply headers members */
1461 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1462 NULL, NULL, NULL, NULL, NULL);
1463 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1464 &reply_resp_flag, &reply_error_flag,
1465 &reply_data_size);
1466
1467 if (!send_get_flag)
1468 return 0;
1469
1470 if (reply_resp_flag && !reply_error_flag) {
1471 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1472 / sizeof(unsigned int);
1473
1474 if (*reply_len < ret_size*sizeof(unsigned int)) {
1475 snd_printdd(KERN_ERR "reply too long for buf");
1476 return -EINVAL;
1477 } else if (ret_size != reply_data_size) {
1478 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1479 return -EINVAL;
1480 } else {
1481 *reply_len = ret_size*sizeof(unsigned int);
1482 memcpy(reply, scp_reply.data, *reply_len);
1483 }
1484 } else {
1485 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1486 return -EIO;
1487 }
1488
1489 return status;
1490}
1491
Ian Minettd5c21b82012-09-20 20:29:18 -07001492/*
Ian Minett5aaca442012-12-20 18:53:34 -08001493 * Set DSP parameters
1494 */
1495static int dspio_set_param(struct hda_codec *codec, int mod_id,
1496 int req, void *data, unsigned int len)
1497{
1498 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1499}
1500
1501static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
1502 int req, unsigned int data)
1503{
1504 return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
1505}
1506
1507/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001508 * Allocate a DSP DMA channel via an SCP message
1509 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001510static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1511{
1512 int status = 0;
1513 unsigned int size = sizeof(dma_chan);
1514
1515 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1516 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1517 SCP_GET, NULL, 0, dma_chan, &size);
1518
1519 if (status < 0) {
1520 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1521 return status;
1522 }
1523
1524 if ((*dma_chan + 1) == 0) {
1525 snd_printdd(KERN_INFO "no free dma channels to allocate");
1526 return -EBUSY;
1527 }
1528
1529 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1530 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1531
1532 return status;
1533}
1534
Ian Minettd5c21b82012-09-20 20:29:18 -07001535/*
1536 * Free a DSP DMA via an SCP message
1537 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001538static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1539{
1540 int status = 0;
1541 unsigned int dummy = 0;
1542
1543 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1544 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1545
1546 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1547 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1548
1549 if (status < 0) {
1550 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1551 return status;
1552 }
1553
1554 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1555
1556 return status;
1557}
1558
1559/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001560 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001561 */
1562static int dsp_set_run_state(struct hda_codec *codec)
1563{
1564 unsigned int dbg_ctrl_reg;
1565 unsigned int halt_state;
1566 int err;
1567
1568 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1569 if (err < 0)
1570 return err;
1571
1572 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1573 DSP_DBGCNTL_STATE_LOBIT;
1574
1575 if (halt_state != 0) {
1576 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1577 DSP_DBGCNTL_SS_MASK);
1578 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1579 dbg_ctrl_reg);
1580 if (err < 0)
1581 return err;
1582
1583 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1584 DSP_DBGCNTL_EXEC_MASK;
1585 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1586 dbg_ctrl_reg);
1587 if (err < 0)
1588 return err;
1589 }
1590
1591 return 0;
1592}
1593
Ian Minettd5c21b82012-09-20 20:29:18 -07001594/*
1595 * Reset the DSP
1596 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001597static int dsp_reset(struct hda_codec *codec)
1598{
1599 unsigned int res;
1600 int retry = 20;
1601
1602 snd_printdd("dsp_reset\n");
1603 do {
1604 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1605 retry--;
1606 } while (res == -EIO && retry);
1607
1608 if (!retry) {
1609 snd_printdd("dsp_reset timeout\n");
1610 return -EIO;
1611 }
1612
1613 return 0;
1614}
1615
Ian Minettd5c21b82012-09-20 20:29:18 -07001616/*
1617 * Convert chip address to DSP address
1618 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001619static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1620 bool *code, bool *yram)
1621{
1622 *code = *yram = false;
1623
1624 if (UC_RANGE(chip_addx, 1)) {
1625 *code = true;
1626 return UC_OFF(chip_addx);
1627 } else if (X_RANGE_ALL(chip_addx, 1)) {
1628 return X_OFF(chip_addx);
1629 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1630 *yram = true;
1631 return Y_OFF(chip_addx);
1632 }
1633
1634 return (unsigned int)INVALID_CHIP_ADDRESS;
1635}
1636
Ian Minettd5c21b82012-09-20 20:29:18 -07001637/*
1638 * Check if the DSP DMA is active
1639 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001640static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1641{
1642 unsigned int dma_chnlstart_reg;
1643
1644 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1645
1646 return ((dma_chnlstart_reg & (1 <<
1647 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1648}
1649
1650static int dsp_dma_setup_common(struct hda_codec *codec,
1651 unsigned int chip_addx,
1652 unsigned int dma_chan,
1653 unsigned int port_map_mask,
1654 bool ovly)
1655{
1656 int status = 0;
1657 unsigned int chnl_prop;
1658 unsigned int dsp_addx;
1659 unsigned int active;
1660 bool code, yram;
1661
1662 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1663
1664 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1665 snd_printdd(KERN_ERR "dma chan num invalid");
1666 return -EINVAL;
1667 }
1668
1669 if (dsp_is_dma_active(codec, dma_chan)) {
1670 snd_printdd(KERN_ERR "dma already active");
1671 return -EBUSY;
1672 }
1673
1674 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1675
1676 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1677 snd_printdd(KERN_ERR "invalid chip addr");
1678 return -ENXIO;
1679 }
1680
1681 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1682 active = 0;
1683
1684 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1685
1686 if (ovly) {
1687 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1688 &chnl_prop);
1689
1690 if (status < 0) {
1691 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1692 return status;
1693 }
1694 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1695 }
1696
1697 if (!code)
1698 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1699 else
1700 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1701
1702 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1703
1704 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1705 if (status < 0) {
1706 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1707 return status;
1708 }
1709 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1710
1711 if (ovly) {
1712 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1713 &active);
1714
1715 if (status < 0) {
1716 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1717 return status;
1718 }
1719 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1720 }
1721
1722 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1723 DSPDMAC_ACTIVE_AAR_MASK;
1724
1725 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1726 if (status < 0) {
1727 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1728 return status;
1729 }
1730
1731 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1732
1733 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1734 port_map_mask);
1735 if (status < 0) {
1736 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1737 return status;
1738 }
1739 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1740
1741 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1742 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1743 if (status < 0) {
1744 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1745 return status;
1746 }
1747 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1748
1749 snd_printdd(
1750 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1751 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1752 chip_addx, dsp_addx, dma_chan,
1753 port_map_mask, chnl_prop, active);
1754
1755 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1756
1757 return 0;
1758}
1759
Ian Minettd5c21b82012-09-20 20:29:18 -07001760/*
1761 * Setup the DSP DMA per-transfer-specific registers
1762 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001763static int dsp_dma_setup(struct hda_codec *codec,
1764 unsigned int chip_addx,
1765 unsigned int count,
1766 unsigned int dma_chan)
1767{
1768 int status = 0;
1769 bool code, yram;
1770 unsigned int dsp_addx;
1771 unsigned int addr_field;
1772 unsigned int incr_field;
1773 unsigned int base_cnt;
1774 unsigned int cur_cnt;
1775 unsigned int dma_cfg = 0;
1776 unsigned int adr_ofs = 0;
1777 unsigned int xfr_cnt = 0;
1778 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1779 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1780
1781 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1782
1783 if (count > max_dma_count) {
1784 snd_printdd(KERN_ERR "count too big");
1785 return -EINVAL;
1786 }
1787
1788 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1789 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1790 snd_printdd(KERN_ERR "invalid chip addr");
1791 return -ENXIO;
1792 }
1793
1794 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1795
1796 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1797 incr_field = 0;
1798
1799 if (!code) {
1800 addr_field <<= 1;
1801 if (yram)
1802 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1803
1804 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1805 }
1806
1807 dma_cfg = addr_field + incr_field;
1808 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1809 dma_cfg);
1810 if (status < 0) {
1811 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1812 return status;
1813 }
1814 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1815
1816 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1817 (code ? 0 : 1));
1818
1819 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1820 adr_ofs);
1821 if (status < 0) {
1822 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1823 return status;
1824 }
1825 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1826
1827 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1828
1829 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1830
1831 xfr_cnt = base_cnt | cur_cnt;
1832
1833 status = chipio_write(codec,
1834 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1835 if (status < 0) {
1836 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1837 return status;
1838 }
1839 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1840
1841 snd_printdd(
1842 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1843 "ADROFS=0x%x, XFRCNT=0x%x\n",
1844 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1845
1846 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1847
1848 return 0;
1849}
1850
Ian Minettd5c21b82012-09-20 20:29:18 -07001851/*
1852 * Start the DSP DMA
1853 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001854static int dsp_dma_start(struct hda_codec *codec,
1855 unsigned int dma_chan, bool ovly)
1856{
1857 unsigned int reg = 0;
1858 int status = 0;
1859
1860 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1861
1862 if (ovly) {
1863 status = chipio_read(codec,
1864 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1865
1866 if (status < 0) {
1867 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1868 return status;
1869 }
1870 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1871
1872 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1873 DSPDMAC_CHNLSTART_DIS_MASK);
1874 }
1875
1876 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1877 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1878 if (status < 0) {
1879 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1880 return status;
1881 }
1882 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1883
1884 return status;
1885}
1886
Ian Minettd5c21b82012-09-20 20:29:18 -07001887/*
1888 * Stop the DSP DMA
1889 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001890static int dsp_dma_stop(struct hda_codec *codec,
1891 unsigned int dma_chan, bool ovly)
1892{
1893 unsigned int reg = 0;
1894 int status = 0;
1895
1896 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1897
1898 if (ovly) {
1899 status = chipio_read(codec,
1900 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1901
1902 if (status < 0) {
1903 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1904 return status;
1905 }
1906 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1907 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1908 DSPDMAC_CHNLSTART_DIS_MASK);
1909 }
1910
1911 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1912 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1913 if (status < 0) {
1914 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1915 return status;
1916 }
1917 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1918
1919 return status;
1920}
1921
Ian Minettd5c21b82012-09-20 20:29:18 -07001922/**
1923 * Allocate router ports
1924 *
1925 * @codec: the HDA codec
1926 * @num_chans: number of channels in the stream
1927 * @ports_per_channel: number of ports per channel
1928 * @start_device: start device
1929 * @port_map: pointer to the port list to hold the allocated ports
1930 *
1931 * Returns zero or a negative error code.
1932 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001933static int dsp_allocate_router_ports(struct hda_codec *codec,
1934 unsigned int num_chans,
1935 unsigned int ports_per_channel,
1936 unsigned int start_device,
1937 unsigned int *port_map)
1938{
1939 int status = 0;
1940 int res;
1941 u8 val;
1942
1943 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1944 if (status < 0)
1945 return status;
1946
1947 val = start_device << 6;
1948 val |= (ports_per_channel - 1) << 4;
1949 val |= num_chans - 1;
1950
1951 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1952 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1953 val);
1954
1955 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1956 VENDOR_CHIPIO_PORT_ALLOC_SET,
1957 MEM_CONNID_DSP);
1958
1959 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1960 if (status < 0)
1961 return status;
1962
1963 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1964 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1965
1966 *port_map = res;
1967
1968 return (res < 0) ? res : 0;
1969}
1970
Ian Minettd5c21b82012-09-20 20:29:18 -07001971/*
1972 * Free router ports
1973 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001974static int dsp_free_router_ports(struct hda_codec *codec)
1975{
1976 int status = 0;
1977
1978 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1979 if (status < 0)
1980 return status;
1981
1982 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1983 VENDOR_CHIPIO_PORT_FREE_SET,
1984 MEM_CONNID_DSP);
1985
1986 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1987
1988 return status;
1989}
1990
Ian Minettd5c21b82012-09-20 20:29:18 -07001991/*
1992 * Allocate DSP ports for the download stream
1993 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001994static int dsp_allocate_ports(struct hda_codec *codec,
1995 unsigned int num_chans,
1996 unsigned int rate_multi, unsigned int *port_map)
1997{
1998 int status;
1999
2000 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
2001
2002 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2003 snd_printdd(KERN_ERR "bad rate multiple");
2004 return -EINVAL;
2005 }
2006
2007 status = dsp_allocate_router_ports(codec, num_chans,
2008 rate_multi, 0, port_map);
2009
2010 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
2011
2012 return status;
2013}
2014
Ian Minett01ef7db2012-09-20 20:29:16 -07002015static int dsp_allocate_ports_format(struct hda_codec *codec,
2016 const unsigned short fmt,
2017 unsigned int *port_map)
2018{
2019 int status;
2020 unsigned int num_chans;
2021
2022 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2023 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2024 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2025
2026 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2027 snd_printdd(KERN_ERR "bad rate multiple");
2028 return -EINVAL;
2029 }
2030
2031 num_chans = get_hdafmt_chs(fmt) + 1;
2032
2033 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2034
2035 return status;
2036}
2037
2038/*
Ian Minettd5c21b82012-09-20 20:29:18 -07002039 * free DSP ports
2040 */
2041static int dsp_free_ports(struct hda_codec *codec)
2042{
2043 int status;
2044
2045 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
2046
2047 status = dsp_free_router_ports(codec);
2048 if (status < 0) {
2049 snd_printdd(KERN_ERR "free router ports fail");
2050 return status;
2051 }
2052 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
2053
2054 return status;
2055}
2056
2057/*
Ian Minett01ef7db2012-09-20 20:29:16 -07002058 * HDA DMA engine stuffs for DSP code download
2059 */
2060struct dma_engine {
2061 struct hda_codec *codec;
2062 unsigned short m_converter_format;
2063 struct snd_dma_buffer *dmab;
2064 unsigned int buf_size;
2065};
2066
2067
2068enum dma_state {
2069 DMA_STATE_STOP = 0,
2070 DMA_STATE_RUN = 1
2071};
2072
2073static int dma_convert_to_hda_format(
Ian Minette97249d2012-09-20 20:29:21 -07002074 unsigned int sample_rate,
2075 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002076 unsigned short *hda_format)
2077{
2078 unsigned int format_val;
2079
2080 format_val = snd_hda_calc_stream_format(
Ian Minette97249d2012-09-20 20:29:21 -07002081 sample_rate,
2082 channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002083 SNDRV_PCM_FORMAT_S32_LE,
Ian Minette97249d2012-09-20 20:29:21 -07002084 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07002085
2086 if (hda_format)
2087 *hda_format = (unsigned short)format_val;
2088
2089 return 0;
2090}
2091
Ian Minettd5c21b82012-09-20 20:29:18 -07002092/*
2093 * Reset DMA for DSP download
2094 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002095static int dma_reset(struct dma_engine *dma)
2096{
2097 struct hda_codec *codec = dma->codec;
2098 struct ca0132_spec *spec = codec->spec;
2099 int status;
2100
2101 if (dma->dmab)
2102 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2103
2104 status = snd_hda_codec_load_dsp_prepare(codec,
2105 dma->m_converter_format,
2106 dma->buf_size,
2107 dma->dmab);
2108 if (status < 0)
2109 return status;
2110 spec->dsp_stream_id = status;
2111 return 0;
2112}
2113
2114static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2115{
2116 bool cmd;
2117
2118 snd_printdd("dma_set_state state=%d\n", state);
2119
2120 switch (state) {
2121 case DMA_STATE_STOP:
2122 cmd = false;
2123 break;
2124 case DMA_STATE_RUN:
2125 cmd = true;
2126 break;
2127 default:
2128 return 0;
2129 }
2130
2131 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2132 return 0;
2133}
2134
2135static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2136{
2137 return dma->dmab->bytes;
2138}
2139
2140static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2141{
2142 return dma->dmab->area;
2143}
2144
2145static int dma_xfer(struct dma_engine *dma,
2146 const unsigned int *data,
2147 unsigned int count)
2148{
2149 memcpy(dma->dmab->area, data, count);
2150 return 0;
2151}
2152
2153static void dma_get_converter_format(
2154 struct dma_engine *dma,
2155 unsigned short *format)
2156{
2157 if (format)
2158 *format = dma->m_converter_format;
2159}
2160
2161static unsigned int dma_get_stream_id(struct dma_engine *dma)
2162{
2163 struct ca0132_spec *spec = dma->codec->spec;
2164
2165 return spec->dsp_stream_id;
2166}
2167
2168struct dsp_image_seg {
2169 u32 magic;
2170 u32 chip_addr;
2171 u32 count;
2172 u32 data[0];
2173};
2174
2175static const u32 g_magic_value = 0x4c46584d;
2176static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2177
2178static bool is_valid(const struct dsp_image_seg *p)
2179{
2180 return p->magic == g_magic_value;
2181}
2182
2183static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2184{
2185 return g_chip_addr_magic_value == p->chip_addr;
2186}
2187
2188static bool is_last(const struct dsp_image_seg *p)
2189{
2190 return p->count == 0;
2191}
2192
2193static size_t dsp_sizeof(const struct dsp_image_seg *p)
2194{
2195 return sizeof(*p) + p->count*sizeof(u32);
2196}
2197
2198static const struct dsp_image_seg *get_next_seg_ptr(
2199 const struct dsp_image_seg *p)
2200{
2201 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2202}
2203
2204/*
2205 * CA0132 chip DSP transfer stuffs. For DSP download.
2206 */
2207#define INVALID_DMA_CHANNEL (~0UL)
2208
Ian Minettd5c21b82012-09-20 20:29:18 -07002209/*
2210 * Program a list of address/data pairs via the ChipIO widget.
2211 * The segment data is in the format of successive pairs of words.
2212 * These are repeated as indicated by the segment's count field.
2213 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002214static int dspxfr_hci_write(struct hda_codec *codec,
2215 const struct dsp_image_seg *fls)
2216{
2217 int status;
2218 const u32 *data;
2219 unsigned int count;
2220
2221 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2222 snd_printdd(KERN_ERR "hci_write invalid params");
2223 return -EINVAL;
2224 }
2225
2226 count = fls->count;
2227 data = (u32 *)(fls->data);
2228 while (count >= 2) {
2229 status = chipio_write(codec, data[0], data[1]);
2230 if (status < 0) {
2231 snd_printdd(KERN_ERR "hci_write chipio failed");
2232 return status;
2233 }
2234 count -= 2;
2235 data += 2;
2236 }
2237 return 0;
2238}
2239
Ian Minettd5c21b82012-09-20 20:29:18 -07002240/**
2241 * Write a block of data into DSP code or data RAM using pre-allocated
2242 * DMA engine.
2243 *
2244 * @codec: the HDA codec
2245 * @fls: pointer to a fast load image
2246 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2247 * no relocation
2248 * @dma_engine: pointer to DMA engine to be used for DSP download
2249 * @dma_chan: The number of DMA channels used for DSP download
2250 * @port_map_mask: port mapping
2251 * @ovly: TRUE if overlay format is required
2252 *
2253 * Returns zero or a negative error code.
2254 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002255static int dspxfr_one_seg(struct hda_codec *codec,
2256 const struct dsp_image_seg *fls,
2257 unsigned int reloc,
2258 struct dma_engine *dma_engine,
2259 unsigned int dma_chan,
2260 unsigned int port_map_mask,
2261 bool ovly)
2262{
2263 int status;
2264 bool comm_dma_setup_done = false;
2265 const unsigned int *data;
2266 unsigned int chip_addx;
2267 unsigned int words_to_write;
2268 unsigned int buffer_size_words;
2269 unsigned char *buffer_addx;
2270 unsigned short hda_format;
2271 unsigned int sample_rate_div;
2272 unsigned int sample_rate_mul;
2273 unsigned int num_chans;
2274 unsigned int hda_frame_size_words;
2275 unsigned int remainder_words;
2276 const u32 *data_remainder;
2277 u32 chip_addx_remainder;
2278 unsigned int run_size_words;
2279 const struct dsp_image_seg *hci_write = NULL;
2280 int retry;
2281
2282 if (fls == NULL)
2283 return -EINVAL;
2284 if (is_hci_prog_list_seg(fls)) {
2285 hci_write = fls;
2286 fls = get_next_seg_ptr(fls);
2287 }
2288
2289 if (hci_write && (!fls || is_last(fls))) {
2290 snd_printdd("hci_write\n");
2291 return dspxfr_hci_write(codec, hci_write);
2292 }
2293
2294 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2295 snd_printdd("Invalid Params\n");
2296 return -EINVAL;
2297 }
2298
2299 data = fls->data;
2300 chip_addx = fls->chip_addr,
2301 words_to_write = fls->count;
2302
2303 if (!words_to_write)
2304 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2305 if (reloc)
2306 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2307
2308 if (!UC_RANGE(chip_addx, words_to_write) &&
2309 !X_RANGE_ALL(chip_addx, words_to_write) &&
2310 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2311 snd_printdd("Invalid chip_addx Params\n");
2312 return -EINVAL;
2313 }
2314
2315 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2316 sizeof(u32);
2317
2318 buffer_addx = dma_get_buffer_addr(dma_engine);
2319
2320 if (buffer_addx == NULL) {
2321 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
2322 return -EINVAL;
2323 }
2324
2325 dma_get_converter_format(dma_engine, &hda_format);
2326 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2327 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2328 num_chans = get_hdafmt_chs(hda_format) + 1;
2329
2330 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2331 (num_chans * sample_rate_mul / sample_rate_div));
2332
2333 buffer_size_words = min(buffer_size_words,
2334 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2335 65536 : 32768));
2336 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2337 snd_printdd(
2338 "chpadr=0x%08x frmsz=%u nchan=%u "
2339 "rate_mul=%u div=%u bufsz=%u\n",
2340 chip_addx, hda_frame_size_words, num_chans,
2341 sample_rate_mul, sample_rate_div, buffer_size_words);
2342
2343 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2344 (buffer_size_words < hda_frame_size_words)) {
2345 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
2346 return -EINVAL;
2347 }
2348
2349 remainder_words = words_to_write % hda_frame_size_words;
2350 data_remainder = data;
2351 chip_addx_remainder = chip_addx;
2352
2353 data += remainder_words;
2354 chip_addx += remainder_words*sizeof(u32);
2355 words_to_write -= remainder_words;
2356
2357 while (words_to_write != 0) {
2358 run_size_words = min(buffer_size_words, words_to_write);
2359 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2360 words_to_write, run_size_words, remainder_words);
2361 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2362 if (!comm_dma_setup_done) {
2363 status = dsp_dma_stop(codec, dma_chan, ovly);
2364 if (status < 0)
2365 return -EIO;
2366 status = dsp_dma_setup_common(codec, chip_addx,
2367 dma_chan, port_map_mask, ovly);
2368 if (status < 0)
2369 return status;
2370 comm_dma_setup_done = true;
2371 }
2372
2373 status = dsp_dma_setup(codec, chip_addx,
2374 run_size_words, dma_chan);
2375 if (status < 0)
2376 return status;
2377 status = dsp_dma_start(codec, dma_chan, ovly);
2378 if (status < 0)
2379 return status;
2380 if (!dsp_is_dma_active(codec, dma_chan)) {
2381 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
2382 return -EIO;
2383 }
2384 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2385 if (status < 0)
2386 return status;
2387 if (remainder_words != 0) {
2388 status = chipio_write_multiple(codec,
2389 chip_addx_remainder,
2390 data_remainder,
2391 remainder_words);
2392 remainder_words = 0;
2393 }
2394 if (hci_write) {
2395 status = dspxfr_hci_write(codec, hci_write);
2396 hci_write = NULL;
2397 }
2398 retry = 5000;
2399 while (dsp_is_dma_active(codec, dma_chan)) {
2400 if (--retry <= 0)
2401 break;
2402 }
2403 snd_printdd(KERN_INFO "+++++ DMA complete");
2404 dma_set_state(dma_engine, DMA_STATE_STOP);
2405 dma_reset(dma_engine);
2406
2407 if (status < 0)
2408 return status;
2409
2410 data += run_size_words;
2411 chip_addx += run_size_words*sizeof(u32);
2412 words_to_write -= run_size_words;
2413 }
2414
2415 if (remainder_words != 0) {
2416 status = chipio_write_multiple(codec, chip_addx_remainder,
2417 data_remainder, remainder_words);
2418 }
2419
2420 return status;
2421}
2422
Ian Minettd5c21b82012-09-20 20:29:18 -07002423/**
2424 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2425 *
2426 * @codec: the HDA codec
2427 * @fls_data: pointer to a fast load image
2428 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2429 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002430 * @sample_rate: sampling rate of the stream used for DSP download
2431 * @number_channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002432 * @ovly: TRUE if overlay format is required
2433 *
2434 * Returns zero or a negative error code.
2435 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002436static int dspxfr_image(struct hda_codec *codec,
2437 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002438 unsigned int reloc,
2439 unsigned int sample_rate,
2440 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002441 bool ovly)
2442{
2443 struct ca0132_spec *spec = codec->spec;
2444 int status;
2445 unsigned short hda_format = 0;
2446 unsigned int response;
2447 unsigned char stream_id = 0;
2448 struct dma_engine *dma_engine;
2449 unsigned int dma_chan;
2450 unsigned int port_map_mask;
2451
2452 if (fls_data == NULL)
2453 return -EINVAL;
2454
2455 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai063bca02012-09-21 13:44:34 +02002456 if (!dma_engine)
2457 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002458
2459 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2460 if (!dma_engine->dmab) {
2461 status = -ENOMEM;
2462 goto exit;
2463 }
2464
2465 dma_engine->codec = codec;
Ian Minette97249d2012-09-20 20:29:21 -07002466 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002467 dma_engine->m_converter_format = hda_format;
2468 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2469 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2470
2471 dma_chan = 0;
2472
2473 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2474 hda_format, &response);
2475
2476 if (status < 0) {
2477 snd_printdd(KERN_ERR "set converter format fail");
2478 goto exit;
2479 }
2480
2481 status = snd_hda_codec_load_dsp_prepare(codec,
2482 dma_engine->m_converter_format,
2483 dma_engine->buf_size,
2484 dma_engine->dmab);
2485 if (status < 0)
2486 goto exit;
2487 spec->dsp_stream_id = status;
2488
2489 if (ovly) {
2490 status = dspio_alloc_dma_chan(codec, &dma_chan);
2491 if (status < 0) {
2492 snd_printdd(KERN_ERR "alloc dmachan fail");
2493 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2494 goto exit;
2495 }
2496 }
2497
2498 port_map_mask = 0;
2499 status = dsp_allocate_ports_format(codec, hda_format,
2500 &port_map_mask);
2501 if (status < 0) {
2502 snd_printdd(KERN_ERR "alloc ports fail");
2503 goto exit;
2504 }
2505
2506 stream_id = dma_get_stream_id(dma_engine);
2507 status = codec_set_converter_stream_channel(codec,
2508 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2509 if (status < 0) {
2510 snd_printdd(KERN_ERR "set stream chan fail");
2511 goto exit;
2512 }
2513
2514 while ((fls_data != NULL) && !is_last(fls_data)) {
2515 if (!is_valid(fls_data)) {
2516 snd_printdd(KERN_ERR "FLS check fail");
2517 status = -EINVAL;
2518 goto exit;
2519 }
2520 status = dspxfr_one_seg(codec, fls_data, reloc,
2521 dma_engine, dma_chan,
2522 port_map_mask, ovly);
2523 if (status < 0)
2524 break;
2525
2526 if (is_hci_prog_list_seg(fls_data))
2527 fls_data = get_next_seg_ptr(fls_data);
2528
2529 if ((fls_data != NULL) && !is_last(fls_data))
2530 fls_data = get_next_seg_ptr(fls_data);
2531 }
2532
2533 if (port_map_mask != 0)
2534 status = dsp_free_ports(codec);
2535
2536 if (status < 0)
2537 goto exit;
2538
2539 status = codec_set_converter_stream_channel(codec,
2540 WIDGET_CHIP_CTRL, 0, 0, &response);
2541
2542exit:
2543 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2544 dspio_free_dma_chan(codec, dma_chan);
2545
2546 if (dma_engine->dmab)
2547 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2548 kfree(dma_engine->dmab);
2549 kfree(dma_engine);
2550
2551 return status;
2552}
2553
2554/*
2555 * CA0132 DSP download stuffs.
2556 */
2557static void dspload_post_setup(struct hda_codec *codec)
2558{
2559 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2560
2561 /*set DSP speaker to 2.0 configuration*/
2562 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2563 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2564
2565 /*update write pointer*/
2566 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2567}
2568
Ian Minettd5c21b82012-09-20 20:29:18 -07002569/**
2570 * Download DSP from a DSP Image Fast Load structure. This structure is a
2571 * linear, non-constant sized element array of structures, each of which
2572 * contain the count of the data to be loaded, the data itself, and the
2573 * corresponding starting chip address of the starting data location.
2574 *
2575 * @codec: the HDA codec
2576 * @fls: pointer to a fast load image
2577 * @ovly: TRUE if overlay format is required
2578 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2579 * no relocation
2580 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2581 * @router_chans: number of audio router channels to be allocated (0 means use
2582 * internal defaults; max is 32)
2583 *
2584 * Returns zero or a negative error code.
2585 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002586static int dspload_image(struct hda_codec *codec,
2587 const struct dsp_image_seg *fls,
2588 bool ovly,
2589 unsigned int reloc,
2590 bool autostart,
2591 int router_chans)
2592{
2593 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07002594 unsigned int sample_rate;
2595 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07002596
2597 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2598 if (router_chans == 0) {
2599 if (!ovly)
2600 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2601 else
2602 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2603 }
2604
Ian Minette97249d2012-09-20 20:29:21 -07002605 sample_rate = 48000;
2606 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07002607
Ian Minette97249d2012-09-20 20:29:21 -07002608 while (channels > 16) {
2609 sample_rate *= 2;
2610 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07002611 }
2612
Ian Minett01ef7db2012-09-20 20:29:16 -07002613 do {
2614 snd_printdd(KERN_INFO "Ready to program DMA");
2615 if (!ovly)
2616 status = dsp_reset(codec);
2617
2618 if (status < 0)
2619 break;
2620
2621 snd_printdd(KERN_INFO "dsp_reset() complete");
Ian Minette97249d2012-09-20 20:29:21 -07002622 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2623 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07002624
2625 if (status < 0)
2626 break;
2627
2628 snd_printdd(KERN_INFO "dspxfr_image() complete");
2629 if (autostart && !ovly) {
2630 dspload_post_setup(codec);
2631 status = dsp_set_run_state(codec);
2632 }
2633
2634 snd_printdd(KERN_INFO "LOAD FINISHED");
2635 } while (0);
2636
2637 return status;
2638}
2639
Ian Minettc3b4eea22012-09-20 20:29:17 -07002640static const struct firmware *fw_efx;
2641
2642static int request_firmware_cached(const struct firmware **firmware_p,
2643 const char *name, struct device *device)
2644{
2645 if (*firmware_p)
2646 return 0; /* already loaded */
2647 return request_firmware(firmware_p, name, device);
2648}
2649
2650static void release_cached_firmware(void)
2651{
2652 if (fw_efx) {
2653 release_firmware(fw_efx);
2654 fw_efx = NULL;
2655 }
2656}
2657
Ian Minett01ef7db2012-09-20 20:29:16 -07002658static bool dspload_is_loaded(struct hda_codec *codec)
2659{
2660 unsigned int data = 0;
2661 int status = 0;
2662
2663 status = chipio_read(codec, 0x40004, &data);
2664 if ((status < 0) || (data != 1))
2665 return false;
2666
2667 return true;
2668}
2669
2670static bool dspload_wait_loaded(struct hda_codec *codec)
2671{
2672 int retry = 100;
2673
2674 do {
2675 msleep(20);
2676 if (dspload_is_loaded(codec)) {
2677 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2678 return true;
2679 }
2680 } while (--retry);
2681
2682 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2683 return false;
2684}
2685
Ian Minett5aaca442012-12-20 18:53:34 -08002686/*
Ian Minett825315b2012-12-20 18:53:36 -08002687 * PCM stuffs
Ian Minett95c6e9c2011-06-15 15:35:17 -07002688 */
Ian Minett825315b2012-12-20 18:53:36 -08002689static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2690 u32 stream_tag,
2691 int channel_id, int format)
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002692{
Ian Minett825315b2012-12-20 18:53:36 -08002693 unsigned int oldval, newval;
2694
2695 if (!nid)
2696 return;
2697
2698 snd_printdd(
2699 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2700 "channel=%d, format=0x%x\n",
2701 nid, stream_tag, channel_id, format);
2702
2703 /* update the format-id if changed */
2704 oldval = snd_hda_codec_read(codec, nid, 0,
2705 AC_VERB_GET_STREAM_FORMAT,
2706 0);
2707 if (oldval != format) {
2708 msleep(20);
2709 snd_hda_codec_write(codec, nid, 0,
2710 AC_VERB_SET_STREAM_FORMAT,
2711 format);
2712 }
2713
2714 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2715 newval = (stream_tag << 4) | channel_id;
2716 if (oldval != newval) {
2717 snd_hda_codec_write(codec, nid, 0,
2718 AC_VERB_SET_CHANNEL_STREAMID,
2719 newval);
2720 }
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002721}
2722
Ian Minett825315b2012-12-20 18:53:36 -08002723static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2724{
2725 unsigned int val;
2726
2727 if (!nid)
2728 return;
2729
2730 snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
2731
2732 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2733 if (!val)
2734 return;
2735
2736 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2738}
2739
2740/*
2741 * PCM callbacks
2742 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002743static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2744 struct hda_codec *codec,
2745 unsigned int stream_tag,
2746 unsigned int format,
2747 struct snd_pcm_substream *substream)
2748{
2749 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002750
2751 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2752
2753 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002754}
2755
2756static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2757 struct hda_codec *codec,
2758 struct snd_pcm_substream *substream)
2759{
2760 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002761
2762 if (spec->dsp_state == DSP_DOWNLOADING)
2763 return 0;
2764
2765 /*If Playback effects are on, allow stream some time to flush
2766 *effects tail*/
2767 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
2768 msleep(50);
2769
2770 ca0132_cleanup_stream(codec, spec->dacs[0]);
2771
2772 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002773}
2774
2775/*
2776 * Digital out
2777 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002778static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2779 struct hda_codec *codec,
2780 struct snd_pcm_substream *substream)
2781{
2782 struct ca0132_spec *spec = codec->spec;
2783 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2784}
2785
Ian Minett95c6e9c2011-06-15 15:35:17 -07002786static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2787 struct hda_codec *codec,
2788 unsigned int stream_tag,
2789 unsigned int format,
2790 struct snd_pcm_substream *substream)
2791{
2792 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002793 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2794 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002795}
2796
2797static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2798 struct hda_codec *codec,
2799 struct snd_pcm_substream *substream)
2800{
2801 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002802 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002803}
2804
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002805static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2806 struct hda_codec *codec,
2807 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002808{
2809 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002810 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002811}
2812
2813/*
Ian Minett825315b2012-12-20 18:53:36 -08002814 * Analog capture
2815 */
2816static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2817 struct hda_codec *codec,
2818 unsigned int stream_tag,
2819 unsigned int format,
2820 struct snd_pcm_substream *substream)
2821{
2822 struct ca0132_spec *spec = codec->spec;
2823
2824 ca0132_setup_stream(codec, spec->adcs[substream->number],
2825 stream_tag, 0, format);
2826
2827 return 0;
2828}
2829
2830static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2831 struct hda_codec *codec,
2832 struct snd_pcm_substream *substream)
2833{
2834 struct ca0132_spec *spec = codec->spec;
2835
2836 if (spec->dsp_state == DSP_DOWNLOADING)
2837 return 0;
2838
2839 ca0132_cleanup_stream(codec, hinfo->nid);
2840 return 0;
2841}
2842
Ian Minette90f29e2012-12-20 18:53:39 -08002843/*
2844 * Controls stuffs.
2845 */
2846
2847/*
2848 * Mixer controls helpers.
2849 */
2850#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2851 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2852 .name = xname, \
2853 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2854 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2855 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2856 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2857 .info = ca0132_volume_info, \
2858 .get = ca0132_volume_get, \
2859 .put = ca0132_volume_put, \
2860 .tlv = { .c = ca0132_volume_tlv }, \
2861 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2862
2863#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2864 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2865 .name = xname, \
2866 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2867 .info = snd_hda_mixer_amp_switch_info, \
2868 .get = ca0132_switch_get, \
2869 .put = ca0132_switch_put, \
2870 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2871
2872/* stereo */
2873#define CA0132_CODEC_VOL(xname, nid, dir) \
2874 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2875#define CA0132_CODEC_MUTE(xname, nid, dir) \
2876 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2877
Ian Minett44f0c972012-12-20 18:53:38 -08002878/* The followings are for tuning of products */
2879#ifdef ENABLE_TUNING_CONTROLS
2880
2881static unsigned int voice_focus_vals_lookup[] = {
28820x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
28830x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
28840x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
28850x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
28860x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
28870x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
28880x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
28890x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
28900x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
28910x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
28920x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
28930x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
28940x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
28950x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
28960x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
28970x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
28980x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
28990x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
29000x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
29010x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
29020x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
29030x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
29040x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
29050x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
29060x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
29070x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
29080x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
2909};
2910
2911static unsigned int mic_svm_vals_lookup[] = {
29120x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
29130x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
29140x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
29150x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
29160x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
29170x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
29180x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
29190x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
29200x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
29210x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
29220x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
29230x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
29240x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
29250x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
29260x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
29270x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
29280x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
2929};
2930
2931static unsigned int equalizer_vals_lookup[] = {
29320xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
29330xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
29340xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
29350xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
29360x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
29370x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
29380x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
29390x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
29400x41C00000
2941};
2942
2943static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
2944 unsigned int *lookup, int idx)
2945{
2946 int i = 0;
2947
2948 for (i = 0; i < TUNING_CTLS_COUNT; i++)
2949 if (nid == ca0132_tuning_ctls[i].nid)
2950 break;
2951
2952 snd_hda_power_up(codec);
2953 dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
2954 ca0132_tuning_ctls[i].req,
2955 &(lookup[idx]), sizeof(unsigned int));
2956 snd_hda_power_down(codec);
2957
2958 return 1;
2959}
2960
2961static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2963{
2964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2965 struct ca0132_spec *spec = codec->spec;
2966 hda_nid_t nid = get_amp_nid(kcontrol);
2967 long *valp = ucontrol->value.integer.value;
2968 int idx = nid - TUNING_CTL_START_NID;
2969
2970 *valp = spec->cur_ctl_vals[idx];
2971 return 0;
2972}
2973
2974static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_info *uinfo)
2976{
2977 int chs = get_amp_channels(kcontrol);
2978 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2979 uinfo->count = chs == 3 ? 2 : 1;
2980 uinfo->value.integer.min = 20;
2981 uinfo->value.integer.max = 180;
2982 uinfo->value.integer.step = 1;
2983
2984 return 0;
2985}
2986
2987static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_value *ucontrol)
2989{
2990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2991 struct ca0132_spec *spec = codec->spec;
2992 hda_nid_t nid = get_amp_nid(kcontrol);
2993 long *valp = ucontrol->value.integer.value;
2994 int idx;
2995
2996 idx = nid - TUNING_CTL_START_NID;
2997 /* any change? */
2998 if (spec->cur_ctl_vals[idx] == *valp)
2999 return 0;
3000
3001 spec->cur_ctl_vals[idx] = *valp;
3002
3003 idx = *valp - 20;
3004 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
3005
3006 return 1;
3007}
3008
3009static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_info *uinfo)
3011{
3012 int chs = get_amp_channels(kcontrol);
3013 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3014 uinfo->count = chs == 3 ? 2 : 1;
3015 uinfo->value.integer.min = 0;
3016 uinfo->value.integer.max = 100;
3017 uinfo->value.integer.step = 1;
3018
3019 return 0;
3020}
3021
3022static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_value *ucontrol)
3024{
3025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3026 struct ca0132_spec *spec = codec->spec;
3027 hda_nid_t nid = get_amp_nid(kcontrol);
3028 long *valp = ucontrol->value.integer.value;
3029 int idx;
3030
3031 idx = nid - TUNING_CTL_START_NID;
3032 /* any change? */
3033 if (spec->cur_ctl_vals[idx] == *valp)
3034 return 0;
3035
3036 spec->cur_ctl_vals[idx] = *valp;
3037
3038 idx = *valp;
3039 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
3040
3041 return 0;
3042}
3043
3044static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_info *uinfo)
3046{
3047 int chs = get_amp_channels(kcontrol);
3048 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3049 uinfo->count = chs == 3 ? 2 : 1;
3050 uinfo->value.integer.min = 0;
3051 uinfo->value.integer.max = 48;
3052 uinfo->value.integer.step = 1;
3053
3054 return 0;
3055}
3056
3057static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
3059{
3060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3061 struct ca0132_spec *spec = codec->spec;
3062 hda_nid_t nid = get_amp_nid(kcontrol);
3063 long *valp = ucontrol->value.integer.value;
3064 int idx;
3065
3066 idx = nid - TUNING_CTL_START_NID;
3067 /* any change? */
3068 if (spec->cur_ctl_vals[idx] == *valp)
3069 return 0;
3070
3071 spec->cur_ctl_vals[idx] = *valp;
3072
3073 idx = *valp;
3074 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3075
3076 return 1;
3077}
3078
3079static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3080static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
3081
3082static int add_tuning_control(struct hda_codec *codec,
3083 hda_nid_t pnid, hda_nid_t nid,
3084 const char *name, int dir)
3085{
3086 char namestr[44];
3087 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3088 struct snd_kcontrol_new knew =
3089 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3090
3091 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3092 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3093 knew.tlv.c = 0;
3094 knew.tlv.p = 0;
3095 switch (pnid) {
3096 case VOICE_FOCUS:
3097 knew.info = voice_focus_ctl_info;
3098 knew.get = tuning_ctl_get;
3099 knew.put = voice_focus_ctl_put;
3100 knew.tlv.p = voice_focus_db_scale;
3101 break;
3102 case MIC_SVM:
3103 knew.info = mic_svm_ctl_info;
3104 knew.get = tuning_ctl_get;
3105 knew.put = mic_svm_ctl_put;
3106 break;
3107 case EQUALIZER:
3108 knew.info = equalizer_ctl_info;
3109 knew.get = tuning_ctl_get;
3110 knew.put = equalizer_ctl_put;
3111 knew.tlv.p = eq_db_scale;
3112 break;
3113 default:
3114 return 0;
3115 }
3116 knew.private_value =
3117 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3118 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3119 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3120}
3121
3122static int add_tuning_ctls(struct hda_codec *codec)
3123{
3124 int i;
3125 int err;
3126
3127 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3128 err = add_tuning_control(codec,
3129 ca0132_tuning_ctls[i].parent_nid,
3130 ca0132_tuning_ctls[i].nid,
3131 ca0132_tuning_ctls[i].name,
3132 ca0132_tuning_ctls[i].direct);
3133 if (err < 0)
3134 return err;
3135 }
3136
3137 return 0;
3138}
3139
3140static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3141{
3142 struct ca0132_spec *spec = codec->spec;
3143 int i;
3144
3145 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3146 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3147 /* SVM level defaults to 0.74. */
3148 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3149
3150 /* EQ defaults to 0dB. */
3151 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3152 spec->cur_ctl_vals[i] = 24;
3153}
3154#endif /*ENABLE_TUNING_CONTROLS*/
3155
Ian Minett825315b2012-12-20 18:53:36 -08003156/*
Ian Minett5aaca442012-12-20 18:53:34 -08003157 * Select the active output.
3158 * If autodetect is enabled, output will be selected based on jack detection.
3159 * If jack inserted, headphone will be selected, else built-in speakers
3160 * If autodetect is disabled, output will be selected based on selection.
3161 */
3162static int ca0132_select_out(struct hda_codec *codec)
3163{
3164 struct ca0132_spec *spec = codec->spec;
3165 unsigned int pin_ctl;
3166 int jack_present;
3167 int auto_jack;
3168 unsigned int tmp;
3169 int err;
3170
3171 snd_printdd(KERN_INFO "ca0132_select_out\n");
3172
3173 snd_hda_power_up(codec);
3174
3175 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3176
3177 if (auto_jack)
3178 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
3179 else
3180 jack_present =
3181 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3182
3183 if (jack_present)
3184 spec->cur_out_type = HEADPHONE_OUT;
3185 else
3186 spec->cur_out_type = SPEAKER_OUT;
3187
3188 if (spec->cur_out_type == SPEAKER_OUT) {
3189 snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
3190 /*speaker out config*/
3191 tmp = FLOAT_ONE;
3192 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3193 if (err < 0)
3194 goto exit;
3195 /*enable speaker EQ*/
3196 tmp = FLOAT_ONE;
3197 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3198 if (err < 0)
3199 goto exit;
3200
3201 /* Setup EAPD */
3202 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3203 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3204 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3205 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3206 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3207 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3208 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3209 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3210
3211 /* disable headphone node */
3212 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3213 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3214 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3215 AC_VERB_SET_PIN_WIDGET_CONTROL,
3216 pin_ctl & 0xBF);
3217 /* enable speaker node */
3218 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3219 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3220 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3221 AC_VERB_SET_PIN_WIDGET_CONTROL,
3222 pin_ctl | 0x40);
3223 } else {
3224 snd_printdd(KERN_INFO "ca0132_select_out hp\n");
3225 /*headphone out config*/
3226 tmp = FLOAT_ZERO;
3227 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3228 if (err < 0)
3229 goto exit;
3230 /*disable speaker EQ*/
3231 tmp = FLOAT_ZERO;
3232 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3233 if (err < 0)
3234 goto exit;
3235
3236 /* Setup EAPD */
3237 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3238 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3239 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3240 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3241 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3242 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3243 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3244 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3245
3246 /* disable speaker*/
3247 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3248 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3249 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3250 AC_VERB_SET_PIN_WIDGET_CONTROL,
3251 pin_ctl & 0xBF);
3252 /* enable headphone*/
3253 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3254 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3255 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3256 AC_VERB_SET_PIN_WIDGET_CONTROL,
3257 pin_ctl | 0x40);
3258 }
3259
3260exit:
3261 snd_hda_power_down(codec);
3262
3263 return err < 0 ? err : 0;
3264}
3265
3266static void ca0132_set_dmic(struct hda_codec *codec, int enable);
3267static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
3268static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
3269
3270/*
3271 * Select the active VIP source
3272 */
3273static int ca0132_set_vipsource(struct hda_codec *codec, int val)
3274{
3275 struct ca0132_spec *spec = codec->spec;
3276 unsigned int tmp;
3277
3278 if (!dspload_is_loaded(codec))
3279 return 0;
3280
3281 /* if CrystalVoice if off, vipsource should be 0 */
3282 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
3283 (val == 0)) {
3284 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3285 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3286 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3287 if (spec->cur_mic_type == DIGITAL_MIC)
3288 tmp = FLOAT_TWO;
3289 else
3290 tmp = FLOAT_ONE;
3291 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3292 tmp = FLOAT_ZERO;
3293 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3294 } else {
3295 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3296 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3297 if (spec->cur_mic_type == DIGITAL_MIC)
3298 tmp = FLOAT_TWO;
3299 else
3300 tmp = FLOAT_ONE;
3301 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3302 tmp = FLOAT_ONE;
3303 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3304 msleep(20);
3305 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3306 }
3307
3308 return 1;
3309}
3310
3311/*
3312 * Select the active microphone.
3313 * If autodetect is enabled, mic will be selected based on jack detection.
3314 * If jack inserted, ext.mic will be selected, else built-in mic
3315 * If autodetect is disabled, mic will be selected based on selection.
3316 */
3317static int ca0132_select_mic(struct hda_codec *codec)
3318{
3319 struct ca0132_spec *spec = codec->spec;
3320 int jack_present;
3321 int auto_jack;
3322
3323 snd_printdd(KERN_INFO "ca0132_select_mic\n");
3324
3325 snd_hda_power_up(codec);
3326
3327 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3328
3329 if (auto_jack)
3330 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3331 else
3332 jack_present =
3333 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3334
3335 if (jack_present)
3336 spec->cur_mic_type = LINE_MIC_IN;
3337 else
3338 spec->cur_mic_type = DIGITAL_MIC;
3339
3340 if (spec->cur_mic_type == DIGITAL_MIC) {
3341 /* enable digital Mic */
3342 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3343 ca0132_set_dmic(codec, 1);
3344 ca0132_mic_boost_set(codec, 0);
3345 /* set voice focus */
3346 ca0132_effects_set(codec, VOICE_FOCUS,
3347 spec->effects_switch
3348 [VOICE_FOCUS - EFFECT_START_NID]);
3349 } else {
3350 /* disable digital Mic */
3351 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3352 ca0132_set_dmic(codec, 0);
3353 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3354 /* disable voice focus */
3355 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3356 }
3357
3358 snd_hda_power_down(codec);
3359
3360 return 0;
3361}
3362
3363/*
Ian Minetta7e76272012-12-20 18:53:35 -08003364 * Check if VNODE settings take effect immediately.
3365 */
3366static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3367 hda_nid_t vnid,
3368 hda_nid_t *shared_nid)
3369{
3370 struct ca0132_spec *spec = codec->spec;
3371 hda_nid_t nid;
3372 bool effective = false;
3373
3374 switch (vnid) {
3375 case VNID_SPK:
3376 nid = spec->shared_out_nid;
3377 effective = true;
3378 break;
3379 case VNID_MIC:
3380 nid = spec->shared_mic_nid;
3381 effective = true;
3382 break;
3383 default:
3384 break;
3385 }
3386
3387 if (effective && shared_nid)
3388 *shared_nid = nid;
3389
3390 return effective;
3391}
3392
3393/*
3394* The following functions are control change helpers.
3395* They return 0 if no changed. Return 1 if changed.
3396*/
3397static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3398{
3399 struct ca0132_spec *spec = codec->spec;
3400 unsigned int tmp;
3401
3402 /* based on CrystalVoice state to enable VoiceFX. */
3403 if (enable) {
3404 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3405 FLOAT_ONE : FLOAT_ZERO;
3406 } else {
3407 tmp = FLOAT_ZERO;
3408 }
3409
3410 dspio_set_uint_param(codec, ca0132_voicefx.mid,
3411 ca0132_voicefx.reqs[0], tmp);
3412
3413 return 1;
3414}
3415
3416/*
Ian Minett5aaca442012-12-20 18:53:34 -08003417 * Set the effects parameters
3418 */
3419static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3420{
3421 struct ca0132_spec *spec = codec->spec;
3422 unsigned int on;
3423 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3424 int err = 0;
3425 int idx = nid - EFFECT_START_NID;
3426
3427 if ((idx < 0) || (idx >= num_fx))
3428 return 0; /* no changed */
3429
3430 /* for out effect, qualify with PE */
3431 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3432 /* if PE if off, turn off out effects. */
3433 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3434 val = 0;
3435 }
3436
3437 /* for in effect, qualify with CrystalVoice */
3438 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3439 /* if CrystalVoice if off, turn off in effects. */
3440 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3441 val = 0;
3442
3443 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3444 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3445 val = 0;
3446 }
3447
3448 snd_printdd(KERN_INFO, "ca0132_effect_set: nid=0x%x, val=%ld\n",
3449 nid, val);
3450
3451 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3452 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
3453 ca0132_effects[idx].reqs[0], on);
3454
3455 if (err < 0)
3456 return 0; /* no changed */
3457
3458 return 1;
3459}
3460
Ian Minetta7e76272012-12-20 18:53:35 -08003461/*
3462 * Turn on/off Playback Enhancements
3463 */
3464static int ca0132_pe_switch_set(struct hda_codec *codec)
3465{
3466 struct ca0132_spec *spec = codec->spec;
3467 hda_nid_t nid;
3468 int i, ret = 0;
3469
3470 snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
3471 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3472
3473 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3474 nid = OUT_EFFECT_START_NID;
3475 /* PE affects all out effects */
3476 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3477 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3478
3479 return ret;
3480}
3481
Ian Minett5aaca442012-12-20 18:53:34 -08003482/* Check if Mic1 is streaming, if so, stop streaming */
3483static int stop_mic1(struct hda_codec *codec)
3484{
3485 struct ca0132_spec *spec = codec->spec;
3486 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
3487 AC_VERB_GET_CONV, 0);
3488 if (oldval != 0)
3489 snd_hda_codec_write(codec, spec->adcs[0], 0,
3490 AC_VERB_SET_CHANNEL_STREAMID,
3491 0);
3492 return oldval;
3493}
3494
3495/* Resume Mic1 streaming if it was stopped. */
3496static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
3497{
3498 struct ca0132_spec *spec = codec->spec;
3499 /* Restore the previous stream and channel */
3500 if (oldval != 0)
3501 snd_hda_codec_write(codec, spec->adcs[0], 0,
3502 AC_VERB_SET_CHANNEL_STREAMID,
3503 oldval);
3504}
3505
3506/*
Ian Minetta7e76272012-12-20 18:53:35 -08003507 * Turn on/off CrystalVoice
Ian Minett5aaca442012-12-20 18:53:34 -08003508 */
Ian Minetta7e76272012-12-20 18:53:35 -08003509static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3510{
3511 struct ca0132_spec *spec = codec->spec;
3512 hda_nid_t nid;
3513 int i, ret = 0;
3514 unsigned int oldval;
3515
3516 snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
3517 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3518
3519 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3520 nid = IN_EFFECT_START_NID;
3521 /* CrystalVoice affects all in effects */
3522 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3523 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3524
3525 /* including VoiceFX */
3526 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3527
3528 /* set correct vipsource */
3529 oldval = stop_mic1(codec);
3530 ret |= ca0132_set_vipsource(codec, 1);
3531 resume_mic1(codec, oldval);
3532 return ret;
3533}
3534
Ian Minett5aaca442012-12-20 18:53:34 -08003535static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3536{
3537 struct ca0132_spec *spec = codec->spec;
3538 int ret = 0;
3539
3540 if (val) /* on */
3541 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3542 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3543 else /* off */
3544 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3545 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3546
3547 return ret;
3548}
3549
Ian Minetta7e76272012-12-20 18:53:35 -08003550static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3551 struct snd_ctl_elem_value *ucontrol)
3552{
3553 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3554 hda_nid_t nid = get_amp_nid(kcontrol);
3555 hda_nid_t shared_nid = 0;
3556 bool effective;
3557 int ret = 0;
3558 struct ca0132_spec *spec = codec->spec;
3559 int auto_jack;
3560
3561 if (nid == VNID_HP_SEL) {
3562 auto_jack =
3563 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3564 if (!auto_jack)
3565 ca0132_select_out(codec);
3566 return 1;
3567 }
3568
3569 if (nid == VNID_AMIC1_SEL) {
3570 auto_jack =
3571 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3572 if (!auto_jack)
3573 ca0132_select_mic(codec);
3574 return 1;
3575 }
3576
3577 if (nid == VNID_HP_ASEL) {
3578 ca0132_select_out(codec);
3579 return 1;
3580 }
3581
3582 if (nid == VNID_AMIC1_ASEL) {
3583 ca0132_select_mic(codec);
3584 return 1;
3585 }
3586
3587 /* if effective conditions, then update hw immediately. */
3588 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3589 if (effective) {
3590 int dir = get_amp_direction(kcontrol);
3591 int ch = get_amp_channels(kcontrol);
3592 unsigned long pval;
3593
3594 mutex_lock(&codec->control_mutex);
3595 pval = kcontrol->private_value;
3596 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3597 0, dir);
3598 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3599 kcontrol->private_value = pval;
3600 mutex_unlock(&codec->control_mutex);
3601 }
3602
3603 return ret;
3604}
3605/* End of control change helpers. */
3606
3607static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3608 struct snd_ctl_elem_info *uinfo)
3609{
3610 unsigned int items = sizeof(ca0132_voicefx_presets)
3611 / sizeof(struct ct_voicefx_preset);
3612
3613 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3614 uinfo->count = 1;
3615 uinfo->value.enumerated.items = items;
3616 if (uinfo->value.enumerated.item >= items)
3617 uinfo->value.enumerated.item = items - 1;
3618 strcpy(uinfo->value.enumerated.name,
3619 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3620 return 0;
3621}
3622
3623static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
3624 struct snd_ctl_elem_value *ucontrol)
3625{
3626 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3627 struct ca0132_spec *spec = codec->spec;
3628
3629 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
3630 return 0;
3631}
3632
3633static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
3634 struct snd_ctl_elem_value *ucontrol)
3635{
3636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3637 struct ca0132_spec *spec = codec->spec;
3638 int i, err = 0;
3639 int sel = ucontrol->value.enumerated.item[0];
3640 unsigned int items = sizeof(ca0132_voicefx_presets)
3641 / sizeof(struct ct_voicefx_preset);
3642
3643 if (sel >= items)
3644 return 0;
3645
3646 snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
3647 sel, ca0132_voicefx_presets[sel].name);
3648
3649 /*
3650 * Idx 0 is default.
3651 * Default needs to qualify with CrystalVoice state.
3652 */
3653 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3654 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
3655 ca0132_voicefx.reqs[i],
3656 ca0132_voicefx_presets[sel].vals[i]);
3657 if (err < 0)
3658 break;
3659 }
3660
3661 if (err >= 0) {
3662 spec->voicefx_val = sel;
3663 /* enable voice fx */
3664 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3665 }
3666
3667 return 1;
3668}
3669
3670static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3671 struct snd_ctl_elem_value *ucontrol)
3672{
3673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3674 struct ca0132_spec *spec = codec->spec;
3675 hda_nid_t nid = get_amp_nid(kcontrol);
3676 int ch = get_amp_channels(kcontrol);
3677 long *valp = ucontrol->value.integer.value;
3678
3679 /* vnode */
3680 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3681 if (ch & 1) {
3682 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3683 valp++;
3684 }
3685 if (ch & 2) {
3686 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3687 valp++;
3688 }
3689 return 0;
3690 }
3691
3692 /* effects, include PE and CrystalVoice */
3693 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3694 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3695 return 0;
3696 }
3697
3698 /* mic boost */
3699 if (nid == spec->input_pins[0]) {
3700 *valp = spec->cur_mic_boost;
3701 return 0;
3702 }
3703
3704 return 0;
3705}
3706
3707static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3708 struct snd_ctl_elem_value *ucontrol)
3709{
3710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3711 struct ca0132_spec *spec = codec->spec;
3712 hda_nid_t nid = get_amp_nid(kcontrol);
3713 int ch = get_amp_channels(kcontrol);
3714 long *valp = ucontrol->value.integer.value;
3715 int changed = 1;
3716
3717 snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
3718 nid, *valp);
3719
3720 snd_hda_power_up(codec);
3721 /* vnode */
3722 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3723 if (ch & 1) {
3724 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3725 valp++;
3726 }
3727 if (ch & 2) {
3728 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3729 valp++;
3730 }
3731 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3732 goto exit;
3733 }
3734
3735 /* PE */
3736 if (nid == PLAY_ENHANCEMENT) {
3737 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3738 changed = ca0132_pe_switch_set(codec);
3739 goto exit;
3740 }
3741
3742 /* CrystalVoice */
3743 if (nid == CRYSTAL_VOICE) {
3744 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3745 changed = ca0132_cvoice_switch_set(codec);
3746 goto exit;
3747 }
3748
3749 /* out and in effects */
3750 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3751 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3752 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3753 changed = ca0132_effects_set(codec, nid, *valp);
3754 goto exit;
3755 }
3756
3757 /* mic boost */
3758 if (nid == spec->input_pins[0]) {
3759 spec->cur_mic_boost = *valp;
3760
3761 /* Mic boost does not apply to Digital Mic */
3762 if (spec->cur_mic_type != DIGITAL_MIC)
3763 changed = ca0132_mic_boost_set(codec, *valp);
3764 goto exit;
3765 }
3766
3767exit:
3768 snd_hda_power_down(codec);
3769 return changed;
3770}
3771
3772/*
3773 * Volume related
3774 */
3775static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3776 struct snd_ctl_elem_info *uinfo)
3777{
3778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3779 struct ca0132_spec *spec = codec->spec;
3780 hda_nid_t nid = get_amp_nid(kcontrol);
3781 int ch = get_amp_channels(kcontrol);
3782 int dir = get_amp_direction(kcontrol);
3783 unsigned long pval;
3784 int err;
3785
3786 switch (nid) {
3787 case VNID_SPK:
3788 /* follow shared_out info */
3789 nid = spec->shared_out_nid;
3790 mutex_lock(&codec->control_mutex);
3791 pval = kcontrol->private_value;
3792 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3793 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3794 kcontrol->private_value = pval;
3795 mutex_unlock(&codec->control_mutex);
3796 break;
3797 case VNID_MIC:
3798 /* follow shared_mic info */
3799 nid = spec->shared_mic_nid;
3800 mutex_lock(&codec->control_mutex);
3801 pval = kcontrol->private_value;
3802 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3803 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3804 kcontrol->private_value = pval;
3805 mutex_unlock(&codec->control_mutex);
3806 break;
3807 default:
3808 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3809 }
3810 return err;
3811}
3812
3813static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
3814 struct snd_ctl_elem_value *ucontrol)
3815{
3816 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3817 struct ca0132_spec *spec = codec->spec;
3818 hda_nid_t nid = get_amp_nid(kcontrol);
3819 int ch = get_amp_channels(kcontrol);
3820 long *valp = ucontrol->value.integer.value;
3821
3822 /* store the left and right volume */
3823 if (ch & 1) {
3824 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3825 valp++;
3826 }
3827 if (ch & 2) {
3828 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3829 valp++;
3830 }
3831 return 0;
3832}
3833
3834static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
3835 struct snd_ctl_elem_value *ucontrol)
3836{
3837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3838 struct ca0132_spec *spec = codec->spec;
3839 hda_nid_t nid = get_amp_nid(kcontrol);
3840 int ch = get_amp_channels(kcontrol);
3841 long *valp = ucontrol->value.integer.value;
3842 hda_nid_t shared_nid = 0;
3843 bool effective;
3844 int changed = 1;
3845
3846 /* store the left and right volume */
3847 if (ch & 1) {
3848 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3849 valp++;
3850 }
3851 if (ch & 2) {
3852 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3853 valp++;
3854 }
3855
3856 /* if effective conditions, then update hw immediately. */
3857 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3858 if (effective) {
3859 int dir = get_amp_direction(kcontrol);
3860 unsigned long pval;
3861
3862 snd_hda_power_up(codec);
3863 mutex_lock(&codec->control_mutex);
3864 pval = kcontrol->private_value;
3865 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3866 0, dir);
3867 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3868 kcontrol->private_value = pval;
3869 mutex_unlock(&codec->control_mutex);
3870 snd_hda_power_down(codec);
3871 }
3872
3873 return changed;
3874}
3875
3876static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3877 unsigned int size, unsigned int __user *tlv)
3878{
3879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3880 struct ca0132_spec *spec = codec->spec;
3881 hda_nid_t nid = get_amp_nid(kcontrol);
3882 int ch = get_amp_channels(kcontrol);
3883 int dir = get_amp_direction(kcontrol);
3884 unsigned long pval;
3885 int err;
3886
3887 switch (nid) {
3888 case VNID_SPK:
3889 /* follow shared_out tlv */
3890 nid = spec->shared_out_nid;
3891 mutex_lock(&codec->control_mutex);
3892 pval = kcontrol->private_value;
3893 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3894 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3895 kcontrol->private_value = pval;
3896 mutex_unlock(&codec->control_mutex);
3897 break;
3898 case VNID_MIC:
3899 /* follow shared_mic tlv */
3900 nid = spec->shared_mic_nid;
3901 mutex_lock(&codec->control_mutex);
3902 pval = kcontrol->private_value;
3903 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3904 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3905 kcontrol->private_value = pval;
3906 mutex_unlock(&codec->control_mutex);
3907 break;
3908 default:
3909 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3910 }
3911 return err;
3912}
3913
3914static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3915 const char *pfx, int dir)
3916{
3917 char namestr[44];
3918 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3919 struct snd_kcontrol_new knew =
3920 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3921 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
3922 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3923}
3924
3925static int add_voicefx(struct hda_codec *codec)
3926{
3927 struct snd_kcontrol_new knew =
3928 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3929 VOICEFX, 1, 0, HDA_INPUT);
3930 knew.info = ca0132_voicefx_info;
3931 knew.get = ca0132_voicefx_get;
3932 knew.put = ca0132_voicefx_put;
3933 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
3934}
3935
3936/*
3937 * When changing Node IDs for Mixer Controls below, make sure to update
3938 * Node IDs in ca0132_config() as well.
3939 */
3940static struct snd_kcontrol_new ca0132_mixer[] = {
3941 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3942 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3943 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3944 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3945 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3946 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3947 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3948 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3949 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3950 0x12, 1, HDA_INPUT),
3951 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3952 VNID_HP_SEL, 1, HDA_OUTPUT),
3953 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3954 VNID_AMIC1_SEL, 1, HDA_INPUT),
3955 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
3956 VNID_HP_ASEL, 1, HDA_OUTPUT),
3957 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
3958 VNID_AMIC1_ASEL, 1, HDA_INPUT),
3959 { } /* end */
3960};
3961
Ian Minette90f29e2012-12-20 18:53:39 -08003962static int ca0132_build_controls(struct hda_codec *codec)
3963{
3964 struct ca0132_spec *spec = codec->spec;
3965 int i, num_fx;
3966 int err = 0;
3967
3968 /* Add Mixer controls */
3969 for (i = 0; i < spec->num_mixers; i++) {
3970 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
3971 if (err < 0)
3972 return err;
3973 }
3974
3975 /* Add in and out effects controls.
3976 * VoiceFX, PE and CrystalVoice are added separately.
3977 */
3978 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3979 for (i = 0; i < num_fx; i++) {
3980 err = add_fx_switch(codec, ca0132_effects[i].nid,
3981 ca0132_effects[i].name,
3982 ca0132_effects[i].direct);
3983 if (err < 0)
3984 return err;
3985 }
3986
3987 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
3988 if (err < 0)
3989 return err;
3990
3991 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
3992 if (err < 0)
3993 return err;
3994
3995 add_voicefx(codec);
3996
3997#ifdef ENABLE_TUNING_CONTROLS
3998 add_tuning_ctls(codec);
3999#endif
4000
4001 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4002 if (err < 0)
4003 return err;
4004
4005 if (spec->dig_out) {
4006 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
4007 spec->dig_out);
4008 if (err < 0)
4009 return err;
4010 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
4011 if (err < 0)
4012 return err;
4013 /* spec->multiout.share_spdif = 1; */
4014 }
4015
4016 if (spec->dig_in) {
4017 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
4018 if (err < 0)
4019 return err;
4020 }
4021 return 0;
4022}
4023
Ian Minett5aaca442012-12-20 18:53:34 -08004024/*
Ian Minette90f29e2012-12-20 18:53:39 -08004025 * PCM
Ian Minett95c6e9c2011-06-15 15:35:17 -07004026 */
4027static struct hda_pcm_stream ca0132_pcm_analog_playback = {
4028 .substreams = 1,
4029 .channels_min = 2,
Ian Minett825315b2012-12-20 18:53:36 -08004030 .channels_max = 6,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004031 .ops = {
4032 .prepare = ca0132_playback_pcm_prepare,
4033 .cleanup = ca0132_playback_pcm_cleanup
4034 },
4035};
4036
4037static struct hda_pcm_stream ca0132_pcm_analog_capture = {
4038 .substreams = 1,
4039 .channels_min = 2,
4040 .channels_max = 2,
Ian Minett825315b2012-12-20 18:53:36 -08004041 .ops = {
4042 .prepare = ca0132_capture_pcm_prepare,
4043 .cleanup = ca0132_capture_pcm_cleanup
4044 },
Ian Minett95c6e9c2011-06-15 15:35:17 -07004045};
4046
4047static struct hda_pcm_stream ca0132_pcm_digital_playback = {
4048 .substreams = 1,
4049 .channels_min = 2,
4050 .channels_max = 2,
4051 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02004052 .open = ca0132_dig_playback_pcm_open,
4053 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004054 .prepare = ca0132_dig_playback_pcm_prepare,
4055 .cleanup = ca0132_dig_playback_pcm_cleanup
4056 },
4057};
4058
4059static struct hda_pcm_stream ca0132_pcm_digital_capture = {
4060 .substreams = 1,
4061 .channels_min = 2,
4062 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004063};
4064
4065static int ca0132_build_pcms(struct hda_codec *codec)
4066{
4067 struct ca0132_spec *spec = codec->spec;
4068 struct hda_pcm *info = spec->pcm_rec;
4069
4070 codec->pcm_info = info;
4071 codec->num_pcms = 0;
4072
4073 info->name = "CA0132 Analog";
4074 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
4075 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
4076 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4077 spec->multiout.max_channels;
4078 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
Ian Minett825315b2012-12-20 18:53:36 -08004079 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004080 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
4081 codec->num_pcms++;
4082
Ian Minett825315b2012-12-20 18:53:36 -08004083 info++;
4084 info->name = "CA0132 Analog Mic-In2";
4085 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4086 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4087 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
4088 codec->num_pcms++;
4089
4090 info++;
4091 info->name = "CA0132 What U Hear";
4092 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4093 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4094 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
4095 codec->num_pcms++;
4096
Ian Minett95c6e9c2011-06-15 15:35:17 -07004097 if (!spec->dig_out && !spec->dig_in)
4098 return 0;
4099
4100 info++;
4101 info->name = "CA0132 Digital";
4102 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4103 if (spec->dig_out) {
4104 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4105 ca0132_pcm_digital_playback;
4106 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
4107 }
4108 if (spec->dig_in) {
4109 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4110 ca0132_pcm_digital_capture;
4111 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
4112 }
4113 codec->num_pcms++;
4114
4115 return 0;
4116}
4117
Ian Minetta73d5112012-12-20 18:53:37 -08004118static void ca0132_init_unsol(struct hda_codec *codec)
4119{
4120 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
4121 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
4122}
4123
Ian Minett5aaca442012-12-20 18:53:34 -08004124static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
4125{
4126 unsigned int caps;
4127
4128 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
4129 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
4130 snd_hda_override_amp_caps(codec, nid, dir, caps);
4131}
4132
4133/*
4134 * Switch between Digital built-in mic and analog mic.
4135 */
4136static void ca0132_set_dmic(struct hda_codec *codec, int enable)
4137{
4138 struct ca0132_spec *spec = codec->spec;
4139 unsigned int tmp;
4140 u8 val;
4141 unsigned int oldval;
4142
4143 snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
4144
4145 oldval = stop_mic1(codec);
4146 ca0132_set_vipsource(codec, 0);
4147 if (enable) {
4148 /* set DMic input as 2-ch */
4149 tmp = FLOAT_TWO;
4150 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4151
4152 val = spec->dmic_ctl;
4153 val |= 0x80;
4154 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4155 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4156
4157 if (!(spec->dmic_ctl & 0x20))
4158 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
4159 } else {
4160 /* set AMic input as mono */
4161 tmp = FLOAT_ONE;
4162 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4163
4164 val = spec->dmic_ctl;
4165 /* clear bit7 and bit5 to disable dmic */
4166 val &= 0x5f;
4167 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4168 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4169
4170 if (!(spec->dmic_ctl & 0x20))
4171 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
4172 }
4173 ca0132_set_vipsource(codec, 1);
4174 resume_mic1(codec, oldval);
4175}
4176
4177/*
4178 * Initialization for Digital Mic.
4179 */
4180static void ca0132_init_dmic(struct hda_codec *codec)
4181{
4182 struct ca0132_spec *spec = codec->spec;
4183 u8 val;
4184
4185 /* Setup Digital Mic here, but don't enable.
4186 * Enable based on jack detect.
4187 */
4188
4189 /* MCLK uses MPIO1, set to enable.
4190 * Bit 2-0: MPIO select
4191 * Bit 3: set to disable
4192 * Bit 7-4: reserved
4193 */
4194 val = 0x01;
4195 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4196 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
4197
4198 /* Data1 uses MPIO3. Data2 not use
4199 * Bit 2-0: Data1 MPIO select
4200 * Bit 3: set disable Data1
4201 * Bit 6-4: Data2 MPIO select
4202 * Bit 7: set disable Data2
4203 */
4204 val = 0x83;
4205 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4206 VENDOR_CHIPIO_DMIC_PIN_SET, val);
4207
4208 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
4209 * Bit 3-0: Channel mask
4210 * Bit 4: set for 48KHz, clear for 32KHz
4211 * Bit 5: mode
4212 * Bit 6: set to select Data2, clear for Data1
4213 * Bit 7: set to enable DMic, clear for AMic
4214 */
4215 val = 0x23;
4216 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
4217 spec->dmic_ctl = val;
4218 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4219 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4220}
4221
4222/*
4223 * Initialization for Analog Mic 2
4224 */
4225static void ca0132_init_analog_mic2(struct hda_codec *codec)
4226{
4227 struct ca0132_spec *spec = codec->spec;
4228
4229 mutex_lock(&spec->chipio_mutex);
4230 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4231 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
4232 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4233 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4234 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4235 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4236 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4237 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
4238 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4239 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4240 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4241 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4242 mutex_unlock(&spec->chipio_mutex);
4243}
4244
4245static void ca0132_refresh_widget_caps(struct hda_codec *codec)
4246{
4247 struct ca0132_spec *spec = codec->spec;
4248 int i;
4249 hda_nid_t nid;
4250
4251 snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
4252 nid = codec->start_nid;
4253 for (i = 0; i < codec->num_nodes; i++, nid++)
4254 codec->wcaps[i] = snd_hda_param_read(codec, nid,
4255 AC_PAR_AUDIO_WIDGET_CAP);
4256
4257 for (i = 0; i < spec->multiout.num_dacs; i++)
4258 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
4259
4260 for (i = 0; i < spec->num_outputs; i++)
4261 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
4262
4263 for (i = 0; i < spec->num_inputs; i++) {
4264 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
4265 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
4266 }
4267}
4268
4269/*
4270 * Setup default parameters for DSP
4271 */
4272static void ca0132_setup_defaults(struct hda_codec *codec)
4273{
4274 unsigned int tmp;
4275 int num_fx;
4276 int idx, i;
4277
4278 if (!dspload_is_loaded(codec))
4279 return;
4280
4281 /* out, in effects + voicefx */
4282 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
4283 for (idx = 0; idx < num_fx; idx++) {
4284 for (i = 0; i <= ca0132_effects[idx].params; i++) {
4285 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
4286 ca0132_effects[idx].reqs[i],
4287 ca0132_effects[idx].def_vals[i]);
4288 }
4289 }
4290
4291 /*remove DSP headroom*/
4292 tmp = FLOAT_ZERO;
4293 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
4294
4295 /*set speaker EQ bypass attenuation*/
4296 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
4297
4298 /* set AMic1 and AMic2 as mono mic */
4299 tmp = FLOAT_ONE;
4300 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4301 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
4302
4303 /* set AMic1 as CrystalVoice input */
4304 tmp = FLOAT_ONE;
4305 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4306
4307 /* set WUH source */
4308 tmp = FLOAT_TWO;
4309 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
4310}
4311
4312/*
4313 * Initialization of flags in chip
4314 */
4315static void ca0132_init_flags(struct hda_codec *codec)
4316{
4317 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4318 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4319 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4320 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4321 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4322 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4323}
4324
4325/*
4326 * Initialization of parameters in chip
4327 */
4328static void ca0132_init_params(struct hda_codec *codec)
4329{
4330 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4331 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4332}
Ian Minett95c6e9c2011-06-15 15:35:17 -07004333
Ian Minette90f29e2012-12-20 18:53:39 -08004334static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4335{
4336 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4337 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4338 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4339 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4340 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4341 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4342
4343 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4344 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4345 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4346}
4347
4348static bool ca0132_download_dsp_images(struct hda_codec *codec)
4349{
4350 bool dsp_loaded = false;
4351 const struct dsp_image_seg *dsp_os_image;
4352
4353 if (request_firmware_cached(&fw_efx, EFX_FILE,
4354 codec->bus->card->dev) != 0)
4355 return false;
4356
4357 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
4358 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4359 dsp_loaded = dspload_wait_loaded(codec);
4360
4361 return dsp_loaded;
4362}
4363
4364static void ca0132_download_dsp(struct hda_codec *codec)
4365{
4366 struct ca0132_spec *spec = codec->spec;
4367
4368 spec->dsp_state = DSP_DOWNLOAD_INIT;
4369
4370 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4371 chipio_enable_clocks(codec);
4372 spec->dsp_state = DSP_DOWNLOADING;
4373 if (!ca0132_download_dsp_images(codec))
4374 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4375 else
4376 spec->dsp_state = DSP_DOWNLOADED;
4377 }
4378
4379 if (spec->dsp_state == DSP_DOWNLOADED)
4380 ca0132_set_dsp_msr(codec, true);
4381}
4382
Ian Minett95c6e9c2011-06-15 15:35:17 -07004383static void ca0132_config(struct hda_codec *codec)
4384{
4385 struct ca0132_spec *spec = codec->spec;
4386 struct auto_pin_cfg *cfg = &spec->autocfg;
4387
Ian Minetta7e76272012-12-20 18:53:35 -08004388 spec->dacs[0] = 0x2;
4389 spec->dacs[1] = 0x3;
4390 spec->dacs[2] = 0x4;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02004391
Ian Minett95c6e9c2011-06-15 15:35:17 -07004392 spec->multiout.dac_nids = spec->dacs;
Ian Minetta7e76272012-12-20 18:53:35 -08004393 spec->multiout.num_dacs = 3;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004394 spec->multiout.max_channels = 2;
4395
Ian Minetta7e76272012-12-20 18:53:35 -08004396 spec->num_outputs = 2;
4397 spec->out_pins[0] = 0x0b; /* speaker out */
4398 spec->out_pins[1] = 0x10; /* headphone out */
4399 spec->shared_out_nid = 0x2;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004400
Ian Minetta7e76272012-12-20 18:53:35 -08004401 spec->num_inputs = 3;
4402 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4403 spec->adcs[1] = 0x8; /* analog mic2 */
4404 spec->adcs[2] = 0xa; /* what u hear */
4405 spec->shared_mic_nid = 0x7;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004406
Ian Minett95c6e9c2011-06-15 15:35:17 -07004407 spec->input_pins[0] = 0x12;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004408 spec->input_pins[1] = 0x11;
Ian Minetta7e76272012-12-20 18:53:35 -08004409 spec->input_pins[2] = 0x13;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02004410
4411 /* SPDIF I/O */
4412 spec->dig_out = 0x05;
4413 spec->multiout.dig_out_nid = spec->dig_out;
4414 cfg->dig_out_pins[0] = 0x0c;
4415 cfg->dig_outs = 1;
4416 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
4417 spec->dig_in = 0x09;
4418 cfg->dig_in_pin = 0x0e;
4419 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004420}
4421
Ian Minette90f29e2012-12-20 18:53:39 -08004422static void ca0132_process_dsp_response(struct hda_codec *codec)
4423{
4424 struct ca0132_spec *spec = codec->spec;
4425
4426 snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
4427 if (spec->wait_scp) {
4428 if (dspio_get_response_data(codec) >= 0)
4429 spec->wait_scp = 0;
4430 }
4431
4432 dspio_clear_response_queue(codec);
4433}
4434
4435static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4436{
4437 snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
4438
4439
4440 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4441 ca0132_process_dsp_response(codec);
4442 } else {
4443 res = snd_hda_jack_get_action(codec,
4444 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4445
4446 snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
4447
4448 switch (res) {
4449 case UNSOL_TAG_HP:
4450 ca0132_select_out(codec);
4451 snd_hda_jack_report_sync(codec);
4452 break;
4453 case UNSOL_TAG_AMIC1:
4454 ca0132_select_mic(codec);
4455 snd_hda_jack_report_sync(codec);
4456 break;
4457 default:
4458 break;
4459 }
4460 }
4461}
4462
Ian Minett5aaca442012-12-20 18:53:34 -08004463/*
4464 * Verbs tables.
4465 */
4466
4467/* Sends before DSP download. */
4468static struct hda_verb ca0132_base_init_verbs[] = {
4469 /*enable ct extension*/
4470 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4471 /*enable DSP node unsol, needed for DSP download*/
4472 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4473 {}
4474};
4475
4476/* Send at exit. */
4477static struct hda_verb ca0132_base_exit_verbs[] = {
4478 /*set afg to D3*/
4479 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4480 /*disable ct extension*/
4481 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4482 {}
4483};
4484
4485/* Other verbs tables. Sends after DSP download. */
4486static struct hda_verb ca0132_init_verbs0[] = {
4487 /* chip init verbs */
4488 {0x15, 0x70D, 0xF0},
4489 {0x15, 0x70E, 0xFE},
4490 {0x15, 0x707, 0x75},
4491 {0x15, 0x707, 0xD3},
4492 {0x15, 0x707, 0x09},
4493 {0x15, 0x707, 0x53},
4494 {0x15, 0x707, 0xD4},
4495 {0x15, 0x707, 0xEF},
4496 {0x15, 0x707, 0x75},
4497 {0x15, 0x707, 0xD3},
4498 {0x15, 0x707, 0x09},
4499 {0x15, 0x707, 0x02},
4500 {0x15, 0x707, 0x37},
4501 {0x15, 0x707, 0x78},
4502 {0x15, 0x53C, 0xCE},
4503 {0x15, 0x575, 0xC9},
4504 {0x15, 0x53D, 0xCE},
4505 {0x15, 0x5B7, 0xC9},
4506 {0x15, 0x70D, 0xE8},
4507 {0x15, 0x70E, 0xFE},
4508 {0x15, 0x707, 0x02},
4509 {0x15, 0x707, 0x68},
4510 {0x15, 0x707, 0x62},
4511 {0x15, 0x53A, 0xCE},
4512 {0x15, 0x546, 0xC9},
4513 {0x15, 0x53B, 0xCE},
4514 {0x15, 0x5E8, 0xC9},
4515 {0x15, 0x717, 0x0D},
4516 {0x15, 0x718, 0x20},
4517 {}
4518};
4519
4520static struct hda_verb ca0132_init_verbs1[] = {
4521 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4522 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4523 /* config EAPD */
4524 {0x0b, 0x78D, 0x00},
4525 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4526 /*{0x10, 0x78D, 0x02},*/
4527 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4528 {}
4529};
4530
Ian Minett95c6e9c2011-06-15 15:35:17 -07004531static void ca0132_init_chip(struct hda_codec *codec)
4532{
4533 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08004534 int num_fx;
4535 int i;
4536 unsigned int on;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004537
4538 mutex_init(&spec->chipio_mutex);
Ian Minett5aaca442012-12-20 18:53:34 -08004539
4540 spec->cur_out_type = SPEAKER_OUT;
4541 spec->cur_mic_type = DIGITAL_MIC;
4542 spec->cur_mic_boost = 0;
4543
4544 for (i = 0; i < VNODES_COUNT; i++) {
4545 spec->vnode_lvol[i] = 0x5a;
4546 spec->vnode_rvol[i] = 0x5a;
4547 spec->vnode_lswitch[i] = 0;
4548 spec->vnode_rswitch[i] = 0;
4549 }
4550
4551 /*
4552 * Default states for effects are in ca0132_effects[].
4553 */
4554 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4555 for (i = 0; i < num_fx; i++) {
4556 on = (unsigned int)ca0132_effects[i].reqs[0];
4557 spec->effects_switch[i] = on ? 1 : 0;
4558 }
4559
4560 spec->voicefx_val = 0;
4561 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4562 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4563
Ian Minett44f0c972012-12-20 18:53:38 -08004564#ifdef ENABLE_TUNING_CONTROLS
4565 ca0132_init_tuning_defaults(codec);
4566#endif
Ian Minett95c6e9c2011-06-15 15:35:17 -07004567}
4568
4569static void ca0132_exit_chip(struct hda_codec *codec)
4570{
4571 /* put any chip cleanup stuffs here. */
Ian Minett5aaca442012-12-20 18:53:34 -08004572
4573 if (dspload_is_loaded(codec))
4574 dsp_reset(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004575}
4576
4577static int ca0132_init(struct hda_codec *codec)
4578{
4579 struct ca0132_spec *spec = codec->spec;
4580 struct auto_pin_cfg *cfg = &spec->autocfg;
4581 int i;
4582
Ian Minett5aaca442012-12-20 18:53:34 -08004583 spec->dsp_state = DSP_DOWNLOAD_INIT;
4584 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4585
4586 snd_hda_power_up(codec);
4587
4588 ca0132_init_params(codec);
4589 ca0132_init_flags(codec);
4590 snd_hda_sequence_write(codec, spec->base_init_verbs);
Ian Minett01ef7db2012-09-20 20:29:16 -07004591#ifdef CONFIG_SND_HDA_DSP_LOADER
4592 ca0132_download_dsp(codec);
4593#endif
Ian Minett5aaca442012-12-20 18:53:34 -08004594 ca0132_refresh_widget_caps(codec);
4595 ca0132_setup_defaults(codec);
4596 ca0132_init_analog_mic2(codec);
4597 ca0132_init_dmic(codec);
Ian Minett01ef7db2012-09-20 20:29:16 -07004598
Ian Minett5aaca442012-12-20 18:53:34 -08004599 for (i = 0; i < spec->num_outputs; i++)
4600 init_output(codec, spec->out_pins[i], spec->dacs[0]);
4601
Ian Minett95c6e9c2011-06-15 15:35:17 -07004602 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
4603
4604 for (i = 0; i < spec->num_inputs; i++)
4605 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4606
4607 init_input(codec, cfg->dig_in_pin, spec->dig_in);
4608
Ian Minett5aaca442012-12-20 18:53:34 -08004609 for (i = 0; i < spec->num_init_verbs; i++)
4610 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4611
Ian Minetta73d5112012-12-20 18:53:37 -08004612 ca0132_init_unsol(codec);
4613
Ian Minett5aaca442012-12-20 18:53:34 -08004614 ca0132_select_out(codec);
4615 ca0132_select_mic(codec);
4616
Ian Minetta73d5112012-12-20 18:53:37 -08004617 snd_hda_jack_report_sync(codec);
4618
Ian Minett5aaca442012-12-20 18:53:34 -08004619 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004620
4621 return 0;
4622}
4623
Ian Minett95c6e9c2011-06-15 15:35:17 -07004624static void ca0132_free(struct hda_codec *codec)
4625{
Ian Minett5aaca442012-12-20 18:53:34 -08004626 struct ca0132_spec *spec = codec->spec;
4627
4628 snd_hda_power_up(codec);
4629 snd_hda_sequence_write(codec, spec->base_exit_verbs);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004630 ca0132_exit_chip(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004631 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004632 kfree(codec->spec);
4633}
4634
4635static struct hda_codec_ops ca0132_patch_ops = {
4636 .build_controls = ca0132_build_controls,
4637 .build_pcms = ca0132_build_pcms,
4638 .init = ca0132_init,
4639 .free = ca0132_free,
Ian Minetta73d5112012-12-20 18:53:37 -08004640 .unsol_event = ca0132_unsol_event,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004641};
4642
Ian Minett95c6e9c2011-06-15 15:35:17 -07004643static int patch_ca0132(struct hda_codec *codec)
4644{
4645 struct ca0132_spec *spec;
Ian Minetta73d5112012-12-20 18:53:37 -08004646 int err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004647
4648 snd_printdd("patch_ca0132\n");
4649
4650 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4651 if (!spec)
4652 return -ENOMEM;
4653 codec->spec = spec;
4654
Ian Minetta7e76272012-12-20 18:53:35 -08004655 spec->num_mixers = 1;
4656 spec->mixers[0] = ca0132_mixer;
4657
Ian Minett5aaca442012-12-20 18:53:34 -08004658 spec->base_init_verbs = ca0132_base_init_verbs;
4659 spec->base_exit_verbs = ca0132_base_exit_verbs;
4660 spec->init_verbs[0] = ca0132_init_verbs0;
4661 spec->init_verbs[1] = ca0132_init_verbs1;
4662 spec->num_init_verbs = 2;
4663
Ian Minett95c6e9c2011-06-15 15:35:17 -07004664 ca0132_init_chip(codec);
4665
4666 ca0132_config(codec);
4667
Ian Minetta73d5112012-12-20 18:53:37 -08004668 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4669 if (err < 0)
4670 return err;
4671
Ian Minett95c6e9c2011-06-15 15:35:17 -07004672 codec->patch_ops = ca0132_patch_ops;
4673
4674 return 0;
4675}
4676
4677/*
4678 * patch entries
4679 */
4680static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4681 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4682 {} /* terminator */
4683};
4684
4685MODULE_ALIAS("snd-hda-codec-id:11020011");
4686
4687MODULE_LICENSE("GPL");
4688MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
4689
4690static struct hda_codec_preset_list ca0132_list = {
4691 .preset = snd_hda_preset_ca0132,
4692 .owner = THIS_MODULE,
4693};
4694
4695static int __init patch_ca0132_init(void)
4696{
4697 return snd_hda_add_codec_preset(&ca0132_list);
4698}
4699
4700static void __exit patch_ca0132_exit(void)
4701{
Ian Minettc3b4eea22012-09-20 20:29:17 -07004702 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07004703 snd_hda_delete_codec_preset(&ca0132_list);
4704}
4705
4706module_init(patch_ca0132_init)
4707module_exit(patch_ca0132_exit)