blob: 9ea5660e1a3a00d323756b39e1dc642e9d1ef291 [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 Minett95c6e9c2011-06-15 15:35:17 -0700778};
779
Ian Minett01ef7db2012-09-20 20:29:16 -0700780/*
781 * CA0132 codec access
782 */
783unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
784 unsigned int verb, unsigned int parm, unsigned int *res)
785{
786 unsigned int response;
787 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
788 *res = response;
789
790 return ((response == -1) ? -1 : 0);
791}
792
793static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
794 unsigned short converter_format, unsigned int *res)
795{
796 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
797 converter_format & 0xffff, res);
798}
799
800static int codec_set_converter_stream_channel(struct hda_codec *codec,
801 hda_nid_t nid, unsigned char stream,
802 unsigned char channel, unsigned int *res)
803{
804 unsigned char converter_stream_channel = 0;
805
806 converter_stream_channel = (stream << 4) | (channel & 0x0f);
807 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
808 converter_stream_channel, res);
809}
810
Ian Minett95c6e9c2011-06-15 15:35:17 -0700811/* Chip access helper function */
812static int chipio_send(struct hda_codec *codec,
813 unsigned int reg,
814 unsigned int data)
815{
816 unsigned int res;
817 int retry = 50;
818
819 /* send bits of data specified by reg */
820 do {
821 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
822 reg, data);
823 if (res == VENDOR_STATUS_CHIPIO_OK)
824 return 0;
825 } while (--retry);
826 return -EIO;
827}
828
829/*
830 * Write chip address through the vendor widget -- NOT protected by the Mutex!
831 */
832static int chipio_write_address(struct hda_codec *codec,
833 unsigned int chip_addx)
834{
Ian Minett4861af82012-09-20 20:29:20 -0700835 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700836 int res;
837
Ian Minett4861af82012-09-20 20:29:20 -0700838 if (spec->curr_chip_addx == chip_addx)
839 return 0;
840
Ian Minett95c6e9c2011-06-15 15:35:17 -0700841 /* send low 16 bits of the address */
842 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
843 chip_addx & 0xffff);
844
845 if (res != -EIO) {
846 /* send high 16 bits of the address */
847 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
848 chip_addx >> 16);
849 }
850
Ian Minett4861af82012-09-20 20:29:20 -0700851 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
852
Ian Minett95c6e9c2011-06-15 15:35:17 -0700853 return res;
854}
855
856/*
857 * Write data through the vendor widget -- NOT protected by the Mutex!
858 */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700859static int chipio_write_data(struct hda_codec *codec, unsigned int data)
860{
Ian Minett5aaca442012-12-20 18:53:34 -0800861 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700862 int res;
863
864 /* send low 16 bits of the data */
865 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
866
867 if (res != -EIO) {
868 /* send high 16 bits of the data */
869 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
870 data >> 16);
871 }
872
Ian Minett5aaca442012-12-20 18:53:34 -0800873 /*If no error encountered, automatically increment the address
874 as per chip behaviour*/
875 spec->curr_chip_addx = (res != -EIO) ?
876 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700877 return res;
878}
879
Ian Minettd5c21b82012-09-20 20:29:18 -0700880/*
881 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
882 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700883static int chipio_write_data_multiple(struct hda_codec *codec,
884 const u32 *data,
885 unsigned int count)
886{
887 int status = 0;
888
889 if (data == NULL) {
890 snd_printdd(KERN_ERR "chipio_write_data null ptr");
891 return -EINVAL;
892 }
893
894 while ((count-- != 0) && (status == 0))
895 status = chipio_write_data(codec, *data++);
896
897 return status;
898}
899
900
Ian Minett95c6e9c2011-06-15 15:35:17 -0700901/*
902 * Read data through the vendor widget -- NOT protected by the Mutex!
903 */
904static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
905{
Ian Minett5aaca442012-12-20 18:53:34 -0800906 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700907 int res;
908
909 /* post read */
910 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
911
912 if (res != -EIO) {
913 /* read status */
914 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
915 }
916
917 if (res != -EIO) {
918 /* read data */
919 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
920 VENDOR_CHIPIO_HIC_READ_DATA,
921 0);
922 }
923
Ian Minett5aaca442012-12-20 18:53:34 -0800924 /*If no error encountered, automatically increment the address
925 as per chip behaviour*/
926 spec->curr_chip_addx = (res != -EIO) ?
927 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700928 return res;
929}
930
931/*
932 * Write given value to the given address through the chip I/O widget.
933 * protected by the Mutex
934 */
935static int chipio_write(struct hda_codec *codec,
936 unsigned int chip_addx, const unsigned int data)
937{
938 struct ca0132_spec *spec = codec->spec;
939 int err;
940
941 mutex_lock(&spec->chipio_mutex);
942
943 /* write the address, and if successful proceed to write data */
944 err = chipio_write_address(codec, chip_addx);
945 if (err < 0)
946 goto exit;
947
948 err = chipio_write_data(codec, data);
949 if (err < 0)
950 goto exit;
951
952exit:
953 mutex_unlock(&spec->chipio_mutex);
954 return err;
955}
956
Ian Minettd5c21b82012-09-20 20:29:18 -0700957/*
958 * Write multiple values to the given address through the chip I/O widget.
959 * protected by the Mutex
960 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700961static int chipio_write_multiple(struct hda_codec *codec,
962 u32 chip_addx,
963 const u32 *data,
964 unsigned int count)
965{
966 struct ca0132_spec *spec = codec->spec;
967 int status;
968
969 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -0700970 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -0700971 if (status < 0)
972 goto error;
973
974 status = chipio_write_data_multiple(codec, data, count);
975error:
976 mutex_unlock(&spec->chipio_mutex);
977
978 return status;
979}
980
Ian Minett95c6e9c2011-06-15 15:35:17 -0700981/*
982 * Read the given address through the chip I/O widget
983 * protected by the Mutex
984 */
985static int chipio_read(struct hda_codec *codec,
986 unsigned int chip_addx, unsigned int *data)
987{
988 struct ca0132_spec *spec = codec->spec;
989 int err;
990
991 mutex_lock(&spec->chipio_mutex);
992
993 /* write the address, and if successful proceed to write data */
994 err = chipio_write_address(codec, chip_addx);
995 if (err < 0)
996 goto exit;
997
998 err = chipio_read_data(codec, data);
999 if (err < 0)
1000 goto exit;
1001
1002exit:
1003 mutex_unlock(&spec->chipio_mutex);
1004 return err;
1005}
1006
Ian Minettd5c21b82012-09-20 20:29:18 -07001007/*
1008 * Set chip control flags through the chip I/O widget.
1009 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001010static void chipio_set_control_flag(struct hda_codec *codec,
1011 enum control_flag_id flag_id,
1012 bool flag_state)
1013{
1014 unsigned int val;
1015 unsigned int flag_bit;
1016
1017 flag_bit = (flag_state ? 1 : 0);
1018 val = (flag_bit << 7) | (flag_id);
1019 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1020 VENDOR_CHIPIO_FLAG_SET, val);
1021}
1022
Ian Minettd5c21b82012-09-20 20:29:18 -07001023/*
1024 * Set chip parameters through the chip I/O widget.
1025 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001026static void chipio_set_control_param(struct hda_codec *codec,
1027 enum control_param_id param_id, int param_val)
1028{
1029 struct ca0132_spec *spec = codec->spec;
1030 int val;
1031
1032 if ((param_id < 32) && (param_val < 8)) {
1033 val = (param_val << 5) | (param_id);
1034 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1035 VENDOR_CHIPIO_PARAM_SET, val);
1036 } else {
1037 mutex_lock(&spec->chipio_mutex);
1038 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1039 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1040 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1041 param_id);
1042 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1043 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1044 param_val);
1045 }
1046 mutex_unlock(&spec->chipio_mutex);
1047 }
1048}
1049
Ian Minettd5c21b82012-09-20 20:29:18 -07001050/*
1051 * Set sampling rate of the connection point.
1052 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001053static void chipio_set_conn_rate(struct hda_codec *codec,
1054 int connid, enum ca0132_sample_rate rate)
1055{
1056 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1057 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1058 rate);
1059}
1060
Ian Minettd5c21b82012-09-20 20:29:18 -07001061/*
1062 * Enable clocks.
1063 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001064static void chipio_enable_clocks(struct hda_codec *codec)
1065{
1066 struct ca0132_spec *spec = codec->spec;
1067
1068 mutex_lock(&spec->chipio_mutex);
1069 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1070 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1071 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1072 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1073 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1074 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1075 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1076 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1077 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1078 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1079 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1080 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1081 mutex_unlock(&spec->chipio_mutex);
1082}
1083
1084/*
1085 * CA0132 DSP IO stuffs
1086 */
1087static int dspio_send(struct hda_codec *codec, unsigned int reg,
1088 unsigned int data)
1089{
1090 unsigned int res;
1091 int retry = 50;
1092
1093 /* send bits of data specified by reg to dsp */
1094 do {
1095 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1096 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1097 return res;
1098 } while (--retry);
1099
1100 return -EIO;
1101}
1102
Ian Minettd5c21b82012-09-20 20:29:18 -07001103/*
1104 * Wait for DSP to be ready for commands
1105 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001106static void dspio_write_wait(struct hda_codec *codec)
1107{
Ian Minett4861af82012-09-20 20:29:20 -07001108 int status;
1109 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001110
Ian Minett01ef7db2012-09-20 20:29:16 -07001111 do {
Ian Minett4861af82012-09-20 20:29:20 -07001112 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1113 VENDOR_DSPIO_STATUS, 0);
1114 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1115 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1116 break;
1117 msleep(1);
1118 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001119}
1120
Ian Minettd5c21b82012-09-20 20:29:18 -07001121/*
1122 * Write SCP data to DSP
1123 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001124static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1125{
1126 struct ca0132_spec *spec = codec->spec;
1127 int status;
1128
1129 dspio_write_wait(codec);
1130
1131 mutex_lock(&spec->chipio_mutex);
1132 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1133 scp_data & 0xffff);
1134 if (status < 0)
1135 goto error;
1136
1137 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1138 scp_data >> 16);
1139 if (status < 0)
1140 goto error;
1141
1142 /* OK, now check if the write itself has executed*/
1143 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1144 VENDOR_DSPIO_STATUS, 0);
1145error:
1146 mutex_unlock(&spec->chipio_mutex);
1147
1148 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1149 -EIO : 0;
1150}
1151
Ian Minettd5c21b82012-09-20 20:29:18 -07001152/*
1153 * Write multiple SCP data to DSP
1154 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001155static int dspio_write_multiple(struct hda_codec *codec,
1156 unsigned int *buffer, unsigned int size)
1157{
1158 int status = 0;
1159 unsigned int count;
1160
1161 if ((buffer == NULL))
1162 return -EINVAL;
1163
1164 count = 0;
1165 while (count < size) {
1166 status = dspio_write(codec, *buffer++);
1167 if (status != 0)
1168 break;
1169 count++;
1170 }
1171
1172 return status;
1173}
1174
Ian Minetta73d5112012-12-20 18:53:37 -08001175static int dspio_read(struct hda_codec *codec, unsigned int *data)
1176{
1177 int status;
1178
1179 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1180 if (status == -EIO)
1181 return status;
1182
1183 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1184 if (status == -EIO ||
1185 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1186 return -EIO;
1187
1188 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1189 VENDOR_DSPIO_SCP_READ_DATA, 0);
1190
1191 return 0;
1192}
1193
1194static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1195 unsigned int *buf_size, unsigned int size_count)
1196{
1197 int status = 0;
1198 unsigned int size = *buf_size;
1199 unsigned int count;
1200 unsigned int skip_count;
1201 unsigned int dummy;
1202
1203 if ((buffer == NULL))
1204 return -1;
1205
1206 count = 0;
1207 while (count < size && count < size_count) {
1208 status = dspio_read(codec, buffer++);
1209 if (status != 0)
1210 break;
1211 count++;
1212 }
1213
1214 skip_count = count;
1215 if (status == 0) {
1216 while (skip_count < size) {
1217 status = dspio_read(codec, &dummy);
1218 if (status != 0)
1219 break;
1220 skip_count++;
1221 }
1222 }
1223 *buf_size = count;
1224
1225 return status;
1226}
1227
Ian Minettd5c21b82012-09-20 20:29:18 -07001228/*
1229 * Construct the SCP header using corresponding fields
1230 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001231static inline unsigned int
1232make_scp_header(unsigned int target_id, unsigned int source_id,
1233 unsigned int get_flag, unsigned int req,
1234 unsigned int device_flag, unsigned int resp_flag,
1235 unsigned int error_flag, unsigned int data_size)
1236{
1237 unsigned int header = 0;
1238
1239 header = (data_size & 0x1f) << 27;
1240 header |= (error_flag & 0x01) << 26;
1241 header |= (resp_flag & 0x01) << 25;
1242 header |= (device_flag & 0x01) << 24;
1243 header |= (req & 0x7f) << 17;
1244 header |= (get_flag & 0x01) << 16;
1245 header |= (source_id & 0xff) << 8;
1246 header |= target_id & 0xff;
1247
1248 return header;
1249}
1250
Ian Minettd5c21b82012-09-20 20:29:18 -07001251/*
1252 * Extract corresponding fields from SCP header
1253 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001254static inline void
1255extract_scp_header(unsigned int header,
1256 unsigned int *target_id, unsigned int *source_id,
1257 unsigned int *get_flag, unsigned int *req,
1258 unsigned int *device_flag, unsigned int *resp_flag,
1259 unsigned int *error_flag, unsigned int *data_size)
1260{
1261 if (data_size)
1262 *data_size = (header >> 27) & 0x1f;
1263 if (error_flag)
1264 *error_flag = (header >> 26) & 0x01;
1265 if (resp_flag)
1266 *resp_flag = (header >> 25) & 0x01;
1267 if (device_flag)
1268 *device_flag = (header >> 24) & 0x01;
1269 if (req)
1270 *req = (header >> 17) & 0x7f;
1271 if (get_flag)
1272 *get_flag = (header >> 16) & 0x01;
1273 if (source_id)
1274 *source_id = (header >> 8) & 0xff;
1275 if (target_id)
1276 *target_id = header & 0xff;
1277}
1278
1279#define SCP_MAX_DATA_WORDS (16)
1280
1281/* Structure to contain any SCP message */
1282struct scp_msg {
1283 unsigned int hdr;
1284 unsigned int data[SCP_MAX_DATA_WORDS];
1285};
1286
Ian Minetta73d5112012-12-20 18:53:37 -08001287static void dspio_clear_response_queue(struct hda_codec *codec)
1288{
1289 unsigned int dummy = 0;
1290 int status = -1;
1291
1292 /* clear all from the response queue */
1293 do {
1294 status = dspio_read(codec, &dummy);
1295 } while (status == 0);
1296}
1297
1298static int dspio_get_response_data(struct hda_codec *codec)
1299{
1300 struct ca0132_spec *spec = codec->spec;
1301 unsigned int data = 0;
1302 unsigned int count;
1303
1304 if (dspio_read(codec, &data) < 0)
1305 return -EIO;
1306
1307 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1308 spec->scp_resp_header = data;
1309 spec->scp_resp_count = data >> 27;
1310 count = spec->wait_num_data;
1311 dspio_read_multiple(codec, spec->scp_resp_data,
1312 &spec->scp_resp_count, count);
1313 return 0;
1314 }
1315
1316 return -EIO;
1317}
1318
Ian Minettd5c21b82012-09-20 20:29:18 -07001319/*
1320 * Send SCP message to DSP
1321 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001322static int dspio_send_scp_message(struct hda_codec *codec,
1323 unsigned char *send_buf,
1324 unsigned int send_buf_size,
1325 unsigned char *return_buf,
1326 unsigned int return_buf_size,
1327 unsigned int *bytes_returned)
1328{
1329 struct ca0132_spec *spec = codec->spec;
1330 int retry;
1331 int status = -1;
1332 unsigned int scp_send_size = 0;
1333 unsigned int total_size;
1334 bool waiting_for_resp = false;
1335 unsigned int header;
1336 struct scp_msg *ret_msg;
1337 unsigned int resp_src_id, resp_target_id;
1338 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1339
1340 if (bytes_returned)
1341 *bytes_returned = 0;
1342
1343 /* get scp header from buffer */
1344 header = *((unsigned int *)send_buf);
1345 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1346 &device_flag, NULL, NULL, &data_size);
1347 scp_send_size = data_size + 1;
1348 total_size = (scp_send_size * 4);
1349
1350 if (send_buf_size < total_size)
1351 return -EINVAL;
1352
1353 if (get_flag || device_flag) {
1354 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1355 return -EINVAL;
1356
1357 spec->wait_scp_header = *((unsigned int *)send_buf);
1358
1359 /* swap source id with target id */
1360 resp_target_id = src_id;
1361 resp_src_id = target_id;
1362 spec->wait_scp_header &= 0xffff0000;
1363 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1364 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1365 spec->wait_scp = 1;
1366 waiting_for_resp = true;
1367 }
1368
1369 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1370 scp_send_size);
1371 if (status < 0) {
1372 spec->wait_scp = 0;
1373 return status;
1374 }
1375
1376 if (waiting_for_resp) {
1377 memset(return_buf, 0, return_buf_size);
1378 retry = 50;
1379 do {
1380 msleep(20);
1381 } while (spec->wait_scp && (--retry != 0));
1382 waiting_for_resp = false;
1383 if (retry != 0) {
1384 ret_msg = (struct scp_msg *)return_buf;
1385 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1386 memcpy(&ret_msg->data, spec->scp_resp_data,
1387 spec->wait_num_data);
1388 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1389 status = 0;
1390 } else {
1391 status = -EIO;
1392 }
1393 spec->wait_scp = 0;
1394 }
1395
1396 return status;
1397}
1398
Ian Minettd5c21b82012-09-20 20:29:18 -07001399/**
1400 * Prepare and send the SCP message to DSP
1401 * @codec: the HDA codec
1402 * @mod_id: ID of the DSP module to send the command
1403 * @req: ID of request to send to the DSP module
1404 * @dir: SET or GET
1405 * @data: pointer to the data to send with the request, request specific
1406 * @len: length of the data, in bytes
1407 * @reply: point to the buffer to hold data returned for a reply
1408 * @reply_len: length of the reply buffer returned from GET
1409 *
1410 * Returns zero or a negative error code.
1411 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001412static int dspio_scp(struct hda_codec *codec,
1413 int mod_id, int req, int dir, void *data, unsigned int len,
1414 void *reply, unsigned int *reply_len)
1415{
1416 int status = 0;
1417 struct scp_msg scp_send, scp_reply;
1418 unsigned int ret_bytes, send_size, ret_size;
1419 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1420 unsigned int reply_data_size;
1421
1422 memset(&scp_send, 0, sizeof(scp_send));
1423 memset(&scp_reply, 0, sizeof(scp_reply));
1424
1425 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1426 return -EINVAL;
1427
1428 if (dir == SCP_GET && reply == NULL) {
1429 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
1430 return -EINVAL;
1431 }
1432
1433 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1434 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
1435 return -EINVAL;
1436 }
1437
1438 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1439 0, 0, 0, len/sizeof(unsigned int));
1440 if (data != NULL && len > 0) {
1441 len = min((unsigned int)(sizeof(scp_send.data)), len);
1442 memcpy(scp_send.data, data, len);
1443 }
1444
1445 ret_bytes = 0;
1446 send_size = sizeof(unsigned int) + len;
1447 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1448 send_size, (unsigned char *)&scp_reply,
1449 sizeof(scp_reply), &ret_bytes);
1450
1451 if (status < 0) {
1452 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1453 return status;
1454 }
1455
1456 /* extract send and reply headers members */
1457 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1458 NULL, NULL, NULL, NULL, NULL);
1459 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1460 &reply_resp_flag, &reply_error_flag,
1461 &reply_data_size);
1462
1463 if (!send_get_flag)
1464 return 0;
1465
1466 if (reply_resp_flag && !reply_error_flag) {
1467 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1468 / sizeof(unsigned int);
1469
1470 if (*reply_len < ret_size*sizeof(unsigned int)) {
1471 snd_printdd(KERN_ERR "reply too long for buf");
1472 return -EINVAL;
1473 } else if (ret_size != reply_data_size) {
1474 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1475 return -EINVAL;
1476 } else {
1477 *reply_len = ret_size*sizeof(unsigned int);
1478 memcpy(reply, scp_reply.data, *reply_len);
1479 }
1480 } else {
1481 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1482 return -EIO;
1483 }
1484
1485 return status;
1486}
1487
Ian Minettd5c21b82012-09-20 20:29:18 -07001488/*
Ian Minett5aaca442012-12-20 18:53:34 -08001489 * Set DSP parameters
1490 */
1491static int dspio_set_param(struct hda_codec *codec, int mod_id,
1492 int req, void *data, unsigned int len)
1493{
1494 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1495}
1496
1497static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
1498 int req, unsigned int data)
1499{
1500 return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
1501}
1502
1503/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001504 * Allocate a DSP DMA channel via an SCP message
1505 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001506static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1507{
1508 int status = 0;
1509 unsigned int size = sizeof(dma_chan);
1510
1511 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1512 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1513 SCP_GET, NULL, 0, dma_chan, &size);
1514
1515 if (status < 0) {
1516 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1517 return status;
1518 }
1519
1520 if ((*dma_chan + 1) == 0) {
1521 snd_printdd(KERN_INFO "no free dma channels to allocate");
1522 return -EBUSY;
1523 }
1524
1525 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1526 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1527
1528 return status;
1529}
1530
Ian Minettd5c21b82012-09-20 20:29:18 -07001531/*
1532 * Free a DSP DMA via an SCP message
1533 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001534static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1535{
1536 int status = 0;
1537 unsigned int dummy = 0;
1538
1539 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1540 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1541
1542 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1543 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1544
1545 if (status < 0) {
1546 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1547 return status;
1548 }
1549
1550 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1551
1552 return status;
1553}
1554
1555/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001556 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001557 */
1558static int dsp_set_run_state(struct hda_codec *codec)
1559{
1560 unsigned int dbg_ctrl_reg;
1561 unsigned int halt_state;
1562 int err;
1563
1564 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1565 if (err < 0)
1566 return err;
1567
1568 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1569 DSP_DBGCNTL_STATE_LOBIT;
1570
1571 if (halt_state != 0) {
1572 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1573 DSP_DBGCNTL_SS_MASK);
1574 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1575 dbg_ctrl_reg);
1576 if (err < 0)
1577 return err;
1578
1579 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1580 DSP_DBGCNTL_EXEC_MASK;
1581 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1582 dbg_ctrl_reg);
1583 if (err < 0)
1584 return err;
1585 }
1586
1587 return 0;
1588}
1589
Ian Minettd5c21b82012-09-20 20:29:18 -07001590/*
1591 * Reset the DSP
1592 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001593static int dsp_reset(struct hda_codec *codec)
1594{
1595 unsigned int res;
1596 int retry = 20;
1597
1598 snd_printdd("dsp_reset\n");
1599 do {
1600 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1601 retry--;
1602 } while (res == -EIO && retry);
1603
1604 if (!retry) {
1605 snd_printdd("dsp_reset timeout\n");
1606 return -EIO;
1607 }
1608
1609 return 0;
1610}
1611
Ian Minettd5c21b82012-09-20 20:29:18 -07001612/*
1613 * Convert chip address to DSP address
1614 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001615static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1616 bool *code, bool *yram)
1617{
1618 *code = *yram = false;
1619
1620 if (UC_RANGE(chip_addx, 1)) {
1621 *code = true;
1622 return UC_OFF(chip_addx);
1623 } else if (X_RANGE_ALL(chip_addx, 1)) {
1624 return X_OFF(chip_addx);
1625 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1626 *yram = true;
1627 return Y_OFF(chip_addx);
1628 }
1629
1630 return (unsigned int)INVALID_CHIP_ADDRESS;
1631}
1632
Ian Minettd5c21b82012-09-20 20:29:18 -07001633/*
1634 * Check if the DSP DMA is active
1635 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001636static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1637{
1638 unsigned int dma_chnlstart_reg;
1639
1640 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1641
1642 return ((dma_chnlstart_reg & (1 <<
1643 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1644}
1645
1646static int dsp_dma_setup_common(struct hda_codec *codec,
1647 unsigned int chip_addx,
1648 unsigned int dma_chan,
1649 unsigned int port_map_mask,
1650 bool ovly)
1651{
1652 int status = 0;
1653 unsigned int chnl_prop;
1654 unsigned int dsp_addx;
1655 unsigned int active;
1656 bool code, yram;
1657
1658 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1659
1660 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1661 snd_printdd(KERN_ERR "dma chan num invalid");
1662 return -EINVAL;
1663 }
1664
1665 if (dsp_is_dma_active(codec, dma_chan)) {
1666 snd_printdd(KERN_ERR "dma already active");
1667 return -EBUSY;
1668 }
1669
1670 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1671
1672 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1673 snd_printdd(KERN_ERR "invalid chip addr");
1674 return -ENXIO;
1675 }
1676
1677 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1678 active = 0;
1679
1680 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1681
1682 if (ovly) {
1683 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1684 &chnl_prop);
1685
1686 if (status < 0) {
1687 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1688 return status;
1689 }
1690 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1691 }
1692
1693 if (!code)
1694 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1695 else
1696 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1697
1698 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1699
1700 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1701 if (status < 0) {
1702 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1703 return status;
1704 }
1705 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1706
1707 if (ovly) {
1708 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1709 &active);
1710
1711 if (status < 0) {
1712 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1713 return status;
1714 }
1715 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1716 }
1717
1718 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1719 DSPDMAC_ACTIVE_AAR_MASK;
1720
1721 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1722 if (status < 0) {
1723 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1724 return status;
1725 }
1726
1727 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1728
1729 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1730 port_map_mask);
1731 if (status < 0) {
1732 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1733 return status;
1734 }
1735 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1736
1737 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1738 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1739 if (status < 0) {
1740 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1741 return status;
1742 }
1743 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1744
1745 snd_printdd(
1746 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1747 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1748 chip_addx, dsp_addx, dma_chan,
1749 port_map_mask, chnl_prop, active);
1750
1751 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1752
1753 return 0;
1754}
1755
Ian Minettd5c21b82012-09-20 20:29:18 -07001756/*
1757 * Setup the DSP DMA per-transfer-specific registers
1758 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001759static int dsp_dma_setup(struct hda_codec *codec,
1760 unsigned int chip_addx,
1761 unsigned int count,
1762 unsigned int dma_chan)
1763{
1764 int status = 0;
1765 bool code, yram;
1766 unsigned int dsp_addx;
1767 unsigned int addr_field;
1768 unsigned int incr_field;
1769 unsigned int base_cnt;
1770 unsigned int cur_cnt;
1771 unsigned int dma_cfg = 0;
1772 unsigned int adr_ofs = 0;
1773 unsigned int xfr_cnt = 0;
1774 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1775 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1776
1777 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1778
1779 if (count > max_dma_count) {
1780 snd_printdd(KERN_ERR "count too big");
1781 return -EINVAL;
1782 }
1783
1784 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1785 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1786 snd_printdd(KERN_ERR "invalid chip addr");
1787 return -ENXIO;
1788 }
1789
1790 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1791
1792 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1793 incr_field = 0;
1794
1795 if (!code) {
1796 addr_field <<= 1;
1797 if (yram)
1798 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1799
1800 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1801 }
1802
1803 dma_cfg = addr_field + incr_field;
1804 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1805 dma_cfg);
1806 if (status < 0) {
1807 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1808 return status;
1809 }
1810 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1811
1812 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1813 (code ? 0 : 1));
1814
1815 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1816 adr_ofs);
1817 if (status < 0) {
1818 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1819 return status;
1820 }
1821 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1822
1823 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1824
1825 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1826
1827 xfr_cnt = base_cnt | cur_cnt;
1828
1829 status = chipio_write(codec,
1830 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1831 if (status < 0) {
1832 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1833 return status;
1834 }
1835 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1836
1837 snd_printdd(
1838 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1839 "ADROFS=0x%x, XFRCNT=0x%x\n",
1840 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1841
1842 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1843
1844 return 0;
1845}
1846
Ian Minettd5c21b82012-09-20 20:29:18 -07001847/*
1848 * Start the DSP DMA
1849 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001850static int dsp_dma_start(struct hda_codec *codec,
1851 unsigned int dma_chan, bool ovly)
1852{
1853 unsigned int reg = 0;
1854 int status = 0;
1855
1856 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1857
1858 if (ovly) {
1859 status = chipio_read(codec,
1860 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1861
1862 if (status < 0) {
1863 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1864 return status;
1865 }
1866 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1867
1868 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1869 DSPDMAC_CHNLSTART_DIS_MASK);
1870 }
1871
1872 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1873 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1874 if (status < 0) {
1875 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1876 return status;
1877 }
1878 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1879
1880 return status;
1881}
1882
Ian Minettd5c21b82012-09-20 20:29:18 -07001883/*
1884 * Stop the DSP DMA
1885 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001886static int dsp_dma_stop(struct hda_codec *codec,
1887 unsigned int dma_chan, bool ovly)
1888{
1889 unsigned int reg = 0;
1890 int status = 0;
1891
1892 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1893
1894 if (ovly) {
1895 status = chipio_read(codec,
1896 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1897
1898 if (status < 0) {
1899 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1900 return status;
1901 }
1902 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1903 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1904 DSPDMAC_CHNLSTART_DIS_MASK);
1905 }
1906
1907 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1908 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1909 if (status < 0) {
1910 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1911 return status;
1912 }
1913 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1914
1915 return status;
1916}
1917
Ian Minettd5c21b82012-09-20 20:29:18 -07001918/**
1919 * Allocate router ports
1920 *
1921 * @codec: the HDA codec
1922 * @num_chans: number of channels in the stream
1923 * @ports_per_channel: number of ports per channel
1924 * @start_device: start device
1925 * @port_map: pointer to the port list to hold the allocated ports
1926 *
1927 * Returns zero or a negative error code.
1928 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001929static int dsp_allocate_router_ports(struct hda_codec *codec,
1930 unsigned int num_chans,
1931 unsigned int ports_per_channel,
1932 unsigned int start_device,
1933 unsigned int *port_map)
1934{
1935 int status = 0;
1936 int res;
1937 u8 val;
1938
1939 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1940 if (status < 0)
1941 return status;
1942
1943 val = start_device << 6;
1944 val |= (ports_per_channel - 1) << 4;
1945 val |= num_chans - 1;
1946
1947 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1948 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1949 val);
1950
1951 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1952 VENDOR_CHIPIO_PORT_ALLOC_SET,
1953 MEM_CONNID_DSP);
1954
1955 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1956 if (status < 0)
1957 return status;
1958
1959 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1960 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1961
1962 *port_map = res;
1963
1964 return (res < 0) ? res : 0;
1965}
1966
Ian Minettd5c21b82012-09-20 20:29:18 -07001967/*
1968 * Free router ports
1969 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001970static int dsp_free_router_ports(struct hda_codec *codec)
1971{
1972 int status = 0;
1973
1974 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1975 if (status < 0)
1976 return status;
1977
1978 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1979 VENDOR_CHIPIO_PORT_FREE_SET,
1980 MEM_CONNID_DSP);
1981
1982 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1983
1984 return status;
1985}
1986
Ian Minettd5c21b82012-09-20 20:29:18 -07001987/*
1988 * Allocate DSP ports for the download stream
1989 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001990static int dsp_allocate_ports(struct hda_codec *codec,
1991 unsigned int num_chans,
1992 unsigned int rate_multi, unsigned int *port_map)
1993{
1994 int status;
1995
1996 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1997
1998 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1999 snd_printdd(KERN_ERR "bad rate multiple");
2000 return -EINVAL;
2001 }
2002
2003 status = dsp_allocate_router_ports(codec, num_chans,
2004 rate_multi, 0, port_map);
2005
2006 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
2007
2008 return status;
2009}
2010
Ian Minett01ef7db2012-09-20 20:29:16 -07002011static int dsp_allocate_ports_format(struct hda_codec *codec,
2012 const unsigned short fmt,
2013 unsigned int *port_map)
2014{
2015 int status;
2016 unsigned int num_chans;
2017
2018 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2019 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2020 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2021
2022 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2023 snd_printdd(KERN_ERR "bad rate multiple");
2024 return -EINVAL;
2025 }
2026
2027 num_chans = get_hdafmt_chs(fmt) + 1;
2028
2029 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2030
2031 return status;
2032}
2033
2034/*
Ian Minettd5c21b82012-09-20 20:29:18 -07002035 * free DSP ports
2036 */
2037static int dsp_free_ports(struct hda_codec *codec)
2038{
2039 int status;
2040
2041 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
2042
2043 status = dsp_free_router_ports(codec);
2044 if (status < 0) {
2045 snd_printdd(KERN_ERR "free router ports fail");
2046 return status;
2047 }
2048 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
2049
2050 return status;
2051}
2052
2053/*
Ian Minett01ef7db2012-09-20 20:29:16 -07002054 * HDA DMA engine stuffs for DSP code download
2055 */
2056struct dma_engine {
2057 struct hda_codec *codec;
2058 unsigned short m_converter_format;
2059 struct snd_dma_buffer *dmab;
2060 unsigned int buf_size;
2061};
2062
2063
2064enum dma_state {
2065 DMA_STATE_STOP = 0,
2066 DMA_STATE_RUN = 1
2067};
2068
2069static int dma_convert_to_hda_format(
Ian Minette97249d2012-09-20 20:29:21 -07002070 unsigned int sample_rate,
2071 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002072 unsigned short *hda_format)
2073{
2074 unsigned int format_val;
2075
2076 format_val = snd_hda_calc_stream_format(
Ian Minette97249d2012-09-20 20:29:21 -07002077 sample_rate,
2078 channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002079 SNDRV_PCM_FORMAT_S32_LE,
Ian Minette97249d2012-09-20 20:29:21 -07002080 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07002081
2082 if (hda_format)
2083 *hda_format = (unsigned short)format_val;
2084
2085 return 0;
2086}
2087
Ian Minettd5c21b82012-09-20 20:29:18 -07002088/*
2089 * Reset DMA for DSP download
2090 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002091static int dma_reset(struct dma_engine *dma)
2092{
2093 struct hda_codec *codec = dma->codec;
2094 struct ca0132_spec *spec = codec->spec;
2095 int status;
2096
2097 if (dma->dmab)
2098 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2099
2100 status = snd_hda_codec_load_dsp_prepare(codec,
2101 dma->m_converter_format,
2102 dma->buf_size,
2103 dma->dmab);
2104 if (status < 0)
2105 return status;
2106 spec->dsp_stream_id = status;
2107 return 0;
2108}
2109
2110static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2111{
2112 bool cmd;
2113
2114 snd_printdd("dma_set_state state=%d\n", state);
2115
2116 switch (state) {
2117 case DMA_STATE_STOP:
2118 cmd = false;
2119 break;
2120 case DMA_STATE_RUN:
2121 cmd = true;
2122 break;
2123 default:
2124 return 0;
2125 }
2126
2127 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2128 return 0;
2129}
2130
2131static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2132{
2133 return dma->dmab->bytes;
2134}
2135
2136static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2137{
2138 return dma->dmab->area;
2139}
2140
2141static int dma_xfer(struct dma_engine *dma,
2142 const unsigned int *data,
2143 unsigned int count)
2144{
2145 memcpy(dma->dmab->area, data, count);
2146 return 0;
2147}
2148
2149static void dma_get_converter_format(
2150 struct dma_engine *dma,
2151 unsigned short *format)
2152{
2153 if (format)
2154 *format = dma->m_converter_format;
2155}
2156
2157static unsigned int dma_get_stream_id(struct dma_engine *dma)
2158{
2159 struct ca0132_spec *spec = dma->codec->spec;
2160
2161 return spec->dsp_stream_id;
2162}
2163
2164struct dsp_image_seg {
2165 u32 magic;
2166 u32 chip_addr;
2167 u32 count;
2168 u32 data[0];
2169};
2170
2171static const u32 g_magic_value = 0x4c46584d;
2172static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2173
2174static bool is_valid(const struct dsp_image_seg *p)
2175{
2176 return p->magic == g_magic_value;
2177}
2178
2179static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2180{
2181 return g_chip_addr_magic_value == p->chip_addr;
2182}
2183
2184static bool is_last(const struct dsp_image_seg *p)
2185{
2186 return p->count == 0;
2187}
2188
2189static size_t dsp_sizeof(const struct dsp_image_seg *p)
2190{
2191 return sizeof(*p) + p->count*sizeof(u32);
2192}
2193
2194static const struct dsp_image_seg *get_next_seg_ptr(
2195 const struct dsp_image_seg *p)
2196{
2197 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2198}
2199
2200/*
2201 * CA0132 chip DSP transfer stuffs. For DSP download.
2202 */
2203#define INVALID_DMA_CHANNEL (~0UL)
2204
Ian Minettd5c21b82012-09-20 20:29:18 -07002205/*
2206 * Program a list of address/data pairs via the ChipIO widget.
2207 * The segment data is in the format of successive pairs of words.
2208 * These are repeated as indicated by the segment's count field.
2209 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002210static int dspxfr_hci_write(struct hda_codec *codec,
2211 const struct dsp_image_seg *fls)
2212{
2213 int status;
2214 const u32 *data;
2215 unsigned int count;
2216
2217 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2218 snd_printdd(KERN_ERR "hci_write invalid params");
2219 return -EINVAL;
2220 }
2221
2222 count = fls->count;
2223 data = (u32 *)(fls->data);
2224 while (count >= 2) {
2225 status = chipio_write(codec, data[0], data[1]);
2226 if (status < 0) {
2227 snd_printdd(KERN_ERR "hci_write chipio failed");
2228 return status;
2229 }
2230 count -= 2;
2231 data += 2;
2232 }
2233 return 0;
2234}
2235
Ian Minettd5c21b82012-09-20 20:29:18 -07002236/**
2237 * Write a block of data into DSP code or data RAM using pre-allocated
2238 * DMA engine.
2239 *
2240 * @codec: the HDA codec
2241 * @fls: pointer to a fast load image
2242 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2243 * no relocation
2244 * @dma_engine: pointer to DMA engine to be used for DSP download
2245 * @dma_chan: The number of DMA channels used for DSP download
2246 * @port_map_mask: port mapping
2247 * @ovly: TRUE if overlay format is required
2248 *
2249 * Returns zero or a negative error code.
2250 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002251static int dspxfr_one_seg(struct hda_codec *codec,
2252 const struct dsp_image_seg *fls,
2253 unsigned int reloc,
2254 struct dma_engine *dma_engine,
2255 unsigned int dma_chan,
2256 unsigned int port_map_mask,
2257 bool ovly)
2258{
2259 int status;
2260 bool comm_dma_setup_done = false;
2261 const unsigned int *data;
2262 unsigned int chip_addx;
2263 unsigned int words_to_write;
2264 unsigned int buffer_size_words;
2265 unsigned char *buffer_addx;
2266 unsigned short hda_format;
2267 unsigned int sample_rate_div;
2268 unsigned int sample_rate_mul;
2269 unsigned int num_chans;
2270 unsigned int hda_frame_size_words;
2271 unsigned int remainder_words;
2272 const u32 *data_remainder;
2273 u32 chip_addx_remainder;
2274 unsigned int run_size_words;
2275 const struct dsp_image_seg *hci_write = NULL;
2276 int retry;
2277
2278 if (fls == NULL)
2279 return -EINVAL;
2280 if (is_hci_prog_list_seg(fls)) {
2281 hci_write = fls;
2282 fls = get_next_seg_ptr(fls);
2283 }
2284
2285 if (hci_write && (!fls || is_last(fls))) {
2286 snd_printdd("hci_write\n");
2287 return dspxfr_hci_write(codec, hci_write);
2288 }
2289
2290 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2291 snd_printdd("Invalid Params\n");
2292 return -EINVAL;
2293 }
2294
2295 data = fls->data;
2296 chip_addx = fls->chip_addr,
2297 words_to_write = fls->count;
2298
2299 if (!words_to_write)
2300 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2301 if (reloc)
2302 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2303
2304 if (!UC_RANGE(chip_addx, words_to_write) &&
2305 !X_RANGE_ALL(chip_addx, words_to_write) &&
2306 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2307 snd_printdd("Invalid chip_addx Params\n");
2308 return -EINVAL;
2309 }
2310
2311 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2312 sizeof(u32);
2313
2314 buffer_addx = dma_get_buffer_addr(dma_engine);
2315
2316 if (buffer_addx == NULL) {
2317 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
2318 return -EINVAL;
2319 }
2320
2321 dma_get_converter_format(dma_engine, &hda_format);
2322 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2323 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2324 num_chans = get_hdafmt_chs(hda_format) + 1;
2325
2326 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2327 (num_chans * sample_rate_mul / sample_rate_div));
2328
2329 buffer_size_words = min(buffer_size_words,
2330 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2331 65536 : 32768));
2332 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2333 snd_printdd(
2334 "chpadr=0x%08x frmsz=%u nchan=%u "
2335 "rate_mul=%u div=%u bufsz=%u\n",
2336 chip_addx, hda_frame_size_words, num_chans,
2337 sample_rate_mul, sample_rate_div, buffer_size_words);
2338
2339 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2340 (buffer_size_words < hda_frame_size_words)) {
2341 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
2342 return -EINVAL;
2343 }
2344
2345 remainder_words = words_to_write % hda_frame_size_words;
2346 data_remainder = data;
2347 chip_addx_remainder = chip_addx;
2348
2349 data += remainder_words;
2350 chip_addx += remainder_words*sizeof(u32);
2351 words_to_write -= remainder_words;
2352
2353 while (words_to_write != 0) {
2354 run_size_words = min(buffer_size_words, words_to_write);
2355 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2356 words_to_write, run_size_words, remainder_words);
2357 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2358 if (!comm_dma_setup_done) {
2359 status = dsp_dma_stop(codec, dma_chan, ovly);
2360 if (status < 0)
2361 return -EIO;
2362 status = dsp_dma_setup_common(codec, chip_addx,
2363 dma_chan, port_map_mask, ovly);
2364 if (status < 0)
2365 return status;
2366 comm_dma_setup_done = true;
2367 }
2368
2369 status = dsp_dma_setup(codec, chip_addx,
2370 run_size_words, dma_chan);
2371 if (status < 0)
2372 return status;
2373 status = dsp_dma_start(codec, dma_chan, ovly);
2374 if (status < 0)
2375 return status;
2376 if (!dsp_is_dma_active(codec, dma_chan)) {
2377 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
2378 return -EIO;
2379 }
2380 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2381 if (status < 0)
2382 return status;
2383 if (remainder_words != 0) {
2384 status = chipio_write_multiple(codec,
2385 chip_addx_remainder,
2386 data_remainder,
2387 remainder_words);
2388 remainder_words = 0;
2389 }
2390 if (hci_write) {
2391 status = dspxfr_hci_write(codec, hci_write);
2392 hci_write = NULL;
2393 }
2394 retry = 5000;
2395 while (dsp_is_dma_active(codec, dma_chan)) {
2396 if (--retry <= 0)
2397 break;
2398 }
2399 snd_printdd(KERN_INFO "+++++ DMA complete");
2400 dma_set_state(dma_engine, DMA_STATE_STOP);
2401 dma_reset(dma_engine);
2402
2403 if (status < 0)
2404 return status;
2405
2406 data += run_size_words;
2407 chip_addx += run_size_words*sizeof(u32);
2408 words_to_write -= run_size_words;
2409 }
2410
2411 if (remainder_words != 0) {
2412 status = chipio_write_multiple(codec, chip_addx_remainder,
2413 data_remainder, remainder_words);
2414 }
2415
2416 return status;
2417}
2418
Ian Minettd5c21b82012-09-20 20:29:18 -07002419/**
2420 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2421 *
2422 * @codec: the HDA codec
2423 * @fls_data: pointer to a fast load image
2424 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2425 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002426 * @sample_rate: sampling rate of the stream used for DSP download
2427 * @number_channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002428 * @ovly: TRUE if overlay format is required
2429 *
2430 * Returns zero or a negative error code.
2431 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002432static int dspxfr_image(struct hda_codec *codec,
2433 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002434 unsigned int reloc,
2435 unsigned int sample_rate,
2436 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002437 bool ovly)
2438{
2439 struct ca0132_spec *spec = codec->spec;
2440 int status;
2441 unsigned short hda_format = 0;
2442 unsigned int response;
2443 unsigned char stream_id = 0;
2444 struct dma_engine *dma_engine;
2445 unsigned int dma_chan;
2446 unsigned int port_map_mask;
2447
2448 if (fls_data == NULL)
2449 return -EINVAL;
2450
2451 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai063bca02012-09-21 13:44:34 +02002452 if (!dma_engine)
2453 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002454
2455 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2456 if (!dma_engine->dmab) {
2457 status = -ENOMEM;
2458 goto exit;
2459 }
2460
2461 dma_engine->codec = codec;
Ian Minette97249d2012-09-20 20:29:21 -07002462 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002463 dma_engine->m_converter_format = hda_format;
2464 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2465 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2466
2467 dma_chan = 0;
2468
2469 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2470 hda_format, &response);
2471
2472 if (status < 0) {
2473 snd_printdd(KERN_ERR "set converter format fail");
2474 goto exit;
2475 }
2476
2477 status = snd_hda_codec_load_dsp_prepare(codec,
2478 dma_engine->m_converter_format,
2479 dma_engine->buf_size,
2480 dma_engine->dmab);
2481 if (status < 0)
2482 goto exit;
2483 spec->dsp_stream_id = status;
2484
2485 if (ovly) {
2486 status = dspio_alloc_dma_chan(codec, &dma_chan);
2487 if (status < 0) {
2488 snd_printdd(KERN_ERR "alloc dmachan fail");
2489 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2490 goto exit;
2491 }
2492 }
2493
2494 port_map_mask = 0;
2495 status = dsp_allocate_ports_format(codec, hda_format,
2496 &port_map_mask);
2497 if (status < 0) {
2498 snd_printdd(KERN_ERR "alloc ports fail");
2499 goto exit;
2500 }
2501
2502 stream_id = dma_get_stream_id(dma_engine);
2503 status = codec_set_converter_stream_channel(codec,
2504 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2505 if (status < 0) {
2506 snd_printdd(KERN_ERR "set stream chan fail");
2507 goto exit;
2508 }
2509
2510 while ((fls_data != NULL) && !is_last(fls_data)) {
2511 if (!is_valid(fls_data)) {
2512 snd_printdd(KERN_ERR "FLS check fail");
2513 status = -EINVAL;
2514 goto exit;
2515 }
2516 status = dspxfr_one_seg(codec, fls_data, reloc,
2517 dma_engine, dma_chan,
2518 port_map_mask, ovly);
2519 if (status < 0)
2520 break;
2521
2522 if (is_hci_prog_list_seg(fls_data))
2523 fls_data = get_next_seg_ptr(fls_data);
2524
2525 if ((fls_data != NULL) && !is_last(fls_data))
2526 fls_data = get_next_seg_ptr(fls_data);
2527 }
2528
2529 if (port_map_mask != 0)
2530 status = dsp_free_ports(codec);
2531
2532 if (status < 0)
2533 goto exit;
2534
2535 status = codec_set_converter_stream_channel(codec,
2536 WIDGET_CHIP_CTRL, 0, 0, &response);
2537
2538exit:
2539 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2540 dspio_free_dma_chan(codec, dma_chan);
2541
2542 if (dma_engine->dmab)
2543 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2544 kfree(dma_engine->dmab);
2545 kfree(dma_engine);
2546
2547 return status;
2548}
2549
2550/*
2551 * CA0132 DSP download stuffs.
2552 */
2553static void dspload_post_setup(struct hda_codec *codec)
2554{
2555 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2556
2557 /*set DSP speaker to 2.0 configuration*/
2558 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2559 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2560
2561 /*update write pointer*/
2562 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2563}
2564
Ian Minettd5c21b82012-09-20 20:29:18 -07002565/**
2566 * Download DSP from a DSP Image Fast Load structure. This structure is a
2567 * linear, non-constant sized element array of structures, each of which
2568 * contain the count of the data to be loaded, the data itself, and the
2569 * corresponding starting chip address of the starting data location.
2570 *
2571 * @codec: the HDA codec
2572 * @fls: pointer to a fast load image
2573 * @ovly: TRUE if overlay format is required
2574 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2575 * no relocation
2576 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2577 * @router_chans: number of audio router channels to be allocated (0 means use
2578 * internal defaults; max is 32)
2579 *
2580 * Returns zero or a negative error code.
2581 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002582static int dspload_image(struct hda_codec *codec,
2583 const struct dsp_image_seg *fls,
2584 bool ovly,
2585 unsigned int reloc,
2586 bool autostart,
2587 int router_chans)
2588{
2589 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07002590 unsigned int sample_rate;
2591 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07002592
2593 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2594 if (router_chans == 0) {
2595 if (!ovly)
2596 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2597 else
2598 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2599 }
2600
Ian Minette97249d2012-09-20 20:29:21 -07002601 sample_rate = 48000;
2602 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07002603
Ian Minette97249d2012-09-20 20:29:21 -07002604 while (channels > 16) {
2605 sample_rate *= 2;
2606 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07002607 }
2608
Ian Minett01ef7db2012-09-20 20:29:16 -07002609 do {
2610 snd_printdd(KERN_INFO "Ready to program DMA");
2611 if (!ovly)
2612 status = dsp_reset(codec);
2613
2614 if (status < 0)
2615 break;
2616
2617 snd_printdd(KERN_INFO "dsp_reset() complete");
Ian Minette97249d2012-09-20 20:29:21 -07002618 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2619 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07002620
2621 if (status < 0)
2622 break;
2623
2624 snd_printdd(KERN_INFO "dspxfr_image() complete");
2625 if (autostart && !ovly) {
2626 dspload_post_setup(codec);
2627 status = dsp_set_run_state(codec);
2628 }
2629
2630 snd_printdd(KERN_INFO "LOAD FINISHED");
2631 } while (0);
2632
2633 return status;
2634}
2635
Ian Minettc3b4eea22012-09-20 20:29:17 -07002636static const struct firmware *fw_efx;
2637
2638static int request_firmware_cached(const struct firmware **firmware_p,
2639 const char *name, struct device *device)
2640{
2641 if (*firmware_p)
2642 return 0; /* already loaded */
2643 return request_firmware(firmware_p, name, device);
2644}
2645
2646static void release_cached_firmware(void)
2647{
2648 if (fw_efx) {
2649 release_firmware(fw_efx);
2650 fw_efx = NULL;
2651 }
2652}
2653
Ian Minett01ef7db2012-09-20 20:29:16 -07002654static bool dspload_is_loaded(struct hda_codec *codec)
2655{
2656 unsigned int data = 0;
2657 int status = 0;
2658
2659 status = chipio_read(codec, 0x40004, &data);
2660 if ((status < 0) || (data != 1))
2661 return false;
2662
2663 return true;
2664}
2665
2666static bool dspload_wait_loaded(struct hda_codec *codec)
2667{
2668 int retry = 100;
2669
2670 do {
2671 msleep(20);
2672 if (dspload_is_loaded(codec)) {
2673 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2674 return true;
2675 }
2676 } while (--retry);
2677
2678 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2679 return false;
2680}
2681
Ian Minett5aaca442012-12-20 18:53:34 -08002682/*
2683 * Controls stuffs.
2684 */
Ian Minettef6b2ea2012-12-20 18:53:33 -08002685
2686/*
2687 * Mixer controls helpers.
2688 */
2689#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2690 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2691 .name = xname, \
2692 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2693 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2694 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2695 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2696 .info = ca0132_volume_info, \
2697 .get = ca0132_volume_get, \
2698 .put = ca0132_volume_put, \
2699 .tlv = { .c = ca0132_volume_tlv }, \
2700 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2701
2702#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2703 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2704 .name = xname, \
2705 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2706 .info = snd_hda_mixer_amp_switch_info, \
2707 .get = ca0132_switch_get, \
2708 .put = ca0132_switch_put, \
2709 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2710
2711/* stereo */
2712#define CA0132_CODEC_VOL(xname, nid, dir) \
2713 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2714#define CA0132_CODEC_MUTE(xname, nid, dir) \
2715 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2716
Ian Minett95c6e9c2011-06-15 15:35:17 -07002717/*
Ian Minett825315b2012-12-20 18:53:36 -08002718 * PCM stuffs
Ian Minett95c6e9c2011-06-15 15:35:17 -07002719 */
Ian Minett825315b2012-12-20 18:53:36 -08002720static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2721 u32 stream_tag,
2722 int channel_id, int format)
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002723{
Ian Minett825315b2012-12-20 18:53:36 -08002724 unsigned int oldval, newval;
2725
2726 if (!nid)
2727 return;
2728
2729 snd_printdd(
2730 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2731 "channel=%d, format=0x%x\n",
2732 nid, stream_tag, channel_id, format);
2733
2734 /* update the format-id if changed */
2735 oldval = snd_hda_codec_read(codec, nid, 0,
2736 AC_VERB_GET_STREAM_FORMAT,
2737 0);
2738 if (oldval != format) {
2739 msleep(20);
2740 snd_hda_codec_write(codec, nid, 0,
2741 AC_VERB_SET_STREAM_FORMAT,
2742 format);
2743 }
2744
2745 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2746 newval = (stream_tag << 4) | channel_id;
2747 if (oldval != newval) {
2748 snd_hda_codec_write(codec, nid, 0,
2749 AC_VERB_SET_CHANNEL_STREAMID,
2750 newval);
2751 }
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002752}
2753
Ian Minett825315b2012-12-20 18:53:36 -08002754static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2755{
2756 unsigned int val;
2757
2758 if (!nid)
2759 return;
2760
2761 snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
2762
2763 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2764 if (!val)
2765 return;
2766
2767 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2768 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2769}
2770
2771/*
2772 * PCM callbacks
2773 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002774static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2775 struct hda_codec *codec,
2776 unsigned int stream_tag,
2777 unsigned int format,
2778 struct snd_pcm_substream *substream)
2779{
2780 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002781
2782 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2783
2784 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002785}
2786
2787static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2788 struct hda_codec *codec,
2789 struct snd_pcm_substream *substream)
2790{
2791 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002792
2793 if (spec->dsp_state == DSP_DOWNLOADING)
2794 return 0;
2795
2796 /*If Playback effects are on, allow stream some time to flush
2797 *effects tail*/
2798 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
2799 msleep(50);
2800
2801 ca0132_cleanup_stream(codec, spec->dacs[0]);
2802
2803 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002804}
2805
2806/*
2807 * Digital out
2808 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002809static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2810 struct hda_codec *codec,
2811 struct snd_pcm_substream *substream)
2812{
2813 struct ca0132_spec *spec = codec->spec;
2814 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2815}
2816
Ian Minett95c6e9c2011-06-15 15:35:17 -07002817static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2818 struct hda_codec *codec,
2819 unsigned int stream_tag,
2820 unsigned int format,
2821 struct snd_pcm_substream *substream)
2822{
2823 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002824 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2825 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002826}
2827
2828static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2829 struct hda_codec *codec,
2830 struct snd_pcm_substream *substream)
2831{
2832 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002833 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002834}
2835
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002836static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2837 struct hda_codec *codec,
2838 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002839{
2840 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002841 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002842}
2843
2844/*
Ian Minett825315b2012-12-20 18:53:36 -08002845 * Analog capture
2846 */
2847static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2848 struct hda_codec *codec,
2849 unsigned int stream_tag,
2850 unsigned int format,
2851 struct snd_pcm_substream *substream)
2852{
2853 struct ca0132_spec *spec = codec->spec;
2854
2855 ca0132_setup_stream(codec, spec->adcs[substream->number],
2856 stream_tag, 0, format);
2857
2858 return 0;
2859}
2860
2861static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2862 struct hda_codec *codec,
2863 struct snd_pcm_substream *substream)
2864{
2865 struct ca0132_spec *spec = codec->spec;
2866
2867 if (spec->dsp_state == DSP_DOWNLOADING)
2868 return 0;
2869
2870 ca0132_cleanup_stream(codec, hinfo->nid);
2871 return 0;
2872}
2873
2874/*
Ian Minett5aaca442012-12-20 18:53:34 -08002875 * Select the active output.
2876 * If autodetect is enabled, output will be selected based on jack detection.
2877 * If jack inserted, headphone will be selected, else built-in speakers
2878 * If autodetect is disabled, output will be selected based on selection.
2879 */
2880static int ca0132_select_out(struct hda_codec *codec)
2881{
2882 struct ca0132_spec *spec = codec->spec;
2883 unsigned int pin_ctl;
2884 int jack_present;
2885 int auto_jack;
2886 unsigned int tmp;
2887 int err;
2888
2889 snd_printdd(KERN_INFO "ca0132_select_out\n");
2890
2891 snd_hda_power_up(codec);
2892
2893 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
2894
2895 if (auto_jack)
2896 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
2897 else
2898 jack_present =
2899 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
2900
2901 if (jack_present)
2902 spec->cur_out_type = HEADPHONE_OUT;
2903 else
2904 spec->cur_out_type = SPEAKER_OUT;
2905
2906 if (spec->cur_out_type == SPEAKER_OUT) {
2907 snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
2908 /*speaker out config*/
2909 tmp = FLOAT_ONE;
2910 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
2911 if (err < 0)
2912 goto exit;
2913 /*enable speaker EQ*/
2914 tmp = FLOAT_ONE;
2915 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
2916 if (err < 0)
2917 goto exit;
2918
2919 /* Setup EAPD */
2920 snd_hda_codec_write(codec, spec->out_pins[1], 0,
2921 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
2922 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2923 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
2924 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2925 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
2926 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2927 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
2928
2929 /* disable headphone node */
2930 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
2931 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2932 snd_hda_codec_write(codec, spec->out_pins[1], 0,
2933 AC_VERB_SET_PIN_WIDGET_CONTROL,
2934 pin_ctl & 0xBF);
2935 /* enable speaker node */
2936 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
2937 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2938 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2939 AC_VERB_SET_PIN_WIDGET_CONTROL,
2940 pin_ctl | 0x40);
2941 } else {
2942 snd_printdd(KERN_INFO "ca0132_select_out hp\n");
2943 /*headphone out config*/
2944 tmp = FLOAT_ZERO;
2945 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
2946 if (err < 0)
2947 goto exit;
2948 /*disable speaker EQ*/
2949 tmp = FLOAT_ZERO;
2950 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
2951 if (err < 0)
2952 goto exit;
2953
2954 /* Setup EAPD */
2955 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2956 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
2957 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2958 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
2959 snd_hda_codec_write(codec, spec->out_pins[1], 0,
2960 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
2961 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2962 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
2963
2964 /* disable speaker*/
2965 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
2966 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2967 snd_hda_codec_write(codec, spec->out_pins[0], 0,
2968 AC_VERB_SET_PIN_WIDGET_CONTROL,
2969 pin_ctl & 0xBF);
2970 /* enable headphone*/
2971 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
2972 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2973 snd_hda_codec_write(codec, spec->out_pins[1], 0,
2974 AC_VERB_SET_PIN_WIDGET_CONTROL,
2975 pin_ctl | 0x40);
2976 }
2977
2978exit:
2979 snd_hda_power_down(codec);
2980
2981 return err < 0 ? err : 0;
2982}
2983
2984static void ca0132_set_dmic(struct hda_codec *codec, int enable);
2985static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
2986static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
2987
2988/*
2989 * Select the active VIP source
2990 */
2991static int ca0132_set_vipsource(struct hda_codec *codec, int val)
2992{
2993 struct ca0132_spec *spec = codec->spec;
2994 unsigned int tmp;
2995
2996 if (!dspload_is_loaded(codec))
2997 return 0;
2998
2999 /* if CrystalVoice if off, vipsource should be 0 */
3000 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
3001 (val == 0)) {
3002 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3003 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3004 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3005 if (spec->cur_mic_type == DIGITAL_MIC)
3006 tmp = FLOAT_TWO;
3007 else
3008 tmp = FLOAT_ONE;
3009 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3010 tmp = FLOAT_ZERO;
3011 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3012 } else {
3013 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3014 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3015 if (spec->cur_mic_type == DIGITAL_MIC)
3016 tmp = FLOAT_TWO;
3017 else
3018 tmp = FLOAT_ONE;
3019 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3020 tmp = FLOAT_ONE;
3021 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3022 msleep(20);
3023 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3024 }
3025
3026 return 1;
3027}
3028
3029/*
3030 * Select the active microphone.
3031 * If autodetect is enabled, mic will be selected based on jack detection.
3032 * If jack inserted, ext.mic will be selected, else built-in mic
3033 * If autodetect is disabled, mic will be selected based on selection.
3034 */
3035static int ca0132_select_mic(struct hda_codec *codec)
3036{
3037 struct ca0132_spec *spec = codec->spec;
3038 int jack_present;
3039 int auto_jack;
3040
3041 snd_printdd(KERN_INFO "ca0132_select_mic\n");
3042
3043 snd_hda_power_up(codec);
3044
3045 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3046
3047 if (auto_jack)
3048 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3049 else
3050 jack_present =
3051 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3052
3053 if (jack_present)
3054 spec->cur_mic_type = LINE_MIC_IN;
3055 else
3056 spec->cur_mic_type = DIGITAL_MIC;
3057
3058 if (spec->cur_mic_type == DIGITAL_MIC) {
3059 /* enable digital Mic */
3060 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3061 ca0132_set_dmic(codec, 1);
3062 ca0132_mic_boost_set(codec, 0);
3063 /* set voice focus */
3064 ca0132_effects_set(codec, VOICE_FOCUS,
3065 spec->effects_switch
3066 [VOICE_FOCUS - EFFECT_START_NID]);
3067 } else {
3068 /* disable digital Mic */
3069 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3070 ca0132_set_dmic(codec, 0);
3071 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3072 /* disable voice focus */
3073 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3074 }
3075
3076 snd_hda_power_down(codec);
3077
3078 return 0;
3079}
3080
3081/*
Ian Minetta7e76272012-12-20 18:53:35 -08003082 * Check if VNODE settings take effect immediately.
3083 */
3084static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3085 hda_nid_t vnid,
3086 hda_nid_t *shared_nid)
3087{
3088 struct ca0132_spec *spec = codec->spec;
3089 hda_nid_t nid;
3090 bool effective = false;
3091
3092 switch (vnid) {
3093 case VNID_SPK:
3094 nid = spec->shared_out_nid;
3095 effective = true;
3096 break;
3097 case VNID_MIC:
3098 nid = spec->shared_mic_nid;
3099 effective = true;
3100 break;
3101 default:
3102 break;
3103 }
3104
3105 if (effective && shared_nid)
3106 *shared_nid = nid;
3107
3108 return effective;
3109}
3110
3111/*
3112* The following functions are control change helpers.
3113* They return 0 if no changed. Return 1 if changed.
3114*/
3115static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3116{
3117 struct ca0132_spec *spec = codec->spec;
3118 unsigned int tmp;
3119
3120 /* based on CrystalVoice state to enable VoiceFX. */
3121 if (enable) {
3122 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3123 FLOAT_ONE : FLOAT_ZERO;
3124 } else {
3125 tmp = FLOAT_ZERO;
3126 }
3127
3128 dspio_set_uint_param(codec, ca0132_voicefx.mid,
3129 ca0132_voicefx.reqs[0], tmp);
3130
3131 return 1;
3132}
3133
3134/*
Ian Minett5aaca442012-12-20 18:53:34 -08003135 * Set the effects parameters
3136 */
3137static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3138{
3139 struct ca0132_spec *spec = codec->spec;
3140 unsigned int on;
3141 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3142 int err = 0;
3143 int idx = nid - EFFECT_START_NID;
3144
3145 if ((idx < 0) || (idx >= num_fx))
3146 return 0; /* no changed */
3147
3148 /* for out effect, qualify with PE */
3149 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3150 /* if PE if off, turn off out effects. */
3151 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3152 val = 0;
3153 }
3154
3155 /* for in effect, qualify with CrystalVoice */
3156 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3157 /* if CrystalVoice if off, turn off in effects. */
3158 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3159 val = 0;
3160
3161 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3162 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3163 val = 0;
3164 }
3165
3166 snd_printdd(KERN_INFO, "ca0132_effect_set: nid=0x%x, val=%ld\n",
3167 nid, val);
3168
3169 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3170 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
3171 ca0132_effects[idx].reqs[0], on);
3172
3173 if (err < 0)
3174 return 0; /* no changed */
3175
3176 return 1;
3177}
3178
Ian Minetta7e76272012-12-20 18:53:35 -08003179/*
3180 * Turn on/off Playback Enhancements
3181 */
3182static int ca0132_pe_switch_set(struct hda_codec *codec)
3183{
3184 struct ca0132_spec *spec = codec->spec;
3185 hda_nid_t nid;
3186 int i, ret = 0;
3187
3188 snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
3189 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3190
3191 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3192 nid = OUT_EFFECT_START_NID;
3193 /* PE affects all out effects */
3194 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3195 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3196
3197 return ret;
3198}
3199
Ian Minett5aaca442012-12-20 18:53:34 -08003200/* Check if Mic1 is streaming, if so, stop streaming */
3201static int stop_mic1(struct hda_codec *codec)
3202{
3203 struct ca0132_spec *spec = codec->spec;
3204 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
3205 AC_VERB_GET_CONV, 0);
3206 if (oldval != 0)
3207 snd_hda_codec_write(codec, spec->adcs[0], 0,
3208 AC_VERB_SET_CHANNEL_STREAMID,
3209 0);
3210 return oldval;
3211}
3212
3213/* Resume Mic1 streaming if it was stopped. */
3214static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
3215{
3216 struct ca0132_spec *spec = codec->spec;
3217 /* Restore the previous stream and channel */
3218 if (oldval != 0)
3219 snd_hda_codec_write(codec, spec->adcs[0], 0,
3220 AC_VERB_SET_CHANNEL_STREAMID,
3221 oldval);
3222}
3223
3224/*
Ian Minetta7e76272012-12-20 18:53:35 -08003225 * Turn on/off CrystalVoice
Ian Minett5aaca442012-12-20 18:53:34 -08003226 */
Ian Minetta7e76272012-12-20 18:53:35 -08003227static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3228{
3229 struct ca0132_spec *spec = codec->spec;
3230 hda_nid_t nid;
3231 int i, ret = 0;
3232 unsigned int oldval;
3233
3234 snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
3235 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3236
3237 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3238 nid = IN_EFFECT_START_NID;
3239 /* CrystalVoice affects all in effects */
3240 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3241 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3242
3243 /* including VoiceFX */
3244 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3245
3246 /* set correct vipsource */
3247 oldval = stop_mic1(codec);
3248 ret |= ca0132_set_vipsource(codec, 1);
3249 resume_mic1(codec, oldval);
3250 return ret;
3251}
3252
Ian Minett5aaca442012-12-20 18:53:34 -08003253static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3254{
3255 struct ca0132_spec *spec = codec->spec;
3256 int ret = 0;
3257
3258 if (val) /* on */
3259 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3260 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3261 else /* off */
3262 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3263 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3264
3265 return ret;
3266}
3267
Ian Minetta7e76272012-12-20 18:53:35 -08003268static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3269 struct snd_ctl_elem_value *ucontrol)
3270{
3271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3272 hda_nid_t nid = get_amp_nid(kcontrol);
3273 hda_nid_t shared_nid = 0;
3274 bool effective;
3275 int ret = 0;
3276 struct ca0132_spec *spec = codec->spec;
3277 int auto_jack;
3278
3279 if (nid == VNID_HP_SEL) {
3280 auto_jack =
3281 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3282 if (!auto_jack)
3283 ca0132_select_out(codec);
3284 return 1;
3285 }
3286
3287 if (nid == VNID_AMIC1_SEL) {
3288 auto_jack =
3289 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3290 if (!auto_jack)
3291 ca0132_select_mic(codec);
3292 return 1;
3293 }
3294
3295 if (nid == VNID_HP_ASEL) {
3296 ca0132_select_out(codec);
3297 return 1;
3298 }
3299
3300 if (nid == VNID_AMIC1_ASEL) {
3301 ca0132_select_mic(codec);
3302 return 1;
3303 }
3304
3305 /* if effective conditions, then update hw immediately. */
3306 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3307 if (effective) {
3308 int dir = get_amp_direction(kcontrol);
3309 int ch = get_amp_channels(kcontrol);
3310 unsigned long pval;
3311
3312 mutex_lock(&codec->control_mutex);
3313 pval = kcontrol->private_value;
3314 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3315 0, dir);
3316 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3317 kcontrol->private_value = pval;
3318 mutex_unlock(&codec->control_mutex);
3319 }
3320
3321 return ret;
3322}
3323/* End of control change helpers. */
3324
3325static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3326 struct snd_ctl_elem_info *uinfo)
3327{
3328 unsigned int items = sizeof(ca0132_voicefx_presets)
3329 / sizeof(struct ct_voicefx_preset);
3330
3331 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3332 uinfo->count = 1;
3333 uinfo->value.enumerated.items = items;
3334 if (uinfo->value.enumerated.item >= items)
3335 uinfo->value.enumerated.item = items - 1;
3336 strcpy(uinfo->value.enumerated.name,
3337 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3338 return 0;
3339}
3340
3341static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
3342 struct snd_ctl_elem_value *ucontrol)
3343{
3344 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3345 struct ca0132_spec *spec = codec->spec;
3346
3347 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
3348 return 0;
3349}
3350
3351static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_value *ucontrol)
3353{
3354 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3355 struct ca0132_spec *spec = codec->spec;
3356 int i, err = 0;
3357 int sel = ucontrol->value.enumerated.item[0];
3358 unsigned int items = sizeof(ca0132_voicefx_presets)
3359 / sizeof(struct ct_voicefx_preset);
3360
3361 if (sel >= items)
3362 return 0;
3363
3364 snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
3365 sel, ca0132_voicefx_presets[sel].name);
3366
3367 /*
3368 * Idx 0 is default.
3369 * Default needs to qualify with CrystalVoice state.
3370 */
3371 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3372 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
3373 ca0132_voicefx.reqs[i],
3374 ca0132_voicefx_presets[sel].vals[i]);
3375 if (err < 0)
3376 break;
3377 }
3378
3379 if (err >= 0) {
3380 spec->voicefx_val = sel;
3381 /* enable voice fx */
3382 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3383 }
3384
3385 return 1;
3386}
3387
3388static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3389 struct snd_ctl_elem_value *ucontrol)
3390{
3391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3392 struct ca0132_spec *spec = codec->spec;
3393 hda_nid_t nid = get_amp_nid(kcontrol);
3394 int ch = get_amp_channels(kcontrol);
3395 long *valp = ucontrol->value.integer.value;
3396
3397 /* vnode */
3398 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3399 if (ch & 1) {
3400 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3401 valp++;
3402 }
3403 if (ch & 2) {
3404 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3405 valp++;
3406 }
3407 return 0;
3408 }
3409
3410 /* effects, include PE and CrystalVoice */
3411 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3412 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3413 return 0;
3414 }
3415
3416 /* mic boost */
3417 if (nid == spec->input_pins[0]) {
3418 *valp = spec->cur_mic_boost;
3419 return 0;
3420 }
3421
3422 return 0;
3423}
3424
3425static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3426 struct snd_ctl_elem_value *ucontrol)
3427{
3428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3429 struct ca0132_spec *spec = codec->spec;
3430 hda_nid_t nid = get_amp_nid(kcontrol);
3431 int ch = get_amp_channels(kcontrol);
3432 long *valp = ucontrol->value.integer.value;
3433 int changed = 1;
3434
3435 snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
3436 nid, *valp);
3437
3438 snd_hda_power_up(codec);
3439 /* vnode */
3440 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3441 if (ch & 1) {
3442 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3443 valp++;
3444 }
3445 if (ch & 2) {
3446 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3447 valp++;
3448 }
3449 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3450 goto exit;
3451 }
3452
3453 /* PE */
3454 if (nid == PLAY_ENHANCEMENT) {
3455 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3456 changed = ca0132_pe_switch_set(codec);
3457 goto exit;
3458 }
3459
3460 /* CrystalVoice */
3461 if (nid == CRYSTAL_VOICE) {
3462 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3463 changed = ca0132_cvoice_switch_set(codec);
3464 goto exit;
3465 }
3466
3467 /* out and in effects */
3468 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3469 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3470 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3471 changed = ca0132_effects_set(codec, nid, *valp);
3472 goto exit;
3473 }
3474
3475 /* mic boost */
3476 if (nid == spec->input_pins[0]) {
3477 spec->cur_mic_boost = *valp;
3478
3479 /* Mic boost does not apply to Digital Mic */
3480 if (spec->cur_mic_type != DIGITAL_MIC)
3481 changed = ca0132_mic_boost_set(codec, *valp);
3482 goto exit;
3483 }
3484
3485exit:
3486 snd_hda_power_down(codec);
3487 return changed;
3488}
3489
3490/*
3491 * Volume related
3492 */
3493static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3494 struct snd_ctl_elem_info *uinfo)
3495{
3496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3497 struct ca0132_spec *spec = codec->spec;
3498 hda_nid_t nid = get_amp_nid(kcontrol);
3499 int ch = get_amp_channels(kcontrol);
3500 int dir = get_amp_direction(kcontrol);
3501 unsigned long pval;
3502 int err;
3503
3504 switch (nid) {
3505 case VNID_SPK:
3506 /* follow shared_out info */
3507 nid = spec->shared_out_nid;
3508 mutex_lock(&codec->control_mutex);
3509 pval = kcontrol->private_value;
3510 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3511 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3512 kcontrol->private_value = pval;
3513 mutex_unlock(&codec->control_mutex);
3514 break;
3515 case VNID_MIC:
3516 /* follow shared_mic info */
3517 nid = spec->shared_mic_nid;
3518 mutex_lock(&codec->control_mutex);
3519 pval = kcontrol->private_value;
3520 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3521 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3522 kcontrol->private_value = pval;
3523 mutex_unlock(&codec->control_mutex);
3524 break;
3525 default:
3526 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3527 }
3528 return err;
3529}
3530
3531static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
3532 struct snd_ctl_elem_value *ucontrol)
3533{
3534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3535 struct ca0132_spec *spec = codec->spec;
3536 hda_nid_t nid = get_amp_nid(kcontrol);
3537 int ch = get_amp_channels(kcontrol);
3538 long *valp = ucontrol->value.integer.value;
3539
3540 /* store the left and right volume */
3541 if (ch & 1) {
3542 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3543 valp++;
3544 }
3545 if (ch & 2) {
3546 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3547 valp++;
3548 }
3549 return 0;
3550}
3551
3552static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
3553 struct snd_ctl_elem_value *ucontrol)
3554{
3555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3556 struct ca0132_spec *spec = codec->spec;
3557 hda_nid_t nid = get_amp_nid(kcontrol);
3558 int ch = get_amp_channels(kcontrol);
3559 long *valp = ucontrol->value.integer.value;
3560 hda_nid_t shared_nid = 0;
3561 bool effective;
3562 int changed = 1;
3563
3564 /* store the left and right volume */
3565 if (ch & 1) {
3566 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3567 valp++;
3568 }
3569 if (ch & 2) {
3570 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3571 valp++;
3572 }
3573
3574 /* if effective conditions, then update hw immediately. */
3575 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3576 if (effective) {
3577 int dir = get_amp_direction(kcontrol);
3578 unsigned long pval;
3579
3580 snd_hda_power_up(codec);
3581 mutex_lock(&codec->control_mutex);
3582 pval = kcontrol->private_value;
3583 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3584 0, dir);
3585 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3586 kcontrol->private_value = pval;
3587 mutex_unlock(&codec->control_mutex);
3588 snd_hda_power_down(codec);
3589 }
3590
3591 return changed;
3592}
3593
3594static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3595 unsigned int size, unsigned int __user *tlv)
3596{
3597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3598 struct ca0132_spec *spec = codec->spec;
3599 hda_nid_t nid = get_amp_nid(kcontrol);
3600 int ch = get_amp_channels(kcontrol);
3601 int dir = get_amp_direction(kcontrol);
3602 unsigned long pval;
3603 int err;
3604
3605 switch (nid) {
3606 case VNID_SPK:
3607 /* follow shared_out tlv */
3608 nid = spec->shared_out_nid;
3609 mutex_lock(&codec->control_mutex);
3610 pval = kcontrol->private_value;
3611 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3612 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3613 kcontrol->private_value = pval;
3614 mutex_unlock(&codec->control_mutex);
3615 break;
3616 case VNID_MIC:
3617 /* follow shared_mic tlv */
3618 nid = spec->shared_mic_nid;
3619 mutex_lock(&codec->control_mutex);
3620 pval = kcontrol->private_value;
3621 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3622 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3623 kcontrol->private_value = pval;
3624 mutex_unlock(&codec->control_mutex);
3625 break;
3626 default:
3627 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3628 }
3629 return err;
3630}
3631
3632static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3633 const char *pfx, int dir)
3634{
3635 char namestr[44];
3636 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3637 struct snd_kcontrol_new knew =
3638 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3639 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
3640 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3641}
3642
3643static int add_voicefx(struct hda_codec *codec)
3644{
3645 struct snd_kcontrol_new knew =
3646 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3647 VOICEFX, 1, 0, HDA_INPUT);
3648 knew.info = ca0132_voicefx_info;
3649 knew.get = ca0132_voicefx_get;
3650 knew.put = ca0132_voicefx_put;
3651 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
3652}
3653
3654/*
3655 * When changing Node IDs for Mixer Controls below, make sure to update
3656 * Node IDs in ca0132_config() as well.
3657 */
3658static struct snd_kcontrol_new ca0132_mixer[] = {
3659 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3660 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3661 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3662 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3663 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3664 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3665 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3666 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3667 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3668 0x12, 1, HDA_INPUT),
3669 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3670 VNID_HP_SEL, 1, HDA_OUTPUT),
3671 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3672 VNID_AMIC1_SEL, 1, HDA_INPUT),
3673 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
3674 VNID_HP_ASEL, 1, HDA_OUTPUT),
3675 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
3676 VNID_AMIC1_ASEL, 1, HDA_INPUT),
3677 { } /* end */
3678};
3679
Ian Minett5aaca442012-12-20 18:53:34 -08003680/*
Ian Minett95c6e9c2011-06-15 15:35:17 -07003681 */
3682static struct hda_pcm_stream ca0132_pcm_analog_playback = {
3683 .substreams = 1,
3684 .channels_min = 2,
Ian Minett825315b2012-12-20 18:53:36 -08003685 .channels_max = 6,
Ian Minett95c6e9c2011-06-15 15:35:17 -07003686 .ops = {
3687 .prepare = ca0132_playback_pcm_prepare,
3688 .cleanup = ca0132_playback_pcm_cleanup
3689 },
3690};
3691
3692static struct hda_pcm_stream ca0132_pcm_analog_capture = {
3693 .substreams = 1,
3694 .channels_min = 2,
3695 .channels_max = 2,
Ian Minett825315b2012-12-20 18:53:36 -08003696 .ops = {
3697 .prepare = ca0132_capture_pcm_prepare,
3698 .cleanup = ca0132_capture_pcm_cleanup
3699 },
Ian Minett95c6e9c2011-06-15 15:35:17 -07003700};
3701
3702static struct hda_pcm_stream ca0132_pcm_digital_playback = {
3703 .substreams = 1,
3704 .channels_min = 2,
3705 .channels_max = 2,
3706 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003707 .open = ca0132_dig_playback_pcm_open,
3708 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07003709 .prepare = ca0132_dig_playback_pcm_prepare,
3710 .cleanup = ca0132_dig_playback_pcm_cleanup
3711 },
3712};
3713
3714static struct hda_pcm_stream ca0132_pcm_digital_capture = {
3715 .substreams = 1,
3716 .channels_min = 2,
3717 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07003718};
3719
3720static int ca0132_build_pcms(struct hda_codec *codec)
3721{
3722 struct ca0132_spec *spec = codec->spec;
3723 struct hda_pcm *info = spec->pcm_rec;
3724
3725 codec->pcm_info = info;
3726 codec->num_pcms = 0;
3727
3728 info->name = "CA0132 Analog";
3729 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
3730 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
3731 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3732 spec->multiout.max_channels;
3733 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
Ian Minett825315b2012-12-20 18:53:36 -08003734 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003735 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
3736 codec->num_pcms++;
3737
Ian Minett825315b2012-12-20 18:53:36 -08003738 info++;
3739 info->name = "CA0132 Analog Mic-In2";
3740 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
3741 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
3742 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
3743 codec->num_pcms++;
3744
3745 info++;
3746 info->name = "CA0132 What U Hear";
3747 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
3748 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
3749 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
3750 codec->num_pcms++;
3751
Ian Minett95c6e9c2011-06-15 15:35:17 -07003752 if (!spec->dig_out && !spec->dig_in)
3753 return 0;
3754
3755 info++;
3756 info->name = "CA0132 Digital";
3757 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3758 if (spec->dig_out) {
3759 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3760 ca0132_pcm_digital_playback;
3761 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
3762 }
3763 if (spec->dig_in) {
3764 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3765 ca0132_pcm_digital_capture;
3766 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
3767 }
3768 codec->num_pcms++;
3769
3770 return 0;
3771}
3772
Ian Minett95c6e9c2011-06-15 15:35:17 -07003773static int ca0132_build_controls(struct hda_codec *codec)
3774{
3775 struct ca0132_spec *spec = codec->spec;
Ian Minetta7e76272012-12-20 18:53:35 -08003776 int i, num_fx;
3777 int err = 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003778
Ian Minetta7e76272012-12-20 18:53:35 -08003779 /* Add Mixer controls */
3780 for (i = 0; i < spec->num_mixers; i++) {
3781 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003782 if (err < 0)
3783 return err;
3784 }
3785
Ian Minetta7e76272012-12-20 18:53:35 -08003786 /* Add in and out effects controls.
3787 * VoiceFX, PE and CrystalVoice are added separately.
3788 */
3789 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3790 for (i = 0; i < num_fx; i++) {
3791 err = add_fx_switch(codec, ca0132_effects[i].nid,
3792 ca0132_effects[i].name,
3793 ca0132_effects[i].direct);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003794 if (err < 0)
3795 return err;
3796 }
3797
Ian Minetta7e76272012-12-20 18:53:35 -08003798 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
3799 if (err < 0)
3800 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003801
Ian Minetta7e76272012-12-20 18:53:35 -08003802 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
3803 if (err < 0)
3804 return err;
3805
3806 add_voicefx(codec);
3807
3808 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3809 if (err < 0)
3810 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003811
3812 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +02003813 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
3814 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003815 if (err < 0)
3816 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02003817 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003818 if (err < 0)
3819 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02003820 /* spec->multiout.share_spdif = 1; */
Ian Minett95c6e9c2011-06-15 15:35:17 -07003821 }
3822
3823 if (spec->dig_in) {
3824 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
3825 if (err < 0)
3826 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003827 }
3828 return 0;
3829}
3830
Ian Minetta73d5112012-12-20 18:53:37 -08003831static void ca0132_init_unsol(struct hda_codec *codec)
3832{
3833 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
3834 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
3835}
3836
Ian Minett5aaca442012-12-20 18:53:34 -08003837static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
3838{
3839 unsigned int caps;
3840
3841 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
3842 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
3843 snd_hda_override_amp_caps(codec, nid, dir, caps);
3844}
3845
3846/*
3847 * Switch between Digital built-in mic and analog mic.
3848 */
3849static void ca0132_set_dmic(struct hda_codec *codec, int enable)
3850{
3851 struct ca0132_spec *spec = codec->spec;
3852 unsigned int tmp;
3853 u8 val;
3854 unsigned int oldval;
3855
3856 snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
3857
3858 oldval = stop_mic1(codec);
3859 ca0132_set_vipsource(codec, 0);
3860 if (enable) {
3861 /* set DMic input as 2-ch */
3862 tmp = FLOAT_TWO;
3863 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3864
3865 val = spec->dmic_ctl;
3866 val |= 0x80;
3867 snd_hda_codec_write(codec, spec->input_pins[0], 0,
3868 VENDOR_CHIPIO_DMIC_CTL_SET, val);
3869
3870 if (!(spec->dmic_ctl & 0x20))
3871 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
3872 } else {
3873 /* set AMic input as mono */
3874 tmp = FLOAT_ONE;
3875 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3876
3877 val = spec->dmic_ctl;
3878 /* clear bit7 and bit5 to disable dmic */
3879 val &= 0x5f;
3880 snd_hda_codec_write(codec, spec->input_pins[0], 0,
3881 VENDOR_CHIPIO_DMIC_CTL_SET, val);
3882
3883 if (!(spec->dmic_ctl & 0x20))
3884 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
3885 }
3886 ca0132_set_vipsource(codec, 1);
3887 resume_mic1(codec, oldval);
3888}
3889
3890/*
3891 * Initialization for Digital Mic.
3892 */
3893static void ca0132_init_dmic(struct hda_codec *codec)
3894{
3895 struct ca0132_spec *spec = codec->spec;
3896 u8 val;
3897
3898 /* Setup Digital Mic here, but don't enable.
3899 * Enable based on jack detect.
3900 */
3901
3902 /* MCLK uses MPIO1, set to enable.
3903 * Bit 2-0: MPIO select
3904 * Bit 3: set to disable
3905 * Bit 7-4: reserved
3906 */
3907 val = 0x01;
3908 snd_hda_codec_write(codec, spec->input_pins[0], 0,
3909 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
3910
3911 /* Data1 uses MPIO3. Data2 not use
3912 * Bit 2-0: Data1 MPIO select
3913 * Bit 3: set disable Data1
3914 * Bit 6-4: Data2 MPIO select
3915 * Bit 7: set disable Data2
3916 */
3917 val = 0x83;
3918 snd_hda_codec_write(codec, spec->input_pins[0], 0,
3919 VENDOR_CHIPIO_DMIC_PIN_SET, val);
3920
3921 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
3922 * Bit 3-0: Channel mask
3923 * Bit 4: set for 48KHz, clear for 32KHz
3924 * Bit 5: mode
3925 * Bit 6: set to select Data2, clear for Data1
3926 * Bit 7: set to enable DMic, clear for AMic
3927 */
3928 val = 0x23;
3929 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
3930 spec->dmic_ctl = val;
3931 snd_hda_codec_write(codec, spec->input_pins[0], 0,
3932 VENDOR_CHIPIO_DMIC_CTL_SET, val);
3933}
3934
3935/*
3936 * Initialization for Analog Mic 2
3937 */
3938static void ca0132_init_analog_mic2(struct hda_codec *codec)
3939{
3940 struct ca0132_spec *spec = codec->spec;
3941
3942 mutex_lock(&spec->chipio_mutex);
3943 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3944 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
3945 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3946 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
3947 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3948 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
3949 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3950 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
3951 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3952 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
3953 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3954 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
3955 mutex_unlock(&spec->chipio_mutex);
3956}
3957
3958static void ca0132_refresh_widget_caps(struct hda_codec *codec)
3959{
3960 struct ca0132_spec *spec = codec->spec;
3961 int i;
3962 hda_nid_t nid;
3963
3964 snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
3965 nid = codec->start_nid;
3966 for (i = 0; i < codec->num_nodes; i++, nid++)
3967 codec->wcaps[i] = snd_hda_param_read(codec, nid,
3968 AC_PAR_AUDIO_WIDGET_CAP);
3969
3970 for (i = 0; i < spec->multiout.num_dacs; i++)
3971 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
3972
3973 for (i = 0; i < spec->num_outputs; i++)
3974 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
3975
3976 for (i = 0; i < spec->num_inputs; i++) {
3977 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
3978 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
3979 }
3980}
3981
3982/*
3983 * Setup default parameters for DSP
3984 */
3985static void ca0132_setup_defaults(struct hda_codec *codec)
3986{
3987 unsigned int tmp;
3988 int num_fx;
3989 int idx, i;
3990
3991 if (!dspload_is_loaded(codec))
3992 return;
3993
3994 /* out, in effects + voicefx */
3995 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
3996 for (idx = 0; idx < num_fx; idx++) {
3997 for (i = 0; i <= ca0132_effects[idx].params; i++) {
3998 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
3999 ca0132_effects[idx].reqs[i],
4000 ca0132_effects[idx].def_vals[i]);
4001 }
4002 }
4003
4004 /*remove DSP headroom*/
4005 tmp = FLOAT_ZERO;
4006 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
4007
4008 /*set speaker EQ bypass attenuation*/
4009 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
4010
4011 /* set AMic1 and AMic2 as mono mic */
4012 tmp = FLOAT_ONE;
4013 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4014 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
4015
4016 /* set AMic1 as CrystalVoice input */
4017 tmp = FLOAT_ONE;
4018 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4019
4020 /* set WUH source */
4021 tmp = FLOAT_TWO;
4022 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
4023}
4024
4025/*
4026 * Initialization of flags in chip
4027 */
4028static void ca0132_init_flags(struct hda_codec *codec)
4029{
4030 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4031 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4032 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4033 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4034 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4035 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4036}
4037
4038/*
4039 * Initialization of parameters in chip
4040 */
4041static void ca0132_init_params(struct hda_codec *codec)
4042{
4043 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4044 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4045}
Ian Minett95c6e9c2011-06-15 15:35:17 -07004046
Ian Minett95c6e9c2011-06-15 15:35:17 -07004047static void ca0132_config(struct hda_codec *codec)
4048{
4049 struct ca0132_spec *spec = codec->spec;
4050 struct auto_pin_cfg *cfg = &spec->autocfg;
4051
Ian Minetta7e76272012-12-20 18:53:35 -08004052 spec->dacs[0] = 0x2;
4053 spec->dacs[1] = 0x3;
4054 spec->dacs[2] = 0x4;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02004055
Ian Minett95c6e9c2011-06-15 15:35:17 -07004056 spec->multiout.dac_nids = spec->dacs;
Ian Minetta7e76272012-12-20 18:53:35 -08004057 spec->multiout.num_dacs = 3;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004058 spec->multiout.max_channels = 2;
4059
Ian Minetta7e76272012-12-20 18:53:35 -08004060 spec->num_outputs = 2;
4061 spec->out_pins[0] = 0x0b; /* speaker out */
4062 spec->out_pins[1] = 0x10; /* headphone out */
4063 spec->shared_out_nid = 0x2;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004064
Ian Minetta7e76272012-12-20 18:53:35 -08004065 spec->num_inputs = 3;
4066 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4067 spec->adcs[1] = 0x8; /* analog mic2 */
4068 spec->adcs[2] = 0xa; /* what u hear */
4069 spec->shared_mic_nid = 0x7;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004070
Ian Minett95c6e9c2011-06-15 15:35:17 -07004071 spec->input_pins[0] = 0x12;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004072 spec->input_pins[1] = 0x11;
Ian Minetta7e76272012-12-20 18:53:35 -08004073 spec->input_pins[2] = 0x13;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02004074
4075 /* SPDIF I/O */
4076 spec->dig_out = 0x05;
4077 spec->multiout.dig_out_nid = spec->dig_out;
4078 cfg->dig_out_pins[0] = 0x0c;
4079 cfg->dig_outs = 1;
4080 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
4081 spec->dig_in = 0x09;
4082 cfg->dig_in_pin = 0x0e;
4083 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004084}
4085
Ian Minett5aaca442012-12-20 18:53:34 -08004086/*
4087 * Verbs tables.
4088 */
4089
4090/* Sends before DSP download. */
4091static struct hda_verb ca0132_base_init_verbs[] = {
4092 /*enable ct extension*/
4093 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4094 /*enable DSP node unsol, needed for DSP download*/
4095 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4096 {}
4097};
4098
4099/* Send at exit. */
4100static struct hda_verb ca0132_base_exit_verbs[] = {
4101 /*set afg to D3*/
4102 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4103 /*disable ct extension*/
4104 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4105 {}
4106};
4107
4108/* Other verbs tables. Sends after DSP download. */
4109static struct hda_verb ca0132_init_verbs0[] = {
4110 /* chip init verbs */
4111 {0x15, 0x70D, 0xF0},
4112 {0x15, 0x70E, 0xFE},
4113 {0x15, 0x707, 0x75},
4114 {0x15, 0x707, 0xD3},
4115 {0x15, 0x707, 0x09},
4116 {0x15, 0x707, 0x53},
4117 {0x15, 0x707, 0xD4},
4118 {0x15, 0x707, 0xEF},
4119 {0x15, 0x707, 0x75},
4120 {0x15, 0x707, 0xD3},
4121 {0x15, 0x707, 0x09},
4122 {0x15, 0x707, 0x02},
4123 {0x15, 0x707, 0x37},
4124 {0x15, 0x707, 0x78},
4125 {0x15, 0x53C, 0xCE},
4126 {0x15, 0x575, 0xC9},
4127 {0x15, 0x53D, 0xCE},
4128 {0x15, 0x5B7, 0xC9},
4129 {0x15, 0x70D, 0xE8},
4130 {0x15, 0x70E, 0xFE},
4131 {0x15, 0x707, 0x02},
4132 {0x15, 0x707, 0x68},
4133 {0x15, 0x707, 0x62},
4134 {0x15, 0x53A, 0xCE},
4135 {0x15, 0x546, 0xC9},
4136 {0x15, 0x53B, 0xCE},
4137 {0x15, 0x5E8, 0xC9},
4138 {0x15, 0x717, 0x0D},
4139 {0x15, 0x718, 0x20},
4140 {}
4141};
4142
4143static struct hda_verb ca0132_init_verbs1[] = {
4144 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4145 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4146 /* config EAPD */
4147 {0x0b, 0x78D, 0x00},
4148 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4149 /*{0x10, 0x78D, 0x02},*/
4150 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4151 {}
4152};
4153
Ian Minett95c6e9c2011-06-15 15:35:17 -07004154static void ca0132_init_chip(struct hda_codec *codec)
4155{
4156 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08004157 int num_fx;
4158 int i;
4159 unsigned int on;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004160
4161 mutex_init(&spec->chipio_mutex);
Ian Minett5aaca442012-12-20 18:53:34 -08004162
4163 spec->cur_out_type = SPEAKER_OUT;
4164 spec->cur_mic_type = DIGITAL_MIC;
4165 spec->cur_mic_boost = 0;
4166
4167 for (i = 0; i < VNODES_COUNT; i++) {
4168 spec->vnode_lvol[i] = 0x5a;
4169 spec->vnode_rvol[i] = 0x5a;
4170 spec->vnode_lswitch[i] = 0;
4171 spec->vnode_rswitch[i] = 0;
4172 }
4173
4174 /*
4175 * Default states for effects are in ca0132_effects[].
4176 */
4177 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4178 for (i = 0; i < num_fx; i++) {
4179 on = (unsigned int)ca0132_effects[i].reqs[0];
4180 spec->effects_switch[i] = on ? 1 : 0;
4181 }
4182
4183 spec->voicefx_val = 0;
4184 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4185 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4186
Ian Minett95c6e9c2011-06-15 15:35:17 -07004187}
4188
4189static void ca0132_exit_chip(struct hda_codec *codec)
4190{
4191 /* put any chip cleanup stuffs here. */
Ian Minett5aaca442012-12-20 18:53:34 -08004192
4193 if (dspload_is_loaded(codec))
4194 dsp_reset(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004195}
4196
Ian Minett01ef7db2012-09-20 20:29:16 -07004197static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4198{
4199 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4200 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4201 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4202 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4203 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4204 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4205
4206 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4207 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4208 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4209}
4210
4211static bool ca0132_download_dsp_images(struct hda_codec *codec)
4212{
4213 bool dsp_loaded = false;
4214 const struct dsp_image_seg *dsp_os_image;
Ian Minett01ef7db2012-09-20 20:29:16 -07004215
Ian Minettc3b4eea22012-09-20 20:29:17 -07004216 if (request_firmware_cached(&fw_efx, EFX_FILE,
4217 codec->bus->card->dev) != 0)
Ian Minett01ef7db2012-09-20 20:29:16 -07004218 return false;
4219
Ian Minettc3b4eea22012-09-20 20:29:17 -07004220 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
Ian Minett01ef7db2012-09-20 20:29:16 -07004221 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4222 dsp_loaded = dspload_wait_loaded(codec);
4223
Ian Minett01ef7db2012-09-20 20:29:16 -07004224 return dsp_loaded;
4225}
4226
4227static void ca0132_download_dsp(struct hda_codec *codec)
4228{
4229 struct ca0132_spec *spec = codec->spec;
4230
4231 spec->dsp_state = DSP_DOWNLOAD_INIT;
4232
4233 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4234 chipio_enable_clocks(codec);
4235 spec->dsp_state = DSP_DOWNLOADING;
4236 if (!ca0132_download_dsp_images(codec))
4237 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4238 else
4239 spec->dsp_state = DSP_DOWNLOADED;
4240 }
4241
4242 if (spec->dsp_state == DSP_DOWNLOADED)
4243 ca0132_set_dsp_msr(codec, true);
4244}
4245
Ian Minetta73d5112012-12-20 18:53:37 -08004246static void ca0132_process_dsp_response(struct hda_codec *codec)
4247{
4248 struct ca0132_spec *spec = codec->spec;
4249
4250 snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
4251 if (spec->wait_scp) {
4252 if (dspio_get_response_data(codec) >= 0)
4253 spec->wait_scp = 0;
4254 }
4255
4256 dspio_clear_response_queue(codec);
4257}
4258
4259static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4260{
4261 snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
4262
4263
4264 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4265 ca0132_process_dsp_response(codec);
4266 } else {
4267 res = snd_hda_jack_get_action(codec,
4268 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4269
4270 snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
4271
4272 switch (res) {
4273 case UNSOL_TAG_HP:
4274 ca0132_select_out(codec);
4275 snd_hda_jack_report_sync(codec);
4276 break;
4277 case UNSOL_TAG_AMIC1:
4278 ca0132_select_mic(codec);
4279 snd_hda_jack_report_sync(codec);
4280 break;
4281 default:
4282 break;
4283 }
4284 }
4285}
4286
Ian Minett95c6e9c2011-06-15 15:35:17 -07004287static int ca0132_init(struct hda_codec *codec)
4288{
4289 struct ca0132_spec *spec = codec->spec;
4290 struct auto_pin_cfg *cfg = &spec->autocfg;
4291 int i;
4292
Ian Minett5aaca442012-12-20 18:53:34 -08004293 spec->dsp_state = DSP_DOWNLOAD_INIT;
4294 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4295
4296 snd_hda_power_up(codec);
4297
4298 ca0132_init_params(codec);
4299 ca0132_init_flags(codec);
4300 snd_hda_sequence_write(codec, spec->base_init_verbs);
Ian Minett01ef7db2012-09-20 20:29:16 -07004301#ifdef CONFIG_SND_HDA_DSP_LOADER
4302 ca0132_download_dsp(codec);
4303#endif
Ian Minett5aaca442012-12-20 18:53:34 -08004304 ca0132_refresh_widget_caps(codec);
4305 ca0132_setup_defaults(codec);
4306 ca0132_init_analog_mic2(codec);
4307 ca0132_init_dmic(codec);
Ian Minett01ef7db2012-09-20 20:29:16 -07004308
Ian Minett5aaca442012-12-20 18:53:34 -08004309 for (i = 0; i < spec->num_outputs; i++)
4310 init_output(codec, spec->out_pins[i], spec->dacs[0]);
4311
Ian Minett95c6e9c2011-06-15 15:35:17 -07004312 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
4313
4314 for (i = 0; i < spec->num_inputs; i++)
4315 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4316
4317 init_input(codec, cfg->dig_in_pin, spec->dig_in);
4318
Ian Minett5aaca442012-12-20 18:53:34 -08004319 for (i = 0; i < spec->num_init_verbs; i++)
4320 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4321
Ian Minetta73d5112012-12-20 18:53:37 -08004322 ca0132_init_unsol(codec);
4323
Ian Minett5aaca442012-12-20 18:53:34 -08004324 ca0132_select_out(codec);
4325 ca0132_select_mic(codec);
4326
Ian Minetta73d5112012-12-20 18:53:37 -08004327 snd_hda_jack_report_sync(codec);
4328
Ian Minett5aaca442012-12-20 18:53:34 -08004329 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004330
4331 return 0;
4332}
4333
Ian Minett95c6e9c2011-06-15 15:35:17 -07004334static void ca0132_free(struct hda_codec *codec)
4335{
Ian Minett5aaca442012-12-20 18:53:34 -08004336 struct ca0132_spec *spec = codec->spec;
4337
4338 snd_hda_power_up(codec);
4339 snd_hda_sequence_write(codec, spec->base_exit_verbs);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004340 ca0132_exit_chip(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004341 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004342 kfree(codec->spec);
4343}
4344
4345static struct hda_codec_ops ca0132_patch_ops = {
4346 .build_controls = ca0132_build_controls,
4347 .build_pcms = ca0132_build_pcms,
4348 .init = ca0132_init,
4349 .free = ca0132_free,
Ian Minetta73d5112012-12-20 18:53:37 -08004350 .unsol_event = ca0132_unsol_event,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004351};
4352
Ian Minett95c6e9c2011-06-15 15:35:17 -07004353static int patch_ca0132(struct hda_codec *codec)
4354{
4355 struct ca0132_spec *spec;
Ian Minetta73d5112012-12-20 18:53:37 -08004356 int err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004357
4358 snd_printdd("patch_ca0132\n");
4359
4360 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4361 if (!spec)
4362 return -ENOMEM;
4363 codec->spec = spec;
4364
Ian Minetta7e76272012-12-20 18:53:35 -08004365 spec->num_mixers = 1;
4366 spec->mixers[0] = ca0132_mixer;
4367
Ian Minett5aaca442012-12-20 18:53:34 -08004368 spec->base_init_verbs = ca0132_base_init_verbs;
4369 spec->base_exit_verbs = ca0132_base_exit_verbs;
4370 spec->init_verbs[0] = ca0132_init_verbs0;
4371 spec->init_verbs[1] = ca0132_init_verbs1;
4372 spec->num_init_verbs = 2;
4373
Ian Minett95c6e9c2011-06-15 15:35:17 -07004374 ca0132_init_chip(codec);
4375
4376 ca0132_config(codec);
4377
Ian Minetta73d5112012-12-20 18:53:37 -08004378 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4379 if (err < 0)
4380 return err;
4381
Ian Minett95c6e9c2011-06-15 15:35:17 -07004382 codec->patch_ops = ca0132_patch_ops;
4383
4384 return 0;
4385}
4386
4387/*
4388 * patch entries
4389 */
4390static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4391 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4392 {} /* terminator */
4393};
4394
4395MODULE_ALIAS("snd-hda-codec-id:11020011");
4396
4397MODULE_LICENSE("GPL");
4398MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
4399
4400static struct hda_codec_preset_list ca0132_list = {
4401 .preset = snd_hda_preset_ca0132,
4402 .owner = THIS_MODULE,
4403};
4404
4405static int __init patch_ca0132_init(void)
4406{
4407 return snd_hda_add_codec_preset(&ca0132_list);
4408}
4409
4410static void __exit patch_ca0132_exit(void)
4411{
Ian Minettc3b4eea22012-09-20 20:29:17 -07004412 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07004413 snd_hda_delete_codec_preset(&ca0132_list);
4414}
4415
4416module_init(patch_ca0132_init)
4417module_exit(patch_ca0132_exit)