blob: fe1829b4e2d333ffebda602ee5da8d76c1f3cbb6 [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>
Ian Minett95c6e9c2011-06-15 15:35:17 -070027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Ian Minett4aa3bb02012-09-20 20:29:15 -070029#include <linux/firmware.h>
Jérémy Lefaurea9291f42017-10-12 22:36:31 -040030#include <linux/kernel.h>
Connor McAdamsaa317042018-05-08 13:20:03 -040031#include <linux/types.h>
32#include <linux/io.h>
33#include <linux/pci.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070034#include <sound/core.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050035#include <sound/hda_codec.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070036#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020037#include "hda_auto_parser.h"
Ian Minett5aaca442012-12-20 18:53:34 -080038#include "hda_jack.h"
Ian Minett95c6e9c2011-06-15 15:35:17 -070039
Ian Minettbcd109c2012-09-20 20:29:14 -070040#include "ca0132_regs.h"
41
Ian Minettef6b2ea2012-12-20 18:53:33 -080042/* Enable this to see controls for tuning purpose. */
43/*#define ENABLE_TUNING_CONTROLS*/
44
Takashi Sakamoto8e142e92018-05-02 22:48:16 +090045#ifdef ENABLE_TUNING_CONTROLS
46#include <sound/tlv.h>
47#endif
48
Ian Minettef6b2ea2012-12-20 18:53:33 -080049#define FLOAT_ZERO 0x00000000
50#define FLOAT_ONE 0x3f800000
51#define FLOAT_TWO 0x40000000
Connor McAdams38ba69f2018-05-08 13:20:07 -040052#define FLOAT_THREE 0x40400000
Connor McAdams7cb9d942018-05-08 13:20:10 -040053#define FLOAT_EIGHT 0x41000000
Ian Minettef6b2ea2012-12-20 18:53:33 -080054#define FLOAT_MINUS_5 0xc0a00000
55
Ian Minettef6b2ea2012-12-20 18:53:33 -080056#define UNSOL_TAG_DSP 0x16
57
Ian Minett4aa3bb02012-09-20 20:29:15 -070058#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
59#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
60
61#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
62#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
63#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
64
65#define MASTERCONTROL 0x80
Ian Minettef6b2ea2012-12-20 18:53:33 -080066#define MASTERCONTROL_ALLOC_DMA_CHAN 10
67#define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
Ian Minett4aa3bb02012-09-20 20:29:15 -070068
Ian Minett95c6e9c2011-06-15 15:35:17 -070069#define WIDGET_CHIP_CTRL 0x15
70#define WIDGET_DSP_CTRL 0x16
71
Ian Minett4aa3bb02012-09-20 20:29:15 -070072#define MEM_CONNID_MICIN1 3
73#define MEM_CONNID_MICIN2 5
74#define MEM_CONNID_MICOUT1 12
75#define MEM_CONNID_MICOUT2 14
76#define MEM_CONNID_WUH 10
77#define MEM_CONNID_DSP 16
78#define MEM_CONNID_DMIC 100
79
80#define SCP_SET 0
81#define SCP_GET 1
82
Ian Minett01ef7db2012-09-20 20:29:16 -070083#define EFX_FILE "ctefx.bin"
Connor McAdams8a19bce2018-05-08 13:20:01 -040084#define SBZ_EFX_FILE "ctefx-sbz.bin"
85#define R3DI_EFX_FILE "ctefx-r3di.bin"
Ian Minett01ef7db2012-09-20 20:29:16 -070086
Takashi Iwai24f3ced2013-02-04 18:25:51 +010087#ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
Ian Minett01ef7db2012-09-20 20:29:16 -070088MODULE_FIRMWARE(EFX_FILE);
Connor McAdams8a19bce2018-05-08 13:20:01 -040089MODULE_FIRMWARE(SBZ_EFX_FILE);
90MODULE_FIRMWARE(R3DI_EFX_FILE);
Takashi Iwai7a527ed2013-01-15 17:44:20 +010091#endif
Ian Minett01ef7db2012-09-20 20:29:16 -070092
Takashi Sakamoto3a03f832018-05-15 22:12:58 +090093static const char *const dirstr[2] = { "Playback", "Capture" };
Ian Minettef6b2ea2012-12-20 18:53:33 -080094
Connor McAdams7cb9d942018-05-08 13:20:10 -040095#define NUM_OF_OUTPUTS 3
Ian Minettef6b2ea2012-12-20 18:53:33 -080096enum {
97 SPEAKER_OUT,
Connor McAdams7cb9d942018-05-08 13:20:10 -040098 HEADPHONE_OUT,
99 SURROUND_OUT
Ian Minettef6b2ea2012-12-20 18:53:33 -0800100};
101
102enum {
103 DIGITAL_MIC,
104 LINE_MIC_IN
105};
106
Connor McAdams7cb9d942018-05-08 13:20:10 -0400107/* Strings for Input Source Enum Control */
Takashi Sakamoto3a03f832018-05-15 22:12:58 +0900108static const char *const in_src_str[3] = {"Rear Mic", "Line", "Front Mic" };
Connor McAdams7cb9d942018-05-08 13:20:10 -0400109#define IN_SRC_NUM_OF_INPUTS 3
110enum {
111 REAR_MIC,
112 REAR_LINE_IN,
113 FRONT_MIC,
114};
115
Ian Minettef6b2ea2012-12-20 18:53:33 -0800116enum {
117#define VNODE_START_NID 0x80
118 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
119 VNID_MIC,
120 VNID_HP_SEL,
121 VNID_AMIC1_SEL,
122 VNID_HP_ASEL,
123 VNID_AMIC1_ASEL,
124 VNODE_END_NID,
125#define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
126
127#define EFFECT_START_NID 0x90
128#define OUT_EFFECT_START_NID EFFECT_START_NID
129 SURROUND = OUT_EFFECT_START_NID,
130 CRYSTALIZER,
131 DIALOG_PLUS,
132 SMART_VOLUME,
133 X_BASS,
134 EQUALIZER,
135 OUT_EFFECT_END_NID,
136#define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
137
138#define IN_EFFECT_START_NID OUT_EFFECT_END_NID
139 ECHO_CANCELLATION = IN_EFFECT_START_NID,
140 VOICE_FOCUS,
141 MIC_SVM,
142 NOISE_REDUCTION,
143 IN_EFFECT_END_NID,
144#define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
145
146 VOICEFX = IN_EFFECT_END_NID,
147 PLAY_ENHANCEMENT,
148 CRYSTAL_VOICE,
Connor McAdams7cb9d942018-05-08 13:20:10 -0400149 EFFECT_END_NID,
150 OUTPUT_SOURCE_ENUM,
Connor McAdams47cdf762018-05-08 13:20:13 -0400151 INPUT_SOURCE_ENUM,
152 XBASS_XOVER,
153 EQ_PRESET_ENUM,
154 SMART_VOLUME_ENUM,
155 MIC_BOOST_ENUM
Ian Minettef6b2ea2012-12-20 18:53:33 -0800156#define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
157};
158
159/* Effects values size*/
160#define EFFECT_VALS_MAX_COUNT 12
161
Connor McAdams47cdf762018-05-08 13:20:13 -0400162/*
163 * Default values for the effect slider controls, they are in order of their
164 * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
165 * X-bass.
166 */
167static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
168/* Amount of effect level sliders for ca0132_alt controls. */
169#define EFFECT_LEVEL_SLIDERS 5
170
Dylan Reide8412ca2013-04-04 13:55:09 -0700171/* Latency introduced by DSP blocks in milliseconds. */
172#define DSP_CAPTURE_INIT_LATENCY 0
173#define DSP_CRYSTAL_VOICE_LATENCY 124
174#define DSP_PLAYBACK_INIT_LATENCY 13
175#define DSP_PLAY_ENHANCEMENT_LATENCY 30
176#define DSP_SPEAKER_OUT_LATENCY 7
177
Ian Minettef6b2ea2012-12-20 18:53:33 -0800178struct ct_effect {
Takashi Iwai975cc022013-06-28 11:56:49 +0200179 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minettef6b2ea2012-12-20 18:53:33 -0800180 hda_nid_t nid;
181 int mid; /*effect module ID*/
182 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
183 int direct; /* 0:output; 1:input*/
184 int params; /* number of default non-on/off params */
185 /*effect default values, 1st is on/off. */
186 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
187};
188
189#define EFX_DIR_OUT 0
190#define EFX_DIR_IN 1
191
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900192static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800193 { .name = "Surround",
194 .nid = SURROUND,
195 .mid = 0x96,
196 .reqs = {0, 1},
197 .direct = EFX_DIR_OUT,
198 .params = 1,
199 .def_vals = {0x3F800000, 0x3F2B851F}
200 },
201 { .name = "Crystalizer",
202 .nid = CRYSTALIZER,
203 .mid = 0x96,
204 .reqs = {7, 8},
205 .direct = EFX_DIR_OUT,
206 .params = 1,
207 .def_vals = {0x3F800000, 0x3F266666}
208 },
209 { .name = "Dialog Plus",
210 .nid = DIALOG_PLUS,
211 .mid = 0x96,
212 .reqs = {2, 3},
213 .direct = EFX_DIR_OUT,
214 .params = 1,
215 .def_vals = {0x00000000, 0x3F000000}
216 },
217 { .name = "Smart Volume",
218 .nid = SMART_VOLUME,
219 .mid = 0x96,
220 .reqs = {4, 5, 6},
221 .direct = EFX_DIR_OUT,
222 .params = 2,
223 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
224 },
225 { .name = "X-Bass",
226 .nid = X_BASS,
227 .mid = 0x96,
228 .reqs = {24, 23, 25},
229 .direct = EFX_DIR_OUT,
230 .params = 2,
231 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
232 },
233 { .name = "Equalizer",
234 .nid = EQUALIZER,
235 .mid = 0x96,
236 .reqs = {9, 10, 11, 12, 13, 14,
237 15, 16, 17, 18, 19, 20},
238 .direct = EFX_DIR_OUT,
239 .params = 11,
240 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
241 0x00000000, 0x00000000, 0x00000000, 0x00000000,
242 0x00000000, 0x00000000, 0x00000000, 0x00000000}
243 },
244 { .name = "Echo Cancellation",
245 .nid = ECHO_CANCELLATION,
246 .mid = 0x95,
247 .reqs = {0, 1, 2, 3},
248 .direct = EFX_DIR_IN,
249 .params = 3,
250 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
251 },
252 { .name = "Voice Focus",
253 .nid = VOICE_FOCUS,
254 .mid = 0x95,
255 .reqs = {6, 7, 8, 9},
256 .direct = EFX_DIR_IN,
257 .params = 3,
258 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
259 },
260 { .name = "Mic SVM",
261 .nid = MIC_SVM,
262 .mid = 0x95,
263 .reqs = {44, 45},
264 .direct = EFX_DIR_IN,
265 .params = 1,
266 .def_vals = {0x00000000, 0x3F3D70A4}
267 },
268 { .name = "Noise Reduction",
269 .nid = NOISE_REDUCTION,
270 .mid = 0x95,
271 .reqs = {4, 5},
272 .direct = EFX_DIR_IN,
273 .params = 1,
274 .def_vals = {0x3F800000, 0x3F000000}
275 },
276 { .name = "VoiceFX",
277 .nid = VOICEFX,
278 .mid = 0x95,
279 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
280 .direct = EFX_DIR_IN,
281 .params = 8,
282 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
283 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
284 0x00000000}
285 }
286};
287
288/* Tuning controls */
289#ifdef ENABLE_TUNING_CONTROLS
290
291enum {
292#define TUNING_CTL_START_NID 0xC0
293 WEDGE_ANGLE = TUNING_CTL_START_NID,
294 SVM_LEVEL,
295 EQUALIZER_BAND_0,
296 EQUALIZER_BAND_1,
297 EQUALIZER_BAND_2,
298 EQUALIZER_BAND_3,
299 EQUALIZER_BAND_4,
300 EQUALIZER_BAND_5,
301 EQUALIZER_BAND_6,
302 EQUALIZER_BAND_7,
303 EQUALIZER_BAND_8,
304 EQUALIZER_BAND_9,
305 TUNING_CTL_END_NID
306#define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
307};
308
309struct ct_tuning_ctl {
Takashi Iwai975cc022013-06-28 11:56:49 +0200310 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minettef6b2ea2012-12-20 18:53:33 -0800311 hda_nid_t parent_nid;
312 hda_nid_t nid;
313 int mid; /*effect module ID*/
314 int req; /*effect module request*/
315 int direct; /* 0:output; 1:input*/
316 unsigned int def_val;/*effect default values*/
317};
318
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900319static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800320 { .name = "Wedge Angle",
321 .parent_nid = VOICE_FOCUS,
322 .nid = WEDGE_ANGLE,
323 .mid = 0x95,
324 .req = 8,
325 .direct = EFX_DIR_IN,
326 .def_val = 0x41F00000
327 },
328 { .name = "SVM Level",
329 .parent_nid = MIC_SVM,
330 .nid = SVM_LEVEL,
331 .mid = 0x95,
332 .req = 45,
333 .direct = EFX_DIR_IN,
334 .def_val = 0x3F3D70A4
335 },
336 { .name = "EQ Band0",
337 .parent_nid = EQUALIZER,
338 .nid = EQUALIZER_BAND_0,
339 .mid = 0x96,
340 .req = 11,
341 .direct = EFX_DIR_OUT,
342 .def_val = 0x00000000
343 },
344 { .name = "EQ Band1",
345 .parent_nid = EQUALIZER,
346 .nid = EQUALIZER_BAND_1,
347 .mid = 0x96,
348 .req = 12,
349 .direct = EFX_DIR_OUT,
350 .def_val = 0x00000000
351 },
352 { .name = "EQ Band2",
353 .parent_nid = EQUALIZER,
354 .nid = EQUALIZER_BAND_2,
355 .mid = 0x96,
356 .req = 13,
357 .direct = EFX_DIR_OUT,
358 .def_val = 0x00000000
359 },
360 { .name = "EQ Band3",
361 .parent_nid = EQUALIZER,
362 .nid = EQUALIZER_BAND_3,
363 .mid = 0x96,
364 .req = 14,
365 .direct = EFX_DIR_OUT,
366 .def_val = 0x00000000
367 },
368 { .name = "EQ Band4",
369 .parent_nid = EQUALIZER,
370 .nid = EQUALIZER_BAND_4,
371 .mid = 0x96,
372 .req = 15,
373 .direct = EFX_DIR_OUT,
374 .def_val = 0x00000000
375 },
376 { .name = "EQ Band5",
377 .parent_nid = EQUALIZER,
378 .nid = EQUALIZER_BAND_5,
379 .mid = 0x96,
380 .req = 16,
381 .direct = EFX_DIR_OUT,
382 .def_val = 0x00000000
383 },
384 { .name = "EQ Band6",
385 .parent_nid = EQUALIZER,
386 .nid = EQUALIZER_BAND_6,
387 .mid = 0x96,
388 .req = 17,
389 .direct = EFX_DIR_OUT,
390 .def_val = 0x00000000
391 },
392 { .name = "EQ Band7",
393 .parent_nid = EQUALIZER,
394 .nid = EQUALIZER_BAND_7,
395 .mid = 0x96,
396 .req = 18,
397 .direct = EFX_DIR_OUT,
398 .def_val = 0x00000000
399 },
400 { .name = "EQ Band8",
401 .parent_nid = EQUALIZER,
402 .nid = EQUALIZER_BAND_8,
403 .mid = 0x96,
404 .req = 19,
405 .direct = EFX_DIR_OUT,
406 .def_val = 0x00000000
407 },
408 { .name = "EQ Band9",
409 .parent_nid = EQUALIZER,
410 .nid = EQUALIZER_BAND_9,
411 .mid = 0x96,
412 .req = 20,
413 .direct = EFX_DIR_OUT,
414 .def_val = 0x00000000
415 }
416};
417#endif
418
419/* Voice FX Presets */
420#define VOICEFX_MAX_PARAM_COUNT 9
421
422struct ct_voicefx {
423 char *name;
424 hda_nid_t nid;
425 int mid;
426 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
427};
428
429struct ct_voicefx_preset {
430 char *name; /*preset name*/
431 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
432};
433
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900434static const struct ct_voicefx ca0132_voicefx = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800435 .name = "VoiceFX Capture Switch",
436 .nid = VOICEFX,
437 .mid = 0x95,
438 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
439};
440
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900441static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800442 { .name = "Neutral",
443 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
444 0x44FA0000, 0x3F800000, 0x3F800000,
445 0x3F800000, 0x00000000, 0x00000000 }
446 },
447 { .name = "Female2Male",
448 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
449 0x44FA0000, 0x3F19999A, 0x3F866666,
450 0x3F800000, 0x00000000, 0x00000000 }
451 },
452 { .name = "Male2Female",
453 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
454 0x450AC000, 0x4017AE14, 0x3F6B851F,
455 0x3F800000, 0x00000000, 0x00000000 }
456 },
457 { .name = "ScrappyKid",
458 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
459 0x44FA0000, 0x40400000, 0x3F28F5C3,
460 0x3F800000, 0x00000000, 0x00000000 }
461 },
462 { .name = "Elderly",
463 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
464 0x44E10000, 0x3FB33333, 0x3FB9999A,
465 0x3F800000, 0x3E3A2E43, 0x00000000 }
466 },
467 { .name = "Orc",
468 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
469 0x45098000, 0x3F266666, 0x3FC00000,
470 0x3F800000, 0x00000000, 0x00000000 }
471 },
472 { .name = "Elf",
473 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
474 0x45193000, 0x3F8E147B, 0x3F75C28F,
475 0x3F800000, 0x00000000, 0x00000000 }
476 },
477 { .name = "Dwarf",
478 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
479 0x45007000, 0x3F451EB8, 0x3F7851EC,
480 0x3F800000, 0x00000000, 0x00000000 }
481 },
482 { .name = "AlienBrute",
483 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
484 0x451F6000, 0x3F266666, 0x3FA7D945,
485 0x3F800000, 0x3CF5C28F, 0x00000000 }
486 },
487 { .name = "Robot",
488 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
489 0x44FA0000, 0x3FB2718B, 0x3F800000,
490 0xBC07010E, 0x00000000, 0x00000000 }
491 },
492 { .name = "Marine",
493 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
494 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
495 0x3F0A3D71, 0x00000000, 0x00000000 }
496 },
497 { .name = "Emo",
498 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
499 0x44FA0000, 0x3F800000, 0x3F800000,
500 0x3E4CCCCD, 0x00000000, 0x00000000 }
501 },
502 { .name = "DeepVoice",
503 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
504 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
505 0x3F800000, 0x00000000, 0x00000000 }
506 },
507 { .name = "Munchkin",
508 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
509 0x44FA0000, 0x3F800000, 0x3F1A043C,
510 0x3F800000, 0x00000000, 0x00000000 }
511 }
512};
513
Connor McAdams47cdf762018-05-08 13:20:13 -0400514/* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
515
516#define EQ_PRESET_MAX_PARAM_COUNT 11
517
518struct ct_eq {
519 char *name;
520 hda_nid_t nid;
521 int mid;
522 int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
523};
524
525struct ct_eq_preset {
526 char *name; /*preset name*/
527 unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
528};
529
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900530static const struct ct_eq ca0132_alt_eq_enum = {
Connor McAdams47cdf762018-05-08 13:20:13 -0400531 .name = "FX: Equalizer Preset Switch",
532 .nid = EQ_PRESET_ENUM,
533 .mid = 0x96,
534 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
535};
536
537
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900538static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
Connor McAdams47cdf762018-05-08 13:20:13 -0400539 { .name = "Flat",
540 .vals = { 0x00000000, 0x00000000, 0x00000000,
541 0x00000000, 0x00000000, 0x00000000,
542 0x00000000, 0x00000000, 0x00000000,
543 0x00000000, 0x00000000 }
544 },
545 { .name = "Acoustic",
546 .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
547 0x40000000, 0x00000000, 0x00000000,
548 0x00000000, 0x00000000, 0x40000000,
549 0x40000000, 0x40000000 }
550 },
551 { .name = "Classical",
552 .vals = { 0x00000000, 0x00000000, 0x40C00000,
553 0x40C00000, 0x40466666, 0x00000000,
554 0x00000000, 0x00000000, 0x00000000,
555 0x40466666, 0x40466666 }
556 },
557 { .name = "Country",
558 .vals = { 0x00000000, 0xBF99999A, 0x00000000,
559 0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
560 0x00000000, 0x00000000, 0x40000000,
561 0x40466666, 0x40800000 }
562 },
563 { .name = "Dance",
564 .vals = { 0x00000000, 0xBF99999A, 0x40000000,
565 0x40466666, 0x40866666, 0xBF99999A,
566 0xBF99999A, 0x00000000, 0x00000000,
567 0x40800000, 0x40800000 }
568 },
569 { .name = "Jazz",
570 .vals = { 0x00000000, 0x00000000, 0x00000000,
571 0x3F8CCCCD, 0x40800000, 0x40800000,
572 0x40800000, 0x00000000, 0x3F8CCCCD,
573 0x40466666, 0x40466666 }
574 },
575 { .name = "New Age",
576 .vals = { 0x00000000, 0x00000000, 0x40000000,
577 0x40000000, 0x00000000, 0x00000000,
578 0x00000000, 0x3F8CCCCD, 0x40000000,
579 0x40000000, 0x40000000 }
580 },
581 { .name = "Pop",
582 .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
583 0x40000000, 0x40000000, 0x00000000,
584 0xBF99999A, 0xBF99999A, 0x00000000,
585 0x40466666, 0x40C00000 }
586 },
587 { .name = "Rock",
588 .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
589 0x3F8CCCCD, 0x40000000, 0xBF99999A,
590 0xBF99999A, 0x00000000, 0x00000000,
591 0x40800000, 0x40800000 }
592 },
593 { .name = "Vocal",
594 .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
595 0xBF99999A, 0x00000000, 0x40466666,
596 0x40800000, 0x40466666, 0x00000000,
597 0x00000000, 0x3F8CCCCD }
598 }
599};
600
Connor McAdams7cb9d942018-05-08 13:20:10 -0400601/* DSP command sequences for ca0132_alt_select_out */
602#define ALT_OUT_SET_MAX_COMMANDS 9 /* Max number of commands in sequence */
603struct ca0132_alt_out_set {
604 char *name; /*preset name*/
605 unsigned char commands;
606 unsigned int mids[ALT_OUT_SET_MAX_COMMANDS];
607 unsigned int reqs[ALT_OUT_SET_MAX_COMMANDS];
608 unsigned int vals[ALT_OUT_SET_MAX_COMMANDS];
609};
610
611static const struct ca0132_alt_out_set alt_out_presets[] = {
612 { .name = "Line Out",
613 .commands = 7,
614 .mids = { 0x96, 0x96, 0x96, 0x8F,
615 0x96, 0x96, 0x96 },
616 .reqs = { 0x19, 0x17, 0x18, 0x01,
617 0x1F, 0x15, 0x3A },
618 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
619 0x00000000, 0x00000000, 0x00000000,
620 0x00000000 }
621 },
622 { .name = "Headphone",
623 .commands = 7,
624 .mids = { 0x96, 0x96, 0x96, 0x8F,
625 0x96, 0x96, 0x96 },
626 .reqs = { 0x19, 0x17, 0x18, 0x01,
627 0x1F, 0x15, 0x3A },
628 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
629 0x00000000, 0x00000000, 0x00000000,
630 0x00000000 }
631 },
632 { .name = "Surround",
633 .commands = 8,
634 .mids = { 0x96, 0x8F, 0x96, 0x96,
635 0x96, 0x96, 0x96, 0x96 },
636 .reqs = { 0x18, 0x01, 0x1F, 0x15,
637 0x3A, 0x1A, 0x1B, 0x1C },
638 .vals = { 0x00000000, 0x00000000, 0x00000000,
639 0x00000000, 0x00000000, 0x00000000,
640 0x00000000, 0x00000000 }
641 }
642};
643
Connor McAdams017310f2018-05-08 13:20:11 -0400644/*
645 * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
646 * and I don't know what the third req is, but it's always zero. I assume it's
647 * some sort of update or set command to tell the DSP there's new volume info.
648 */
649#define DSP_VOL_OUT 0
650#define DSP_VOL_IN 1
651
652struct ct_dsp_volume_ctl {
653 hda_nid_t vnid;
654 int mid; /* module ID*/
655 unsigned int reqs[3]; /* scp req ID */
656};
657
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900658static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
Connor McAdams017310f2018-05-08 13:20:11 -0400659 { .vnid = VNID_SPK,
660 .mid = 0x32,
661 .reqs = {3, 4, 2}
662 },
663 { .vnid = VNID_MIC,
664 .mid = 0x37,
665 .reqs = {2, 3, 1}
666 }
667};
668
Ian Minett95c6e9c2011-06-15 15:35:17 -0700669enum hda_cmd_vendor_io {
670 /* for DspIO node */
671 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
672 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
673
674 VENDOR_DSPIO_STATUS = 0xF01,
675 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
676 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
677 VENDOR_DSPIO_DSP_INIT = 0x703,
678 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
679 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
680
681 /* for ChipIO node */
682 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
683 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
684 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
685 VENDOR_CHIPIO_DATA_LOW = 0x300,
686 VENDOR_CHIPIO_DATA_HIGH = 0x400,
687
Connor McAdamsbf85a912018-09-18 14:33:29 -0400688 VENDOR_CHIPIO_8051_WRITE_DIRECT = 0x500,
689 VENDOR_CHIPIO_8051_READ_DIRECT = 0xD00,
690
Ian Minett95c6e9c2011-06-15 15:35:17 -0700691 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
692 VENDOR_CHIPIO_STATUS = 0xF01,
693 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
694 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
695
Ian Minett4aa3bb02012-09-20 20:29:15 -0700696 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
697 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
Connor McAdamsbf85a912018-09-18 14:33:29 -0400698 VENDOR_CHIPIO_8051_PMEM_READ = 0xF08,
699 VENDOR_CHIPIO_8051_IRAM_WRITE = 0x709,
700 VENDOR_CHIPIO_8051_IRAM_READ = 0xF09,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700701
Ian Minett95c6e9c2011-06-15 15:35:17 -0700702 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700703 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700704
705 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
706 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
707 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
708 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
709 VENDOR_CHIPIO_FLAG_SET = 0x70F,
710 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700711 VENDOR_CHIPIO_PARAM_SET = 0x710,
712 VENDOR_CHIPIO_PARAM_GET = 0xF10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700713
714 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
715 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
716 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
717 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
718
Ian Minett4aa3bb02012-09-20 20:29:15 -0700719 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
720 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
721 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
722 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
723
724 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
725 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
726 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
727 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
728 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
729 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
730
731 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
Ian Minett95c6e9c2011-06-15 15:35:17 -0700732};
733
734/*
735 * Control flag IDs
736 */
737enum control_flag_id {
738 /* Connection manager stream setup is bypassed/enabled */
739 CONTROL_FLAG_C_MGR = 0,
740 /* DSP DMA is bypassed/enabled */
741 CONTROL_FLAG_DMA = 1,
742 /* 8051 'idle' mode is disabled/enabled */
743 CONTROL_FLAG_IDLE_ENABLE = 2,
744 /* Tracker for the SPDIF-in path is bypassed/enabled */
745 CONTROL_FLAG_TRACKER = 3,
746 /* DigitalOut to Spdif2Out connection is disabled/enabled */
747 CONTROL_FLAG_SPDIF2OUT = 4,
748 /* Digital Microphone is disabled/enabled */
749 CONTROL_FLAG_DMIC = 5,
750 /* ADC_B rate is 48 kHz/96 kHz */
751 CONTROL_FLAG_ADC_B_96KHZ = 6,
752 /* ADC_C rate is 48 kHz/96 kHz */
753 CONTROL_FLAG_ADC_C_96KHZ = 7,
754 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
755 CONTROL_FLAG_DAC_96KHZ = 8,
756 /* DSP rate is 48 kHz/96 kHz */
757 CONTROL_FLAG_DSP_96KHZ = 9,
758 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
759 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
760 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
761 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
762 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
763 CONTROL_FLAG_DECODE_LOOP = 12,
764 /* De-emphasis filter on DAC-1 disabled/enabled */
765 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
766 /* De-emphasis filter on DAC-2 disabled/enabled */
767 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
768 /* De-emphasis filter on DAC-3 disabled/enabled */
769 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
770 /* High-pass filter on ADC_B disabled/enabled */
771 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
772 /* High-pass filter on ADC_C disabled/enabled */
773 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
774 /* Common mode on Port_A disabled/enabled */
775 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
776 /* Common mode on Port_D disabled/enabled */
777 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
778 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
779 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
780 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700781 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700782 /* ASI rate is 48kHz/96kHz */
783 CONTROL_FLAG_ASI_96KHZ = 22,
784 /* DAC power settings able to control attached ports no/yes */
785 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
786 /* Clock Stop OK reporting is disabled/enabled */
787 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
788 /* Number of control flags */
789 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
790};
791
792/*
793 * Control parameter IDs
794 */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700795enum control_param_id {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800796 /* 0: None, 1: Mic1In*/
797 CONTROL_PARAM_VIP_SOURCE = 1,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700798 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
799 CONTROL_PARAM_SPDIF1_SOURCE = 2,
Ian Minettef6b2ea2012-12-20 18:53:33 -0800800 /* Port A output stage gain setting to use when 16 Ohm output
801 * impedance is selected*/
802 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
803 /* Port D output stage gain setting to use when 16 Ohm output
804 * impedance is selected*/
805 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700806
Connor McAdamsbf85a912018-09-18 14:33:29 -0400807 /*
808 * This control param name was found in the 8051 memory, and makes
809 * sense given the fact the AE-5 uses it and has the ASI flag set.
810 */
811 CONTROL_PARAM_ASI = 23,
812
Ian Minett95c6e9c2011-06-15 15:35:17 -0700813 /* Stream Control */
814
815 /* Select stream with the given ID */
816 CONTROL_PARAM_STREAM_ID = 24,
817 /* Source connection point for the selected stream */
818 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
819 /* Destination connection point for the selected stream */
820 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
821 /* Number of audio channels in the selected stream */
822 CONTROL_PARAM_STREAMS_CHANNELS = 27,
823 /*Enable control for the selected stream */
824 CONTROL_PARAM_STREAM_CONTROL = 28,
825
826 /* Connection Point Control */
827
828 /* Select connection point with the given ID */
829 CONTROL_PARAM_CONN_POINT_ID = 29,
830 /* Connection point sample rate */
831 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
832
833 /* Node Control */
834
835 /* Select HDA node with the given ID */
836 CONTROL_PARAM_NODE_ID = 31
837};
838
839/*
840 * Dsp Io Status codes
841 */
842enum hda_vendor_status_dspio {
843 /* Success */
844 VENDOR_STATUS_DSPIO_OK = 0x00,
845 /* Busy, unable to accept new command, the host must retry */
846 VENDOR_STATUS_DSPIO_BUSY = 0x01,
847 /* SCP command queue is full */
848 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
849 /* SCP response queue is empty */
850 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
851};
852
853/*
854 * Chip Io Status codes
855 */
856enum hda_vendor_status_chipio {
857 /* Success */
858 VENDOR_STATUS_CHIPIO_OK = 0x00,
859 /* Busy, unable to accept new command, the host must retry */
860 VENDOR_STATUS_CHIPIO_BUSY = 0x01
861};
862
863/*
864 * CA0132 sample rate
865 */
866enum ca0132_sample_rate {
867 SR_6_000 = 0x00,
868 SR_8_000 = 0x01,
869 SR_9_600 = 0x02,
870 SR_11_025 = 0x03,
871 SR_16_000 = 0x04,
872 SR_22_050 = 0x05,
873 SR_24_000 = 0x06,
874 SR_32_000 = 0x07,
875 SR_44_100 = 0x08,
876 SR_48_000 = 0x09,
877 SR_88_200 = 0x0A,
878 SR_96_000 = 0x0B,
879 SR_144_000 = 0x0C,
880 SR_176_400 = 0x0D,
881 SR_192_000 = 0x0E,
882 SR_384_000 = 0x0F,
883
884 SR_COUNT = 0x10,
885
886 SR_RATE_UNKNOWN = 0x1F
887};
888
Ian Minett01ef7db2012-09-20 20:29:16 -0700889enum dsp_download_state {
890 DSP_DOWNLOAD_FAILED = -1,
891 DSP_DOWNLOAD_INIT = 0,
892 DSP_DOWNLOADING = 1,
893 DSP_DOWNLOADED = 2
894};
895
Ian Minett01ef7db2012-09-20 20:29:16 -0700896/* retrieve parameters from hda format */
897#define get_hdafmt_chs(fmt) (fmt & 0xf)
898#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
899#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
900#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700901
902/*
903 * CA0132 specific
904 */
905
906struct ca0132_spec {
Takashi Sakamotob0eaa072018-05-15 22:12:57 +0900907 const struct snd_kcontrol_new *mixers[5];
Ian Minetta7e76272012-12-20 18:53:35 -0800908 unsigned int num_mixers;
Ian Minett5aaca442012-12-20 18:53:34 -0800909 const struct hda_verb *base_init_verbs;
910 const struct hda_verb *base_exit_verbs;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200911 const struct hda_verb *chip_init_verbs;
Connor McAdamse42c7c72018-08-08 13:34:18 -0400912 const struct hda_verb *desktop_init_verbs;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200913 struct hda_verb *spec_init_verbs;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700914 struct auto_pin_cfg autocfg;
Ian Minett5aaca442012-12-20 18:53:34 -0800915
916 /* Nodes configurations */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700917 struct hda_multi_out multiout;
918 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
919 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
Ian Minett5aaca442012-12-20 18:53:34 -0800920 unsigned int num_outputs;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700921 hda_nid_t input_pins[AUTO_PIN_LAST];
922 hda_nid_t adcs[AUTO_PIN_LAST];
923 hda_nid_t dig_out;
924 hda_nid_t dig_in;
925 unsigned int num_inputs;
Ian Minetta7e76272012-12-20 18:53:35 -0800926 hda_nid_t shared_mic_nid;
927 hda_nid_t shared_out_nid;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200928 hda_nid_t unsol_tag_hp;
Connor McAdams63177af2018-05-08 13:20:02 -0400929 hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200930 hda_nid_t unsol_tag_amic1;
Ian Minett01ef7db2012-09-20 20:29:16 -0700931
932 /* chip access */
933 struct mutex chipio_mutex; /* chip access mutex */
934 u32 curr_chip_addx;
935
936 /* DSP download related */
937 enum dsp_download_state dsp_state;
938 unsigned int dsp_stream_id;
939 unsigned int wait_scp;
940 unsigned int wait_scp_header;
941 unsigned int wait_num_data;
942 unsigned int scp_resp_header;
943 unsigned int scp_resp_data[4];
944 unsigned int scp_resp_count;
Connor McAdams8a19bce2018-05-08 13:20:01 -0400945 bool alt_firmware_present;
Connor McAdams38ba69f2018-05-08 13:20:07 -0400946 bool startup_check_entered;
Connor McAdamse93ac302018-05-08 13:20:05 -0400947 bool dsp_reload;
Ian Minett5aaca442012-12-20 18:53:34 -0800948
949 /* mixer and effects related */
950 unsigned char dmic_ctl;
951 int cur_out_type;
952 int cur_mic_type;
953 long vnode_lvol[VNODES_COUNT];
954 long vnode_rvol[VNODES_COUNT];
955 long vnode_lswitch[VNODES_COUNT];
956 long vnode_rswitch[VNODES_COUNT];
957 long effects_switch[EFFECTS_COUNT];
958 long voicefx_val;
959 long cur_mic_boost;
Connor McAdams7cb9d942018-05-08 13:20:10 -0400960 /* ca0132_alt control related values */
961 unsigned char in_enum_val;
962 unsigned char out_enum_val;
Connor McAdams47cdf762018-05-08 13:20:13 -0400963 unsigned char mic_boost_enum_val;
964 unsigned char smart_volume_setting;
965 long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
966 long xbass_xover_freq;
967 long eq_preset_val;
968 unsigned int tlv[4];
969 struct hda_vmaster_mute_hook vmaster_mute;
970
Ian Minett44f0c972012-12-20 18:53:38 -0800971
Chih-Chung Chang993884f2013-03-25 10:39:23 -0700972 struct hda_codec *codec;
973 struct delayed_work unsol_hp_work;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200974 int quirk;
Chih-Chung Chang993884f2013-03-25 10:39:23 -0700975
Ian Minett44f0c972012-12-20 18:53:38 -0800976#ifdef ENABLE_TUNING_CONTROLS
977 long cur_ctl_vals[TUNING_CTLS_COUNT];
978#endif
Connor McAdamsaa317042018-05-08 13:20:03 -0400979 /*
Connor McAdams08eca6b2018-08-08 13:34:17 -0400980 * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
981 * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
982 * things.
Connor McAdamsaa317042018-05-08 13:20:03 -0400983 */
Connor McAdams08eca6b2018-08-08 13:34:17 -0400984 bool use_pci_mmio;
Connor McAdamsaa317042018-05-08 13:20:03 -0400985 void __iomem *mem_base;
Connor McAdams009b8f92018-05-08 13:20:06 -0400986
987 /*
988 * Whether or not to use the alt functions like alt_select_out,
989 * alt_select_in, etc. Only used on desktop codecs for now, because of
990 * surround sound support.
991 */
992 bool use_alt_functions;
Connor McAdams47cdf762018-05-08 13:20:13 -0400993
994 /*
995 * Whether or not to use alt controls: volume effect sliders, EQ
996 * presets, smart volume presets, and new control names with FX prefix.
997 * Renames PlayEnhancement and CrystalVoice too.
998 */
999 bool use_alt_controls;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001000};
1001
Ian Minett01ef7db2012-09-20 20:29:16 -07001002/*
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001003 * CA0132 quirks table
1004 */
1005enum {
1006 QUIRK_NONE,
1007 QUIRK_ALIENWARE,
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04001008 QUIRK_ALIENWARE_M17XR4,
Connor McAdams8a19bce2018-05-08 13:20:01 -04001009 QUIRK_SBZ,
1010 QUIRK_R3DI,
Connor McAdams8f8c5232018-08-08 13:34:15 -04001011 QUIRK_R3D,
Connor McAdamsf6276462018-09-18 14:33:30 -04001012 QUIRK_AE5,
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001013};
1014
Takashi Iwaife14f392015-08-10 16:53:32 +02001015static const struct hda_pintbl alienware_pincfgs[] = {
1016 { 0x0b, 0x90170110 }, /* Builtin Speaker */
1017 { 0x0c, 0x411111f0 }, /* N/A */
1018 { 0x0d, 0x411111f0 }, /* N/A */
1019 { 0x0e, 0x411111f0 }, /* N/A */
1020 { 0x0f, 0x0321101f }, /* HP */
1021 { 0x10, 0x411111f0 }, /* Headset? disabled for now */
1022 { 0x11, 0x03a11021 }, /* Mic */
1023 { 0x12, 0xd5a30140 }, /* Builtin Mic */
1024 { 0x13, 0x411111f0 }, /* N/A */
1025 { 0x18, 0x411111f0 }, /* N/A */
1026 {}
1027};
1028
Connor McAdams63177af2018-05-08 13:20:02 -04001029/* Sound Blaster Z pin configs taken from Windows Driver */
1030static const struct hda_pintbl sbz_pincfgs[] = {
1031 { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1032 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1033 { 0x0d, 0x014510f0 }, /* Digital Out */
1034 { 0x0e, 0x01c510f0 }, /* SPDIF In */
1035 { 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
1036 { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1037 { 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
1038 { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1039 { 0x13, 0x908700f0 }, /* What U Hear In*/
1040 { 0x18, 0x50d000f0 }, /* N/A */
1041 {}
1042};
1043
Connor McAdams7f73df92018-08-08 13:34:16 -04001044/* Recon3D pin configs taken from Windows Driver */
1045static const struct hda_pintbl r3d_pincfgs[] = {
1046 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1047 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1048 { 0x0d, 0x014510f0 }, /* Digital Out */
1049 { 0x0e, 0x01c520f0 }, /* SPDIF In */
1050 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1051 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1052 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1053 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1054 { 0x13, 0x908700f0 }, /* What U Hear In*/
1055 { 0x18, 0x50d000f0 }, /* N/A */
1056 {}
1057};
1058
Connor McAdamsd06feaf2018-09-18 14:33:31 -04001059/* Sound Blaster AE-5 pin configs taken from Windows Driver */
1060static const struct hda_pintbl ae5_pincfgs[] = {
1061 { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1062 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1063 { 0x0d, 0x014510f0 }, /* Digital Out */
1064 { 0x0e, 0x01c510f0 }, /* SPDIF In */
1065 { 0x0f, 0x01017114 }, /* Port A -- Rear L/R. */
1066 { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1067 { 0x11, 0x01a170ff }, /* Port B -- LineMicIn2 / Rear Headphone */
1068 { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1069 { 0x13, 0x908700f0 }, /* What U Hear In*/
1070 { 0x18, 0x50d000f0 }, /* N/A */
1071 {}
1072};
1073
Connor McAdams63177af2018-05-08 13:20:02 -04001074/* Recon3D integrated pin configs taken from Windows Driver */
1075static const struct hda_pintbl r3di_pincfgs[] = {
1076 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1077 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1078 { 0x0d, 0x014510f0 }, /* Digital Out */
1079 { 0x0e, 0x41c520f0 }, /* SPDIF In */
1080 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1081 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1082 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1083 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1084 { 0x13, 0x908700f0 }, /* What U Hear In*/
1085 { 0x18, 0x500000f0 }, /* N/A */
1086 {}
1087};
1088
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001089static const struct snd_pci_quirk ca0132_quirks[] = {
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04001090 SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
Gabriele Martino5328e1e2015-12-09 17:05:58 +01001091 SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
1092 SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
Sven Hahneb5337cf2016-11-25 14:16:43 +01001093 SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
Connor McAdams8a19bce2018-05-08 13:20:01 -04001094 SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
1095 SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
1096 SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
Alastair Bridgewaterdad59262018-07-11 18:09:45 -04001097 SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
Alastair Bridgewaterc5a59d22018-07-11 18:09:46 -04001098 SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
Connor McAdams8f8c5232018-08-08 13:34:15 -04001099 SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
Connor McAdamsf6276462018-09-18 14:33:30 -04001100 SND_PCI_QUIRK(0x1102, 0x0051, "Sound Blaster AE-5", QUIRK_AE5),
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001101 {}
1102};
1103
1104/*
Ian Minett01ef7db2012-09-20 20:29:16 -07001105 * CA0132 codec access
1106 */
Sachin Kamat399ae722013-09-13 15:14:22 +05301107static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
Ian Minett01ef7db2012-09-20 20:29:16 -07001108 unsigned int verb, unsigned int parm, unsigned int *res)
1109{
1110 unsigned int response;
1111 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
1112 *res = response;
1113
1114 return ((response == -1) ? -1 : 0);
1115}
1116
1117static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
1118 unsigned short converter_format, unsigned int *res)
1119{
1120 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
1121 converter_format & 0xffff, res);
1122}
1123
1124static int codec_set_converter_stream_channel(struct hda_codec *codec,
1125 hda_nid_t nid, unsigned char stream,
1126 unsigned char channel, unsigned int *res)
1127{
1128 unsigned char converter_stream_channel = 0;
1129
1130 converter_stream_channel = (stream << 4) | (channel & 0x0f);
1131 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
1132 converter_stream_channel, res);
1133}
1134
Ian Minett95c6e9c2011-06-15 15:35:17 -07001135/* Chip access helper function */
1136static int chipio_send(struct hda_codec *codec,
1137 unsigned int reg,
1138 unsigned int data)
1139{
1140 unsigned int res;
Ian Minett6d675302013-02-08 18:31:43 -08001141 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett95c6e9c2011-06-15 15:35:17 -07001142
1143 /* send bits of data specified by reg */
1144 do {
1145 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1146 reg, data);
1147 if (res == VENDOR_STATUS_CHIPIO_OK)
1148 return 0;
Ian Minett6d675302013-02-08 18:31:43 -08001149 msleep(20);
1150 } while (time_before(jiffies, timeout));
1151
Ian Minett95c6e9c2011-06-15 15:35:17 -07001152 return -EIO;
1153}
1154
1155/*
1156 * Write chip address through the vendor widget -- NOT protected by the Mutex!
1157 */
1158static int chipio_write_address(struct hda_codec *codec,
1159 unsigned int chip_addx)
1160{
Ian Minett4861af82012-09-20 20:29:20 -07001161 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001162 int res;
1163
Ian Minett4861af82012-09-20 20:29:20 -07001164 if (spec->curr_chip_addx == chip_addx)
1165 return 0;
1166
Ian Minett95c6e9c2011-06-15 15:35:17 -07001167 /* send low 16 bits of the address */
1168 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
1169 chip_addx & 0xffff);
1170
1171 if (res != -EIO) {
1172 /* send high 16 bits of the address */
1173 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
1174 chip_addx >> 16);
1175 }
1176
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001177 spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
Ian Minett4861af82012-09-20 20:29:20 -07001178
Ian Minett95c6e9c2011-06-15 15:35:17 -07001179 return res;
1180}
1181
1182/*
1183 * Write data through the vendor widget -- NOT protected by the Mutex!
1184 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07001185static int chipio_write_data(struct hda_codec *codec, unsigned int data)
1186{
Ian Minett5aaca442012-12-20 18:53:34 -08001187 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001188 int res;
1189
1190 /* send low 16 bits of the data */
1191 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
1192
1193 if (res != -EIO) {
1194 /* send high 16 bits of the data */
1195 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
1196 data >> 16);
1197 }
1198
Ian Minett5aaca442012-12-20 18:53:34 -08001199 /*If no error encountered, automatically increment the address
1200 as per chip behaviour*/
1201 spec->curr_chip_addx = (res != -EIO) ?
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001202 (spec->curr_chip_addx + 4) : ~0U;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001203 return res;
1204}
1205
Ian Minettd5c21b82012-09-20 20:29:18 -07001206/*
1207 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
1208 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001209static int chipio_write_data_multiple(struct hda_codec *codec,
1210 const u32 *data,
1211 unsigned int count)
1212{
1213 int status = 0;
1214
1215 if (data == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001216 codec_dbg(codec, "chipio_write_data null ptr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001217 return -EINVAL;
1218 }
1219
1220 while ((count-- != 0) && (status == 0))
1221 status = chipio_write_data(codec, *data++);
1222
1223 return status;
1224}
1225
1226
Ian Minett95c6e9c2011-06-15 15:35:17 -07001227/*
1228 * Read data through the vendor widget -- NOT protected by the Mutex!
1229 */
1230static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
1231{
Ian Minett5aaca442012-12-20 18:53:34 -08001232 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001233 int res;
1234
1235 /* post read */
1236 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
1237
1238 if (res != -EIO) {
1239 /* read status */
1240 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1241 }
1242
1243 if (res != -EIO) {
1244 /* read data */
1245 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1246 VENDOR_CHIPIO_HIC_READ_DATA,
1247 0);
1248 }
1249
Ian Minett5aaca442012-12-20 18:53:34 -08001250 /*If no error encountered, automatically increment the address
1251 as per chip behaviour*/
1252 spec->curr_chip_addx = (res != -EIO) ?
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001253 (spec->curr_chip_addx + 4) : ~0U;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001254 return res;
1255}
1256
1257/*
1258 * Write given value to the given address through the chip I/O widget.
1259 * protected by the Mutex
1260 */
1261static int chipio_write(struct hda_codec *codec,
1262 unsigned int chip_addx, const unsigned int data)
1263{
1264 struct ca0132_spec *spec = codec->spec;
1265 int err;
1266
1267 mutex_lock(&spec->chipio_mutex);
1268
1269 /* write the address, and if successful proceed to write data */
1270 err = chipio_write_address(codec, chip_addx);
1271 if (err < 0)
1272 goto exit;
1273
1274 err = chipio_write_data(codec, data);
1275 if (err < 0)
1276 goto exit;
1277
1278exit:
1279 mutex_unlock(&spec->chipio_mutex);
1280 return err;
1281}
1282
Ian Minettd5c21b82012-09-20 20:29:18 -07001283/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04001284 * Write given value to the given address through the chip I/O widget.
1285 * not protected by the Mutex
1286 */
1287static int chipio_write_no_mutex(struct hda_codec *codec,
1288 unsigned int chip_addx, const unsigned int data)
1289{
1290 int err;
1291
1292
1293 /* write the address, and if successful proceed to write data */
1294 err = chipio_write_address(codec, chip_addx);
1295 if (err < 0)
1296 goto exit;
1297
1298 err = chipio_write_data(codec, data);
1299 if (err < 0)
1300 goto exit;
1301
1302exit:
1303 return err;
1304}
1305
1306/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001307 * Write multiple values to the given address through the chip I/O widget.
1308 * protected by the Mutex
1309 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001310static int chipio_write_multiple(struct hda_codec *codec,
1311 u32 chip_addx,
1312 const u32 *data,
1313 unsigned int count)
1314{
1315 struct ca0132_spec *spec = codec->spec;
1316 int status;
1317
1318 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -07001319 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -07001320 if (status < 0)
1321 goto error;
1322
1323 status = chipio_write_data_multiple(codec, data, count);
1324error:
1325 mutex_unlock(&spec->chipio_mutex);
1326
1327 return status;
1328}
1329
Ian Minett95c6e9c2011-06-15 15:35:17 -07001330/*
1331 * Read the given address through the chip I/O widget
1332 * protected by the Mutex
1333 */
1334static int chipio_read(struct hda_codec *codec,
1335 unsigned int chip_addx, unsigned int *data)
1336{
1337 struct ca0132_spec *spec = codec->spec;
1338 int err;
1339
1340 mutex_lock(&spec->chipio_mutex);
1341
1342 /* write the address, and if successful proceed to write data */
1343 err = chipio_write_address(codec, chip_addx);
1344 if (err < 0)
1345 goto exit;
1346
1347 err = chipio_read_data(codec, data);
1348 if (err < 0)
1349 goto exit;
1350
1351exit:
1352 mutex_unlock(&spec->chipio_mutex);
1353 return err;
1354}
1355
Ian Minettd5c21b82012-09-20 20:29:18 -07001356/*
1357 * Set chip control flags through the chip I/O widget.
1358 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001359static void chipio_set_control_flag(struct hda_codec *codec,
1360 enum control_flag_id flag_id,
1361 bool flag_state)
1362{
1363 unsigned int val;
1364 unsigned int flag_bit;
1365
1366 flag_bit = (flag_state ? 1 : 0);
1367 val = (flag_bit << 7) | (flag_id);
1368 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1369 VENDOR_CHIPIO_FLAG_SET, val);
1370}
1371
Ian Minettd5c21b82012-09-20 20:29:18 -07001372/*
1373 * Set chip parameters through the chip I/O widget.
1374 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001375static void chipio_set_control_param(struct hda_codec *codec,
1376 enum control_param_id param_id, int param_val)
1377{
1378 struct ca0132_spec *spec = codec->spec;
1379 int val;
1380
1381 if ((param_id < 32) && (param_val < 8)) {
1382 val = (param_val << 5) | (param_id);
1383 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1384 VENDOR_CHIPIO_PARAM_SET, val);
1385 } else {
1386 mutex_lock(&spec->chipio_mutex);
1387 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1388 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1389 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1390 param_id);
1391 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1392 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1393 param_val);
1394 }
1395 mutex_unlock(&spec->chipio_mutex);
1396 }
1397}
1398
Ian Minettd5c21b82012-09-20 20:29:18 -07001399/*
Connor McAdams009b8f92018-05-08 13:20:06 -04001400 * Set chip parameters through the chip I/O widget. NO MUTEX.
1401 */
1402static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
1403 enum control_param_id param_id, int param_val)
1404{
1405 int val;
1406
1407 if ((param_id < 32) && (param_val < 8)) {
1408 val = (param_val << 5) | (param_id);
1409 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1410 VENDOR_CHIPIO_PARAM_SET, val);
1411 } else {
1412 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1413 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1414 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1415 param_id);
1416 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1417 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1418 param_val);
1419 }
1420 }
1421}
Connor McAdams38ba69f2018-05-08 13:20:07 -04001422/*
1423 * Connect stream to a source point, and then connect
1424 * that source point to a destination point.
1425 */
1426static void chipio_set_stream_source_dest(struct hda_codec *codec,
1427 int streamid, int source_point, int dest_point)
1428{
1429 chipio_set_control_param_no_mutex(codec,
1430 CONTROL_PARAM_STREAM_ID, streamid);
1431 chipio_set_control_param_no_mutex(codec,
1432 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
1433 chipio_set_control_param_no_mutex(codec,
1434 CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
1435}
1436
1437/*
1438 * Set number of channels in the selected stream.
1439 */
1440static void chipio_set_stream_channels(struct hda_codec *codec,
1441 int streamid, unsigned int channels)
1442{
1443 chipio_set_control_param_no_mutex(codec,
1444 CONTROL_PARAM_STREAM_ID, streamid);
1445 chipio_set_control_param_no_mutex(codec,
1446 CONTROL_PARAM_STREAMS_CHANNELS, channels);
1447}
Connor McAdams009b8f92018-05-08 13:20:06 -04001448
1449/*
1450 * Enable/Disable audio stream.
1451 */
1452static void chipio_set_stream_control(struct hda_codec *codec,
1453 int streamid, int enable)
1454{
1455 chipio_set_control_param_no_mutex(codec,
1456 CONTROL_PARAM_STREAM_ID, streamid);
1457 chipio_set_control_param_no_mutex(codec,
1458 CONTROL_PARAM_STREAM_CONTROL, enable);
1459}
1460
Connor McAdams38ba69f2018-05-08 13:20:07 -04001461
1462/*
1463 * Set sampling rate of the connection point. NO MUTEX.
1464 */
1465static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
1466 int connid, enum ca0132_sample_rate rate)
1467{
1468 chipio_set_control_param_no_mutex(codec,
1469 CONTROL_PARAM_CONN_POINT_ID, connid);
1470 chipio_set_control_param_no_mutex(codec,
1471 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
1472}
1473
Connor McAdams009b8f92018-05-08 13:20:06 -04001474/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001475 * Set sampling rate of the connection point.
1476 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001477static void chipio_set_conn_rate(struct hda_codec *codec,
1478 int connid, enum ca0132_sample_rate rate)
1479{
1480 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1481 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1482 rate);
1483}
1484
Ian Minettd5c21b82012-09-20 20:29:18 -07001485/*
1486 * Enable clocks.
1487 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001488static void chipio_enable_clocks(struct hda_codec *codec)
1489{
1490 struct ca0132_spec *spec = codec->spec;
1491
1492 mutex_lock(&spec->chipio_mutex);
1493 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1494 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1495 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1496 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1497 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1498 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1499 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1500 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1501 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1502 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1503 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1504 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1505 mutex_unlock(&spec->chipio_mutex);
1506}
1507
1508/*
1509 * CA0132 DSP IO stuffs
1510 */
1511static int dspio_send(struct hda_codec *codec, unsigned int reg,
1512 unsigned int data)
1513{
Takashi Iwaib645d792013-01-15 17:39:29 +01001514 int res;
Ian Minett6d675302013-02-08 18:31:43 -08001515 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001516
1517 /* send bits of data specified by reg to dsp */
1518 do {
1519 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1520 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1521 return res;
Ian Minett6d675302013-02-08 18:31:43 -08001522 msleep(20);
1523 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001524
1525 return -EIO;
1526}
1527
Ian Minettd5c21b82012-09-20 20:29:18 -07001528/*
1529 * Wait for DSP to be ready for commands
1530 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001531static void dspio_write_wait(struct hda_codec *codec)
1532{
Ian Minett4861af82012-09-20 20:29:20 -07001533 int status;
1534 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001535
Ian Minett01ef7db2012-09-20 20:29:16 -07001536 do {
Ian Minett4861af82012-09-20 20:29:20 -07001537 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1538 VENDOR_DSPIO_STATUS, 0);
1539 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1540 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1541 break;
1542 msleep(1);
1543 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001544}
1545
Ian Minettd5c21b82012-09-20 20:29:18 -07001546/*
1547 * Write SCP data to DSP
1548 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001549static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1550{
1551 struct ca0132_spec *spec = codec->spec;
1552 int status;
1553
1554 dspio_write_wait(codec);
1555
1556 mutex_lock(&spec->chipio_mutex);
1557 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1558 scp_data & 0xffff);
1559 if (status < 0)
1560 goto error;
1561
1562 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1563 scp_data >> 16);
1564 if (status < 0)
1565 goto error;
1566
1567 /* OK, now check if the write itself has executed*/
1568 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1569 VENDOR_DSPIO_STATUS, 0);
1570error:
1571 mutex_unlock(&spec->chipio_mutex);
1572
1573 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1574 -EIO : 0;
1575}
1576
Ian Minettd5c21b82012-09-20 20:29:18 -07001577/*
1578 * Write multiple SCP data to DSP
1579 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001580static int dspio_write_multiple(struct hda_codec *codec,
1581 unsigned int *buffer, unsigned int size)
1582{
1583 int status = 0;
1584 unsigned int count;
1585
Matthias Kaehlckea16fbb82017-03-15 15:41:23 -07001586 if (buffer == NULL)
Ian Minett01ef7db2012-09-20 20:29:16 -07001587 return -EINVAL;
1588
1589 count = 0;
1590 while (count < size) {
1591 status = dspio_write(codec, *buffer++);
1592 if (status != 0)
1593 break;
1594 count++;
1595 }
1596
1597 return status;
1598}
1599
Ian Minetta73d5112012-12-20 18:53:37 -08001600static int dspio_read(struct hda_codec *codec, unsigned int *data)
1601{
1602 int status;
1603
1604 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1605 if (status == -EIO)
1606 return status;
1607
1608 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1609 if (status == -EIO ||
1610 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1611 return -EIO;
1612
1613 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1614 VENDOR_DSPIO_SCP_READ_DATA, 0);
1615
1616 return 0;
1617}
1618
1619static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1620 unsigned int *buf_size, unsigned int size_count)
1621{
1622 int status = 0;
1623 unsigned int size = *buf_size;
1624 unsigned int count;
1625 unsigned int skip_count;
1626 unsigned int dummy;
1627
Matthias Kaehlckea16fbb82017-03-15 15:41:23 -07001628 if (buffer == NULL)
Ian Minetta73d5112012-12-20 18:53:37 -08001629 return -1;
1630
1631 count = 0;
1632 while (count < size && count < size_count) {
1633 status = dspio_read(codec, buffer++);
1634 if (status != 0)
1635 break;
1636 count++;
1637 }
1638
1639 skip_count = count;
1640 if (status == 0) {
1641 while (skip_count < size) {
1642 status = dspio_read(codec, &dummy);
1643 if (status != 0)
1644 break;
1645 skip_count++;
1646 }
1647 }
1648 *buf_size = count;
1649
1650 return status;
1651}
1652
Ian Minettd5c21b82012-09-20 20:29:18 -07001653/*
1654 * Construct the SCP header using corresponding fields
1655 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001656static inline unsigned int
1657make_scp_header(unsigned int target_id, unsigned int source_id,
1658 unsigned int get_flag, unsigned int req,
1659 unsigned int device_flag, unsigned int resp_flag,
1660 unsigned int error_flag, unsigned int data_size)
1661{
1662 unsigned int header = 0;
1663
1664 header = (data_size & 0x1f) << 27;
1665 header |= (error_flag & 0x01) << 26;
1666 header |= (resp_flag & 0x01) << 25;
1667 header |= (device_flag & 0x01) << 24;
1668 header |= (req & 0x7f) << 17;
1669 header |= (get_flag & 0x01) << 16;
1670 header |= (source_id & 0xff) << 8;
1671 header |= target_id & 0xff;
1672
1673 return header;
1674}
1675
Ian Minettd5c21b82012-09-20 20:29:18 -07001676/*
1677 * Extract corresponding fields from SCP header
1678 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001679static inline void
1680extract_scp_header(unsigned int header,
1681 unsigned int *target_id, unsigned int *source_id,
1682 unsigned int *get_flag, unsigned int *req,
1683 unsigned int *device_flag, unsigned int *resp_flag,
1684 unsigned int *error_flag, unsigned int *data_size)
1685{
1686 if (data_size)
1687 *data_size = (header >> 27) & 0x1f;
1688 if (error_flag)
1689 *error_flag = (header >> 26) & 0x01;
1690 if (resp_flag)
1691 *resp_flag = (header >> 25) & 0x01;
1692 if (device_flag)
1693 *device_flag = (header >> 24) & 0x01;
1694 if (req)
1695 *req = (header >> 17) & 0x7f;
1696 if (get_flag)
1697 *get_flag = (header >> 16) & 0x01;
1698 if (source_id)
1699 *source_id = (header >> 8) & 0xff;
1700 if (target_id)
1701 *target_id = header & 0xff;
1702}
1703
1704#define SCP_MAX_DATA_WORDS (16)
1705
1706/* Structure to contain any SCP message */
1707struct scp_msg {
1708 unsigned int hdr;
1709 unsigned int data[SCP_MAX_DATA_WORDS];
1710};
1711
Ian Minetta73d5112012-12-20 18:53:37 -08001712static void dspio_clear_response_queue(struct hda_codec *codec)
1713{
1714 unsigned int dummy = 0;
1715 int status = -1;
1716
1717 /* clear all from the response queue */
1718 do {
1719 status = dspio_read(codec, &dummy);
1720 } while (status == 0);
1721}
1722
1723static int dspio_get_response_data(struct hda_codec *codec)
1724{
1725 struct ca0132_spec *spec = codec->spec;
1726 unsigned int data = 0;
1727 unsigned int count;
1728
1729 if (dspio_read(codec, &data) < 0)
1730 return -EIO;
1731
1732 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1733 spec->scp_resp_header = data;
1734 spec->scp_resp_count = data >> 27;
1735 count = spec->wait_num_data;
1736 dspio_read_multiple(codec, spec->scp_resp_data,
1737 &spec->scp_resp_count, count);
1738 return 0;
1739 }
1740
1741 return -EIO;
1742}
1743
Ian Minettd5c21b82012-09-20 20:29:18 -07001744/*
1745 * Send SCP message to DSP
1746 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001747static int dspio_send_scp_message(struct hda_codec *codec,
1748 unsigned char *send_buf,
1749 unsigned int send_buf_size,
1750 unsigned char *return_buf,
1751 unsigned int return_buf_size,
1752 unsigned int *bytes_returned)
1753{
1754 struct ca0132_spec *spec = codec->spec;
Ian Minett01ef7db2012-09-20 20:29:16 -07001755 int status = -1;
1756 unsigned int scp_send_size = 0;
1757 unsigned int total_size;
1758 bool waiting_for_resp = false;
1759 unsigned int header;
1760 struct scp_msg *ret_msg;
1761 unsigned int resp_src_id, resp_target_id;
1762 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1763
1764 if (bytes_returned)
1765 *bytes_returned = 0;
1766
1767 /* get scp header from buffer */
1768 header = *((unsigned int *)send_buf);
1769 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1770 &device_flag, NULL, NULL, &data_size);
1771 scp_send_size = data_size + 1;
1772 total_size = (scp_send_size * 4);
1773
1774 if (send_buf_size < total_size)
1775 return -EINVAL;
1776
1777 if (get_flag || device_flag) {
1778 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1779 return -EINVAL;
1780
1781 spec->wait_scp_header = *((unsigned int *)send_buf);
1782
1783 /* swap source id with target id */
1784 resp_target_id = src_id;
1785 resp_src_id = target_id;
1786 spec->wait_scp_header &= 0xffff0000;
1787 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1788 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1789 spec->wait_scp = 1;
1790 waiting_for_resp = true;
1791 }
1792
1793 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1794 scp_send_size);
1795 if (status < 0) {
1796 spec->wait_scp = 0;
1797 return status;
1798 }
1799
1800 if (waiting_for_resp) {
Ian Minett6d675302013-02-08 18:31:43 -08001801 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001802 memset(return_buf, 0, return_buf_size);
Ian Minett01ef7db2012-09-20 20:29:16 -07001803 do {
1804 msleep(20);
Ian Minett6d675302013-02-08 18:31:43 -08001805 } while (spec->wait_scp && time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001806 waiting_for_resp = false;
Ian Minett6d675302013-02-08 18:31:43 -08001807 if (!spec->wait_scp) {
Ian Minett01ef7db2012-09-20 20:29:16 -07001808 ret_msg = (struct scp_msg *)return_buf;
1809 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1810 memcpy(&ret_msg->data, spec->scp_resp_data,
1811 spec->wait_num_data);
1812 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1813 status = 0;
1814 } else {
1815 status = -EIO;
1816 }
1817 spec->wait_scp = 0;
1818 }
1819
1820 return status;
1821}
1822
Ian Minettd5c21b82012-09-20 20:29:18 -07001823/**
1824 * Prepare and send the SCP message to DSP
1825 * @codec: the HDA codec
1826 * @mod_id: ID of the DSP module to send the command
1827 * @req: ID of request to send to the DSP module
1828 * @dir: SET or GET
1829 * @data: pointer to the data to send with the request, request specific
1830 * @len: length of the data, in bytes
1831 * @reply: point to the buffer to hold data returned for a reply
1832 * @reply_len: length of the reply buffer returned from GET
1833 *
1834 * Returns zero or a negative error code.
1835 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001836static int dspio_scp(struct hda_codec *codec,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001837 int mod_id, int src_id, int req, int dir, const void *data,
1838 unsigned int len, void *reply, unsigned int *reply_len)
Ian Minett01ef7db2012-09-20 20:29:16 -07001839{
1840 int status = 0;
1841 struct scp_msg scp_send, scp_reply;
1842 unsigned int ret_bytes, send_size, ret_size;
1843 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1844 unsigned int reply_data_size;
1845
1846 memset(&scp_send, 0, sizeof(scp_send));
1847 memset(&scp_reply, 0, sizeof(scp_reply));
1848
1849 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1850 return -EINVAL;
1851
1852 if (dir == SCP_GET && reply == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001853 codec_dbg(codec, "dspio_scp get but has no buffer\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001854 return -EINVAL;
1855 }
1856
1857 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001858 codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001859 return -EINVAL;
1860 }
1861
Connor McAdams447fd8e2018-05-08 13:20:09 -04001862 scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
Ian Minett01ef7db2012-09-20 20:29:16 -07001863 0, 0, 0, len/sizeof(unsigned int));
1864 if (data != NULL && len > 0) {
1865 len = min((unsigned int)(sizeof(scp_send.data)), len);
1866 memcpy(scp_send.data, data, len);
1867 }
1868
1869 ret_bytes = 0;
1870 send_size = sizeof(unsigned int) + len;
1871 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1872 send_size, (unsigned char *)&scp_reply,
1873 sizeof(scp_reply), &ret_bytes);
1874
1875 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001876 codec_dbg(codec, "dspio_scp: send scp msg failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001877 return status;
1878 }
1879
1880 /* extract send and reply headers members */
1881 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1882 NULL, NULL, NULL, NULL, NULL);
1883 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1884 &reply_resp_flag, &reply_error_flag,
1885 &reply_data_size);
1886
1887 if (!send_get_flag)
1888 return 0;
1889
1890 if (reply_resp_flag && !reply_error_flag) {
1891 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1892 / sizeof(unsigned int);
1893
1894 if (*reply_len < ret_size*sizeof(unsigned int)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001895 codec_dbg(codec, "reply too long for buf\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001896 return -EINVAL;
1897 } else if (ret_size != reply_data_size) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001898 codec_dbg(codec, "RetLen and HdrLen .NE.\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001899 return -EINVAL;
Arnd Bergmann46a049d2017-01-11 14:39:44 +01001900 } else if (!reply) {
1901 codec_dbg(codec, "NULL reply\n");
1902 return -EINVAL;
Ian Minett01ef7db2012-09-20 20:29:16 -07001903 } else {
1904 *reply_len = ret_size*sizeof(unsigned int);
1905 memcpy(reply, scp_reply.data, *reply_len);
1906 }
1907 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001908 codec_dbg(codec, "reply ill-formed or errflag set\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001909 return -EIO;
1910 }
1911
1912 return status;
1913}
1914
Ian Minettd5c21b82012-09-20 20:29:18 -07001915/*
Ian Minett5aaca442012-12-20 18:53:34 -08001916 * Set DSP parameters
1917 */
1918static int dspio_set_param(struct hda_codec *codec, int mod_id,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001919 int src_id, int req, const void *data, unsigned int len)
Ian Minett5aaca442012-12-20 18:53:34 -08001920{
Connor McAdams447fd8e2018-05-08 13:20:09 -04001921 return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
1922 NULL);
Ian Minett5aaca442012-12-20 18:53:34 -08001923}
1924
1925static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001926 int req, const unsigned int data)
Ian Minett5aaca442012-12-20 18:53:34 -08001927{
Connor McAdams447fd8e2018-05-08 13:20:09 -04001928 return dspio_set_param(codec, mod_id, 0x20, req, &data,
1929 sizeof(unsigned int));
1930}
1931
1932static int dspio_set_uint_param_no_source(struct hda_codec *codec, int mod_id,
1933 int req, const unsigned int data)
1934{
1935 return dspio_set_param(codec, mod_id, 0x00, req, &data,
1936 sizeof(unsigned int));
Ian Minett5aaca442012-12-20 18:53:34 -08001937}
1938
1939/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001940 * Allocate a DSP DMA channel via an SCP message
1941 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001942static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1943{
1944 int status = 0;
1945 unsigned int size = sizeof(dma_chan);
1946
Takashi Iwai4e76a882014-02-25 12:21:03 +01001947 codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
Connor McAdams447fd8e2018-05-08 13:20:09 -04001948 status = dspio_scp(codec, MASTERCONTROL, 0x20,
1949 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
1950 dma_chan, &size);
Ian Minett01ef7db2012-09-20 20:29:16 -07001951
1952 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001953 codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001954 return status;
1955 }
1956
1957 if ((*dma_chan + 1) == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001958 codec_dbg(codec, "no free dma channels to allocate\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001959 return -EBUSY;
1960 }
1961
Takashi Iwai4e76a882014-02-25 12:21:03 +01001962 codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1963 codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001964
1965 return status;
1966}
1967
Ian Minettd5c21b82012-09-20 20:29:18 -07001968/*
1969 * Free a DSP DMA via an SCP message
1970 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001971static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1972{
1973 int status = 0;
1974 unsigned int dummy = 0;
1975
Takashi Iwai4e76a882014-02-25 12:21:03 +01001976 codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
1977 codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
Ian Minett01ef7db2012-09-20 20:29:16 -07001978
Connor McAdams447fd8e2018-05-08 13:20:09 -04001979 status = dspio_scp(codec, MASTERCONTROL, 0x20,
1980 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
1981 sizeof(dma_chan), NULL, &dummy);
Ian Minett01ef7db2012-09-20 20:29:16 -07001982
1983 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001984 codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001985 return status;
1986 }
1987
Takashi Iwai4e76a882014-02-25 12:21:03 +01001988 codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001989
1990 return status;
1991}
1992
1993/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001994 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001995 */
1996static int dsp_set_run_state(struct hda_codec *codec)
1997{
1998 unsigned int dbg_ctrl_reg;
1999 unsigned int halt_state;
2000 int err;
2001
2002 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
2003 if (err < 0)
2004 return err;
2005
2006 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
2007 DSP_DBGCNTL_STATE_LOBIT;
2008
2009 if (halt_state != 0) {
2010 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
2011 DSP_DBGCNTL_SS_MASK);
2012 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2013 dbg_ctrl_reg);
2014 if (err < 0)
2015 return err;
2016
2017 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
2018 DSP_DBGCNTL_EXEC_MASK;
2019 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2020 dbg_ctrl_reg);
2021 if (err < 0)
2022 return err;
2023 }
2024
2025 return 0;
2026}
2027
Ian Minettd5c21b82012-09-20 20:29:18 -07002028/*
2029 * Reset the DSP
2030 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002031static int dsp_reset(struct hda_codec *codec)
2032{
2033 unsigned int res;
2034 int retry = 20;
2035
Takashi Iwai4e76a882014-02-25 12:21:03 +01002036 codec_dbg(codec, "dsp_reset\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002037 do {
2038 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
2039 retry--;
2040 } while (res == -EIO && retry);
2041
2042 if (!retry) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002043 codec_dbg(codec, "dsp_reset timeout\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002044 return -EIO;
2045 }
2046
2047 return 0;
2048}
2049
Ian Minettd5c21b82012-09-20 20:29:18 -07002050/*
2051 * Convert chip address to DSP address
2052 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002053static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
2054 bool *code, bool *yram)
2055{
2056 *code = *yram = false;
2057
2058 if (UC_RANGE(chip_addx, 1)) {
2059 *code = true;
2060 return UC_OFF(chip_addx);
2061 } else if (X_RANGE_ALL(chip_addx, 1)) {
2062 return X_OFF(chip_addx);
2063 } else if (Y_RANGE_ALL(chip_addx, 1)) {
2064 *yram = true;
2065 return Y_OFF(chip_addx);
2066 }
2067
Takashi Iwai4a8b89f2013-02-12 10:15:15 +01002068 return INVALID_CHIP_ADDRESS;
Ian Minett01ef7db2012-09-20 20:29:16 -07002069}
2070
Ian Minettd5c21b82012-09-20 20:29:18 -07002071/*
2072 * Check if the DSP DMA is active
2073 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002074static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
2075{
2076 unsigned int dma_chnlstart_reg;
2077
2078 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
2079
2080 return ((dma_chnlstart_reg & (1 <<
2081 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
2082}
2083
2084static int dsp_dma_setup_common(struct hda_codec *codec,
2085 unsigned int chip_addx,
2086 unsigned int dma_chan,
2087 unsigned int port_map_mask,
2088 bool ovly)
2089{
2090 int status = 0;
2091 unsigned int chnl_prop;
2092 unsigned int dsp_addx;
2093 unsigned int active;
2094 bool code, yram;
2095
Takashi Iwai4e76a882014-02-25 12:21:03 +01002096 codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002097
2098 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002099 codec_dbg(codec, "dma chan num invalid\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002100 return -EINVAL;
2101 }
2102
2103 if (dsp_is_dma_active(codec, dma_chan)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002104 codec_dbg(codec, "dma already active\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002105 return -EBUSY;
2106 }
2107
2108 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2109
2110 if (dsp_addx == INVALID_CHIP_ADDRESS) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002111 codec_dbg(codec, "invalid chip addr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002112 return -ENXIO;
2113 }
2114
2115 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
2116 active = 0;
2117
Takashi Iwai4e76a882014-02-25 12:21:03 +01002118 codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002119
2120 if (ovly) {
2121 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
2122 &chnl_prop);
2123
2124 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002125 codec_dbg(codec, "read CHNLPROP Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002126 return status;
2127 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002128 codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002129 }
2130
2131 if (!code)
2132 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2133 else
2134 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2135
2136 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
2137
2138 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
2139 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002140 codec_dbg(codec, "write CHNLPROP Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002141 return status;
2142 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002143 codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002144
2145 if (ovly) {
2146 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
2147 &active);
2148
2149 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002150 codec_dbg(codec, "read ACTIVE Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002151 return status;
2152 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002153 codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002154 }
2155
2156 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
2157 DSPDMAC_ACTIVE_AAR_MASK;
2158
2159 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
2160 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002161 codec_dbg(codec, "write ACTIVE Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002162 return status;
2163 }
2164
Takashi Iwai4e76a882014-02-25 12:21:03 +01002165 codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002166
2167 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
2168 port_map_mask);
2169 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002170 codec_dbg(codec, "write AUDCHSEL Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002171 return status;
2172 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002173 codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002174
2175 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
2176 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
2177 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002178 codec_dbg(codec, "write IRQCNT Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002179 return status;
2180 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002181 codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002182
Takashi Iwai4e76a882014-02-25 12:21:03 +01002183 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002184 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
2185 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
2186 chip_addx, dsp_addx, dma_chan,
2187 port_map_mask, chnl_prop, active);
2188
Takashi Iwai4e76a882014-02-25 12:21:03 +01002189 codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002190
2191 return 0;
2192}
2193
Ian Minettd5c21b82012-09-20 20:29:18 -07002194/*
2195 * Setup the DSP DMA per-transfer-specific registers
2196 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002197static int dsp_dma_setup(struct hda_codec *codec,
2198 unsigned int chip_addx,
2199 unsigned int count,
2200 unsigned int dma_chan)
2201{
2202 int status = 0;
2203 bool code, yram;
2204 unsigned int dsp_addx;
2205 unsigned int addr_field;
2206 unsigned int incr_field;
2207 unsigned int base_cnt;
2208 unsigned int cur_cnt;
2209 unsigned int dma_cfg = 0;
2210 unsigned int adr_ofs = 0;
2211 unsigned int xfr_cnt = 0;
2212 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
2213 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
2214
Takashi Iwai4e76a882014-02-25 12:21:03 +01002215 codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002216
2217 if (count > max_dma_count) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002218 codec_dbg(codec, "count too big\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002219 return -EINVAL;
2220 }
2221
2222 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2223 if (dsp_addx == INVALID_CHIP_ADDRESS) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002224 codec_dbg(codec, "invalid chip addr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002225 return -ENXIO;
2226 }
2227
Takashi Iwai4e76a882014-02-25 12:21:03 +01002228 codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002229
2230 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
2231 incr_field = 0;
2232
2233 if (!code) {
2234 addr_field <<= 1;
2235 if (yram)
2236 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
2237
2238 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
2239 }
2240
2241 dma_cfg = addr_field + incr_field;
2242 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
2243 dma_cfg);
2244 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002245 codec_dbg(codec, "write DMACFG Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002246 return status;
2247 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002248 codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002249
2250 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
2251 (code ? 0 : 1));
2252
2253 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
2254 adr_ofs);
2255 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002256 codec_dbg(codec, "write DSPADROFS Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002257 return status;
2258 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002259 codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002260
2261 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
2262
2263 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
2264
2265 xfr_cnt = base_cnt | cur_cnt;
2266
2267 status = chipio_write(codec,
2268 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
2269 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002270 codec_dbg(codec, "write XFRCNT Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002271 return status;
2272 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002273 codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002274
Takashi Iwai4e76a882014-02-25 12:21:03 +01002275 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002276 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
2277 "ADROFS=0x%x, XFRCNT=0x%x\n",
2278 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
2279
Takashi Iwai4e76a882014-02-25 12:21:03 +01002280 codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002281
2282 return 0;
2283}
2284
Ian Minettd5c21b82012-09-20 20:29:18 -07002285/*
2286 * Start the DSP DMA
2287 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002288static int dsp_dma_start(struct hda_codec *codec,
2289 unsigned int dma_chan, bool ovly)
2290{
2291 unsigned int reg = 0;
2292 int status = 0;
2293
Takashi Iwai4e76a882014-02-25 12:21:03 +01002294 codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002295
2296 if (ovly) {
2297 status = chipio_read(codec,
2298 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2299
2300 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002301 codec_dbg(codec, "read CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002302 return status;
2303 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002304 codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002305
2306 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2307 DSPDMAC_CHNLSTART_DIS_MASK);
2308 }
2309
2310 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2311 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
2312 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002313 codec_dbg(codec, "write CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002314 return status;
2315 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002316 codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002317
2318 return status;
2319}
2320
Ian Minettd5c21b82012-09-20 20:29:18 -07002321/*
2322 * Stop the DSP DMA
2323 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002324static int dsp_dma_stop(struct hda_codec *codec,
2325 unsigned int dma_chan, bool ovly)
2326{
2327 unsigned int reg = 0;
2328 int status = 0;
2329
Takashi Iwai4e76a882014-02-25 12:21:03 +01002330 codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002331
2332 if (ovly) {
2333 status = chipio_read(codec,
2334 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2335
2336 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002337 codec_dbg(codec, "read CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002338 return status;
2339 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002340 codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002341 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2342 DSPDMAC_CHNLSTART_DIS_MASK);
2343 }
2344
2345 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2346 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
2347 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002348 codec_dbg(codec, "write CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002349 return status;
2350 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002351 codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002352
2353 return status;
2354}
2355
Ian Minettd5c21b82012-09-20 20:29:18 -07002356/**
2357 * Allocate router ports
2358 *
2359 * @codec: the HDA codec
2360 * @num_chans: number of channels in the stream
2361 * @ports_per_channel: number of ports per channel
2362 * @start_device: start device
2363 * @port_map: pointer to the port list to hold the allocated ports
2364 *
2365 * Returns zero or a negative error code.
2366 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002367static int dsp_allocate_router_ports(struct hda_codec *codec,
2368 unsigned int num_chans,
2369 unsigned int ports_per_channel,
2370 unsigned int start_device,
2371 unsigned int *port_map)
2372{
2373 int status = 0;
2374 int res;
2375 u8 val;
2376
2377 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2378 if (status < 0)
2379 return status;
2380
2381 val = start_device << 6;
2382 val |= (ports_per_channel - 1) << 4;
2383 val |= num_chans - 1;
2384
2385 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2386 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
2387 val);
2388
2389 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2390 VENDOR_CHIPIO_PORT_ALLOC_SET,
2391 MEM_CONNID_DSP);
2392
2393 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2394 if (status < 0)
2395 return status;
2396
2397 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
2398 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
2399
2400 *port_map = res;
2401
2402 return (res < 0) ? res : 0;
2403}
2404
Ian Minettd5c21b82012-09-20 20:29:18 -07002405/*
2406 * Free router ports
2407 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002408static int dsp_free_router_ports(struct hda_codec *codec)
2409{
2410 int status = 0;
2411
2412 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2413 if (status < 0)
2414 return status;
2415
2416 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2417 VENDOR_CHIPIO_PORT_FREE_SET,
2418 MEM_CONNID_DSP);
2419
2420 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2421
2422 return status;
2423}
2424
Ian Minettd5c21b82012-09-20 20:29:18 -07002425/*
2426 * Allocate DSP ports for the download stream
2427 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002428static int dsp_allocate_ports(struct hda_codec *codec,
2429 unsigned int num_chans,
2430 unsigned int rate_multi, unsigned int *port_map)
2431{
2432 int status;
2433
Takashi Iwai4e76a882014-02-25 12:21:03 +01002434 codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002435
2436 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002437 codec_dbg(codec, "bad rate multiple\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002438 return -EINVAL;
2439 }
2440
2441 status = dsp_allocate_router_ports(codec, num_chans,
2442 rate_multi, 0, port_map);
2443
Takashi Iwai4e76a882014-02-25 12:21:03 +01002444 codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002445
2446 return status;
2447}
2448
Ian Minett01ef7db2012-09-20 20:29:16 -07002449static int dsp_allocate_ports_format(struct hda_codec *codec,
2450 const unsigned short fmt,
2451 unsigned int *port_map)
2452{
2453 int status;
2454 unsigned int num_chans;
2455
2456 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2457 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2458 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2459
2460 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002461 codec_dbg(codec, "bad rate multiple\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002462 return -EINVAL;
2463 }
2464
2465 num_chans = get_hdafmt_chs(fmt) + 1;
2466
2467 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2468
2469 return status;
2470}
2471
2472/*
Ian Minettd5c21b82012-09-20 20:29:18 -07002473 * free DSP ports
2474 */
2475static int dsp_free_ports(struct hda_codec *codec)
2476{
2477 int status;
2478
Takashi Iwai4e76a882014-02-25 12:21:03 +01002479 codec_dbg(codec, " dsp_free_ports() -- begin\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002480
2481 status = dsp_free_router_ports(codec);
2482 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002483 codec_dbg(codec, "free router ports fail\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002484 return status;
2485 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002486 codec_dbg(codec, " dsp_free_ports() -- complete\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002487
2488 return status;
2489}
2490
2491/*
Ian Minett01ef7db2012-09-20 20:29:16 -07002492 * HDA DMA engine stuffs for DSP code download
2493 */
2494struct dma_engine {
2495 struct hda_codec *codec;
2496 unsigned short m_converter_format;
2497 struct snd_dma_buffer *dmab;
2498 unsigned int buf_size;
2499};
2500
2501
2502enum dma_state {
2503 DMA_STATE_STOP = 0,
2504 DMA_STATE_RUN = 1
2505};
2506
Takashi Iwai6194b992014-06-06 18:12:16 +02002507static int dma_convert_to_hda_format(struct hda_codec *codec,
Ian Minette97249d2012-09-20 20:29:21 -07002508 unsigned int sample_rate,
2509 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002510 unsigned short *hda_format)
2511{
2512 unsigned int format_val;
2513
Takashi Iwaib7d023e2015-04-16 08:19:06 +02002514 format_val = snd_hdac_calc_stream_format(sample_rate,
2515 channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07002516
2517 if (hda_format)
2518 *hda_format = (unsigned short)format_val;
2519
2520 return 0;
2521}
2522
Ian Minettd5c21b82012-09-20 20:29:18 -07002523/*
2524 * Reset DMA for DSP download
2525 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002526static int dma_reset(struct dma_engine *dma)
2527{
2528 struct hda_codec *codec = dma->codec;
2529 struct ca0132_spec *spec = codec->spec;
2530 int status;
2531
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002532 if (dma->dmab->area)
Ian Minett01ef7db2012-09-20 20:29:16 -07002533 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2534
2535 status = snd_hda_codec_load_dsp_prepare(codec,
2536 dma->m_converter_format,
2537 dma->buf_size,
2538 dma->dmab);
2539 if (status < 0)
2540 return status;
2541 spec->dsp_stream_id = status;
2542 return 0;
2543}
2544
2545static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2546{
2547 bool cmd;
2548
Ian Minett01ef7db2012-09-20 20:29:16 -07002549 switch (state) {
2550 case DMA_STATE_STOP:
2551 cmd = false;
2552 break;
2553 case DMA_STATE_RUN:
2554 cmd = true;
2555 break;
2556 default:
2557 return 0;
2558 }
2559
2560 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2561 return 0;
2562}
2563
2564static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2565{
2566 return dma->dmab->bytes;
2567}
2568
2569static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2570{
2571 return dma->dmab->area;
2572}
2573
2574static int dma_xfer(struct dma_engine *dma,
2575 const unsigned int *data,
2576 unsigned int count)
2577{
2578 memcpy(dma->dmab->area, data, count);
2579 return 0;
2580}
2581
2582static void dma_get_converter_format(
2583 struct dma_engine *dma,
2584 unsigned short *format)
2585{
2586 if (format)
2587 *format = dma->m_converter_format;
2588}
2589
2590static unsigned int dma_get_stream_id(struct dma_engine *dma)
2591{
2592 struct ca0132_spec *spec = dma->codec->spec;
2593
2594 return spec->dsp_stream_id;
2595}
2596
2597struct dsp_image_seg {
2598 u32 magic;
2599 u32 chip_addr;
2600 u32 count;
2601 u32 data[0];
2602};
2603
2604static const u32 g_magic_value = 0x4c46584d;
2605static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2606
2607static bool is_valid(const struct dsp_image_seg *p)
2608{
2609 return p->magic == g_magic_value;
2610}
2611
2612static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2613{
2614 return g_chip_addr_magic_value == p->chip_addr;
2615}
2616
2617static bool is_last(const struct dsp_image_seg *p)
2618{
2619 return p->count == 0;
2620}
2621
2622static size_t dsp_sizeof(const struct dsp_image_seg *p)
2623{
2624 return sizeof(*p) + p->count*sizeof(u32);
2625}
2626
2627static const struct dsp_image_seg *get_next_seg_ptr(
2628 const struct dsp_image_seg *p)
2629{
2630 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2631}
2632
2633/*
2634 * CA0132 chip DSP transfer stuffs. For DSP download.
2635 */
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002636#define INVALID_DMA_CHANNEL (~0U)
Ian Minett01ef7db2012-09-20 20:29:16 -07002637
Ian Minettd5c21b82012-09-20 20:29:18 -07002638/*
2639 * Program a list of address/data pairs via the ChipIO widget.
2640 * The segment data is in the format of successive pairs of words.
2641 * These are repeated as indicated by the segment's count field.
2642 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002643static int dspxfr_hci_write(struct hda_codec *codec,
2644 const struct dsp_image_seg *fls)
2645{
2646 int status;
2647 const u32 *data;
2648 unsigned int count;
2649
2650 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002651 codec_dbg(codec, "hci_write invalid params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002652 return -EINVAL;
2653 }
2654
2655 count = fls->count;
2656 data = (u32 *)(fls->data);
2657 while (count >= 2) {
2658 status = chipio_write(codec, data[0], data[1]);
2659 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002660 codec_dbg(codec, "hci_write chipio failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002661 return status;
2662 }
2663 count -= 2;
2664 data += 2;
2665 }
2666 return 0;
2667}
2668
Ian Minettd5c21b82012-09-20 20:29:18 -07002669/**
2670 * Write a block of data into DSP code or data RAM using pre-allocated
2671 * DMA engine.
2672 *
2673 * @codec: the HDA codec
2674 * @fls: pointer to a fast load image
2675 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2676 * no relocation
2677 * @dma_engine: pointer to DMA engine to be used for DSP download
2678 * @dma_chan: The number of DMA channels used for DSP download
2679 * @port_map_mask: port mapping
2680 * @ovly: TRUE if overlay format is required
2681 *
2682 * Returns zero or a negative error code.
2683 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002684static int dspxfr_one_seg(struct hda_codec *codec,
2685 const struct dsp_image_seg *fls,
2686 unsigned int reloc,
2687 struct dma_engine *dma_engine,
2688 unsigned int dma_chan,
2689 unsigned int port_map_mask,
2690 bool ovly)
2691{
Ian Minett406261c2012-12-20 18:53:41 -08002692 int status = 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002693 bool comm_dma_setup_done = false;
2694 const unsigned int *data;
2695 unsigned int chip_addx;
2696 unsigned int words_to_write;
2697 unsigned int buffer_size_words;
2698 unsigned char *buffer_addx;
2699 unsigned short hda_format;
2700 unsigned int sample_rate_div;
2701 unsigned int sample_rate_mul;
2702 unsigned int num_chans;
2703 unsigned int hda_frame_size_words;
2704 unsigned int remainder_words;
2705 const u32 *data_remainder;
2706 u32 chip_addx_remainder;
2707 unsigned int run_size_words;
2708 const struct dsp_image_seg *hci_write = NULL;
Ian Minett6d675302013-02-08 18:31:43 -08002709 unsigned long timeout;
2710 bool dma_active;
Ian Minett01ef7db2012-09-20 20:29:16 -07002711
2712 if (fls == NULL)
2713 return -EINVAL;
2714 if (is_hci_prog_list_seg(fls)) {
2715 hci_write = fls;
2716 fls = get_next_seg_ptr(fls);
2717 }
2718
2719 if (hci_write && (!fls || is_last(fls))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002720 codec_dbg(codec, "hci_write\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002721 return dspxfr_hci_write(codec, hci_write);
2722 }
2723
2724 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002725 codec_dbg(codec, "Invalid Params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002726 return -EINVAL;
2727 }
2728
2729 data = fls->data;
2730 chip_addx = fls->chip_addr,
2731 words_to_write = fls->count;
2732
2733 if (!words_to_write)
2734 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2735 if (reloc)
2736 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2737
2738 if (!UC_RANGE(chip_addx, words_to_write) &&
2739 !X_RANGE_ALL(chip_addx, words_to_write) &&
2740 !Y_RANGE_ALL(chip_addx, words_to_write)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002741 codec_dbg(codec, "Invalid chip_addx Params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002742 return -EINVAL;
2743 }
2744
2745 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2746 sizeof(u32);
2747
2748 buffer_addx = dma_get_buffer_addr(dma_engine);
2749
2750 if (buffer_addx == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002751 codec_dbg(codec, "dma_engine buffer NULL\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002752 return -EINVAL;
2753 }
2754
2755 dma_get_converter_format(dma_engine, &hda_format);
2756 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2757 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2758 num_chans = get_hdafmt_chs(hda_format) + 1;
2759
2760 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2761 (num_chans * sample_rate_mul / sample_rate_div));
2762
Xi Wang3bc085a2013-03-07 00:13:51 -05002763 if (hda_frame_size_words == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002764 codec_dbg(codec, "frmsz zero\n");
Xi Wang3bc085a2013-03-07 00:13:51 -05002765 return -EINVAL;
2766 }
2767
Ian Minett01ef7db2012-09-20 20:29:16 -07002768 buffer_size_words = min(buffer_size_words,
2769 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2770 65536 : 32768));
2771 buffer_size_words -= buffer_size_words % hda_frame_size_words;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002772 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002773 "chpadr=0x%08x frmsz=%u nchan=%u "
2774 "rate_mul=%u div=%u bufsz=%u\n",
2775 chip_addx, hda_frame_size_words, num_chans,
2776 sample_rate_mul, sample_rate_div, buffer_size_words);
2777
Xi Wang3bc085a2013-03-07 00:13:51 -05002778 if (buffer_size_words < hda_frame_size_words) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002779 codec_dbg(codec, "dspxfr_one_seg:failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002780 return -EINVAL;
2781 }
2782
2783 remainder_words = words_to_write % hda_frame_size_words;
2784 data_remainder = data;
2785 chip_addx_remainder = chip_addx;
2786
2787 data += remainder_words;
2788 chip_addx += remainder_words*sizeof(u32);
2789 words_to_write -= remainder_words;
2790
2791 while (words_to_write != 0) {
2792 run_size_words = min(buffer_size_words, words_to_write);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002793 codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
Ian Minett01ef7db2012-09-20 20:29:16 -07002794 words_to_write, run_size_words, remainder_words);
2795 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2796 if (!comm_dma_setup_done) {
2797 status = dsp_dma_stop(codec, dma_chan, ovly);
2798 if (status < 0)
Takashi Iwai425a7882013-01-15 17:41:21 +01002799 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002800 status = dsp_dma_setup_common(codec, chip_addx,
2801 dma_chan, port_map_mask, ovly);
2802 if (status < 0)
2803 return status;
2804 comm_dma_setup_done = true;
2805 }
2806
2807 status = dsp_dma_setup(codec, chip_addx,
2808 run_size_words, dma_chan);
2809 if (status < 0)
2810 return status;
2811 status = dsp_dma_start(codec, dma_chan, ovly);
2812 if (status < 0)
2813 return status;
2814 if (!dsp_is_dma_active(codec, dma_chan)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002815 codec_dbg(codec, "dspxfr:DMA did not start\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002816 return -EIO;
2817 }
2818 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2819 if (status < 0)
2820 return status;
2821 if (remainder_words != 0) {
2822 status = chipio_write_multiple(codec,
2823 chip_addx_remainder,
2824 data_remainder,
2825 remainder_words);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002826 if (status < 0)
2827 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002828 remainder_words = 0;
2829 }
2830 if (hci_write) {
2831 status = dspxfr_hci_write(codec, hci_write);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002832 if (status < 0)
2833 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002834 hci_write = NULL;
2835 }
Ian Minett6d675302013-02-08 18:31:43 -08002836
2837 timeout = jiffies + msecs_to_jiffies(2000);
2838 do {
2839 dma_active = dsp_is_dma_active(codec, dma_chan);
2840 if (!dma_active)
Ian Minett01ef7db2012-09-20 20:29:16 -07002841 break;
Ian Minett6d675302013-02-08 18:31:43 -08002842 msleep(20);
2843 } while (time_before(jiffies, timeout));
2844 if (dma_active)
2845 break;
2846
Takashi Iwai4e76a882014-02-25 12:21:03 +01002847 codec_dbg(codec, "+++++ DMA complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002848 dma_set_state(dma_engine, DMA_STATE_STOP);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002849 status = dma_reset(dma_engine);
Ian Minett01ef7db2012-09-20 20:29:16 -07002850
2851 if (status < 0)
2852 return status;
2853
2854 data += run_size_words;
2855 chip_addx += run_size_words*sizeof(u32);
2856 words_to_write -= run_size_words;
2857 }
2858
2859 if (remainder_words != 0) {
2860 status = chipio_write_multiple(codec, chip_addx_remainder,
2861 data_remainder, remainder_words);
2862 }
2863
2864 return status;
2865}
2866
Ian Minettd5c21b82012-09-20 20:29:18 -07002867/**
2868 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2869 *
2870 * @codec: the HDA codec
2871 * @fls_data: pointer to a fast load image
2872 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2873 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002874 * @sample_rate: sampling rate of the stream used for DSP download
Takashi Iwaie60b2c72014-11-10 16:47:26 +01002875 * @channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002876 * @ovly: TRUE if overlay format is required
2877 *
2878 * Returns zero or a negative error code.
2879 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002880static int dspxfr_image(struct hda_codec *codec,
2881 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002882 unsigned int reloc,
2883 unsigned int sample_rate,
2884 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002885 bool ovly)
2886{
2887 struct ca0132_spec *spec = codec->spec;
2888 int status;
2889 unsigned short hda_format = 0;
2890 unsigned int response;
2891 unsigned char stream_id = 0;
2892 struct dma_engine *dma_engine;
2893 unsigned int dma_chan;
2894 unsigned int port_map_mask;
2895
2896 if (fls_data == NULL)
2897 return -EINVAL;
2898
2899 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai549e8292013-01-15 17:42:15 +01002900 if (!dma_engine)
2901 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002902
2903 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2904 if (!dma_engine->dmab) {
Takashi Iwai549e8292013-01-15 17:42:15 +01002905 kfree(dma_engine);
2906 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002907 }
2908
2909 dma_engine->codec = codec;
Takashi Iwai6194b992014-06-06 18:12:16 +02002910 dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002911 dma_engine->m_converter_format = hda_format;
2912 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2913 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2914
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002915 dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002916
2917 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2918 hda_format, &response);
2919
2920 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002921 codec_dbg(codec, "set converter format fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002922 goto exit;
2923 }
2924
2925 status = snd_hda_codec_load_dsp_prepare(codec,
2926 dma_engine->m_converter_format,
2927 dma_engine->buf_size,
2928 dma_engine->dmab);
2929 if (status < 0)
2930 goto exit;
2931 spec->dsp_stream_id = status;
2932
2933 if (ovly) {
2934 status = dspio_alloc_dma_chan(codec, &dma_chan);
2935 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002936 codec_dbg(codec, "alloc dmachan fail\n");
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002937 dma_chan = INVALID_DMA_CHANNEL;
Ian Minett01ef7db2012-09-20 20:29:16 -07002938 goto exit;
2939 }
2940 }
2941
2942 port_map_mask = 0;
2943 status = dsp_allocate_ports_format(codec, hda_format,
2944 &port_map_mask);
2945 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002946 codec_dbg(codec, "alloc ports fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002947 goto exit;
2948 }
2949
2950 stream_id = dma_get_stream_id(dma_engine);
2951 status = codec_set_converter_stream_channel(codec,
2952 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2953 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002954 codec_dbg(codec, "set stream chan fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002955 goto exit;
2956 }
2957
2958 while ((fls_data != NULL) && !is_last(fls_data)) {
2959 if (!is_valid(fls_data)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002960 codec_dbg(codec, "FLS check fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002961 status = -EINVAL;
2962 goto exit;
2963 }
2964 status = dspxfr_one_seg(codec, fls_data, reloc,
2965 dma_engine, dma_chan,
2966 port_map_mask, ovly);
2967 if (status < 0)
2968 break;
2969
2970 if (is_hci_prog_list_seg(fls_data))
2971 fls_data = get_next_seg_ptr(fls_data);
2972
2973 if ((fls_data != NULL) && !is_last(fls_data))
2974 fls_data = get_next_seg_ptr(fls_data);
2975 }
2976
2977 if (port_map_mask != 0)
2978 status = dsp_free_ports(codec);
2979
2980 if (status < 0)
2981 goto exit;
2982
2983 status = codec_set_converter_stream_channel(codec,
2984 WIDGET_CHIP_CTRL, 0, 0, &response);
2985
2986exit:
2987 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2988 dspio_free_dma_chan(codec, dma_chan);
2989
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002990 if (dma_engine->dmab->area)
Ian Minett01ef7db2012-09-20 20:29:16 -07002991 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2992 kfree(dma_engine->dmab);
2993 kfree(dma_engine);
2994
2995 return status;
2996}
2997
2998/*
2999 * CA0132 DSP download stuffs.
3000 */
3001static void dspload_post_setup(struct hda_codec *codec)
3002{
Connor McAdams009b8f92018-05-08 13:20:06 -04003003 struct ca0132_spec *spec = codec->spec;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003004 codec_dbg(codec, "---- dspload_post_setup ------\n");
Connor McAdams009b8f92018-05-08 13:20:06 -04003005 if (!spec->use_alt_functions) {
3006 /*set DSP speaker to 2.0 configuration*/
3007 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
3008 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
Ian Minett01ef7db2012-09-20 20:29:16 -07003009
Connor McAdams009b8f92018-05-08 13:20:06 -04003010 /*update write pointer*/
3011 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
3012 }
Ian Minett01ef7db2012-09-20 20:29:16 -07003013}
3014
Ian Minettd5c21b82012-09-20 20:29:18 -07003015/**
Takashi Iwaie60b2c72014-11-10 16:47:26 +01003016 * dspload_image - Download DSP from a DSP Image Fast Load structure.
Ian Minettd5c21b82012-09-20 20:29:18 -07003017 *
3018 * @codec: the HDA codec
3019 * @fls: pointer to a fast load image
3020 * @ovly: TRUE if overlay format is required
3021 * @reloc: Relocation address for loading single-segment overlays, or 0 for
3022 * no relocation
3023 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
3024 * @router_chans: number of audio router channels to be allocated (0 means use
3025 * internal defaults; max is 32)
3026 *
Takashi Iwaie60b2c72014-11-10 16:47:26 +01003027 * Download DSP from a DSP Image Fast Load structure. This structure is a
3028 * linear, non-constant sized element array of structures, each of which
3029 * contain the count of the data to be loaded, the data itself, and the
3030 * corresponding starting chip address of the starting data location.
Ian Minettd5c21b82012-09-20 20:29:18 -07003031 * Returns zero or a negative error code.
3032 */
Ian Minett01ef7db2012-09-20 20:29:16 -07003033static int dspload_image(struct hda_codec *codec,
3034 const struct dsp_image_seg *fls,
3035 bool ovly,
3036 unsigned int reloc,
3037 bool autostart,
3038 int router_chans)
3039{
3040 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07003041 unsigned int sample_rate;
3042 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07003043
Takashi Iwai4e76a882014-02-25 12:21:03 +01003044 codec_dbg(codec, "---- dspload_image begin ------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003045 if (router_chans == 0) {
3046 if (!ovly)
3047 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
3048 else
3049 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
3050 }
3051
Ian Minette97249d2012-09-20 20:29:21 -07003052 sample_rate = 48000;
3053 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07003054
Ian Minette97249d2012-09-20 20:29:21 -07003055 while (channels > 16) {
3056 sample_rate *= 2;
3057 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07003058 }
3059
Ian Minett01ef7db2012-09-20 20:29:16 -07003060 do {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003061 codec_dbg(codec, "Ready to program DMA\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003062 if (!ovly)
3063 status = dsp_reset(codec);
3064
3065 if (status < 0)
3066 break;
3067
Takashi Iwai4e76a882014-02-25 12:21:03 +01003068 codec_dbg(codec, "dsp_reset() complete\n");
Ian Minette97249d2012-09-20 20:29:21 -07003069 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
3070 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07003071
3072 if (status < 0)
3073 break;
3074
Takashi Iwai4e76a882014-02-25 12:21:03 +01003075 codec_dbg(codec, "dspxfr_image() complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003076 if (autostart && !ovly) {
3077 dspload_post_setup(codec);
3078 status = dsp_set_run_state(codec);
3079 }
3080
Takashi Iwai4e76a882014-02-25 12:21:03 +01003081 codec_dbg(codec, "LOAD FINISHED\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003082 } while (0);
3083
3084 return status;
3085}
3086
Takashi Iwaif6644172013-02-11 14:18:29 +01003087#ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07003088static bool dspload_is_loaded(struct hda_codec *codec)
3089{
3090 unsigned int data = 0;
3091 int status = 0;
3092
3093 status = chipio_read(codec, 0x40004, &data);
3094 if ((status < 0) || (data != 1))
3095 return false;
3096
3097 return true;
3098}
Takashi Iwaif6644172013-02-11 14:18:29 +01003099#else
3100#define dspload_is_loaded(codec) false
3101#endif
Ian Minett01ef7db2012-09-20 20:29:16 -07003102
3103static bool dspload_wait_loaded(struct hda_codec *codec)
3104{
Ian Minett6d675302013-02-08 18:31:43 -08003105 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
Ian Minett01ef7db2012-09-20 20:29:16 -07003106
3107 do {
Ian Minett01ef7db2012-09-20 20:29:16 -07003108 if (dspload_is_loaded(codec)) {
Takashi Iwaid9684bb2015-10-26 16:54:16 +01003109 codec_info(codec, "ca0132 DSP downloaded and running\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003110 return true;
3111 }
Ian Minett6d675302013-02-08 18:31:43 -08003112 msleep(20);
3113 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07003114
Takashi Iwaid9684bb2015-10-26 16:54:16 +01003115 codec_err(codec, "ca0132 failed to download DSP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003116 return false;
3117}
3118
Ian Minett5aaca442012-12-20 18:53:34 -08003119/*
Connor McAdamse93ac302018-05-08 13:20:05 -04003120 * Setup GPIO for the other variants of Core3D.
3121 */
3122
3123/*
Connor McAdamsa62e4732018-08-08 13:34:12 -04003124 * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
3125 * the mmio address 0x320 is used to set GPIO pins. The format for the data
3126 * The first eight bits are just the number of the pin. So far, I've only seen
3127 * this number go to 7.
3128 */
3129static void ca0132_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
3130 bool enable)
3131{
3132 struct ca0132_spec *spec = codec->spec;
3133 unsigned short gpio_data;
3134
3135 gpio_data = gpio_pin & 0xF;
3136 gpio_data |= ((enable << 8) & 0x100);
3137
3138 writew(gpio_data, spec->mem_base + 0x320);
3139}
3140
3141/*
Connor McAdamse93ac302018-05-08 13:20:05 -04003142 * Sets up the GPIO pins so that they are discoverable. If this isn't done,
3143 * the card shows as having no GPIO pins.
3144 */
3145static void ca0132_gpio_init(struct hda_codec *codec)
3146{
3147 struct ca0132_spec *spec = codec->spec;
3148
3149 switch (spec->quirk) {
3150 case QUIRK_SBZ:
3151 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3152 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
3153 snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
3154 break;
3155 case QUIRK_R3DI:
3156 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3157 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
3158 break;
3159 }
3160
3161}
3162
3163/* Sets the GPIO for audio output. */
3164static void ca0132_gpio_setup(struct hda_codec *codec)
3165{
3166 struct ca0132_spec *spec = codec->spec;
3167
3168 switch (spec->quirk) {
3169 case QUIRK_SBZ:
3170 snd_hda_codec_write(codec, 0x01, 0,
3171 AC_VERB_SET_GPIO_DIRECTION, 0x07);
3172 snd_hda_codec_write(codec, 0x01, 0,
3173 AC_VERB_SET_GPIO_MASK, 0x07);
3174 snd_hda_codec_write(codec, 0x01, 0,
3175 AC_VERB_SET_GPIO_DATA, 0x04);
3176 snd_hda_codec_write(codec, 0x01, 0,
3177 AC_VERB_SET_GPIO_DATA, 0x06);
3178 break;
3179 case QUIRK_R3DI:
3180 snd_hda_codec_write(codec, 0x01, 0,
3181 AC_VERB_SET_GPIO_DIRECTION, 0x1E);
3182 snd_hda_codec_write(codec, 0x01, 0,
3183 AC_VERB_SET_GPIO_MASK, 0x1F);
3184 snd_hda_codec_write(codec, 0x01, 0,
3185 AC_VERB_SET_GPIO_DATA, 0x0C);
3186 break;
3187 }
3188}
3189
3190/*
Connor McAdams7e6ed622018-05-08 13:20:08 -04003191 * GPIO control functions for the Recon3D integrated.
3192 */
3193
3194enum r3di_gpio_bit {
3195 /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
3196 R3DI_MIC_SELECT_BIT = 1,
3197 /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
3198 R3DI_OUT_SELECT_BIT = 2,
3199 /*
3200 * I dunno what this actually does, but it stays on until the dsp
3201 * is downloaded.
3202 */
3203 R3DI_GPIO_DSP_DOWNLOADING = 3,
3204 /*
3205 * Same as above, no clue what it does, but it comes on after the dsp
3206 * is downloaded.
3207 */
3208 R3DI_GPIO_DSP_DOWNLOADED = 4
3209};
3210
3211enum r3di_mic_select {
3212 /* Set GPIO bit 1 to 0 for rear mic */
3213 R3DI_REAR_MIC = 0,
3214 /* Set GPIO bit 1 to 1 for front microphone*/
3215 R3DI_FRONT_MIC = 1
3216};
3217
3218enum r3di_out_select {
3219 /* Set GPIO bit 2 to 0 for headphone */
3220 R3DI_HEADPHONE_OUT = 0,
3221 /* Set GPIO bit 2 to 1 for speaker */
3222 R3DI_LINE_OUT = 1
3223};
3224enum r3di_dsp_status {
3225 /* Set GPIO bit 3 to 1 until DSP is downloaded */
3226 R3DI_DSP_DOWNLOADING = 0,
3227 /* Set GPIO bit 4 to 1 once DSP is downloaded */
3228 R3DI_DSP_DOWNLOADED = 1
3229};
3230
Connor McAdams7cb9d942018-05-08 13:20:10 -04003231
3232static void r3di_gpio_mic_set(struct hda_codec *codec,
3233 enum r3di_mic_select cur_mic)
3234{
3235 unsigned int cur_gpio;
3236
3237 /* Get the current GPIO Data setup */
3238 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3239
3240 switch (cur_mic) {
3241 case R3DI_REAR_MIC:
3242 cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
3243 break;
3244 case R3DI_FRONT_MIC:
3245 cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
3246 break;
3247 }
3248 snd_hda_codec_write(codec, codec->core.afg, 0,
3249 AC_VERB_SET_GPIO_DATA, cur_gpio);
3250}
3251
3252static void r3di_gpio_out_set(struct hda_codec *codec,
3253 enum r3di_out_select cur_out)
3254{
3255 unsigned int cur_gpio;
3256
3257 /* Get the current GPIO Data setup */
3258 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3259
3260 switch (cur_out) {
3261 case R3DI_HEADPHONE_OUT:
3262 cur_gpio &= ~(1 << R3DI_OUT_SELECT_BIT);
3263 break;
3264 case R3DI_LINE_OUT:
3265 cur_gpio |= (1 << R3DI_OUT_SELECT_BIT);
3266 break;
3267 }
3268 snd_hda_codec_write(codec, codec->core.afg, 0,
3269 AC_VERB_SET_GPIO_DATA, cur_gpio);
3270}
3271
Connor McAdams7e6ed622018-05-08 13:20:08 -04003272static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
3273 enum r3di_dsp_status dsp_status)
3274{
3275 unsigned int cur_gpio;
3276
3277 /* Get the current GPIO Data setup */
3278 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3279
3280 switch (dsp_status) {
3281 case R3DI_DSP_DOWNLOADING:
3282 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
3283 snd_hda_codec_write(codec, codec->core.afg, 0,
3284 AC_VERB_SET_GPIO_DATA, cur_gpio);
3285 break;
3286 case R3DI_DSP_DOWNLOADED:
3287 /* Set DOWNLOADING bit to 0. */
3288 cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
3289
3290 snd_hda_codec_write(codec, codec->core.afg, 0,
3291 AC_VERB_SET_GPIO_DATA, cur_gpio);
3292
3293 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
3294 break;
3295 }
3296
3297 snd_hda_codec_write(codec, codec->core.afg, 0,
3298 AC_VERB_SET_GPIO_DATA, cur_gpio);
3299}
3300
3301/*
Ian Minett825315b2012-12-20 18:53:36 -08003302 * PCM callbacks
3303 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07003304static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3305 struct hda_codec *codec,
3306 unsigned int stream_tag,
3307 unsigned int format,
3308 struct snd_pcm_substream *substream)
3309{
3310 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08003311
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003312 snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
Ian Minett825315b2012-12-20 18:53:36 -08003313
3314 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003315}
3316
3317static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3318 struct hda_codec *codec,
3319 struct snd_pcm_substream *substream)
3320{
3321 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08003322
3323 if (spec->dsp_state == DSP_DOWNLOADING)
3324 return 0;
3325
3326 /*If Playback effects are on, allow stream some time to flush
3327 *effects tail*/
3328 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3329 msleep(50);
3330
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003331 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
Ian Minett825315b2012-12-20 18:53:36 -08003332
3333 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003334}
3335
Dylan Reide8412ca2013-04-04 13:55:09 -07003336static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
3337 struct hda_codec *codec,
3338 struct snd_pcm_substream *substream)
3339{
3340 struct ca0132_spec *spec = codec->spec;
3341 unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
3342 struct snd_pcm_runtime *runtime = substream->runtime;
3343
3344 if (spec->dsp_state != DSP_DOWNLOADED)
3345 return 0;
3346
3347 /* Add latency if playback enhancement and either effect is enabled. */
3348 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
3349 if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
3350 (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
3351 latency += DSP_PLAY_ENHANCEMENT_LATENCY;
3352 }
3353
3354 /* Applying Speaker EQ adds latency as well. */
3355 if (spec->cur_out_type == SPEAKER_OUT)
3356 latency += DSP_SPEAKER_OUT_LATENCY;
3357
3358 return (latency * runtime->rate) / 1000;
3359}
3360
Ian Minett95c6e9c2011-06-15 15:35:17 -07003361/*
3362 * Digital out
3363 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003364static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3365 struct hda_codec *codec,
3366 struct snd_pcm_substream *substream)
3367{
3368 struct ca0132_spec *spec = codec->spec;
3369 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3370}
3371
Ian Minett95c6e9c2011-06-15 15:35:17 -07003372static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3373 struct hda_codec *codec,
3374 unsigned int stream_tag,
3375 unsigned int format,
3376 struct snd_pcm_substream *substream)
3377{
3378 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003379 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3380 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003381}
3382
3383static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3384 struct hda_codec *codec,
3385 struct snd_pcm_substream *substream)
3386{
3387 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003388 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003389}
3390
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003391static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3392 struct hda_codec *codec,
3393 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07003394{
3395 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003396 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003397}
3398
3399/*
Ian Minett825315b2012-12-20 18:53:36 -08003400 * Analog capture
3401 */
3402static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3403 struct hda_codec *codec,
3404 unsigned int stream_tag,
3405 unsigned int format,
3406 struct snd_pcm_substream *substream)
3407{
Hsin-Yu Chao13c12db2014-02-19 14:30:35 +08003408 snd_hda_codec_setup_stream(codec, hinfo->nid,
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003409 stream_tag, 0, format);
Ian Minett825315b2012-12-20 18:53:36 -08003410
3411 return 0;
3412}
3413
3414static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3415 struct hda_codec *codec,
3416 struct snd_pcm_substream *substream)
3417{
3418 struct ca0132_spec *spec = codec->spec;
3419
3420 if (spec->dsp_state == DSP_DOWNLOADING)
3421 return 0;
3422
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003423 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Ian Minett825315b2012-12-20 18:53:36 -08003424 return 0;
3425}
3426
Dylan Reide8412ca2013-04-04 13:55:09 -07003427static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
3428 struct hda_codec *codec,
3429 struct snd_pcm_substream *substream)
3430{
3431 struct ca0132_spec *spec = codec->spec;
3432 unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
3433 struct snd_pcm_runtime *runtime = substream->runtime;
3434
3435 if (spec->dsp_state != DSP_DOWNLOADED)
3436 return 0;
3437
3438 if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3439 latency += DSP_CRYSTAL_VOICE_LATENCY;
3440
3441 return (latency * runtime->rate) / 1000;
3442}
3443
Ian Minette90f29e2012-12-20 18:53:39 -08003444/*
3445 * Controls stuffs.
3446 */
3447
3448/*
3449 * Mixer controls helpers.
3450 */
3451#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
3452 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3453 .name = xname, \
3454 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3455 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3456 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
3457 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
3458 .info = ca0132_volume_info, \
3459 .get = ca0132_volume_get, \
3460 .put = ca0132_volume_put, \
3461 .tlv = { .c = ca0132_volume_tlv }, \
3462 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3463
Connor McAdams017310f2018-05-08 13:20:11 -04003464/*
3465 * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
3466 * volume put, which is used for setting the DSP volume. This was done because
3467 * the ca0132 functions were taking too much time and causing lag.
3468 */
3469#define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
3470 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3471 .name = xname, \
3472 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3473 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3474 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
3475 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
3476 .info = snd_hda_mixer_amp_volume_info, \
3477 .get = snd_hda_mixer_amp_volume_get, \
3478 .put = ca0132_alt_volume_put, \
3479 .tlv = { .c = snd_hda_mixer_amp_tlv }, \
3480 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3481
Ian Minette90f29e2012-12-20 18:53:39 -08003482#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
3483 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3484 .name = xname, \
3485 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3486 .info = snd_hda_mixer_amp_switch_info, \
3487 .get = ca0132_switch_get, \
3488 .put = ca0132_switch_put, \
3489 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3490
3491/* stereo */
3492#define CA0132_CODEC_VOL(xname, nid, dir) \
3493 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
Connor McAdams017310f2018-05-08 13:20:11 -04003494#define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
3495 CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
Ian Minette90f29e2012-12-20 18:53:39 -08003496#define CA0132_CODEC_MUTE(xname, nid, dir) \
3497 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
3498
Connor McAdams017310f2018-05-08 13:20:11 -04003499/* lookup tables */
3500/*
3501 * Lookup table with decibel values for the DSP. When volume is changed in
3502 * Windows, the DSP is also sent the dB value in floating point. In Windows,
3503 * these values have decimal points, probably because the Windows driver
3504 * actually uses floating point. We can't here, so I made a lookup table of
3505 * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
3506 * DAC's, and 9 is the maximum.
3507 */
3508static const unsigned int float_vol_db_lookup[] = {
35090xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
35100xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
35110xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
35120xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
35130xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
35140xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
35150xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
35160xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
35170xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
35180xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
35190xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
35200xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
35210xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
35220xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
35230xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
35240x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
35250x40C00000, 0x40E00000, 0x41000000, 0x41100000
3526};
3527
Connor McAdams47cdf762018-05-08 13:20:13 -04003528/*
3529 * This table counts from float 0 to 1 in increments of .01, which is
3530 * useful for a few different sliders.
3531 */
3532static const unsigned int float_zero_to_one_lookup[] = {
35330x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
35340x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
35350x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
35360x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
35370x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
35380x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
35390x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
35400x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
35410x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
35420x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
35430x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
35440x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
35450x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
35460x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
35470x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
35480x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
35490x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
3550};
3551
3552/*
3553 * This table counts from float 10 to 1000, which is the range of the x-bass
3554 * crossover slider in Windows.
3555 */
3556static const unsigned int float_xbass_xover_lookup[] = {
35570x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
35580x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
35590x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
35600x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
35610x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
35620x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
35630x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
35640x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
35650x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
35660x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
35670x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
35680x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
35690x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
35700x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
35710x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
35720x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
35730x44728000, 0x44750000, 0x44778000, 0x447A0000
3574};
3575
Masahiro Yamada4091fb92017-02-27 14:29:56 -08003576/* The following are for tuning of products */
Ian Minett44f0c972012-12-20 18:53:38 -08003577#ifdef ENABLE_TUNING_CONTROLS
3578
3579static unsigned int voice_focus_vals_lookup[] = {
35800x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
35810x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
35820x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
35830x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
35840x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
35850x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
35860x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
35870x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
35880x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
35890x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
35900x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
35910x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
35920x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
35930x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
35940x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
35950x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
35960x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
35970x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
35980x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
35990x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
36000x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
36010x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
36020x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
36030x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
36040x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
36050x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
36060x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
3607};
3608
3609static unsigned int mic_svm_vals_lookup[] = {
36100x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
36110x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
36120x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
36130x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
36140x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
36150x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
36160x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
36170x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
36180x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
36190x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
36200x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
36210x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
36220x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
36230x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
36240x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
36250x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
36260x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
3627};
3628
3629static unsigned int equalizer_vals_lookup[] = {
36300xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
36310xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
36320xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
36330xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
36340x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
36350x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
36360x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
36370x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
36380x41C00000
3639};
3640
3641static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
3642 unsigned int *lookup, int idx)
3643{
3644 int i = 0;
3645
3646 for (i = 0; i < TUNING_CTLS_COUNT; i++)
3647 if (nid == ca0132_tuning_ctls[i].nid)
3648 break;
3649
3650 snd_hda_power_up(codec);
Connor McAdams447fd8e2018-05-08 13:20:09 -04003651 dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
Ian Minett44f0c972012-12-20 18:53:38 -08003652 ca0132_tuning_ctls[i].req,
3653 &(lookup[idx]), sizeof(unsigned int));
3654 snd_hda_power_down(codec);
3655
3656 return 1;
3657}
3658
3659static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
3660 struct snd_ctl_elem_value *ucontrol)
3661{
3662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3663 struct ca0132_spec *spec = codec->spec;
3664 hda_nid_t nid = get_amp_nid(kcontrol);
3665 long *valp = ucontrol->value.integer.value;
3666 int idx = nid - TUNING_CTL_START_NID;
3667
3668 *valp = spec->cur_ctl_vals[idx];
3669 return 0;
3670}
3671
3672static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
3673 struct snd_ctl_elem_info *uinfo)
3674{
3675 int chs = get_amp_channels(kcontrol);
3676 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3677 uinfo->count = chs == 3 ? 2 : 1;
3678 uinfo->value.integer.min = 20;
3679 uinfo->value.integer.max = 180;
3680 uinfo->value.integer.step = 1;
3681
3682 return 0;
3683}
3684
3685static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
3686 struct snd_ctl_elem_value *ucontrol)
3687{
3688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3689 struct ca0132_spec *spec = codec->spec;
3690 hda_nid_t nid = get_amp_nid(kcontrol);
3691 long *valp = ucontrol->value.integer.value;
3692 int idx;
3693
3694 idx = nid - TUNING_CTL_START_NID;
3695 /* any change? */
3696 if (spec->cur_ctl_vals[idx] == *valp)
3697 return 0;
3698
3699 spec->cur_ctl_vals[idx] = *valp;
3700
3701 idx = *valp - 20;
3702 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
3703
3704 return 1;
3705}
3706
3707static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
3708 struct snd_ctl_elem_info *uinfo)
3709{
3710 int chs = get_amp_channels(kcontrol);
3711 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3712 uinfo->count = chs == 3 ? 2 : 1;
3713 uinfo->value.integer.min = 0;
3714 uinfo->value.integer.max = 100;
3715 uinfo->value.integer.step = 1;
3716
3717 return 0;
3718}
3719
3720static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
3721 struct snd_ctl_elem_value *ucontrol)
3722{
3723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3724 struct ca0132_spec *spec = codec->spec;
3725 hda_nid_t nid = get_amp_nid(kcontrol);
3726 long *valp = ucontrol->value.integer.value;
3727 int idx;
3728
3729 idx = nid - TUNING_CTL_START_NID;
3730 /* any change? */
3731 if (spec->cur_ctl_vals[idx] == *valp)
3732 return 0;
3733
3734 spec->cur_ctl_vals[idx] = *valp;
3735
3736 idx = *valp;
3737 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
3738
3739 return 0;
3740}
3741
3742static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
3743 struct snd_ctl_elem_info *uinfo)
3744{
3745 int chs = get_amp_channels(kcontrol);
3746 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3747 uinfo->count = chs == 3 ? 2 : 1;
3748 uinfo->value.integer.min = 0;
3749 uinfo->value.integer.max = 48;
3750 uinfo->value.integer.step = 1;
3751
3752 return 0;
3753}
3754
3755static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3756 struct snd_ctl_elem_value *ucontrol)
3757{
3758 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3759 struct ca0132_spec *spec = codec->spec;
3760 hda_nid_t nid = get_amp_nid(kcontrol);
3761 long *valp = ucontrol->value.integer.value;
3762 int idx;
3763
3764 idx = nid - TUNING_CTL_START_NID;
3765 /* any change? */
3766 if (spec->cur_ctl_vals[idx] == *valp)
3767 return 0;
3768
3769 spec->cur_ctl_vals[idx] = *valp;
3770
3771 idx = *valp;
3772 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3773
3774 return 1;
3775}
3776
Takashi Sakamoto8e142e92018-05-02 22:48:16 +09003777static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3778static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
Ian Minett44f0c972012-12-20 18:53:38 -08003779
3780static int add_tuning_control(struct hda_codec *codec,
3781 hda_nid_t pnid, hda_nid_t nid,
3782 const char *name, int dir)
3783{
Takashi Iwai975cc022013-06-28 11:56:49 +02003784 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minett44f0c972012-12-20 18:53:38 -08003785 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3786 struct snd_kcontrol_new knew =
3787 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3788
3789 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3790 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3791 knew.tlv.c = 0;
3792 knew.tlv.p = 0;
3793 switch (pnid) {
3794 case VOICE_FOCUS:
3795 knew.info = voice_focus_ctl_info;
3796 knew.get = tuning_ctl_get;
3797 knew.put = voice_focus_ctl_put;
3798 knew.tlv.p = voice_focus_db_scale;
3799 break;
3800 case MIC_SVM:
3801 knew.info = mic_svm_ctl_info;
3802 knew.get = tuning_ctl_get;
3803 knew.put = mic_svm_ctl_put;
3804 break;
3805 case EQUALIZER:
3806 knew.info = equalizer_ctl_info;
3807 knew.get = tuning_ctl_get;
3808 knew.put = equalizer_ctl_put;
3809 knew.tlv.p = eq_db_scale;
3810 break;
3811 default:
3812 return 0;
3813 }
3814 knew.private_value =
3815 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3816 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3817 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3818}
3819
3820static int add_tuning_ctls(struct hda_codec *codec)
3821{
3822 int i;
3823 int err;
3824
3825 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3826 err = add_tuning_control(codec,
3827 ca0132_tuning_ctls[i].parent_nid,
3828 ca0132_tuning_ctls[i].nid,
3829 ca0132_tuning_ctls[i].name,
3830 ca0132_tuning_ctls[i].direct);
3831 if (err < 0)
3832 return err;
3833 }
3834
3835 return 0;
3836}
3837
3838static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3839{
3840 struct ca0132_spec *spec = codec->spec;
3841 int i;
3842
3843 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3844 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3845 /* SVM level defaults to 0.74. */
3846 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3847
3848 /* EQ defaults to 0dB. */
3849 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3850 spec->cur_ctl_vals[i] = 24;
3851}
3852#endif /*ENABLE_TUNING_CONTROLS*/
3853
Ian Minett825315b2012-12-20 18:53:36 -08003854/*
Ian Minett5aaca442012-12-20 18:53:34 -08003855 * Select the active output.
3856 * If autodetect is enabled, output will be selected based on jack detection.
3857 * If jack inserted, headphone will be selected, else built-in speakers
3858 * If autodetect is disabled, output will be selected based on selection.
3859 */
3860static int ca0132_select_out(struct hda_codec *codec)
3861{
3862 struct ca0132_spec *spec = codec->spec;
3863 unsigned int pin_ctl;
3864 int jack_present;
3865 int auto_jack;
3866 unsigned int tmp;
3867 int err;
3868
Takashi Iwai4e76a882014-02-25 12:21:03 +01003869 codec_dbg(codec, "ca0132_select_out\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003870
Takashi Iwai664c7152015-04-08 11:43:14 +02003871 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08003872
3873 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3874
3875 if (auto_jack)
Takashi Iwaife14f392015-08-10 16:53:32 +02003876 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
Ian Minett5aaca442012-12-20 18:53:34 -08003877 else
3878 jack_present =
3879 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3880
3881 if (jack_present)
3882 spec->cur_out_type = HEADPHONE_OUT;
3883 else
3884 spec->cur_out_type = SPEAKER_OUT;
3885
3886 if (spec->cur_out_type == SPEAKER_OUT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003887 codec_dbg(codec, "ca0132_select_out speaker\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003888 /*speaker out config*/
3889 tmp = FLOAT_ONE;
3890 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3891 if (err < 0)
3892 goto exit;
3893 /*enable speaker EQ*/
3894 tmp = FLOAT_ONE;
3895 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3896 if (err < 0)
3897 goto exit;
3898
3899 /* Setup EAPD */
3900 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3901 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3902 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3903 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3904 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3905 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3906 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3907 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3908
3909 /* disable headphone node */
3910 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3911 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003912 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3913 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003914 /* enable speaker node */
3915 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
Connor McAdams8a19bce2018-05-08 13:20:01 -04003916 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003917 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3918 pin_ctl | PIN_OUT);
Ian Minett5aaca442012-12-20 18:53:34 -08003919 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003920 codec_dbg(codec, "ca0132_select_out hp\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003921 /*headphone out config*/
3922 tmp = FLOAT_ZERO;
3923 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3924 if (err < 0)
3925 goto exit;
3926 /*disable speaker EQ*/
3927 tmp = FLOAT_ZERO;
3928 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3929 if (err < 0)
3930 goto exit;
3931
3932 /* Setup EAPD */
3933 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3934 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3935 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3936 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3937 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3938 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3939 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3940 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3941
3942 /* disable speaker*/
3943 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3944 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003945 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3946 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003947 /* enable headphone*/
3948 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3949 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003950 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3951 pin_ctl | PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003952 }
3953
3954exit:
Takashi Iwai664c7152015-04-08 11:43:14 +02003955 snd_hda_power_down_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08003956
3957 return err < 0 ? err : 0;
3958}
3959
Connor McAdams7cb9d942018-05-08 13:20:10 -04003960/*
3961 * This function behaves similarly to the ca0132_select_out funciton above,
3962 * except with a few differences. It adds the ability to select the current
3963 * output with an enumerated control "output source" if the auto detect
3964 * mute switch is set to off. If the auto detect mute switch is enabled, it
3965 * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
3966 * It also adds the ability to auto-detect the front headphone port. The only
3967 * way to select surround is to disable auto detect, and set Surround with the
3968 * enumerated control.
3969 */
3970static int ca0132_alt_select_out(struct hda_codec *codec)
3971{
3972 struct ca0132_spec *spec = codec->spec;
3973 unsigned int pin_ctl;
3974 int jack_present;
3975 int auto_jack;
3976 unsigned int i;
3977 unsigned int tmp;
3978 int err;
3979 /* Default Headphone is rear headphone */
3980 hda_nid_t headphone_nid = spec->out_pins[1];
3981
3982 codec_dbg(codec, "%s\n", __func__);
3983
3984 snd_hda_power_up_pm(codec);
3985
3986 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3987
3988 /*
3989 * If headphone rear or front is plugged in, set to headphone.
3990 * If neither is plugged in, set to rear line out. Only if
3991 * hp/speaker auto detect is enabled.
3992 */
3993 if (auto_jack) {
3994 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
3995 snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
3996
3997 if (jack_present)
3998 spec->cur_out_type = HEADPHONE_OUT;
3999 else
4000 spec->cur_out_type = SPEAKER_OUT;
4001 } else
4002 spec->cur_out_type = spec->out_enum_val;
4003
4004 /* Begin DSP output switch */
4005 tmp = FLOAT_ONE;
4006 err = dspio_set_uint_param(codec, 0x96, 0x3A, tmp);
4007 if (err < 0)
4008 goto exit;
4009
4010 switch (spec->cur_out_type) {
4011 case SPEAKER_OUT:
4012 codec_dbg(codec, "%s speaker\n", __func__);
4013 /*speaker out config*/
4014 switch (spec->quirk) {
4015 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004016 ca0132_mmio_gpio_set(codec, 7, false);
4017 ca0132_mmio_gpio_set(codec, 4, true);
4018 ca0132_mmio_gpio_set(codec, 1, true);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004019 chipio_set_control_param(codec, 0x0D, 0x18);
4020 break;
4021 case QUIRK_R3DI:
4022 chipio_set_control_param(codec, 0x0D, 0x24);
4023 r3di_gpio_out_set(codec, R3DI_LINE_OUT);
4024 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004025 case QUIRK_R3D:
4026 chipio_set_control_param(codec, 0x0D, 0x24);
4027 ca0132_mmio_gpio_set(codec, 1, true);
4028 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004029 }
4030
4031 /* disable headphone node */
4032 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4033 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4034 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4035 pin_ctl & ~PIN_HP);
4036 /* enable line-out node */
4037 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4038 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4039 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4040 pin_ctl | PIN_OUT);
4041 /* Enable EAPD */
4042 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4043 AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4044
4045 /* If PlayEnhancement is enabled, set different source */
4046 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4047 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4048 else
4049 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
4050 break;
4051 case HEADPHONE_OUT:
4052 codec_dbg(codec, "%s hp\n", __func__);
4053 /* Headphone out config*/
4054 switch (spec->quirk) {
4055 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004056 ca0132_mmio_gpio_set(codec, 7, true);
4057 ca0132_mmio_gpio_set(codec, 4, true);
4058 ca0132_mmio_gpio_set(codec, 1, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004059 chipio_set_control_param(codec, 0x0D, 0x12);
4060 break;
4061 case QUIRK_R3DI:
4062 chipio_set_control_param(codec, 0x0D, 0x21);
4063 r3di_gpio_out_set(codec, R3DI_HEADPHONE_OUT);
4064 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004065 case QUIRK_R3D:
4066 chipio_set_control_param(codec, 0x0D, 0x21);
4067 ca0132_mmio_gpio_set(codec, 0x1, false);
4068 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004069 }
4070
4071 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4072 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4073
4074 /* disable speaker*/
4075 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4076 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4077 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4078 pin_ctl & ~PIN_HP);
4079
4080 /* enable headphone, either front or rear */
4081
4082 if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
4083 headphone_nid = spec->out_pins[2];
4084 else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
4085 headphone_nid = spec->out_pins[1];
4086
4087 pin_ctl = snd_hda_codec_read(codec, headphone_nid, 0,
4088 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4089 snd_hda_set_pin_ctl(codec, headphone_nid,
4090 pin_ctl | PIN_HP);
4091
4092 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4093 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4094 else
4095 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
4096 break;
4097 case SURROUND_OUT:
4098 codec_dbg(codec, "%s surround\n", __func__);
4099 /* Surround out config*/
4100 switch (spec->quirk) {
4101 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004102 ca0132_mmio_gpio_set(codec, 7, false);
4103 ca0132_mmio_gpio_set(codec, 4, true);
4104 ca0132_mmio_gpio_set(codec, 1, true);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004105 chipio_set_control_param(codec, 0x0D, 0x18);
4106 break;
4107 case QUIRK_R3DI:
4108 chipio_set_control_param(codec, 0x0D, 0x24);
4109 r3di_gpio_out_set(codec, R3DI_LINE_OUT);
4110 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004111 case QUIRK_R3D:
4112 ca0132_mmio_gpio_set(codec, 1, true);
4113 chipio_set_control_param(codec, 0x0D, 0x24);
4114 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004115 }
4116 /* enable line out node */
4117 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4118 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4119 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4120 pin_ctl | PIN_OUT);
4121 /* Disable headphone out */
4122 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4123 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4124 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4125 pin_ctl & ~PIN_HP);
4126 /* Enable EAPD on line out */
4127 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4128 AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4129 /* enable center/lfe out node */
4130 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[2], 0,
4131 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4132 snd_hda_set_pin_ctl(codec, spec->out_pins[2],
4133 pin_ctl | PIN_OUT);
4134 /* Now set rear surround node as out. */
4135 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[3], 0,
4136 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4137 snd_hda_set_pin_ctl(codec, spec->out_pins[3],
4138 pin_ctl | PIN_OUT);
4139
4140 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4141 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4142 else
4143 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
4144 break;
4145 }
4146
4147 /* run through the output dsp commands for line-out */
4148 for (i = 0; i < alt_out_presets[spec->cur_out_type].commands; i++) {
4149 err = dspio_set_uint_param(codec,
4150 alt_out_presets[spec->cur_out_type].mids[i],
4151 alt_out_presets[spec->cur_out_type].reqs[i],
4152 alt_out_presets[spec->cur_out_type].vals[i]);
4153
4154 if (err < 0)
4155 goto exit;
4156 }
4157
4158exit:
4159 snd_hda_power_down_pm(codec);
4160
4161 return err < 0 ? err : 0;
4162}
4163
Chih-Chung Chang993884f2013-03-25 10:39:23 -07004164static void ca0132_unsol_hp_delayed(struct work_struct *work)
4165{
4166 struct ca0132_spec *spec = container_of(
4167 to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02004168 struct hda_jack_tbl *jack;
4169
Connor McAdams7cb9d942018-05-08 13:20:10 -04004170 if (spec->use_alt_functions)
4171 ca0132_alt_select_out(spec->codec);
4172 else
4173 ca0132_select_out(spec->codec);
4174
Gabriele Martinod5c016b2015-05-18 21:15:13 +02004175 jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02004176 if (jack) {
4177 jack->block_report = 0;
4178 snd_hda_jack_report_sync(spec->codec);
4179 }
Chih-Chung Chang993884f2013-03-25 10:39:23 -07004180}
4181
Ian Minett5aaca442012-12-20 18:53:34 -08004182static void ca0132_set_dmic(struct hda_codec *codec, int enable);
4183static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
4184static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
Connor McAdamse0026d02018-05-08 13:20:12 -04004185static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
4186static int stop_mic1(struct hda_codec *codec);
4187static int ca0132_cvoice_switch_set(struct hda_codec *codec);
Connor McAdams47cdf762018-05-08 13:20:13 -04004188static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
Ian Minett5aaca442012-12-20 18:53:34 -08004189
4190/*
4191 * Select the active VIP source
4192 */
4193static int ca0132_set_vipsource(struct hda_codec *codec, int val)
4194{
4195 struct ca0132_spec *spec = codec->spec;
4196 unsigned int tmp;
4197
Dylan Reide8f1bd52013-03-14 17:27:45 -07004198 if (spec->dsp_state != DSP_DOWNLOADED)
Ian Minett5aaca442012-12-20 18:53:34 -08004199 return 0;
4200
4201 /* if CrystalVoice if off, vipsource should be 0 */
4202 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4203 (val == 0)) {
4204 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4205 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4206 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4207 if (spec->cur_mic_type == DIGITAL_MIC)
4208 tmp = FLOAT_TWO;
4209 else
4210 tmp = FLOAT_ONE;
4211 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4212 tmp = FLOAT_ZERO;
4213 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4214 } else {
4215 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4216 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4217 if (spec->cur_mic_type == DIGITAL_MIC)
4218 tmp = FLOAT_TWO;
4219 else
4220 tmp = FLOAT_ONE;
4221 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4222 tmp = FLOAT_ONE;
4223 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4224 msleep(20);
4225 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4226 }
4227
4228 return 1;
4229}
4230
Connor McAdamse0026d02018-05-08 13:20:12 -04004231static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
4232{
4233 struct ca0132_spec *spec = codec->spec;
4234 unsigned int tmp;
4235
4236 if (spec->dsp_state != DSP_DOWNLOADED)
4237 return 0;
4238
4239 codec_dbg(codec, "%s\n", __func__);
4240
4241 chipio_set_stream_control(codec, 0x03, 0);
4242 chipio_set_stream_control(codec, 0x04, 0);
4243
4244 /* if CrystalVoice is off, vipsource should be 0 */
4245 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4246 (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
4247 codec_dbg(codec, "%s: off.", __func__);
4248 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4249
4250 tmp = FLOAT_ZERO;
4251 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4252
4253 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4254 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4255 if (spec->quirk == QUIRK_R3DI)
4256 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4257
4258
4259 if (spec->in_enum_val == REAR_LINE_IN)
4260 tmp = FLOAT_ZERO;
4261 else {
4262 if (spec->quirk == QUIRK_SBZ)
4263 tmp = FLOAT_THREE;
4264 else
4265 tmp = FLOAT_ONE;
4266 }
4267
4268 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4269
4270 } else {
4271 codec_dbg(codec, "%s: on.", __func__);
4272 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4273 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4274 if (spec->quirk == QUIRK_R3DI)
4275 chipio_set_conn_rate(codec, 0x0F, SR_16_000);
4276
4277 if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
4278 tmp = FLOAT_TWO;
4279 else
4280 tmp = FLOAT_ONE;
4281 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4282
4283 tmp = FLOAT_ONE;
4284 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4285
4286 msleep(20);
4287 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4288 }
4289
4290 chipio_set_stream_control(codec, 0x03, 1);
4291 chipio_set_stream_control(codec, 0x04, 1);
4292
4293 return 1;
4294}
4295
Ian Minett5aaca442012-12-20 18:53:34 -08004296/*
4297 * Select the active microphone.
4298 * If autodetect is enabled, mic will be selected based on jack detection.
4299 * If jack inserted, ext.mic will be selected, else built-in mic
4300 * If autodetect is disabled, mic will be selected based on selection.
4301 */
4302static int ca0132_select_mic(struct hda_codec *codec)
4303{
4304 struct ca0132_spec *spec = codec->spec;
4305 int jack_present;
4306 int auto_jack;
4307
Takashi Iwai4e76a882014-02-25 12:21:03 +01004308 codec_dbg(codec, "ca0132_select_mic\n");
Ian Minett5aaca442012-12-20 18:53:34 -08004309
Takashi Iwai664c7152015-04-08 11:43:14 +02004310 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004311
4312 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
4313
4314 if (auto_jack)
Takashi Iwaife14f392015-08-10 16:53:32 +02004315 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
Ian Minett5aaca442012-12-20 18:53:34 -08004316 else
4317 jack_present =
4318 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
4319
4320 if (jack_present)
4321 spec->cur_mic_type = LINE_MIC_IN;
4322 else
4323 spec->cur_mic_type = DIGITAL_MIC;
4324
4325 if (spec->cur_mic_type == DIGITAL_MIC) {
4326 /* enable digital Mic */
4327 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
4328 ca0132_set_dmic(codec, 1);
4329 ca0132_mic_boost_set(codec, 0);
4330 /* set voice focus */
4331 ca0132_effects_set(codec, VOICE_FOCUS,
4332 spec->effects_switch
4333 [VOICE_FOCUS - EFFECT_START_NID]);
4334 } else {
4335 /* disable digital Mic */
4336 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
4337 ca0132_set_dmic(codec, 0);
4338 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
4339 /* disable voice focus */
4340 ca0132_effects_set(codec, VOICE_FOCUS, 0);
4341 }
4342
Takashi Iwai664c7152015-04-08 11:43:14 +02004343 snd_hda_power_down_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004344
4345 return 0;
4346}
4347
4348/*
Connor McAdams7cb9d942018-05-08 13:20:10 -04004349 * Select the active input.
4350 * Mic detection isn't used, because it's kind of pointless on the SBZ.
4351 * The front mic has no jack-detection, so the only way to switch to it
4352 * is to do it manually in alsamixer.
4353 */
4354static int ca0132_alt_select_in(struct hda_codec *codec)
4355{
4356 struct ca0132_spec *spec = codec->spec;
4357 unsigned int tmp;
4358
4359 codec_dbg(codec, "%s\n", __func__);
4360
4361 snd_hda_power_up_pm(codec);
4362
4363 chipio_set_stream_control(codec, 0x03, 0);
4364 chipio_set_stream_control(codec, 0x04, 0);
4365
4366 spec->cur_mic_type = spec->in_enum_val;
4367
4368 switch (spec->cur_mic_type) {
4369 case REAR_MIC:
4370 switch (spec->quirk) {
4371 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004372 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004373 ca0132_mmio_gpio_set(codec, 0, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004374 tmp = FLOAT_THREE;
4375 break;
4376 case QUIRK_R3DI:
4377 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
4378 tmp = FLOAT_ONE;
4379 break;
4380 default:
4381 tmp = FLOAT_ONE;
4382 break;
4383 }
4384
4385 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4386 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4387 if (spec->quirk == QUIRK_R3DI)
4388 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4389
4390 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4391
4392 chipio_set_stream_control(codec, 0x03, 1);
4393 chipio_set_stream_control(codec, 0x04, 1);
4394
4395 if (spec->quirk == QUIRK_SBZ) {
4396 chipio_write(codec, 0x18B098, 0x0000000C);
4397 chipio_write(codec, 0x18B09C, 0x0000000C);
4398 }
Connor McAdams47cdf762018-05-08 13:20:13 -04004399 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004400 break;
4401 case REAR_LINE_IN:
4402 ca0132_mic_boost_set(codec, 0);
4403 switch (spec->quirk) {
4404 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004405 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004406 ca0132_mmio_gpio_set(codec, 0, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004407 break;
4408 case QUIRK_R3DI:
4409 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
4410 break;
4411 }
4412
4413 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4414 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4415 if (spec->quirk == QUIRK_R3DI)
4416 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4417
4418 tmp = FLOAT_ZERO;
4419 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4420
4421 if (spec->quirk == QUIRK_SBZ) {
4422 chipio_write(codec, 0x18B098, 0x00000000);
4423 chipio_write(codec, 0x18B09C, 0x00000000);
4424 }
4425
4426 chipio_set_stream_control(codec, 0x03, 1);
4427 chipio_set_stream_control(codec, 0x04, 1);
4428 break;
4429 case FRONT_MIC:
4430 switch (spec->quirk) {
4431 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004432 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004433 ca0132_mmio_gpio_set(codec, 0, true);
4434 ca0132_mmio_gpio_set(codec, 5, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004435 tmp = FLOAT_THREE;
4436 break;
4437 case QUIRK_R3DI:
4438 r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
4439 tmp = FLOAT_ONE;
4440 break;
4441 default:
4442 tmp = FLOAT_ONE;
4443 break;
4444 }
4445
4446 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4447 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4448 if (spec->quirk == QUIRK_R3DI)
4449 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4450
4451 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4452
4453 chipio_set_stream_control(codec, 0x03, 1);
4454 chipio_set_stream_control(codec, 0x04, 1);
4455
4456 if (spec->quirk == QUIRK_SBZ) {
4457 chipio_write(codec, 0x18B098, 0x0000000C);
4458 chipio_write(codec, 0x18B09C, 0x000000CC);
4459 }
Connor McAdams47cdf762018-05-08 13:20:13 -04004460 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004461 break;
4462 }
Connor McAdamse0026d02018-05-08 13:20:12 -04004463 ca0132_cvoice_switch_set(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004464
4465 snd_hda_power_down_pm(codec);
4466 return 0;
4467
4468}
4469
4470/*
Ian Minetta7e76272012-12-20 18:53:35 -08004471 * Check if VNODE settings take effect immediately.
4472 */
4473static bool ca0132_is_vnode_effective(struct hda_codec *codec,
4474 hda_nid_t vnid,
4475 hda_nid_t *shared_nid)
4476{
4477 struct ca0132_spec *spec = codec->spec;
4478 hda_nid_t nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004479
4480 switch (vnid) {
4481 case VNID_SPK:
4482 nid = spec->shared_out_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004483 break;
4484 case VNID_MIC:
4485 nid = spec->shared_mic_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004486 break;
4487 default:
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004488 return false;
Ian Minetta7e76272012-12-20 18:53:35 -08004489 }
4490
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004491 if (shared_nid)
Ian Minetta7e76272012-12-20 18:53:35 -08004492 *shared_nid = nid;
4493
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004494 return true;
Ian Minetta7e76272012-12-20 18:53:35 -08004495}
4496
4497/*
4498* The following functions are control change helpers.
4499* They return 0 if no changed. Return 1 if changed.
4500*/
4501static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
4502{
4503 struct ca0132_spec *spec = codec->spec;
4504 unsigned int tmp;
4505
4506 /* based on CrystalVoice state to enable VoiceFX. */
4507 if (enable) {
4508 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
4509 FLOAT_ONE : FLOAT_ZERO;
4510 } else {
4511 tmp = FLOAT_ZERO;
4512 }
4513
4514 dspio_set_uint_param(codec, ca0132_voicefx.mid,
4515 ca0132_voicefx.reqs[0], tmp);
4516
4517 return 1;
4518}
4519
4520/*
Ian Minett5aaca442012-12-20 18:53:34 -08004521 * Set the effects parameters
4522 */
4523static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
4524{
4525 struct ca0132_spec *spec = codec->spec;
Connor McAdams009b8f92018-05-08 13:20:06 -04004526 unsigned int on, tmp;
Ian Minett5aaca442012-12-20 18:53:34 -08004527 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4528 int err = 0;
4529 int idx = nid - EFFECT_START_NID;
4530
4531 if ((idx < 0) || (idx >= num_fx))
4532 return 0; /* no changed */
4533
4534 /* for out effect, qualify with PE */
4535 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
4536 /* if PE if off, turn off out effects. */
4537 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4538 val = 0;
4539 }
4540
4541 /* for in effect, qualify with CrystalVoice */
4542 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
4543 /* if CrystalVoice if off, turn off in effects. */
4544 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
4545 val = 0;
4546
4547 /* Voice Focus applies to 2-ch Mic, Digital Mic */
4548 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
4549 val = 0;
Connor McAdams009b8f92018-05-08 13:20:06 -04004550
4551 /* If Voice Focus on SBZ, set to two channel. */
Connor McAdams7cb9d942018-05-08 13:20:10 -04004552 if ((nid == VOICE_FOCUS) && (spec->quirk == QUIRK_SBZ)
4553 && (spec->cur_mic_type != REAR_LINE_IN)) {
Connor McAdams009b8f92018-05-08 13:20:06 -04004554 if (spec->effects_switch[CRYSTAL_VOICE -
4555 EFFECT_START_NID]) {
4556
4557 if (spec->effects_switch[VOICE_FOCUS -
4558 EFFECT_START_NID]) {
4559 tmp = FLOAT_TWO;
4560 val = 1;
4561 } else
4562 tmp = FLOAT_ONE;
4563
4564 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4565 }
4566 }
4567 /*
4568 * For SBZ noise reduction, there's an extra command
4569 * to module ID 0x47. No clue why.
4570 */
Connor McAdams7cb9d942018-05-08 13:20:10 -04004571 if ((nid == NOISE_REDUCTION) && (spec->quirk == QUIRK_SBZ)
4572 && (spec->cur_mic_type != REAR_LINE_IN)) {
Connor McAdams009b8f92018-05-08 13:20:06 -04004573 if (spec->effects_switch[CRYSTAL_VOICE -
4574 EFFECT_START_NID]) {
4575 if (spec->effects_switch[NOISE_REDUCTION -
4576 EFFECT_START_NID])
4577 tmp = FLOAT_ONE;
4578 else
4579 tmp = FLOAT_ZERO;
4580 } else
4581 tmp = FLOAT_ZERO;
4582
4583 dspio_set_uint_param(codec, 0x47, 0x00, tmp);
4584 }
Connor McAdams7cb9d942018-05-08 13:20:10 -04004585
4586 /* If rear line in disable effects. */
4587 if (spec->use_alt_functions &&
4588 spec->in_enum_val == REAR_LINE_IN)
4589 val = 0;
Ian Minett5aaca442012-12-20 18:53:34 -08004590 }
4591
Takashi Iwai4e76a882014-02-25 12:21:03 +01004592 codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
Ian Minett5aaca442012-12-20 18:53:34 -08004593 nid, val);
4594
4595 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
4596 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
4597 ca0132_effects[idx].reqs[0], on);
4598
4599 if (err < 0)
4600 return 0; /* no changed */
4601
4602 return 1;
4603}
4604
Ian Minetta7e76272012-12-20 18:53:35 -08004605/*
4606 * Turn on/off Playback Enhancements
4607 */
4608static int ca0132_pe_switch_set(struct hda_codec *codec)
4609{
4610 struct ca0132_spec *spec = codec->spec;
4611 hda_nid_t nid;
4612 int i, ret = 0;
4613
Takashi Iwai4e76a882014-02-25 12:21:03 +01004614 codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08004615 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
4616
Connor McAdams7cb9d942018-05-08 13:20:10 -04004617 if (spec->use_alt_functions)
4618 ca0132_alt_select_out(codec);
4619
Ian Minetta7e76272012-12-20 18:53:35 -08004620 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
4621 nid = OUT_EFFECT_START_NID;
4622 /* PE affects all out effects */
4623 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
4624 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
4625
4626 return ret;
4627}
4628
Ian Minett5aaca442012-12-20 18:53:34 -08004629/* Check if Mic1 is streaming, if so, stop streaming */
4630static int stop_mic1(struct hda_codec *codec)
4631{
4632 struct ca0132_spec *spec = codec->spec;
4633 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
4634 AC_VERB_GET_CONV, 0);
4635 if (oldval != 0)
4636 snd_hda_codec_write(codec, spec->adcs[0], 0,
4637 AC_VERB_SET_CHANNEL_STREAMID,
4638 0);
4639 return oldval;
4640}
4641
4642/* Resume Mic1 streaming if it was stopped. */
4643static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
4644{
4645 struct ca0132_spec *spec = codec->spec;
4646 /* Restore the previous stream and channel */
4647 if (oldval != 0)
4648 snd_hda_codec_write(codec, spec->adcs[0], 0,
4649 AC_VERB_SET_CHANNEL_STREAMID,
4650 oldval);
4651}
4652
4653/*
Ian Minetta7e76272012-12-20 18:53:35 -08004654 * Turn on/off CrystalVoice
Ian Minett5aaca442012-12-20 18:53:34 -08004655 */
Ian Minetta7e76272012-12-20 18:53:35 -08004656static int ca0132_cvoice_switch_set(struct hda_codec *codec)
4657{
4658 struct ca0132_spec *spec = codec->spec;
4659 hda_nid_t nid;
4660 int i, ret = 0;
4661 unsigned int oldval;
4662
Takashi Iwai4e76a882014-02-25 12:21:03 +01004663 codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08004664 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
4665
4666 i = IN_EFFECT_START_NID - EFFECT_START_NID;
4667 nid = IN_EFFECT_START_NID;
4668 /* CrystalVoice affects all in effects */
4669 for (; nid < IN_EFFECT_END_NID; nid++, i++)
4670 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
4671
4672 /* including VoiceFX */
4673 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
4674
4675 /* set correct vipsource */
4676 oldval = stop_mic1(codec);
Connor McAdamse0026d02018-05-08 13:20:12 -04004677 if (spec->use_alt_functions)
4678 ret |= ca0132_alt_set_vipsource(codec, 1);
4679 else
4680 ret |= ca0132_set_vipsource(codec, 1);
Ian Minetta7e76272012-12-20 18:53:35 -08004681 resume_mic1(codec, oldval);
4682 return ret;
4683}
4684
Ian Minett5aaca442012-12-20 18:53:34 -08004685static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
4686{
4687 struct ca0132_spec *spec = codec->spec;
4688 int ret = 0;
4689
4690 if (val) /* on */
4691 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4692 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
4693 else /* off */
4694 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4695 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
4696
4697 return ret;
4698}
4699
Connor McAdams47cdf762018-05-08 13:20:13 -04004700static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
4701{
4702 struct ca0132_spec *spec = codec->spec;
4703 int ret = 0;
4704
4705 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4706 HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
4707 return ret;
4708}
4709
Ian Minetta7e76272012-12-20 18:53:35 -08004710static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
4711 struct snd_ctl_elem_value *ucontrol)
4712{
4713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4714 hda_nid_t nid = get_amp_nid(kcontrol);
4715 hda_nid_t shared_nid = 0;
4716 bool effective;
4717 int ret = 0;
4718 struct ca0132_spec *spec = codec->spec;
4719 int auto_jack;
4720
4721 if (nid == VNID_HP_SEL) {
4722 auto_jack =
4723 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
Connor McAdams7cb9d942018-05-08 13:20:10 -04004724 if (!auto_jack) {
4725 if (spec->use_alt_functions)
4726 ca0132_alt_select_out(codec);
4727 else
4728 ca0132_select_out(codec);
4729 }
Ian Minetta7e76272012-12-20 18:53:35 -08004730 return 1;
4731 }
4732
4733 if (nid == VNID_AMIC1_SEL) {
4734 auto_jack =
4735 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
4736 if (!auto_jack)
4737 ca0132_select_mic(codec);
4738 return 1;
4739 }
4740
4741 if (nid == VNID_HP_ASEL) {
Connor McAdams7cb9d942018-05-08 13:20:10 -04004742 if (spec->use_alt_functions)
4743 ca0132_alt_select_out(codec);
4744 else
4745 ca0132_select_out(codec);
Ian Minetta7e76272012-12-20 18:53:35 -08004746 return 1;
4747 }
4748
4749 if (nid == VNID_AMIC1_ASEL) {
4750 ca0132_select_mic(codec);
4751 return 1;
4752 }
4753
4754 /* if effective conditions, then update hw immediately. */
4755 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
4756 if (effective) {
4757 int dir = get_amp_direction(kcontrol);
4758 int ch = get_amp_channels(kcontrol);
4759 unsigned long pval;
4760
4761 mutex_lock(&codec->control_mutex);
4762 pval = kcontrol->private_value;
4763 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
4764 0, dir);
4765 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
4766 kcontrol->private_value = pval;
4767 mutex_unlock(&codec->control_mutex);
4768 }
4769
4770 return ret;
4771}
4772/* End of control change helpers. */
Connor McAdams47cdf762018-05-08 13:20:13 -04004773/*
4774 * Below I've added controls to mess with the effect levels, I've only enabled
4775 * them on the Sound Blaster Z, but they would probably also work on the
4776 * Chromebook. I figured they were probably tuned specifically for it, and left
4777 * out for a reason.
4778 */
4779
4780/* Sets DSP effect level from the sliders above the controls */
4781static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
4782 const unsigned int *lookup, int idx)
4783{
4784 int i = 0;
4785 unsigned int y;
4786 /*
4787 * For X_BASS, req 2 is actually crossover freq instead of
4788 * effect level
4789 */
4790 if (nid == X_BASS)
4791 y = 2;
4792 else
4793 y = 1;
4794
4795 snd_hda_power_up(codec);
4796 if (nid == XBASS_XOVER) {
4797 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
4798 if (ca0132_effects[i].nid == X_BASS)
4799 break;
4800
4801 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
4802 ca0132_effects[i].reqs[1],
4803 &(lookup[idx - 1]), sizeof(unsigned int));
4804 } else {
4805 /* Find the actual effect structure */
4806 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
4807 if (nid == ca0132_effects[i].nid)
4808 break;
4809
4810 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
4811 ca0132_effects[i].reqs[y],
4812 &(lookup[idx]), sizeof(unsigned int));
4813 }
4814
4815 snd_hda_power_down(codec);
4816
4817 return 0;
4818}
4819
4820static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
4821 struct snd_ctl_elem_value *ucontrol)
4822{
4823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4824 struct ca0132_spec *spec = codec->spec;
4825 long *valp = ucontrol->value.integer.value;
4826
4827 *valp = spec->xbass_xover_freq;
4828 return 0;
4829}
4830
4831static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
4832 struct snd_ctl_elem_value *ucontrol)
4833{
4834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4835 struct ca0132_spec *spec = codec->spec;
4836 hda_nid_t nid = get_amp_nid(kcontrol);
4837 long *valp = ucontrol->value.integer.value;
4838 int idx = nid - OUT_EFFECT_START_NID;
4839
4840 *valp = spec->fx_ctl_val[idx];
4841 return 0;
4842}
4843
4844/*
4845 * The X-bass crossover starts at 10hz, so the min is 1. The
4846 * frequency is set in multiples of 10.
4847 */
4848static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
4849 struct snd_ctl_elem_info *uinfo)
4850{
4851 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4852 uinfo->count = 1;
4853 uinfo->value.integer.min = 1;
4854 uinfo->value.integer.max = 100;
4855 uinfo->value.integer.step = 1;
4856
4857 return 0;
4858}
4859
4860static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
4861 struct snd_ctl_elem_info *uinfo)
4862{
4863 int chs = get_amp_channels(kcontrol);
4864
4865 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4866 uinfo->count = chs == 3 ? 2 : 1;
4867 uinfo->value.integer.min = 0;
4868 uinfo->value.integer.max = 100;
4869 uinfo->value.integer.step = 1;
4870
4871 return 0;
4872}
4873
4874static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
4875 struct snd_ctl_elem_value *ucontrol)
4876{
4877 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4878 struct ca0132_spec *spec = codec->spec;
4879 hda_nid_t nid = get_amp_nid(kcontrol);
4880 long *valp = ucontrol->value.integer.value;
4881 int idx;
4882
4883 /* any change? */
4884 if (spec->xbass_xover_freq == *valp)
4885 return 0;
4886
4887 spec->xbass_xover_freq = *valp;
4888
4889 idx = *valp;
4890 ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
4891
4892 return 0;
4893}
4894
4895static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
4896 struct snd_ctl_elem_value *ucontrol)
4897{
4898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4899 struct ca0132_spec *spec = codec->spec;
4900 hda_nid_t nid = get_amp_nid(kcontrol);
4901 long *valp = ucontrol->value.integer.value;
4902 int idx;
4903
4904 idx = nid - EFFECT_START_NID;
4905 /* any change? */
4906 if (spec->fx_ctl_val[idx] == *valp)
4907 return 0;
4908
4909 spec->fx_ctl_val[idx] = *valp;
4910
4911 idx = *valp;
4912 ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
4913
4914 return 0;
4915}
4916
4917
4918/*
4919 * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
4920 * only has off or full 30 dB, and didn't like making a volume slider that has
4921 * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
4922 */
4923#define MIC_BOOST_NUM_OF_STEPS 4
4924#define MIC_BOOST_ENUM_MAX_STRLEN 10
4925
4926static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
4927 struct snd_ctl_elem_info *uinfo)
4928{
4929 char *sfx = "dB";
4930 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
4931
4932 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4933 uinfo->count = 1;
4934 uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
4935 if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
4936 uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
4937 sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
4938 strcpy(uinfo->value.enumerated.name, namestr);
4939 return 0;
4940}
4941
4942static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
4943 struct snd_ctl_elem_value *ucontrol)
4944{
4945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4946 struct ca0132_spec *spec = codec->spec;
4947
4948 ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
4949 return 0;
4950}
4951
4952static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
4953 struct snd_ctl_elem_value *ucontrol)
4954{
4955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4956 struct ca0132_spec *spec = codec->spec;
4957 int sel = ucontrol->value.enumerated.item[0];
4958 unsigned int items = MIC_BOOST_NUM_OF_STEPS;
4959
4960 if (sel >= items)
4961 return 0;
4962
4963 codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
4964 sel);
4965
4966 spec->mic_boost_enum_val = sel;
4967
4968 if (spec->in_enum_val != REAR_LINE_IN)
4969 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
4970
4971 return 1;
4972}
4973
Ian Minetta7e76272012-12-20 18:53:35 -08004974
Connor McAdams7cb9d942018-05-08 13:20:10 -04004975/*
4976 * Input Select Control for alternative ca0132 codecs. This exists because
4977 * front microphone has no auto-detect, and we need a way to set the rear
4978 * as line-in
4979 */
4980static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
4981 struct snd_ctl_elem_info *uinfo)
4982{
4983 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4984 uinfo->count = 1;
4985 uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
4986 if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
4987 uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
4988 strcpy(uinfo->value.enumerated.name,
4989 in_src_str[uinfo->value.enumerated.item]);
4990 return 0;
4991}
4992
4993static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
4994 struct snd_ctl_elem_value *ucontrol)
4995{
4996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4997 struct ca0132_spec *spec = codec->spec;
4998
4999 ucontrol->value.enumerated.item[0] = spec->in_enum_val;
5000 return 0;
5001}
5002
5003static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
5004 struct snd_ctl_elem_value *ucontrol)
5005{
5006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5007 struct ca0132_spec *spec = codec->spec;
5008 int sel = ucontrol->value.enumerated.item[0];
5009 unsigned int items = IN_SRC_NUM_OF_INPUTS;
5010
5011 if (sel >= items)
5012 return 0;
5013
5014 codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
5015 sel, in_src_str[sel]);
5016
5017 spec->in_enum_val = sel;
5018
5019 ca0132_alt_select_in(codec);
5020
5021 return 1;
5022}
5023
5024/* Sound Blaster Z Output Select Control */
5025static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
5026 struct snd_ctl_elem_info *uinfo)
5027{
5028 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5029 uinfo->count = 1;
5030 uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
5031 if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
5032 uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
5033 strcpy(uinfo->value.enumerated.name,
5034 alt_out_presets[uinfo->value.enumerated.item].name);
5035 return 0;
5036}
5037
5038static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
5039 struct snd_ctl_elem_value *ucontrol)
5040{
5041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5042 struct ca0132_spec *spec = codec->spec;
5043
5044 ucontrol->value.enumerated.item[0] = spec->out_enum_val;
5045 return 0;
5046}
5047
5048static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
5049 struct snd_ctl_elem_value *ucontrol)
5050{
5051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5052 struct ca0132_spec *spec = codec->spec;
5053 int sel = ucontrol->value.enumerated.item[0];
5054 unsigned int items = NUM_OF_OUTPUTS;
5055 unsigned int auto_jack;
5056
5057 if (sel >= items)
5058 return 0;
5059
5060 codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
5061 sel, alt_out_presets[sel].name);
5062
5063 spec->out_enum_val = sel;
5064
5065 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
5066
5067 if (!auto_jack)
5068 ca0132_alt_select_out(codec);
5069
5070 return 1;
5071}
5072
Connor McAdams47cdf762018-05-08 13:20:13 -04005073/*
5074 * Smart Volume output setting control. Three different settings, Normal,
5075 * which takes the value from the smart volume slider. The two others, loud
5076 * and night, disregard the slider value and have uneditable values.
5077 */
5078#define NUM_OF_SVM_SETTINGS 3
Takashi Sakamoto3a03f832018-05-15 22:12:58 +09005079static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
Connor McAdams47cdf762018-05-08 13:20:13 -04005080
5081static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
5082 struct snd_ctl_elem_info *uinfo)
5083{
5084 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5085 uinfo->count = 1;
5086 uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
5087 if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
5088 uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
5089 strcpy(uinfo->value.enumerated.name,
5090 out_svm_set_enum_str[uinfo->value.enumerated.item]);
5091 return 0;
5092}
5093
5094static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
5095 struct snd_ctl_elem_value *ucontrol)
5096{
5097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5098 struct ca0132_spec *spec = codec->spec;
5099
5100 ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
5101 return 0;
5102}
5103
5104static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
5105 struct snd_ctl_elem_value *ucontrol)
5106{
5107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5108 struct ca0132_spec *spec = codec->spec;
5109 int sel = ucontrol->value.enumerated.item[0];
5110 unsigned int items = NUM_OF_SVM_SETTINGS;
5111 unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
5112 unsigned int tmp;
5113
5114 if (sel >= items)
5115 return 0;
5116
5117 codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
5118 sel, out_svm_set_enum_str[sel]);
5119
5120 spec->smart_volume_setting = sel;
5121
5122 switch (sel) {
5123 case 0:
5124 tmp = FLOAT_ZERO;
5125 break;
5126 case 1:
5127 tmp = FLOAT_ONE;
5128 break;
5129 case 2:
5130 tmp = FLOAT_TWO;
5131 break;
5132 default:
5133 tmp = FLOAT_ZERO;
5134 break;
5135 }
5136 /* Req 2 is the Smart Volume Setting req. */
5137 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
5138 ca0132_effects[idx].reqs[2], tmp);
5139 return 1;
5140}
5141
5142/* Sound Blaster Z EQ preset controls */
5143static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
5144 struct snd_ctl_elem_info *uinfo)
5145{
Fengguang Wuc5f13d72018-05-15 03:02:14 +08005146 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
Connor McAdams47cdf762018-05-08 13:20:13 -04005147
5148 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5149 uinfo->count = 1;
5150 uinfo->value.enumerated.items = items;
5151 if (uinfo->value.enumerated.item >= items)
5152 uinfo->value.enumerated.item = items - 1;
5153 strcpy(uinfo->value.enumerated.name,
5154 ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
5155 return 0;
5156}
5157
5158static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
5159 struct snd_ctl_elem_value *ucontrol)
5160{
5161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5162 struct ca0132_spec *spec = codec->spec;
5163
5164 ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
5165 return 0;
5166}
5167
5168static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
5169 struct snd_ctl_elem_value *ucontrol)
5170{
5171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5172 struct ca0132_spec *spec = codec->spec;
5173 int i, err = 0;
5174 int sel = ucontrol->value.enumerated.item[0];
Fengguang Wuc5f13d72018-05-15 03:02:14 +08005175 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
Connor McAdams47cdf762018-05-08 13:20:13 -04005176
5177 if (sel >= items)
5178 return 0;
5179
5180 codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
5181 ca0132_alt_eq_presets[sel].name);
5182 /*
5183 * Idx 0 is default.
5184 * Default needs to qualify with CrystalVoice state.
5185 */
5186 for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
5187 err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
5188 ca0132_alt_eq_enum.reqs[i],
5189 ca0132_alt_eq_presets[sel].vals[i]);
5190 if (err < 0)
5191 break;
5192 }
5193
5194 if (err >= 0)
5195 spec->eq_preset_val = sel;
5196
5197 return 1;
5198}
5199
Ian Minetta7e76272012-12-20 18:53:35 -08005200static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
5201 struct snd_ctl_elem_info *uinfo)
5202{
Jérémy Lefaurea9291f42017-10-12 22:36:31 -04005203 unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
Ian Minetta7e76272012-12-20 18:53:35 -08005204
5205 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5206 uinfo->count = 1;
5207 uinfo->value.enumerated.items = items;
5208 if (uinfo->value.enumerated.item >= items)
5209 uinfo->value.enumerated.item = items - 1;
5210 strcpy(uinfo->value.enumerated.name,
5211 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
5212 return 0;
5213}
5214
5215static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
5216 struct snd_ctl_elem_value *ucontrol)
5217{
5218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5219 struct ca0132_spec *spec = codec->spec;
5220
5221 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
5222 return 0;
5223}
5224
5225static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
5226 struct snd_ctl_elem_value *ucontrol)
5227{
5228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5229 struct ca0132_spec *spec = codec->spec;
5230 int i, err = 0;
5231 int sel = ucontrol->value.enumerated.item[0];
Ian Minetta7e76272012-12-20 18:53:35 -08005232
Jérémy Lefaurea9291f42017-10-12 22:36:31 -04005233 if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
Ian Minetta7e76272012-12-20 18:53:35 -08005234 return 0;
5235
Takashi Iwai4e76a882014-02-25 12:21:03 +01005236 codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
Ian Minetta7e76272012-12-20 18:53:35 -08005237 sel, ca0132_voicefx_presets[sel].name);
5238
5239 /*
5240 * Idx 0 is default.
5241 * Default needs to qualify with CrystalVoice state.
5242 */
5243 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
5244 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
5245 ca0132_voicefx.reqs[i],
5246 ca0132_voicefx_presets[sel].vals[i]);
5247 if (err < 0)
5248 break;
5249 }
5250
5251 if (err >= 0) {
5252 spec->voicefx_val = sel;
5253 /* enable voice fx */
5254 ca0132_voicefx_set(codec, (sel ? 1 : 0));
5255 }
5256
5257 return 1;
5258}
5259
5260static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
5261 struct snd_ctl_elem_value *ucontrol)
5262{
5263 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5264 struct ca0132_spec *spec = codec->spec;
5265 hda_nid_t nid = get_amp_nid(kcontrol);
5266 int ch = get_amp_channels(kcontrol);
5267 long *valp = ucontrol->value.integer.value;
5268
5269 /* vnode */
5270 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
5271 if (ch & 1) {
5272 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
5273 valp++;
5274 }
5275 if (ch & 2) {
5276 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
5277 valp++;
5278 }
5279 return 0;
5280 }
5281
5282 /* effects, include PE and CrystalVoice */
5283 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
5284 *valp = spec->effects_switch[nid - EFFECT_START_NID];
5285 return 0;
5286 }
5287
5288 /* mic boost */
5289 if (nid == spec->input_pins[0]) {
5290 *valp = spec->cur_mic_boost;
5291 return 0;
5292 }
5293
5294 return 0;
5295}
5296
5297static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
5298 struct snd_ctl_elem_value *ucontrol)
5299{
5300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5301 struct ca0132_spec *spec = codec->spec;
5302 hda_nid_t nid = get_amp_nid(kcontrol);
5303 int ch = get_amp_channels(kcontrol);
5304 long *valp = ucontrol->value.integer.value;
5305 int changed = 1;
5306
Takashi Iwai4e76a882014-02-25 12:21:03 +01005307 codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08005308 nid, *valp);
5309
5310 snd_hda_power_up(codec);
5311 /* vnode */
5312 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
5313 if (ch & 1) {
5314 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
5315 valp++;
5316 }
5317 if (ch & 2) {
5318 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
5319 valp++;
5320 }
5321 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
5322 goto exit;
5323 }
5324
5325 /* PE */
5326 if (nid == PLAY_ENHANCEMENT) {
5327 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5328 changed = ca0132_pe_switch_set(codec);
5329 goto exit;
5330 }
5331
5332 /* CrystalVoice */
5333 if (nid == CRYSTAL_VOICE) {
5334 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5335 changed = ca0132_cvoice_switch_set(codec);
5336 goto exit;
5337 }
5338
5339 /* out and in effects */
5340 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
5341 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
5342 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5343 changed = ca0132_effects_set(codec, nid, *valp);
5344 goto exit;
5345 }
5346
5347 /* mic boost */
5348 if (nid == spec->input_pins[0]) {
5349 spec->cur_mic_boost = *valp;
Connor McAdams7cb9d942018-05-08 13:20:10 -04005350 if (spec->use_alt_functions) {
5351 if (spec->in_enum_val != REAR_LINE_IN)
5352 changed = ca0132_mic_boost_set(codec, *valp);
5353 } else {
5354 /* Mic boost does not apply to Digital Mic */
5355 if (spec->cur_mic_type != DIGITAL_MIC)
5356 changed = ca0132_mic_boost_set(codec, *valp);
5357 }
Ian Minetta7e76272012-12-20 18:53:35 -08005358
Ian Minetta7e76272012-12-20 18:53:35 -08005359 goto exit;
5360 }
5361
5362exit:
5363 snd_hda_power_down(codec);
5364 return changed;
5365}
5366
5367/*
5368 * Volume related
5369 */
Connor McAdams017310f2018-05-08 13:20:11 -04005370/*
5371 * Sets the internal DSP decibel level to match the DAC for output, and the
5372 * ADC for input. Currently only the SBZ sets dsp capture volume level, and
5373 * all alternative codecs set DSP playback volume.
5374 */
5375static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
5376{
5377 struct ca0132_spec *spec = codec->spec;
5378 unsigned int dsp_dir;
5379 unsigned int lookup_val;
5380
5381 if (nid == VNID_SPK)
5382 dsp_dir = DSP_VOL_OUT;
5383 else
5384 dsp_dir = DSP_VOL_IN;
5385
5386 lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
5387
5388 dspio_set_uint_param(codec,
5389 ca0132_alt_vol_ctls[dsp_dir].mid,
5390 ca0132_alt_vol_ctls[dsp_dir].reqs[0],
5391 float_vol_db_lookup[lookup_val]);
5392
5393 lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
5394
5395 dspio_set_uint_param(codec,
5396 ca0132_alt_vol_ctls[dsp_dir].mid,
5397 ca0132_alt_vol_ctls[dsp_dir].reqs[1],
5398 float_vol_db_lookup[lookup_val]);
5399
5400 dspio_set_uint_param(codec,
5401 ca0132_alt_vol_ctls[dsp_dir].mid,
5402 ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
5403}
5404
Ian Minetta7e76272012-12-20 18:53:35 -08005405static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
5406 struct snd_ctl_elem_info *uinfo)
5407{
5408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5409 struct ca0132_spec *spec = codec->spec;
5410 hda_nid_t nid = get_amp_nid(kcontrol);
5411 int ch = get_amp_channels(kcontrol);
5412 int dir = get_amp_direction(kcontrol);
5413 unsigned long pval;
5414 int err;
5415
5416 switch (nid) {
5417 case VNID_SPK:
5418 /* follow shared_out info */
5419 nid = spec->shared_out_nid;
5420 mutex_lock(&codec->control_mutex);
5421 pval = kcontrol->private_value;
5422 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5423 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5424 kcontrol->private_value = pval;
5425 mutex_unlock(&codec->control_mutex);
5426 break;
5427 case VNID_MIC:
5428 /* follow shared_mic info */
5429 nid = spec->shared_mic_nid;
5430 mutex_lock(&codec->control_mutex);
5431 pval = kcontrol->private_value;
5432 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5433 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5434 kcontrol->private_value = pval;
5435 mutex_unlock(&codec->control_mutex);
5436 break;
5437 default:
5438 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5439 }
5440 return err;
5441}
5442
5443static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
5444 struct snd_ctl_elem_value *ucontrol)
5445{
5446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5447 struct ca0132_spec *spec = codec->spec;
5448 hda_nid_t nid = get_amp_nid(kcontrol);
5449 int ch = get_amp_channels(kcontrol);
5450 long *valp = ucontrol->value.integer.value;
5451
5452 /* store the left and right volume */
5453 if (ch & 1) {
5454 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
5455 valp++;
5456 }
5457 if (ch & 2) {
5458 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
5459 valp++;
5460 }
5461 return 0;
5462}
5463
5464static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
5465 struct snd_ctl_elem_value *ucontrol)
5466{
5467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5468 struct ca0132_spec *spec = codec->spec;
5469 hda_nid_t nid = get_amp_nid(kcontrol);
5470 int ch = get_amp_channels(kcontrol);
5471 long *valp = ucontrol->value.integer.value;
5472 hda_nid_t shared_nid = 0;
5473 bool effective;
5474 int changed = 1;
5475
5476 /* store the left and right volume */
5477 if (ch & 1) {
5478 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
5479 valp++;
5480 }
5481 if (ch & 2) {
5482 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
5483 valp++;
5484 }
5485
5486 /* if effective conditions, then update hw immediately. */
5487 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
5488 if (effective) {
5489 int dir = get_amp_direction(kcontrol);
5490 unsigned long pval;
5491
5492 snd_hda_power_up(codec);
5493 mutex_lock(&codec->control_mutex);
5494 pval = kcontrol->private_value;
5495 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
5496 0, dir);
5497 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
5498 kcontrol->private_value = pval;
5499 mutex_unlock(&codec->control_mutex);
5500 snd_hda_power_down(codec);
5501 }
5502
5503 return changed;
5504}
5505
Connor McAdams017310f2018-05-08 13:20:11 -04005506/*
5507 * This function is the same as the one above, because using an if statement
5508 * inside of the above volume control for the DSP volume would cause too much
5509 * lag. This is a lot more smooth.
5510 */
5511static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
5512 struct snd_ctl_elem_value *ucontrol)
5513{
5514 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5515 struct ca0132_spec *spec = codec->spec;
5516 hda_nid_t nid = get_amp_nid(kcontrol);
5517 int ch = get_amp_channels(kcontrol);
5518 long *valp = ucontrol->value.integer.value;
5519 hda_nid_t vnid = 0;
5520 int changed = 1;
5521
5522 switch (nid) {
5523 case 0x02:
5524 vnid = VNID_SPK;
5525 break;
5526 case 0x07:
5527 vnid = VNID_MIC;
5528 break;
5529 }
5530
5531 /* store the left and right volume */
5532 if (ch & 1) {
5533 spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
5534 valp++;
5535 }
5536 if (ch & 2) {
5537 spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
5538 valp++;
5539 }
5540
5541 snd_hda_power_up(codec);
5542 ca0132_alt_dsp_volume_put(codec, vnid);
5543 mutex_lock(&codec->control_mutex);
5544 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
5545 mutex_unlock(&codec->control_mutex);
5546 snd_hda_power_down(codec);
5547
5548 return changed;
5549}
5550
Ian Minetta7e76272012-12-20 18:53:35 -08005551static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
5552 unsigned int size, unsigned int __user *tlv)
5553{
5554 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5555 struct ca0132_spec *spec = codec->spec;
5556 hda_nid_t nid = get_amp_nid(kcontrol);
5557 int ch = get_amp_channels(kcontrol);
5558 int dir = get_amp_direction(kcontrol);
5559 unsigned long pval;
5560 int err;
5561
5562 switch (nid) {
5563 case VNID_SPK:
5564 /* follow shared_out tlv */
5565 nid = spec->shared_out_nid;
5566 mutex_lock(&codec->control_mutex);
5567 pval = kcontrol->private_value;
5568 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5569 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5570 kcontrol->private_value = pval;
5571 mutex_unlock(&codec->control_mutex);
5572 break;
5573 case VNID_MIC:
5574 /* follow shared_mic tlv */
5575 nid = spec->shared_mic_nid;
5576 mutex_lock(&codec->control_mutex);
5577 pval = kcontrol->private_value;
5578 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5579 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5580 kcontrol->private_value = pval;
5581 mutex_unlock(&codec->control_mutex);
5582 break;
5583 default:
5584 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5585 }
5586 return err;
5587}
5588
Connor McAdams47cdf762018-05-08 13:20:13 -04005589/* Add volume slider control for effect level */
5590static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
5591 const char *pfx, int dir)
5592{
Connor McAdams47cdf762018-05-08 13:20:13 -04005593 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5594 int type = dir ? HDA_INPUT : HDA_OUTPUT;
5595 struct snd_kcontrol_new knew =
5596 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
5597
Takashi Sakamoto0cc1aa72018-05-15 22:12:59 +09005598 sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
Connor McAdams47cdf762018-05-08 13:20:13 -04005599
Takashi Iwaibb86124c2018-07-25 23:00:49 +02005600 knew.tlv.c = NULL;
Connor McAdams47cdf762018-05-08 13:20:13 -04005601
5602 switch (nid) {
5603 case XBASS_XOVER:
5604 knew.info = ca0132_alt_xbass_xover_slider_info;
5605 knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
5606 knew.put = ca0132_alt_xbass_xover_slider_put;
5607 break;
5608 default:
5609 knew.info = ca0132_alt_effect_slider_info;
5610 knew.get = ca0132_alt_slider_ctl_get;
5611 knew.put = ca0132_alt_effect_slider_put;
5612 knew.private_value =
5613 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
5614 break;
5615 }
5616
5617 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
5618}
5619
5620/*
5621 * Added FX: prefix for the alternative codecs, because otherwise the surround
5622 * effect would conflict with the Surround sound volume control. Also seems more
5623 * clear as to what the switches do. Left alone for others.
5624 */
Ian Minetta7e76272012-12-20 18:53:35 -08005625static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
5626 const char *pfx, int dir)
5627{
Connor McAdams47cdf762018-05-08 13:20:13 -04005628 struct ca0132_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02005629 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minetta7e76272012-12-20 18:53:35 -08005630 int type = dir ? HDA_INPUT : HDA_OUTPUT;
5631 struct snd_kcontrol_new knew =
5632 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
Connor McAdams47cdf762018-05-08 13:20:13 -04005633 /* If using alt_controls, add FX: prefix. But, don't add FX:
5634 * prefix to OutFX or InFX enable controls.
5635 */
5636 if ((spec->use_alt_controls) && (nid <= IN_EFFECT_END_NID))
Takashi Sakamoto0cc1aa72018-05-15 22:12:59 +09005637 sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
Connor McAdams47cdf762018-05-08 13:20:13 -04005638 else
5639 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
5640
Ian Minetta7e76272012-12-20 18:53:35 -08005641 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
5642}
5643
5644static int add_voicefx(struct hda_codec *codec)
5645{
5646 struct snd_kcontrol_new knew =
5647 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
5648 VOICEFX, 1, 0, HDA_INPUT);
5649 knew.info = ca0132_voicefx_info;
5650 knew.get = ca0132_voicefx_get;
5651 knew.put = ca0132_voicefx_put;
5652 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
5653}
5654
Connor McAdams47cdf762018-05-08 13:20:13 -04005655/* Create the EQ Preset control */
5656static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
5657{
5658 struct snd_kcontrol_new knew =
5659 HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
5660 EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
5661 knew.info = ca0132_alt_eq_preset_info;
5662 knew.get = ca0132_alt_eq_preset_get;
5663 knew.put = ca0132_alt_eq_preset_put;
5664 return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
5665 snd_ctl_new1(&knew, codec));
5666}
5667
5668/*
5669 * Add enumerated control for the three different settings of the smart volume
5670 * output effect. Normal just uses the slider value, and loud and night are
5671 * their own things that ignore that value.
5672 */
5673static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
5674{
5675 struct snd_kcontrol_new knew =
5676 HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
5677 SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
5678 knew.info = ca0132_alt_svm_setting_info;
5679 knew.get = ca0132_alt_svm_setting_get;
5680 knew.put = ca0132_alt_svm_setting_put;
5681 return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
5682 snd_ctl_new1(&knew, codec));
5683
5684}
5685
Ian Minetta7e76272012-12-20 18:53:35 -08005686/*
Connor McAdams7cb9d942018-05-08 13:20:10 -04005687 * Create an Output Select enumerated control for codecs with surround
5688 * out capabilities.
5689 */
5690static int ca0132_alt_add_output_enum(struct hda_codec *codec)
5691{
5692 struct snd_kcontrol_new knew =
5693 HDA_CODEC_MUTE_MONO("Output Select",
5694 OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
5695 knew.info = ca0132_alt_output_select_get_info;
5696 knew.get = ca0132_alt_output_select_get;
5697 knew.put = ca0132_alt_output_select_put;
5698 return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
5699 snd_ctl_new1(&knew, codec));
5700}
5701
5702/*
5703 * Create an Input Source enumerated control for the alternate ca0132 codecs
5704 * because the front microphone has no auto-detect, and Line-in has to be set
5705 * somehow.
5706 */
5707static int ca0132_alt_add_input_enum(struct hda_codec *codec)
5708{
5709 struct snd_kcontrol_new knew =
5710 HDA_CODEC_MUTE_MONO("Input Source",
5711 INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
5712 knew.info = ca0132_alt_input_source_info;
5713 knew.get = ca0132_alt_input_source_get;
5714 knew.put = ca0132_alt_input_source_put;
5715 return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
5716 snd_ctl_new1(&knew, codec));
5717}
5718
5719/*
Connor McAdams47cdf762018-05-08 13:20:13 -04005720 * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
5721 * more control than the original mic boost, which is either full 30dB or off.
5722 */
5723static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
5724{
5725 struct snd_kcontrol_new knew =
5726 HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
5727 MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
5728 knew.info = ca0132_alt_mic_boost_info;
5729 knew.get = ca0132_alt_mic_boost_get;
5730 knew.put = ca0132_alt_mic_boost_put;
5731 return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
5732 snd_ctl_new1(&knew, codec));
5733
5734}
5735
5736/*
5737 * Need to create slave controls for the alternate codecs that have surround
5738 * capabilities.
5739 */
5740static const char * const ca0132_alt_slave_pfxs[] = {
5741 "Front", "Surround", "Center", "LFE", NULL,
5742};
5743
5744/*
5745 * Also need special channel map, because the default one is incorrect.
5746 * I think this has to do with the pin for rear surround being 0x11,
5747 * and the center/lfe being 0x10. Usually the pin order is the opposite.
5748 */
Colin Ian King9c4a6652018-06-21 19:34:57 +01005749static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
Connor McAdams47cdf762018-05-08 13:20:13 -04005750 { .channels = 2,
5751 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
5752 { .channels = 4,
5753 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
5754 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
5755 { .channels = 6,
5756 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
5757 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
5758 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
5759 { }
5760};
5761
5762/* Add the correct chmap for streams with 6 channels. */
5763static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
5764{
5765 int err = 0;
5766 struct hda_pcm *pcm;
5767
5768 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
5769 struct hda_pcm_stream *hinfo =
5770 &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
5771 struct snd_pcm_chmap *chmap;
5772 const struct snd_pcm_chmap_elem *elem;
5773
5774 elem = ca0132_alt_chmaps;
5775 if (hinfo->channels_max == 6) {
5776 err = snd_pcm_add_chmap_ctls(pcm->pcm,
5777 SNDRV_PCM_STREAM_PLAYBACK,
5778 elem, hinfo->channels_max, 0, &chmap);
5779 if (err < 0)
5780 codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
5781 }
5782 }
5783}
5784
5785/*
Ian Minetta7e76272012-12-20 18:53:35 -08005786 * When changing Node IDs for Mixer Controls below, make sure to update
5787 * Node IDs in ca0132_config() as well.
5788 */
Takashi Sakamotob0eaa072018-05-15 22:12:57 +09005789static const struct snd_kcontrol_new ca0132_mixer[] = {
Ian Minetta7e76272012-12-20 18:53:35 -08005790 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
5791 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
5792 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
5793 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5794 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
5795 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
5796 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5797 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5798 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
5799 0x12, 1, HDA_INPUT),
5800 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
5801 VNID_HP_SEL, 1, HDA_OUTPUT),
5802 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
5803 VNID_AMIC1_SEL, 1, HDA_INPUT),
5804 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5805 VNID_HP_ASEL, 1, HDA_OUTPUT),
5806 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
5807 VNID_AMIC1_ASEL, 1, HDA_INPUT),
5808 { } /* end */
5809};
5810
Connor McAdams017310f2018-05-08 13:20:11 -04005811/*
Connor McAdamse25e3442018-08-08 13:34:21 -04005812 * Desktop specific control mixer. Removes auto-detect for mic, and adds
5813 * surround controls. Also sets both the Front Playback and Capture Volume
5814 * controls to alt so they set the DSP's decibel level.
Connor McAdams017310f2018-05-08 13:20:11 -04005815 */
Connor McAdamse25e3442018-08-08 13:34:21 -04005816static const struct snd_kcontrol_new desktop_mixer[] = {
Connor McAdams017310f2018-05-08 13:20:11 -04005817 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
5818 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
Connor McAdams47cdf762018-05-08 13:20:13 -04005819 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
5820 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
5821 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
5822 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
5823 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
5824 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
Connor McAdams017310f2018-05-08 13:20:11 -04005825 CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
5826 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5827 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5828 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5829 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5830 VNID_HP_ASEL, 1, HDA_OUTPUT),
5831 { } /* end */
5832};
5833
5834/*
5835 * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
5836 * because it doesn't set decibel levels for the DSP for capture.
5837 */
Takashi Sakamotob0eaa072018-05-15 22:12:57 +09005838static const struct snd_kcontrol_new r3di_mixer[] = {
Connor McAdams017310f2018-05-08 13:20:11 -04005839 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
5840 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
Connor McAdams47cdf762018-05-08 13:20:13 -04005841 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
5842 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
5843 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
5844 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
5845 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
5846 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
Connor McAdams017310f2018-05-08 13:20:11 -04005847 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
5848 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5849 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5850 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5851 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5852 VNID_HP_ASEL, 1, HDA_OUTPUT),
5853 { } /* end */
5854};
5855
Ian Minette90f29e2012-12-20 18:53:39 -08005856static int ca0132_build_controls(struct hda_codec *codec)
5857{
5858 struct ca0132_spec *spec = codec->spec;
Connor McAdams47cdf762018-05-08 13:20:13 -04005859 int i, num_fx, num_sliders;
Ian Minette90f29e2012-12-20 18:53:39 -08005860 int err = 0;
5861
5862 /* Add Mixer controls */
5863 for (i = 0; i < spec->num_mixers; i++) {
5864 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
5865 if (err < 0)
5866 return err;
5867 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005868 /* Setup vmaster with surround slaves for desktop ca0132 devices */
5869 if (spec->use_alt_functions) {
5870 snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
5871 spec->tlv);
5872 snd_hda_add_vmaster(codec, "Master Playback Volume",
5873 spec->tlv, ca0132_alt_slave_pfxs,
5874 "Playback Volume");
5875 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5876 NULL, ca0132_alt_slave_pfxs,
5877 "Playback Switch",
5878 true, &spec->vmaster_mute.sw_kctl);
5879
5880 }
Ian Minette90f29e2012-12-20 18:53:39 -08005881
5882 /* Add in and out effects controls.
5883 * VoiceFX, PE and CrystalVoice are added separately.
5884 */
5885 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
5886 for (i = 0; i < num_fx; i++) {
Connor McAdamse25e3442018-08-08 13:34:21 -04005887 /* SBZ and R3D break if Echo Cancellation is used. */
5888 if (spec->quirk == QUIRK_SBZ || spec->quirk == QUIRK_R3D) {
Connor McAdams47cdf762018-05-08 13:20:13 -04005889 if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
5890 OUT_EFFECTS_COUNT))
5891 continue;
5892 }
5893
Ian Minette90f29e2012-12-20 18:53:39 -08005894 err = add_fx_switch(codec, ca0132_effects[i].nid,
5895 ca0132_effects[i].name,
5896 ca0132_effects[i].direct);
5897 if (err < 0)
5898 return err;
5899 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005900 /*
5901 * If codec has use_alt_controls set to true, add effect level sliders,
5902 * EQ presets, and Smart Volume presets. Also, change names to add FX
5903 * prefix, and change PlayEnhancement and CrystalVoice to match.
5904 */
5905 if (spec->use_alt_controls) {
5906 ca0132_alt_add_svm_enum(codec);
5907 add_ca0132_alt_eq_presets(codec);
5908 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
5909 "Enable OutFX", 0);
5910 if (err < 0)
5911 return err;
Ian Minette90f29e2012-12-20 18:53:39 -08005912
Connor McAdams47cdf762018-05-08 13:20:13 -04005913 err = add_fx_switch(codec, CRYSTAL_VOICE,
5914 "Enable InFX", 1);
5915 if (err < 0)
5916 return err;
Ian Minette90f29e2012-12-20 18:53:39 -08005917
Connor McAdams47cdf762018-05-08 13:20:13 -04005918 num_sliders = OUT_EFFECTS_COUNT - 1;
5919 for (i = 0; i < num_sliders; i++) {
5920 err = ca0132_alt_add_effect_slider(codec,
5921 ca0132_effects[i].nid,
5922 ca0132_effects[i].name,
5923 ca0132_effects[i].direct);
5924 if (err < 0)
5925 return err;
5926 }
Ian Minette90f29e2012-12-20 18:53:39 -08005927
Connor McAdams47cdf762018-05-08 13:20:13 -04005928 err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
5929 "X-Bass Crossover", EFX_DIR_OUT);
5930
5931 if (err < 0)
5932 return err;
5933 } else {
5934 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
5935 "PlayEnhancement", 0);
5936 if (err < 0)
5937 return err;
5938
5939 err = add_fx_switch(codec, CRYSTAL_VOICE,
5940 "CrystalVoice", 1);
5941 if (err < 0)
5942 return err;
5943 }
Ian Minette90f29e2012-12-20 18:53:39 -08005944 add_voicefx(codec);
5945
Connor McAdams7cb9d942018-05-08 13:20:10 -04005946 /*
5947 * If the codec uses alt_functions, you need the enumerated controls
5948 * to select the new outputs and inputs, plus add the new mic boost
5949 * setting control.
5950 */
5951 if (spec->use_alt_functions) {
5952 ca0132_alt_add_output_enum(codec);
5953 ca0132_alt_add_input_enum(codec);
Connor McAdams47cdf762018-05-08 13:20:13 -04005954 ca0132_alt_add_mic_boost_enum(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04005955 }
Ian Minette90f29e2012-12-20 18:53:39 -08005956#ifdef ENABLE_TUNING_CONTROLS
5957 add_tuning_ctls(codec);
5958#endif
5959
5960 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5961 if (err < 0)
5962 return err;
5963
5964 if (spec->dig_out) {
5965 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
5966 spec->dig_out);
5967 if (err < 0)
5968 return err;
5969 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
5970 if (err < 0)
5971 return err;
5972 /* spec->multiout.share_spdif = 1; */
5973 }
5974
5975 if (spec->dig_in) {
5976 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
5977 if (err < 0)
5978 return err;
5979 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005980
5981 if (spec->use_alt_functions)
5982 ca0132_alt_add_chmap_ctls(codec);
5983
Ian Minette90f29e2012-12-20 18:53:39 -08005984 return 0;
5985}
5986
Ian Minett5aaca442012-12-20 18:53:34 -08005987/*
Ian Minette90f29e2012-12-20 18:53:39 -08005988 * PCM
Ian Minett95c6e9c2011-06-15 15:35:17 -07005989 */
Julia Lawall071f1342016-09-11 15:05:43 +02005990static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07005991 .substreams = 1,
5992 .channels_min = 2,
Ian Minett825315b2012-12-20 18:53:36 -08005993 .channels_max = 6,
Ian Minett95c6e9c2011-06-15 15:35:17 -07005994 .ops = {
5995 .prepare = ca0132_playback_pcm_prepare,
Dylan Reide8412ca2013-04-04 13:55:09 -07005996 .cleanup = ca0132_playback_pcm_cleanup,
5997 .get_delay = ca0132_playback_pcm_delay,
Ian Minett95c6e9c2011-06-15 15:35:17 -07005998 },
5999};
6000
Julia Lawall071f1342016-09-11 15:05:43 +02006001static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07006002 .substreams = 1,
6003 .channels_min = 2,
6004 .channels_max = 2,
Ian Minett825315b2012-12-20 18:53:36 -08006005 .ops = {
6006 .prepare = ca0132_capture_pcm_prepare,
Dylan Reide8412ca2013-04-04 13:55:09 -07006007 .cleanup = ca0132_capture_pcm_cleanup,
6008 .get_delay = ca0132_capture_pcm_delay,
Ian Minett825315b2012-12-20 18:53:36 -08006009 },
Ian Minett95c6e9c2011-06-15 15:35:17 -07006010};
6011
Julia Lawall071f1342016-09-11 15:05:43 +02006012static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07006013 .substreams = 1,
6014 .channels_min = 2,
6015 .channels_max = 2,
6016 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02006017 .open = ca0132_dig_playback_pcm_open,
6018 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07006019 .prepare = ca0132_dig_playback_pcm_prepare,
6020 .cleanup = ca0132_dig_playback_pcm_cleanup
6021 },
6022};
6023
Julia Lawall071f1342016-09-11 15:05:43 +02006024static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07006025 .substreams = 1,
6026 .channels_min = 2,
6027 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07006028};
6029
6030static int ca0132_build_pcms(struct hda_codec *codec)
6031{
6032 struct ca0132_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006033 struct hda_pcm *info;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006034
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006035 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
6036 if (!info)
6037 return -ENOMEM;
Connor McAdams47cdf762018-05-08 13:20:13 -04006038 if (spec->use_alt_functions) {
6039 info->own_chmap = true;
6040 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
6041 = ca0132_alt_chmaps;
6042 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07006043 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
6044 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
6045 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
6046 spec->multiout.max_channels;
6047 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
Ian Minett825315b2012-12-20 18:53:36 -08006048 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006049 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
Ian Minett95c6e9c2011-06-15 15:35:17 -07006050
Connor McAdams009b8f92018-05-08 13:20:06 -04006051 /* With the DSP enabled, desktops don't use this ADC. */
Alastair Bridgewater5f8ddc62018-06-15 21:56:19 -04006052 if (!spec->use_alt_functions) {
Connor McAdams009b8f92018-05-08 13:20:06 -04006053 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
6054 if (!info)
6055 return -ENOMEM;
6056 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
6057 ca0132_pcm_analog_capture;
6058 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
6059 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
6060 }
Ian Minett825315b2012-12-20 18:53:36 -08006061
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006062 info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
6063 if (!info)
6064 return -ENOMEM;
Ian Minett825315b2012-12-20 18:53:36 -08006065 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
6066 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
6067 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
Ian Minett825315b2012-12-20 18:53:36 -08006068
Ian Minett95c6e9c2011-06-15 15:35:17 -07006069 if (!spec->dig_out && !spec->dig_in)
6070 return 0;
6071
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006072 info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
6073 if (!info)
6074 return -ENOMEM;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006075 info->pcm_type = HDA_PCM_TYPE_SPDIF;
6076 if (spec->dig_out) {
6077 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
6078 ca0132_pcm_digital_playback;
6079 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
6080 }
6081 if (spec->dig_in) {
6082 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
6083 ca0132_pcm_digital_capture;
6084 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
6085 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07006086
6087 return 0;
6088}
6089
Ian Minett441aa6a2012-12-20 18:53:40 -08006090static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
6091{
6092 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01006093 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
Ian Minett441aa6a2012-12-20 18:53:40 -08006094 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
6095 snd_hda_codec_write(codec, pin, 0,
6096 AC_VERB_SET_AMP_GAIN_MUTE,
6097 AMP_OUT_UNMUTE);
6098 }
6099 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
6100 snd_hda_codec_write(codec, dac, 0,
6101 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
6102}
6103
6104static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
6105{
6106 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01006107 snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
Ian Minett441aa6a2012-12-20 18:53:40 -08006108 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
6109 snd_hda_codec_write(codec, pin, 0,
6110 AC_VERB_SET_AMP_GAIN_MUTE,
6111 AMP_IN_UNMUTE(0));
6112 }
6113 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
6114 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
6115 AMP_IN_UNMUTE(0));
6116
6117 /* init to 0 dB and unmute. */
6118 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
6119 HDA_AMP_VOLMASK, 0x5a);
6120 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
6121 HDA_AMP_MUTE, 0);
6122 }
6123}
6124
Ian Minett5aaca442012-12-20 18:53:34 -08006125static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
6126{
6127 unsigned int caps;
6128
6129 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
6130 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
6131 snd_hda_override_amp_caps(codec, nid, dir, caps);
6132}
6133
6134/*
6135 * Switch between Digital built-in mic and analog mic.
6136 */
6137static void ca0132_set_dmic(struct hda_codec *codec, int enable)
6138{
6139 struct ca0132_spec *spec = codec->spec;
6140 unsigned int tmp;
6141 u8 val;
6142 unsigned int oldval;
6143
Takashi Iwai4e76a882014-02-25 12:21:03 +01006144 codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
Ian Minett5aaca442012-12-20 18:53:34 -08006145
6146 oldval = stop_mic1(codec);
6147 ca0132_set_vipsource(codec, 0);
6148 if (enable) {
6149 /* set DMic input as 2-ch */
6150 tmp = FLOAT_TWO;
6151 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6152
6153 val = spec->dmic_ctl;
6154 val |= 0x80;
6155 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6156 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6157
6158 if (!(spec->dmic_ctl & 0x20))
6159 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
6160 } else {
6161 /* set AMic input as mono */
6162 tmp = FLOAT_ONE;
6163 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6164
6165 val = spec->dmic_ctl;
6166 /* clear bit7 and bit5 to disable dmic */
6167 val &= 0x5f;
6168 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6169 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6170
6171 if (!(spec->dmic_ctl & 0x20))
6172 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
6173 }
6174 ca0132_set_vipsource(codec, 1);
6175 resume_mic1(codec, oldval);
6176}
6177
6178/*
6179 * Initialization for Digital Mic.
6180 */
6181static void ca0132_init_dmic(struct hda_codec *codec)
6182{
6183 struct ca0132_spec *spec = codec->spec;
6184 u8 val;
6185
6186 /* Setup Digital Mic here, but don't enable.
6187 * Enable based on jack detect.
6188 */
6189
6190 /* MCLK uses MPIO1, set to enable.
6191 * Bit 2-0: MPIO select
6192 * Bit 3: set to disable
6193 * Bit 7-4: reserved
6194 */
6195 val = 0x01;
6196 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6197 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
6198
6199 /* Data1 uses MPIO3. Data2 not use
6200 * Bit 2-0: Data1 MPIO select
6201 * Bit 3: set disable Data1
6202 * Bit 6-4: Data2 MPIO select
6203 * Bit 7: set disable Data2
6204 */
6205 val = 0x83;
6206 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6207 VENDOR_CHIPIO_DMIC_PIN_SET, val);
6208
6209 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
6210 * Bit 3-0: Channel mask
6211 * Bit 4: set for 48KHz, clear for 32KHz
6212 * Bit 5: mode
6213 * Bit 6: set to select Data2, clear for Data1
6214 * Bit 7: set to enable DMic, clear for AMic
6215 */
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04006216 if (spec->quirk == QUIRK_ALIENWARE_M17XR4)
6217 val = 0x33;
6218 else
6219 val = 0x23;
Ian Minett5aaca442012-12-20 18:53:34 -08006220 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
6221 spec->dmic_ctl = val;
6222 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6223 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6224}
6225
6226/*
6227 * Initialization for Analog Mic 2
6228 */
6229static void ca0132_init_analog_mic2(struct hda_codec *codec)
6230{
6231 struct ca0132_spec *spec = codec->spec;
6232
6233 mutex_lock(&spec->chipio_mutex);
6234 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6235 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
6236 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6237 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
6238 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6239 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
6240 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6241 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
6242 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6243 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
6244 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6245 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
6246 mutex_unlock(&spec->chipio_mutex);
6247}
6248
6249static void ca0132_refresh_widget_caps(struct hda_codec *codec)
6250{
6251 struct ca0132_spec *spec = codec->spec;
6252 int i;
Ian Minett5aaca442012-12-20 18:53:34 -08006253
Takashi Iwai4e76a882014-02-25 12:21:03 +01006254 codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
Takashi Iwai7639a062015-03-03 10:07:24 +01006255 snd_hda_codec_update_widgets(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08006256
6257 for (i = 0; i < spec->multiout.num_dacs; i++)
6258 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
6259
6260 for (i = 0; i < spec->num_outputs; i++)
6261 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
6262
6263 for (i = 0; i < spec->num_inputs; i++) {
6264 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
6265 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
6266 }
6267}
6268
6269/*
Connor McAdamsc986f502018-08-08 13:34:19 -04006270 * Recon3D r3d_setup_defaults sub functions.
Connor McAdams7e6ed622018-05-08 13:20:08 -04006271 */
6272
Connor McAdamsc986f502018-08-08 13:34:19 -04006273static void r3d_dsp_scp_startup(struct hda_codec *codec)
Connor McAdams447fd8e2018-05-08 13:20:09 -04006274{
6275 unsigned int tmp;
6276
6277 tmp = 0x00000000;
6278 dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
6279
6280 tmp = 0x00000001;
6281 dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
6282
6283 tmp = 0x00000004;
6284 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6285
6286 tmp = 0x00000005;
6287 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6288
6289 tmp = 0x00000000;
6290 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6291
6292}
6293
Connor McAdamsc986f502018-08-08 13:34:19 -04006294static void r3d_dsp_initial_mic_setup(struct hda_codec *codec)
Connor McAdams7e6ed622018-05-08 13:20:08 -04006295{
6296 unsigned int tmp;
6297
6298 /* Mic 1 Setup */
6299 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6300 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6301 /* This ConnPointID is unique to Recon3Di. Haven't seen it elsewhere */
6302 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
6303 tmp = FLOAT_ONE;
6304 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6305
6306 /* Mic 2 Setup, even though it isn't connected on SBZ */
6307 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
6308 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
6309 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
6310 tmp = FLOAT_ZERO;
6311 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6312}
6313
6314/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04006315 * Initialize Sound Blaster Z analog microphones.
6316 */
6317static void sbz_init_analog_mics(struct hda_codec *codec)
6318{
6319 unsigned int tmp;
6320
6321 /* Mic 1 Setup */
6322 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6323 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6324 tmp = FLOAT_THREE;
6325 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6326
6327 /* Mic 2 Setup, even though it isn't connected on SBZ */
6328 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
6329 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
6330 tmp = FLOAT_ZERO;
6331 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6332
6333}
6334
6335/*
6336 * Sets the source of stream 0x14 to connpointID 0x48, and the destination
6337 * connpointID to 0x91. If this isn't done, the destination is 0x71, and
6338 * you get no sound. I'm guessing this has to do with the Sound Blaster Z
6339 * having an updated DAC, which changes the destination to that DAC.
6340 */
6341static void sbz_connect_streams(struct hda_codec *codec)
6342{
6343 struct ca0132_spec *spec = codec->spec;
6344
6345 mutex_lock(&spec->chipio_mutex);
6346
6347 codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
6348
6349 chipio_set_stream_channels(codec, 0x0C, 6);
6350 chipio_set_stream_control(codec, 0x0C, 1);
6351
6352 /* This value is 0x43 for 96khz, and 0x83 for 192khz. */
6353 chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
6354
6355 /* Setup stream 0x14 with it's source and destination points */
6356 chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
6357 chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
6358 chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
6359 chipio_set_stream_channels(codec, 0x14, 2);
6360 chipio_set_stream_control(codec, 0x14, 1);
6361
6362 codec_dbg(codec, "Connect Streams exited, mutex released.\n");
6363
6364 mutex_unlock(&spec->chipio_mutex);
6365
6366}
6367
6368/*
6369 * Write data through ChipIO to setup proper stream destinations.
6370 * Not sure how it exactly works, but it seems to direct data
6371 * to different destinations. Example is f8 to c0, e0 to c0.
6372 * All I know is, if you don't set these, you get no sound.
6373 */
6374static void sbz_chipio_startup_data(struct hda_codec *codec)
6375{
6376 struct ca0132_spec *spec = codec->spec;
6377
6378 mutex_lock(&spec->chipio_mutex);
6379 codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
6380
6381 /* These control audio output */
6382 chipio_write_no_mutex(codec, 0x190060, 0x0001f8c0);
6383 chipio_write_no_mutex(codec, 0x190064, 0x0001f9c1);
6384 chipio_write_no_mutex(codec, 0x190068, 0x0001fac6);
6385 chipio_write_no_mutex(codec, 0x19006c, 0x0001fbc7);
6386 /* Signal to update I think */
6387 chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
6388
6389 chipio_set_stream_channels(codec, 0x0C, 6);
6390 chipio_set_stream_control(codec, 0x0C, 1);
6391 /* No clue what these control */
6392 chipio_write_no_mutex(codec, 0x190030, 0x0001e0c0);
6393 chipio_write_no_mutex(codec, 0x190034, 0x0001e1c1);
6394 chipio_write_no_mutex(codec, 0x190038, 0x0001e4c2);
6395 chipio_write_no_mutex(codec, 0x19003c, 0x0001e5c3);
6396 chipio_write_no_mutex(codec, 0x190040, 0x0001e2c4);
6397 chipio_write_no_mutex(codec, 0x190044, 0x0001e3c5);
6398 chipio_write_no_mutex(codec, 0x190048, 0x0001e8c6);
6399 chipio_write_no_mutex(codec, 0x19004c, 0x0001e9c7);
6400 chipio_write_no_mutex(codec, 0x190050, 0x0001ecc8);
6401 chipio_write_no_mutex(codec, 0x190054, 0x0001edc9);
6402 chipio_write_no_mutex(codec, 0x190058, 0x0001eaca);
6403 chipio_write_no_mutex(codec, 0x19005c, 0x0001ebcb);
6404
6405 chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
6406
6407 codec_dbg(codec, "Startup Data exited, mutex released.\n");
6408 mutex_unlock(&spec->chipio_mutex);
6409}
6410
Connor McAdams447fd8e2018-05-08 13:20:09 -04006411/*
6412 * Sound Blaster Z uses these after DSP is loaded. Weird SCP commands
6413 * without a 0x20 source like normal.
6414 */
6415static void sbz_dsp_scp_startup(struct hda_codec *codec)
6416{
6417 unsigned int tmp;
6418
6419 tmp = 0x00000003;
6420 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6421
6422 tmp = 0x00000000;
6423 dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
6424
6425 tmp = 0x00000001;
6426 dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
6427
6428 tmp = 0x00000004;
6429 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6430
6431 tmp = 0x00000005;
6432 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6433
6434 tmp = 0x00000000;
6435 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6436
6437}
6438
Connor McAdams38ba69f2018-05-08 13:20:07 -04006439static void sbz_dsp_initial_mic_setup(struct hda_codec *codec)
6440{
6441 unsigned int tmp;
6442
6443 chipio_set_stream_control(codec, 0x03, 0);
6444 chipio_set_stream_control(codec, 0x04, 0);
6445
6446 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6447 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6448
6449 tmp = FLOAT_THREE;
6450 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6451
6452 chipio_set_stream_control(codec, 0x03, 1);
6453 chipio_set_stream_control(codec, 0x04, 1);
6454
6455 chipio_write(codec, 0x18b098, 0x0000000c);
6456 chipio_write(codec, 0x18b09C, 0x0000000c);
6457}
6458
6459/*
Ian Minett5aaca442012-12-20 18:53:34 -08006460 * Setup default parameters for DSP
6461 */
6462static void ca0132_setup_defaults(struct hda_codec *codec)
6463{
Dylan Reide8f1bd52013-03-14 17:27:45 -07006464 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08006465 unsigned int tmp;
6466 int num_fx;
6467 int idx, i;
6468
Dylan Reide8f1bd52013-03-14 17:27:45 -07006469 if (spec->dsp_state != DSP_DOWNLOADED)
Ian Minett5aaca442012-12-20 18:53:34 -08006470 return;
6471
6472 /* out, in effects + voicefx */
6473 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6474 for (idx = 0; idx < num_fx; idx++) {
6475 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6476 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
6477 ca0132_effects[idx].reqs[i],
6478 ca0132_effects[idx].def_vals[i]);
6479 }
6480 }
6481
6482 /*remove DSP headroom*/
6483 tmp = FLOAT_ZERO;
6484 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6485
6486 /*set speaker EQ bypass attenuation*/
6487 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
6488
6489 /* set AMic1 and AMic2 as mono mic */
6490 tmp = FLOAT_ONE;
6491 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6492 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6493
6494 /* set AMic1 as CrystalVoice input */
6495 tmp = FLOAT_ONE;
6496 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
6497
6498 /* set WUH source */
6499 tmp = FLOAT_TWO;
6500 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6501}
6502
6503/*
Connor McAdamsc986f502018-08-08 13:34:19 -04006504 * Setup default parameters for Recon3D/Recon3Di DSP.
Connor McAdams7e6ed622018-05-08 13:20:08 -04006505 */
6506
Connor McAdamsc986f502018-08-08 13:34:19 -04006507static void r3d_setup_defaults(struct hda_codec *codec)
Connor McAdams7e6ed622018-05-08 13:20:08 -04006508{
6509 struct ca0132_spec *spec = codec->spec;
6510 unsigned int tmp;
6511 int num_fx;
6512 int idx, i;
6513
6514 if (spec->dsp_state != DSP_DOWNLOADED)
6515 return;
6516
Connor McAdamsc986f502018-08-08 13:34:19 -04006517 r3d_dsp_scp_startup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006518
Connor McAdamsc986f502018-08-08 13:34:19 -04006519 r3d_dsp_initial_mic_setup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006520
6521 /*remove DSP headroom*/
6522 tmp = FLOAT_ZERO;
6523 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6524
6525 /* set WUH source */
6526 tmp = FLOAT_TWO;
6527 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6528 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6529
6530 /* Set speaker source? */
6531 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
6532
Connor McAdamsc986f502018-08-08 13:34:19 -04006533 if (spec->quirk == QUIRK_R3DI)
6534 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006535
6536 /* Setup effect defaults */
6537 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6538 for (idx = 0; idx < num_fx; idx++) {
6539 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6540 dspio_set_uint_param(codec,
6541 ca0132_effects[idx].mid,
6542 ca0132_effects[idx].reqs[i],
6543 ca0132_effects[idx].def_vals[i]);
6544 }
6545 }
Connor McAdams7e6ed622018-05-08 13:20:08 -04006546}
6547
6548/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04006549 * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
6550 * than the Chromebook setup.
6551 */
6552static void sbz_setup_defaults(struct hda_codec *codec)
6553{
6554 struct ca0132_spec *spec = codec->spec;
6555 unsigned int tmp, stream_format;
6556 int num_fx;
6557 int idx, i;
6558
6559 if (spec->dsp_state != DSP_DOWNLOADED)
6560 return;
6561
Connor McAdams447fd8e2018-05-08 13:20:09 -04006562 sbz_dsp_scp_startup(codec);
Connor McAdams38ba69f2018-05-08 13:20:07 -04006563
6564 sbz_init_analog_mics(codec);
6565
6566 sbz_connect_streams(codec);
6567
6568 sbz_chipio_startup_data(codec);
6569
6570 chipio_set_stream_control(codec, 0x03, 1);
6571 chipio_set_stream_control(codec, 0x04, 1);
6572
6573 /*
6574 * Sets internal input loopback to off, used to have a switch to
6575 * enable input loopback, but turned out to be way too buggy.
6576 */
6577 tmp = FLOAT_ONE;
6578 dspio_set_uint_param(codec, 0x37, 0x08, tmp);
6579 dspio_set_uint_param(codec, 0x37, 0x10, tmp);
6580
6581 /*remove DSP headroom*/
6582 tmp = FLOAT_ZERO;
6583 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6584
6585 /* set WUH source */
6586 tmp = FLOAT_TWO;
6587 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6588 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6589
6590 /* Set speaker source? */
6591 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
6592
6593 sbz_dsp_initial_mic_setup(codec);
6594
6595
6596 /* out, in effects + voicefx */
6597 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6598 for (idx = 0; idx < num_fx; idx++) {
6599 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6600 dspio_set_uint_param(codec,
6601 ca0132_effects[idx].mid,
6602 ca0132_effects[idx].reqs[i],
6603 ca0132_effects[idx].def_vals[i]);
6604 }
6605 }
6606
6607 /*
6608 * Have to make a stream to bind the sound output to, otherwise
6609 * you'll get dead audio. Before I did this, it would bind to an
6610 * audio input, and would never work
6611 */
6612 stream_format = snd_hdac_calc_stream_format(48000, 2,
6613 SNDRV_PCM_FORMAT_S32_LE, 32, 0);
6614
6615 snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
6616 0, stream_format);
6617
6618 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
6619
6620 snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
6621 0, stream_format);
6622
6623 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
6624}
6625
6626/*
Ian Minett5aaca442012-12-20 18:53:34 -08006627 * Initialization of flags in chip
6628 */
6629static void ca0132_init_flags(struct hda_codec *codec)
6630{
Connor McAdams009b8f92018-05-08 13:20:06 -04006631 struct ca0132_spec *spec = codec->spec;
6632
6633 if (spec->use_alt_functions) {
6634 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
6635 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
6636 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
6637 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
6638 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
6639 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
6640 chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
6641 chipio_set_control_flag(codec,
6642 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
6643 chipio_set_control_flag(codec,
6644 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
6645 } else {
6646 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
6647 chipio_set_control_flag(codec,
6648 CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
6649 chipio_set_control_flag(codec,
6650 CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
6651 chipio_set_control_flag(codec,
6652 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
6653 chipio_set_control_flag(codec,
6654 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
6655 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
6656 }
Ian Minett5aaca442012-12-20 18:53:34 -08006657}
6658
6659/*
6660 * Initialization of parameters in chip
6661 */
6662static void ca0132_init_params(struct hda_codec *codec)
6663{
Connor McAdams009b8f92018-05-08 13:20:06 -04006664 struct ca0132_spec *spec = codec->spec;
6665
6666 if (spec->use_alt_functions) {
6667 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6668 chipio_set_conn_rate(codec, 0x0B, SR_48_000);
6669 chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
6670 chipio_set_control_param(codec, 0, 0);
6671 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
6672 }
6673
Ian Minett5aaca442012-12-20 18:53:34 -08006674 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
6675 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
6676}
Ian Minett95c6e9c2011-06-15 15:35:17 -07006677
Ian Minette90f29e2012-12-20 18:53:39 -08006678static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
6679{
6680 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
6681 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
6682 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
6683 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
6684 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
6685 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
6686
Ian Minett406261c2012-12-20 18:53:41 -08006687 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6688 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
Ian Minette90f29e2012-12-20 18:53:39 -08006689 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6690}
6691
6692static bool ca0132_download_dsp_images(struct hda_codec *codec)
6693{
6694 bool dsp_loaded = false;
Connor McAdams8a19bce2018-05-08 13:20:01 -04006695 struct ca0132_spec *spec = codec->spec;
Ian Minette90f29e2012-12-20 18:53:39 -08006696 const struct dsp_image_seg *dsp_os_image;
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006697 const struct firmware *fw_entry;
Connor McAdams8a19bce2018-05-08 13:20:01 -04006698 /*
6699 * Alternate firmwares for different variants. The Recon3Di apparently
6700 * can use the default firmware, but I'll leave the option in case
6701 * it needs it again.
6702 */
6703 switch (spec->quirk) {
6704 case QUIRK_SBZ:
6705 if (request_firmware(&fw_entry, SBZ_EFX_FILE,
6706 codec->card->dev) != 0) {
6707 codec_dbg(codec, "SBZ alt firmware not detected. ");
6708 spec->alt_firmware_present = false;
6709 } else {
6710 codec_dbg(codec, "Sound Blaster Z firmware selected.");
6711 spec->alt_firmware_present = true;
6712 }
6713 break;
6714 case QUIRK_R3DI:
6715 if (request_firmware(&fw_entry, R3DI_EFX_FILE,
6716 codec->card->dev) != 0) {
6717 codec_dbg(codec, "Recon3Di alt firmware not detected.");
6718 spec->alt_firmware_present = false;
6719 } else {
6720 codec_dbg(codec, "Recon3Di firmware selected.");
6721 spec->alt_firmware_present = true;
6722 }
6723 break;
6724 default:
6725 spec->alt_firmware_present = false;
6726 break;
6727 }
6728 /*
6729 * Use default ctefx.bin if no alt firmware is detected, or if none
6730 * exists for your particular codec.
6731 */
6732 if (!spec->alt_firmware_present) {
6733 codec_dbg(codec, "Default firmware selected.");
6734 if (request_firmware(&fw_entry, EFX_FILE,
6735 codec->card->dev) != 0)
6736 return false;
6737 }
Ian Minette90f29e2012-12-20 18:53:39 -08006738
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006739 dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
Dylan Reidd1d28502013-03-14 17:27:44 -07006740 if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
Takashi Iwaid9684bb2015-10-26 16:54:16 +01006741 codec_err(codec, "ca0132 DSP load image failed\n");
Dylan Reidd1d28502013-03-14 17:27:44 -07006742 goto exit_download;
6743 }
6744
Ian Minette90f29e2012-12-20 18:53:39 -08006745 dsp_loaded = dspload_wait_loaded(codec);
6746
Dylan Reidd1d28502013-03-14 17:27:44 -07006747exit_download:
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006748 release_firmware(fw_entry);
6749
Ian Minette90f29e2012-12-20 18:53:39 -08006750 return dsp_loaded;
6751}
6752
6753static void ca0132_download_dsp(struct hda_codec *codec)
6754{
6755 struct ca0132_spec *spec = codec->spec;
6756
Takashi Iwai9a0869f2013-02-07 12:41:40 +01006757#ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
6758 return; /* NOP */
6759#endif
Ian Minette90f29e2012-12-20 18:53:39 -08006760
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02006761 if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
6762 return; /* don't retry failures */
6763
Dylan Reidb714a712013-03-14 17:27:46 -07006764 chipio_enable_clocks(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04006765 if (spec->dsp_state != DSP_DOWNLOADED) {
6766 spec->dsp_state = DSP_DOWNLOADING;
6767
6768 if (!ca0132_download_dsp_images(codec))
6769 spec->dsp_state = DSP_DOWNLOAD_FAILED;
6770 else
6771 spec->dsp_state = DSP_DOWNLOADED;
6772 }
Ian Minette90f29e2012-12-20 18:53:39 -08006773
Connor McAdams009b8f92018-05-08 13:20:06 -04006774 /* For codecs using alt functions, this is already done earlier */
6775 if (spec->dsp_state == DSP_DOWNLOADED && (!spec->use_alt_functions))
Ian Minette90f29e2012-12-20 18:53:39 -08006776 ca0132_set_dsp_msr(codec, true);
6777}
6778
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006779static void ca0132_process_dsp_response(struct hda_codec *codec,
6780 struct hda_jack_callback *callback)
Ian Minette90f29e2012-12-20 18:53:39 -08006781{
6782 struct ca0132_spec *spec = codec->spec;
6783
Takashi Iwai4e76a882014-02-25 12:21:03 +01006784 codec_dbg(codec, "ca0132_process_dsp_response\n");
Ian Minette90f29e2012-12-20 18:53:39 -08006785 if (spec->wait_scp) {
6786 if (dspio_get_response_data(codec) >= 0)
6787 spec->wait_scp = 0;
6788 }
6789
6790 dspio_clear_response_queue(codec);
6791}
6792
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006793static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
Ian Minette90f29e2012-12-20 18:53:39 -08006794{
Chih-Chung Chang993884f2013-03-25 10:39:23 -07006795 struct ca0132_spec *spec = codec->spec;
Takashi Iwai2ebab402016-02-09 10:23:52 +01006796 struct hda_jack_tbl *tbl;
Ian Minette90f29e2012-12-20 18:53:39 -08006797
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006798 /* Delay enabling the HP amp, to let the mic-detection
6799 * state machine run.
6800 */
6801 cancel_delayed_work_sync(&spec->unsol_hp_work);
Takashi Iwai2f35c632015-02-27 22:43:26 +01006802 schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
Takashi Iwai2ebab402016-02-09 10:23:52 +01006803 tbl = snd_hda_jack_tbl_get(codec, cb->nid);
6804 if (tbl)
6805 tbl->block_report = 1;
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006806}
Ian Minette90f29e2012-12-20 18:53:39 -08006807
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006808static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
6809{
Connor McAdamsa1b7f012018-08-08 13:34:14 -04006810 struct ca0132_spec *spec = codec->spec;
6811
6812 if (spec->use_alt_functions)
6813 ca0132_alt_select_in(codec);
6814 else
6815 ca0132_select_mic(codec);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006816}
6817
6818static void ca0132_init_unsol(struct hda_codec *codec)
6819{
Gabriele Martinod5c016b2015-05-18 21:15:13 +02006820 struct ca0132_spec *spec = codec->spec;
6821 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
6822 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006823 amic_callback);
6824 snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
6825 ca0132_process_dsp_response);
Connor McAdams63177af2018-05-08 13:20:02 -04006826 /* Front headphone jack detection */
Connor McAdams009b8f92018-05-08 13:20:06 -04006827 if (spec->use_alt_functions)
Connor McAdams63177af2018-05-08 13:20:02 -04006828 snd_hda_jack_detect_enable_callback(codec,
6829 spec->unsol_tag_front_hp, hp_callback);
Ian Minette90f29e2012-12-20 18:53:39 -08006830}
6831
Ian Minett5aaca442012-12-20 18:53:34 -08006832/*
6833 * Verbs tables.
6834 */
6835
6836/* Sends before DSP download. */
6837static struct hda_verb ca0132_base_init_verbs[] = {
6838 /*enable ct extension*/
6839 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
Ian Minett5aaca442012-12-20 18:53:34 -08006840 {}
6841};
6842
6843/* Send at exit. */
6844static struct hda_verb ca0132_base_exit_verbs[] = {
6845 /*set afg to D3*/
6846 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
6847 /*disable ct extension*/
6848 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
6849 {}
6850};
6851
Connor McAdams8a19bce2018-05-08 13:20:01 -04006852/* Other verbs tables. Sends after DSP download. */
Connor McAdamse93ac302018-05-08 13:20:05 -04006853
Ian Minett5aaca442012-12-20 18:53:34 -08006854static struct hda_verb ca0132_init_verbs0[] = {
6855 /* chip init verbs */
6856 {0x15, 0x70D, 0xF0},
6857 {0x15, 0x70E, 0xFE},
6858 {0x15, 0x707, 0x75},
6859 {0x15, 0x707, 0xD3},
6860 {0x15, 0x707, 0x09},
6861 {0x15, 0x707, 0x53},
6862 {0x15, 0x707, 0xD4},
6863 {0x15, 0x707, 0xEF},
6864 {0x15, 0x707, 0x75},
6865 {0x15, 0x707, 0xD3},
6866 {0x15, 0x707, 0x09},
6867 {0x15, 0x707, 0x02},
6868 {0x15, 0x707, 0x37},
6869 {0x15, 0x707, 0x78},
6870 {0x15, 0x53C, 0xCE},
6871 {0x15, 0x575, 0xC9},
6872 {0x15, 0x53D, 0xCE},
6873 {0x15, 0x5B7, 0xC9},
6874 {0x15, 0x70D, 0xE8},
6875 {0x15, 0x70E, 0xFE},
6876 {0x15, 0x707, 0x02},
6877 {0x15, 0x707, 0x68},
6878 {0x15, 0x707, 0x62},
6879 {0x15, 0x53A, 0xCE},
6880 {0x15, 0x546, 0xC9},
6881 {0x15, 0x53B, 0xCE},
6882 {0x15, 0x5E8, 0xC9},
Connor McAdamse93ac302018-05-08 13:20:05 -04006883 {}
6884};
6885
Connor McAdamse42c7c72018-08-08 13:34:18 -04006886/* Extra init verbs for desktop cards. */
6887static struct hda_verb ca0132_init_verbs1[] = {
Connor McAdamse93ac302018-05-08 13:20:05 -04006888 {0x15, 0x70D, 0x20},
6889 {0x15, 0x70E, 0x19},
6890 {0x15, 0x707, 0x00},
6891 {0x15, 0x539, 0xCE},
6892 {0x15, 0x546, 0xC9},
6893 {0x15, 0x70D, 0xB7},
6894 {0x15, 0x70E, 0x09},
6895 {0x15, 0x707, 0x10},
6896 {0x15, 0x70D, 0xAF},
6897 {0x15, 0x70E, 0x09},
6898 {0x15, 0x707, 0x01},
6899 {0x15, 0x707, 0x05},
6900 {0x15, 0x70D, 0x73},
6901 {0x15, 0x70E, 0x09},
6902 {0x15, 0x707, 0x14},
6903 {0x15, 0x6FF, 0xC4},
Ian Minett5aaca442012-12-20 18:53:34 -08006904 {}
6905};
6906
Ian Minett95c6e9c2011-06-15 15:35:17 -07006907static void ca0132_init_chip(struct hda_codec *codec)
6908{
6909 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08006910 int num_fx;
6911 int i;
6912 unsigned int on;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006913
6914 mutex_init(&spec->chipio_mutex);
Ian Minett5aaca442012-12-20 18:53:34 -08006915
6916 spec->cur_out_type = SPEAKER_OUT;
Connor McAdams7cb9d942018-05-08 13:20:10 -04006917 if (!spec->use_alt_functions)
6918 spec->cur_mic_type = DIGITAL_MIC;
6919 else
6920 spec->cur_mic_type = REAR_MIC;
6921
Ian Minett5aaca442012-12-20 18:53:34 -08006922 spec->cur_mic_boost = 0;
6923
6924 for (i = 0; i < VNODES_COUNT; i++) {
6925 spec->vnode_lvol[i] = 0x5a;
6926 spec->vnode_rvol[i] = 0x5a;
6927 spec->vnode_lswitch[i] = 0;
6928 spec->vnode_rswitch[i] = 0;
6929 }
6930
6931 /*
6932 * Default states for effects are in ca0132_effects[].
6933 */
6934 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
6935 for (i = 0; i < num_fx; i++) {
6936 on = (unsigned int)ca0132_effects[i].reqs[0];
6937 spec->effects_switch[i] = on ? 1 : 0;
6938 }
Connor McAdams47cdf762018-05-08 13:20:13 -04006939 /*
6940 * Sets defaults for the effect slider controls, only for alternative
6941 * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
6942 */
6943 if (spec->use_alt_controls) {
6944 spec->xbass_xover_freq = 8;
6945 for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
6946 spec->fx_ctl_val[i] = effect_slider_defaults[i];
6947 }
Ian Minett5aaca442012-12-20 18:53:34 -08006948
6949 spec->voicefx_val = 0;
6950 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
6951 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
6952
Ian Minett44f0c972012-12-20 18:53:38 -08006953#ifdef ENABLE_TUNING_CONTROLS
6954 ca0132_init_tuning_defaults(codec);
6955#endif
Ian Minett95c6e9c2011-06-15 15:35:17 -07006956}
6957
Connor McAdams2e48b2b2018-05-08 13:20:04 -04006958/*
6959 * Recon3Di exit specific commands.
6960 */
6961/* prevents popping noise on shutdown */
6962static void r3di_gpio_shutdown(struct hda_codec *codec)
6963{
6964 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
6965}
6966
6967/*
6968 * Sound Blaster Z exit specific commands.
6969 */
6970static void sbz_region2_exit(struct hda_codec *codec)
6971{
6972 struct ca0132_spec *spec = codec->spec;
6973 unsigned int i;
6974
6975 for (i = 0; i < 4; i++)
6976 writeb(0x0, spec->mem_base + 0x100);
6977 for (i = 0; i < 8; i++)
6978 writeb(0xb3, spec->mem_base + 0x304);
Connor McAdamsa62e4732018-08-08 13:34:12 -04006979
6980 ca0132_mmio_gpio_set(codec, 0, false);
6981 ca0132_mmio_gpio_set(codec, 1, false);
6982 ca0132_mmio_gpio_set(codec, 4, true);
6983 ca0132_mmio_gpio_set(codec, 5, false);
6984 ca0132_mmio_gpio_set(codec, 7, false);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04006985}
6986
6987static void sbz_set_pin_ctl_default(struct hda_codec *codec)
6988{
6989 hda_nid_t pins[5] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
6990 unsigned int i;
6991
6992 snd_hda_codec_write(codec, 0x11, 0,
6993 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
6994
6995 for (i = 0; i < 5; i++)
6996 snd_hda_codec_write(codec, pins[i], 0,
6997 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
6998}
6999
Connor McAdams2f295f92018-08-08 13:34:22 -04007000static void ca0132_clear_unsolicited(struct hda_codec *codec)
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007001{
7002 hda_nid_t pins[7] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
7003 unsigned int i;
7004
7005 for (i = 0; i < 7; i++) {
7006 snd_hda_codec_write(codec, pins[i], 0,
7007 AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
7008 }
7009}
7010
7011/* On shutdown, sends commands in sets of three */
7012static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
7013 int mask, int data)
7014{
7015 if (dir >= 0)
7016 snd_hda_codec_write(codec, 0x01, 0,
7017 AC_VERB_SET_GPIO_DIRECTION, dir);
7018 if (mask >= 0)
7019 snd_hda_codec_write(codec, 0x01, 0,
7020 AC_VERB_SET_GPIO_MASK, mask);
7021
7022 if (data >= 0)
7023 snd_hda_codec_write(codec, 0x01, 0,
7024 AC_VERB_SET_GPIO_DATA, data);
7025}
7026
7027static void sbz_exit_chip(struct hda_codec *codec)
7028{
Connor McAdams009b8f92018-05-08 13:20:06 -04007029 chipio_set_stream_control(codec, 0x03, 0);
7030 chipio_set_stream_control(codec, 0x04, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007031
7032 /* Mess with GPIO */
7033 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
7034 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
7035 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
7036
Connor McAdams009b8f92018-05-08 13:20:06 -04007037 chipio_set_stream_control(codec, 0x14, 0);
7038 chipio_set_stream_control(codec, 0x0C, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007039
7040 chipio_set_conn_rate(codec, 0x41, SR_192_000);
7041 chipio_set_conn_rate(codec, 0x91, SR_192_000);
7042
7043 chipio_write(codec, 0x18a020, 0x00000083);
7044
7045 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
7046 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
7047 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
7048
Connor McAdams009b8f92018-05-08 13:20:06 -04007049 chipio_set_stream_control(codec, 0x0C, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007050
7051 chipio_set_control_param(codec, 0x0D, 0x24);
7052
Connor McAdams2f295f92018-08-08 13:34:22 -04007053 ca0132_clear_unsolicited(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007054 sbz_set_pin_ctl_default(codec);
7055
7056 snd_hda_codec_write(codec, 0x0B, 0,
7057 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
7058
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007059 sbz_region2_exit(codec);
7060}
7061
Connor McAdams2f295f92018-08-08 13:34:22 -04007062static void r3d_exit_chip(struct hda_codec *codec)
7063{
7064 ca0132_clear_unsolicited(codec);
7065 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
7066 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
7067}
7068
Ian Minett95c6e9c2011-06-15 15:35:17 -07007069static void ca0132_exit_chip(struct hda_codec *codec)
7070{
7071 /* put any chip cleanup stuffs here. */
Ian Minett5aaca442012-12-20 18:53:34 -08007072
7073 if (dspload_is_loaded(codec))
7074 dsp_reset(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007075}
7076
Connor McAdamse93ac302018-05-08 13:20:05 -04007077/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04007078 * This fixes a problem that was hard to reproduce. Very rarely, I would
7079 * boot up, and there would be no sound, but the DSP indicated it had loaded
7080 * properly. I did a few memory dumps to see if anything was different, and
7081 * there were a few areas of memory uninitialized with a1a2a3a4. This function
7082 * checks if those areas are uninitialized, and if they are, it'll attempt to
7083 * reload the card 3 times. Usually it fixes by the second.
7084 */
7085static void sbz_dsp_startup_check(struct hda_codec *codec)
7086{
7087 struct ca0132_spec *spec = codec->spec;
7088 unsigned int dsp_data_check[4];
7089 unsigned int cur_address = 0x390;
7090 unsigned int i;
7091 unsigned int failure = 0;
7092 unsigned int reload = 3;
7093
7094 if (spec->startup_check_entered)
7095 return;
7096
7097 spec->startup_check_entered = true;
7098
7099 for (i = 0; i < 4; i++) {
7100 chipio_read(codec, cur_address, &dsp_data_check[i]);
7101 cur_address += 0x4;
7102 }
7103 for (i = 0; i < 4; i++) {
7104 if (dsp_data_check[i] == 0xa1a2a3a4)
7105 failure = 1;
7106 }
7107
7108 codec_dbg(codec, "Startup Check: %d ", failure);
7109 if (failure)
7110 codec_info(codec, "DSP not initialized properly. Attempting to fix.");
7111 /*
7112 * While the failure condition is true, and we haven't reached our
7113 * three reload limit, continue trying to reload the driver and
7114 * fix the issue.
7115 */
7116 while (failure && (reload != 0)) {
7117 codec_info(codec, "Reloading... Tries left: %d", reload);
7118 sbz_exit_chip(codec);
7119 spec->dsp_state = DSP_DOWNLOAD_INIT;
7120 codec->patch_ops.init(codec);
7121 failure = 0;
7122 for (i = 0; i < 4; i++) {
7123 chipio_read(codec, cur_address, &dsp_data_check[i]);
7124 cur_address += 0x4;
7125 }
7126 for (i = 0; i < 4; i++) {
7127 if (dsp_data_check[i] == 0xa1a2a3a4)
7128 failure = 1;
7129 }
7130 reload--;
7131 }
7132
7133 if (!failure && reload < 3)
7134 codec_info(codec, "DSP fixed.");
7135
7136 if (!failure)
7137 return;
7138
7139 codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
7140}
7141
7142/*
Connor McAdamse93ac302018-05-08 13:20:05 -04007143 * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
7144 * extra precision for decibel values. If you had the dB value in floating point
7145 * you would take the value after the decimal point, multiply by 64, and divide
7146 * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
7147 * implement fixed point or floating point dB volumes. For now, I'll set them
7148 * to 0 just incase a value has lingered from a boot into Windows.
7149 */
7150static void ca0132_alt_vol_setup(struct hda_codec *codec)
7151{
7152 snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
7153 snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
7154 snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
7155 snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
7156 snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
7157 snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
7158 snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
7159 snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
7160}
7161
7162/*
7163 * Extra commands that don't really fit anywhere else.
7164 */
7165static void sbz_pre_dsp_setup(struct hda_codec *codec)
7166{
7167 struct ca0132_spec *spec = codec->spec;
7168
7169 writel(0x00820680, spec->mem_base + 0x01C);
7170 writel(0x00820680, spec->mem_base + 0x01C);
7171
Connor McAdamse93ac302018-05-08 13:20:05 -04007172 chipio_write(codec, 0x18b0a4, 0x000000c2);
7173
7174 snd_hda_codec_write(codec, 0x11, 0,
7175 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
7176}
7177
Connor McAdamse42c7c72018-08-08 13:34:18 -04007178static void r3d_pre_dsp_setup(struct hda_codec *codec)
7179{
Connor McAdamse42c7c72018-08-08 13:34:18 -04007180 chipio_write(codec, 0x18b0a4, 0x000000c2);
7181
7182 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7183 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
7184 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7185 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
7186 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7187 VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
7188
7189 snd_hda_codec_write(codec, 0x11, 0,
7190 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
7191}
7192
Connor McAdamse93ac302018-05-08 13:20:05 -04007193static void r3di_pre_dsp_setup(struct hda_codec *codec)
7194{
7195 chipio_write(codec, 0x18b0a4, 0x000000c2);
7196
7197 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7198 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
7199 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7200 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
7201 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7202 VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
7203
7204 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7205 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
7206 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7207 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
7208 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7209 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
7210 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7211 VENDOR_CHIPIO_8051_DATA_WRITE, 0x40);
7212
7213 snd_hda_codec_write(codec, 0x11, 0,
7214 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
7215}
7216
Connor McAdamse93ac302018-05-08 13:20:05 -04007217/*
7218 * These are sent before the DSP is downloaded. Not sure
7219 * what they do, or if they're necessary. Could possibly
7220 * be removed. Figure they're better to leave in.
7221 */
Connor McAdamse42c7c72018-08-08 13:34:18 -04007222static void ca0132_mmio_init(struct hda_codec *codec)
Connor McAdamse93ac302018-05-08 13:20:05 -04007223{
7224 struct ca0132_spec *spec = codec->spec;
7225
7226 writel(0x00000000, spec->mem_base + 0x400);
7227 writel(0x00000000, spec->mem_base + 0x408);
7228 writel(0x00000000, spec->mem_base + 0x40C);
7229 writel(0x00880680, spec->mem_base + 0x01C);
7230 writel(0x00000083, spec->mem_base + 0xC0C);
7231 writel(0x00000030, spec->mem_base + 0xC00);
7232 writel(0x00000000, spec->mem_base + 0xC04);
7233 writel(0x00000003, spec->mem_base + 0xC0C);
7234 writel(0x00000003, spec->mem_base + 0xC0C);
7235 writel(0x00000003, spec->mem_base + 0xC0C);
7236 writel(0x00000003, spec->mem_base + 0xC0C);
7237 writel(0x000000C1, spec->mem_base + 0xC08);
7238 writel(0x000000F1, spec->mem_base + 0xC08);
7239 writel(0x00000001, spec->mem_base + 0xC08);
7240 writel(0x000000C7, spec->mem_base + 0xC08);
7241 writel(0x000000C1, spec->mem_base + 0xC08);
7242 writel(0x00000080, spec->mem_base + 0xC04);
7243}
7244
7245/*
7246 * Extra init functions for alternative ca0132 codecs. Done
7247 * here so they don't clutter up the main ca0132_init function
7248 * anymore than they have to.
7249 */
7250static void ca0132_alt_init(struct hda_codec *codec)
7251{
7252 struct ca0132_spec *spec = codec->spec;
7253
7254 ca0132_alt_vol_setup(codec);
7255
7256 switch (spec->quirk) {
7257 case QUIRK_SBZ:
7258 codec_dbg(codec, "SBZ alt_init");
7259 ca0132_gpio_init(codec);
7260 sbz_pre_dsp_setup(codec);
7261 snd_hda_sequence_write(codec, spec->chip_init_verbs);
Connor McAdamse42c7c72018-08-08 13:34:18 -04007262 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
Connor McAdamse93ac302018-05-08 13:20:05 -04007263 break;
7264 case QUIRK_R3DI:
7265 codec_dbg(codec, "R3DI alt_init");
7266 ca0132_gpio_init(codec);
7267 ca0132_gpio_setup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007268 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
Connor McAdamse93ac302018-05-08 13:20:05 -04007269 r3di_pre_dsp_setup(codec);
7270 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7271 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
7272 break;
Connor McAdamse42c7c72018-08-08 13:34:18 -04007273 case QUIRK_R3D:
7274 r3d_pre_dsp_setup(codec);
7275 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7276 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
7277 break;
Connor McAdamse93ac302018-05-08 13:20:05 -04007278 }
7279}
7280
Ian Minett95c6e9c2011-06-15 15:35:17 -07007281static int ca0132_init(struct hda_codec *codec)
7282{
7283 struct ca0132_spec *spec = codec->spec;
7284 struct auto_pin_cfg *cfg = &spec->autocfg;
7285 int i;
Connor McAdamse93ac302018-05-08 13:20:05 -04007286 bool dsp_loaded;
7287
7288 /*
7289 * If the DSP is already downloaded, and init has been entered again,
7290 * there's only two reasons for it. One, the codec has awaken from a
7291 * suspended state, and in that case dspload_is_loaded will return
7292 * false, and the init will be ran again. The other reason it gets
7293 * re entered is on startup for some reason it triggers a suspend and
7294 * resume state. In this case, it will check if the DSP is downloaded,
7295 * and not run the init function again. For codecs using alt_functions,
7296 * it will check if the DSP is loaded properly.
7297 */
7298 if (spec->dsp_state == DSP_DOWNLOADED) {
7299 dsp_loaded = dspload_is_loaded(codec);
7300 if (!dsp_loaded) {
7301 spec->dsp_reload = true;
7302 spec->dsp_state = DSP_DOWNLOAD_INIT;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007303 } else {
7304 if (spec->quirk == QUIRK_SBZ)
7305 sbz_dsp_startup_check(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007306 return 0;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007307 }
Connor McAdamse93ac302018-05-08 13:20:05 -04007308 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07007309
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02007310 if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
7311 spec->dsp_state = DSP_DOWNLOAD_INIT;
Takashi Iwai4a8b89f2013-02-12 10:15:15 +01007312 spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
Ian Minett5aaca442012-12-20 18:53:34 -08007313
Connor McAdamse42c7c72018-08-08 13:34:18 -04007314 if (spec->use_pci_mmio)
7315 ca0132_mmio_init(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007316
Takashi Iwai664c7152015-04-08 11:43:14 +02007317 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08007318
Takashi Iwaif8fb1172014-09-11 15:53:26 +02007319 ca0132_init_unsol(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08007320 ca0132_init_params(codec);
7321 ca0132_init_flags(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007322
Ian Minett5aaca442012-12-20 18:53:34 -08007323 snd_hda_sequence_write(codec, spec->base_init_verbs);
Connor McAdamse93ac302018-05-08 13:20:05 -04007324
Alastair Bridgewater365c7f22018-06-15 21:56:17 -04007325 if (spec->use_alt_functions)
Connor McAdamse93ac302018-05-08 13:20:05 -04007326 ca0132_alt_init(codec);
7327
Ian Minett01ef7db2012-09-20 20:29:16 -07007328 ca0132_download_dsp(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007329
Ian Minett5aaca442012-12-20 18:53:34 -08007330 ca0132_refresh_widget_caps(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007331
Connor McAdams7e6ed622018-05-08 13:20:08 -04007332 switch (spec->quirk) {
7333 case QUIRK_R3DI:
Connor McAdamsc986f502018-08-08 13:34:19 -04007334 case QUIRK_R3D:
7335 r3d_setup_defaults(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007336 break;
Alastair Bridgewater126b75e2018-06-15 21:56:18 -04007337 case QUIRK_SBZ:
Connor McAdamsd97420d2018-08-08 13:34:13 -04007338 sbz_setup_defaults(codec);
Alastair Bridgewater126b75e2018-06-15 21:56:18 -04007339 break;
7340 default:
Connor McAdams38ba69f2018-05-08 13:20:07 -04007341 ca0132_setup_defaults(codec);
7342 ca0132_init_analog_mic2(codec);
7343 ca0132_init_dmic(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007344 break;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007345 }
Ian Minett01ef7db2012-09-20 20:29:16 -07007346
Ian Minett5aaca442012-12-20 18:53:34 -08007347 for (i = 0; i < spec->num_outputs; i++)
7348 init_output(codec, spec->out_pins[i], spec->dacs[0]);
7349
Ian Minett95c6e9c2011-06-15 15:35:17 -07007350 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
7351
7352 for (i = 0; i < spec->num_inputs; i++)
7353 init_input(codec, spec->input_pins[i], spec->adcs[i]);
7354
7355 init_input(codec, cfg->dig_in_pin, spec->dig_in);
7356
Connor McAdams009b8f92018-05-08 13:20:06 -04007357 if (!spec->use_alt_functions) {
Connor McAdamse93ac302018-05-08 13:20:05 -04007358 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7359 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7360 VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
7361 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7362 VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
7363 }
7364
Connor McAdams7cb9d942018-05-08 13:20:10 -04007365 if (spec->quirk == QUIRK_SBZ)
Connor McAdamse93ac302018-05-08 13:20:05 -04007366 ca0132_gpio_setup(codec);
7367
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007368 snd_hda_sequence_write(codec, spec->spec_init_verbs);
Connor McAdamsd97420d2018-08-08 13:34:13 -04007369 if (spec->use_alt_functions) {
Connor McAdams7cb9d942018-05-08 13:20:10 -04007370 ca0132_alt_select_out(codec);
7371 ca0132_alt_select_in(codec);
Connor McAdamsd97420d2018-08-08 13:34:13 -04007372 } else {
Connor McAdams7cb9d942018-05-08 13:20:10 -04007373 ca0132_select_out(codec);
7374 ca0132_select_mic(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04007375 }
Ian Minett5aaca442012-12-20 18:53:34 -08007376
Ian Minetta73d5112012-12-20 18:53:37 -08007377 snd_hda_jack_report_sync(codec);
7378
Connor McAdamse93ac302018-05-08 13:20:05 -04007379 /*
7380 * Re set the PlayEnhancement switch on a resume event, because the
7381 * controls will not be reloaded.
7382 */
7383 if (spec->dsp_reload) {
7384 spec->dsp_reload = false;
7385 ca0132_pe_switch_set(codec);
7386 }
7387
Takashi Iwai664c7152015-04-08 11:43:14 +02007388 snd_hda_power_down_pm(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007389
7390 return 0;
7391}
7392
Ian Minett95c6e9c2011-06-15 15:35:17 -07007393static void ca0132_free(struct hda_codec *codec)
7394{
Ian Minett5aaca442012-12-20 18:53:34 -08007395 struct ca0132_spec *spec = codec->spec;
7396
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007397 cancel_delayed_work_sync(&spec->unsol_hp_work);
Ian Minett5aaca442012-12-20 18:53:34 -08007398 snd_hda_power_up(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007399 switch (spec->quirk) {
7400 case QUIRK_SBZ:
7401 sbz_exit_chip(codec);
7402 break;
Connor McAdams2f295f92018-08-08 13:34:22 -04007403 case QUIRK_R3D:
7404 r3d_exit_chip(codec);
7405 break;
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007406 case QUIRK_R3DI:
7407 r3di_gpio_shutdown(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007408 break;
7409 }
Connor McAdams2f295f92018-08-08 13:34:22 -04007410
7411 snd_hda_sequence_write(codec, spec->base_exit_verbs);
7412 ca0132_exit_chip(codec);
7413
Ian Minett5aaca442012-12-20 18:53:34 -08007414 snd_hda_power_down(codec);
Connor McAdamsaa317042018-05-08 13:20:03 -04007415 if (spec->mem_base)
7416 iounmap(spec->mem_base);
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007417 kfree(spec->spec_init_verbs);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007418 kfree(codec->spec);
7419}
7420
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007421static void ca0132_reboot_notify(struct hda_codec *codec)
7422{
7423 codec->patch_ops.free(codec);
7424}
7425
Julia Lawall071f1342016-09-11 15:05:43 +02007426static const struct hda_codec_ops ca0132_patch_ops = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07007427 .build_controls = ca0132_build_controls,
7428 .build_pcms = ca0132_build_pcms,
7429 .init = ca0132_init,
7430 .free = ca0132_free,
Takashi Iwaif8fb1172014-09-11 15:53:26 +02007431 .unsol_event = snd_hda_jack_unsol_event,
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007432 .reboot_notify = ca0132_reboot_notify,
Ian Minett95c6e9c2011-06-15 15:35:17 -07007433};
7434
Ian Minett441aa6a2012-12-20 18:53:40 -08007435static void ca0132_config(struct hda_codec *codec)
7436{
7437 struct ca0132_spec *spec = codec->spec;
Ian Minett441aa6a2012-12-20 18:53:40 -08007438
7439 spec->dacs[0] = 0x2;
7440 spec->dacs[1] = 0x3;
7441 spec->dacs[2] = 0x4;
7442
7443 spec->multiout.dac_nids = spec->dacs;
7444 spec->multiout.num_dacs = 3;
Ian Minett441aa6a2012-12-20 18:53:40 -08007445
Connor McAdams009b8f92018-05-08 13:20:06 -04007446 if (!spec->use_alt_functions)
Connor McAdams63177af2018-05-08 13:20:02 -04007447 spec->multiout.max_channels = 2;
7448 else
7449 spec->multiout.max_channels = 6;
7450
7451 switch (spec->quirk) {
7452 case QUIRK_ALIENWARE:
Connor McAdamsd06feaf2018-09-18 14:33:31 -04007453 codec_dbg(codec, "%s: QUIRK_ALIENWARE applied.\n", __func__);
Takashi Iwaife14f392015-08-10 16:53:32 +02007454 snd_hda_apply_pincfgs(codec, alienware_pincfgs);
Connor McAdamsd06feaf2018-09-18 14:33:31 -04007455 break;
7456 case QUIRK_SBZ:
7457 codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
7458 snd_hda_apply_pincfgs(codec, sbz_pincfgs);
7459 break;
7460 case QUIRK_R3D:
7461 codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
7462 snd_hda_apply_pincfgs(codec, r3d_pincfgs);
7463 break;
7464 case QUIRK_R3DI:
7465 codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
7466 snd_hda_apply_pincfgs(codec, r3di_pincfgs);
7467 break;
7468 case QUIRK_AE5:
7469 codec_dbg(codec, "%s: QUIRK_AE5 applied.\n", __func__);
7470 snd_hda_apply_pincfgs(codec, r3di_pincfgs);
7471 break;
7472 }
Takashi Iwaife14f392015-08-10 16:53:32 +02007473
Connor McAdamsd06feaf2018-09-18 14:33:31 -04007474 switch (spec->quirk) {
7475 case QUIRK_ALIENWARE:
Takashi Iwaife14f392015-08-10 16:53:32 +02007476 spec->num_outputs = 2;
7477 spec->out_pins[0] = 0x0b; /* speaker out */
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007478 spec->out_pins[1] = 0x0f;
Takashi Iwaife14f392015-08-10 16:53:32 +02007479 spec->shared_out_nid = 0x2;
7480 spec->unsol_tag_hp = 0x0f;
7481
7482 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
7483 spec->adcs[1] = 0x8; /* analog mic2 */
7484 spec->adcs[2] = 0xa; /* what u hear */
7485
7486 spec->num_inputs = 3;
7487 spec->input_pins[0] = 0x12;
7488 spec->input_pins[1] = 0x11;
7489 spec->input_pins[2] = 0x13;
7490 spec->shared_mic_nid = 0x7;
7491 spec->unsol_tag_amic1 = 0x11;
Connor McAdams63177af2018-05-08 13:20:02 -04007492 break;
7493 case QUIRK_SBZ:
Connor McAdams7f73df92018-08-08 13:34:16 -04007494 case QUIRK_R3D:
Connor McAdams63177af2018-05-08 13:20:02 -04007495 spec->num_outputs = 2;
7496 spec->out_pins[0] = 0x0B; /* Line out */
7497 spec->out_pins[1] = 0x0F; /* Rear headphone out */
7498 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
7499 spec->out_pins[3] = 0x11; /* Rear surround */
7500 spec->shared_out_nid = 0x2;
7501 spec->unsol_tag_hp = spec->out_pins[1];
7502 spec->unsol_tag_front_hp = spec->out_pins[2];
7503
7504 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
7505 spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
7506 spec->adcs[2] = 0xa; /* what u hear */
7507
7508 spec->num_inputs = 2;
7509 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
7510 spec->input_pins[1] = 0x13; /* What U Hear */
7511 spec->shared_mic_nid = 0x7;
7512 spec->unsol_tag_amic1 = spec->input_pins[0];
7513
7514 /* SPDIF I/O */
7515 spec->dig_out = 0x05;
7516 spec->multiout.dig_out_nid = spec->dig_out;
Connor McAdams63177af2018-05-08 13:20:02 -04007517 spec->dig_in = 0x09;
Connor McAdams63177af2018-05-08 13:20:02 -04007518 break;
Connor McAdamsd06feaf2018-09-18 14:33:31 -04007519 case QUIRK_AE5:
7520 spec->num_outputs = 2;
7521 spec->out_pins[0] = 0x0B; /* Line out */
7522 spec->out_pins[1] = 0x11; /* Rear headphone out */
7523 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
7524 spec->out_pins[3] = 0x0F; /* Rear surround */
7525 spec->shared_out_nid = 0x2;
7526 spec->unsol_tag_hp = spec->out_pins[1];
7527 spec->unsol_tag_front_hp = spec->out_pins[2];
Connor McAdams63177af2018-05-08 13:20:02 -04007528
Connor McAdamsd06feaf2018-09-18 14:33:31 -04007529 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
7530 spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
7531 spec->adcs[2] = 0xa; /* what u hear */
7532
7533 spec->num_inputs = 2;
7534 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
7535 spec->input_pins[1] = 0x13; /* What U Hear */
7536 spec->shared_mic_nid = 0x7;
7537 spec->unsol_tag_amic1 = spec->input_pins[0];
7538
7539 /* SPDIF I/O */
7540 spec->dig_out = 0x05;
7541 spec->multiout.dig_out_nid = spec->dig_out;
7542 break;
7543 case QUIRK_R3DI:
Connor McAdams63177af2018-05-08 13:20:02 -04007544 spec->num_outputs = 2;
7545 spec->out_pins[0] = 0x0B; /* Line out */
7546 spec->out_pins[1] = 0x0F; /* Rear headphone out */
7547 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
7548 spec->out_pins[3] = 0x11; /* Rear surround */
7549 spec->shared_out_nid = 0x2;
7550 spec->unsol_tag_hp = spec->out_pins[1];
7551 spec->unsol_tag_front_hp = spec->out_pins[2];
7552
7553 spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
7554 spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
7555 spec->adcs[2] = 0x0a; /* what u hear */
7556
7557 spec->num_inputs = 2;
7558 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
7559 spec->input_pins[1] = 0x13; /* What U Hear */
7560 spec->shared_mic_nid = 0x7;
7561 spec->unsol_tag_amic1 = spec->input_pins[0];
7562
7563 /* SPDIF I/O */
7564 spec->dig_out = 0x05;
7565 spec->multiout.dig_out_nid = spec->dig_out;
Connor McAdams63177af2018-05-08 13:20:02 -04007566 break;
7567 default:
Takashi Iwaife14f392015-08-10 16:53:32 +02007568 spec->num_outputs = 2;
7569 spec->out_pins[0] = 0x0b; /* speaker out */
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007570 spec->out_pins[1] = 0x10; /* headphone out */
Takashi Iwaife14f392015-08-10 16:53:32 +02007571 spec->shared_out_nid = 0x2;
7572 spec->unsol_tag_hp = spec->out_pins[1];
7573
7574 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
7575 spec->adcs[1] = 0x8; /* analog mic2 */
7576 spec->adcs[2] = 0xa; /* what u hear */
7577
7578 spec->num_inputs = 3;
7579 spec->input_pins[0] = 0x12;
7580 spec->input_pins[1] = 0x11;
7581 spec->input_pins[2] = 0x13;
7582 spec->shared_mic_nid = 0x7;
7583 spec->unsol_tag_amic1 = spec->input_pins[0];
7584
7585 /* SPDIF I/O */
7586 spec->dig_out = 0x05;
7587 spec->multiout.dig_out_nid = spec->dig_out;
Takashi Iwaife14f392015-08-10 16:53:32 +02007588 spec->dig_in = 0x09;
Connor McAdams63177af2018-05-08 13:20:02 -04007589 break;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007590 }
Ian Minett441aa6a2012-12-20 18:53:40 -08007591}
7592
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007593static int ca0132_prepare_verbs(struct hda_codec *codec)
7594{
7595/* Verbs + terminator (an empty element) */
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007596#define NUM_SPEC_VERBS 2
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007597 struct ca0132_spec *spec = codec->spec;
7598
7599 spec->chip_init_verbs = ca0132_init_verbs0;
Connor McAdamse42c7c72018-08-08 13:34:18 -04007600 if (spec->quirk == QUIRK_SBZ || spec->quirk == QUIRK_R3D)
7601 spec->desktop_init_verbs = ca0132_init_verbs1;
Kees Cook6396bb22018-06-12 14:03:40 -07007602 spec->spec_init_verbs = kcalloc(NUM_SPEC_VERBS,
7603 sizeof(struct hda_verb),
7604 GFP_KERNEL);
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007605 if (!spec->spec_init_verbs)
7606 return -ENOMEM;
7607
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007608 /* config EAPD */
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007609 spec->spec_init_verbs[0].nid = 0x0b;
7610 spec->spec_init_verbs[0].param = 0x78D;
7611 spec->spec_init_verbs[0].verb = 0x00;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007612
7613 /* Previously commented configuration */
7614 /*
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007615 spec->spec_init_verbs[2].nid = 0x0b;
7616 spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
7617 spec->spec_init_verbs[2].verb = 0x02;
7618
7619 spec->spec_init_verbs[3].nid = 0x10;
7620 spec->spec_init_verbs[3].param = 0x78D;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007621 spec->spec_init_verbs[3].verb = 0x02;
7622
7623 spec->spec_init_verbs[4].nid = 0x10;
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007624 spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007625 spec->spec_init_verbs[4].verb = 0x02;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007626 */
7627
7628 /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
7629 return 0;
7630}
7631
Ian Minett95c6e9c2011-06-15 15:35:17 -07007632static int patch_ca0132(struct hda_codec *codec)
7633{
7634 struct ca0132_spec *spec;
Ian Minetta73d5112012-12-20 18:53:37 -08007635 int err;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007636 const struct snd_pci_quirk *quirk;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007637
Takashi Iwai4e76a882014-02-25 12:21:03 +01007638 codec_dbg(codec, "patch_ca0132\n");
Ian Minett95c6e9c2011-06-15 15:35:17 -07007639
7640 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
7641 if (!spec)
7642 return -ENOMEM;
7643 codec->spec = spec;
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007644 spec->codec = codec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007645
Takashi Iwai225068a2015-05-29 10:42:14 +02007646 codec->patch_ops = ca0132_patch_ops;
7647 codec->pcm_format_first = 1;
7648 codec->no_sticky_stream = 1;
7649
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007650 /* Detect codec quirk */
7651 quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
7652 if (quirk)
7653 spec->quirk = quirk->value;
7654 else
7655 spec->quirk = QUIRK_NONE;
7656
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02007657 spec->dsp_state = DSP_DOWNLOAD_INIT;
Ian Minetta7e76272012-12-20 18:53:35 -08007658 spec->num_mixers = 1;
Connor McAdams017310f2018-05-08 13:20:11 -04007659
7660 /* Set which mixers each quirk uses. */
7661 switch (spec->quirk) {
7662 case QUIRK_SBZ:
Connor McAdamse25e3442018-08-08 13:34:21 -04007663 spec->mixers[0] = desktop_mixer;
Connor McAdams017310f2018-05-08 13:20:11 -04007664 snd_hda_codec_set_name(codec, "Sound Blaster Z");
7665 break;
Connor McAdamse25e3442018-08-08 13:34:21 -04007666 case QUIRK_R3D:
7667 spec->mixers[0] = desktop_mixer;
7668 snd_hda_codec_set_name(codec, "Recon3D");
7669 break;
Connor McAdams017310f2018-05-08 13:20:11 -04007670 case QUIRK_R3DI:
7671 spec->mixers[0] = r3di_mixer;
7672 snd_hda_codec_set_name(codec, "Recon3Di");
7673 break;
7674 default:
7675 spec->mixers[0] = ca0132_mixer;
7676 break;
7677 }
Ian Minetta7e76272012-12-20 18:53:35 -08007678
Connor McAdams08eca6b2018-08-08 13:34:17 -04007679 /* Setup whether or not to use alt functions/controls/pci_mmio */
Connor McAdams009b8f92018-05-08 13:20:06 -04007680 switch (spec->quirk) {
7681 case QUIRK_SBZ:
Connor McAdamse42c7c72018-08-08 13:34:18 -04007682 case QUIRK_R3D:
Connor McAdams08eca6b2018-08-08 13:34:17 -04007683 spec->use_alt_controls = true;
7684 spec->use_alt_functions = true;
7685 spec->use_pci_mmio = true;
7686 break;
Connor McAdams009b8f92018-05-08 13:20:06 -04007687 case QUIRK_R3DI:
Connor McAdams47cdf762018-05-08 13:20:13 -04007688 spec->use_alt_controls = true;
Connor McAdams009b8f92018-05-08 13:20:06 -04007689 spec->use_alt_functions = true;
Connor McAdams08eca6b2018-08-08 13:34:17 -04007690 spec->use_pci_mmio = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007691 break;
7692 default:
Connor McAdams47cdf762018-05-08 13:20:13 -04007693 spec->use_alt_controls = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007694 spec->use_alt_functions = false;
Connor McAdams08eca6b2018-08-08 13:34:17 -04007695 spec->use_pci_mmio = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007696 break;
7697 }
7698
Connor McAdams08eca6b2018-08-08 13:34:17 -04007699 if (spec->use_pci_mmio) {
7700 spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
7701 if (spec->mem_base == NULL) {
7702 codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
7703 spec->quirk = QUIRK_NONE;
7704 }
7705 }
7706
Ian Minett5aaca442012-12-20 18:53:34 -08007707 spec->base_init_verbs = ca0132_base_init_verbs;
7708 spec->base_exit_verbs = ca0132_base_exit_verbs;
Ian Minett5aaca442012-12-20 18:53:34 -08007709
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007710 INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
7711
Ian Minett95c6e9c2011-06-15 15:35:17 -07007712 ca0132_init_chip(codec);
7713
7714 ca0132_config(codec);
7715
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007716 err = ca0132_prepare_verbs(codec);
7717 if (err < 0)
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007718 goto error;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007719
Ian Minetta73d5112012-12-20 18:53:37 -08007720 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
7721 if (err < 0)
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007722 goto error;
Ian Minetta73d5112012-12-20 18:53:37 -08007723
Ian Minett95c6e9c2011-06-15 15:35:17 -07007724 return 0;
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007725
7726 error:
7727 ca0132_free(codec);
7728 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007729}
7730
7731/*
7732 * patch entries
7733 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007734static struct hda_device_id snd_hda_id_ca0132[] = {
7735 HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
Ian Minett95c6e9c2011-06-15 15:35:17 -07007736 {} /* terminator */
7737};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007738MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007739
7740MODULE_LICENSE("GPL");
Ian Minett406261c2012-12-20 18:53:41 -08007741MODULE_DESCRIPTION("Creative Sound Core3D codec");
Ian Minett95c6e9c2011-06-15 15:35:17 -07007742
Takashi Iwaid8a766a2015-02-17 15:25:37 +01007743static struct hda_codec_driver ca0132_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007744 .id = snd_hda_id_ca0132,
Ian Minett95c6e9c2011-06-15 15:35:17 -07007745};
7746
Takashi Iwaid8a766a2015-02-17 15:25:37 +01007747module_hda_codec_driver(ca0132_driver);