source: GPL/trunk/alsa-kernel/pci/hda/patch_cmedia.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

File size: 10.9 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Universal Interface for Intel High Definition Audio Codec
4 *
5 * HD audio interface patch for C-Media CMI9880
6 *
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 */
9
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/module.h>
13#include <sound/core.h>
14#include <sound/hda_codec.h>
15#include "hda_local.h"
16#include "hda_auto_parser.h"
17#include "hda_jack.h"
18#include "hda_generic.h"
19
20#ifdef TARGET_OS2
21#define KBUILD_MODNAME "patch_cmedia"
22#endif
23
24/* CM9825 Offset Definitions */
25
26#define CM9825_VERB_SET_HPF_1 0x781
27#define CM9825_VERB_SET_HPF_2 0x785
28#define CM9825_VERB_SET_PLL 0x7a0
29#define CM9825_VERB_SET_NEG 0x7a1
30#define CM9825_VERB_SET_ADCL 0x7a2
31#define CM9825_VERB_SET_DACL 0x7a3
32#define CM9825_VERB_SET_MBIAS 0x7a4
33#define CM9825_VERB_SET_VNEG 0x7a8
34#define CM9825_VERB_SET_D2S 0x7a9
35#define CM9825_VERB_SET_DACTRL 0x7aa
36#define CM9825_VERB_SET_PDNEG 0x7ac
37#define CM9825_VERB_SET_VDO 0x7ad
38#define CM9825_VERB_SET_CDALR 0x7b0
39#define CM9825_VERB_SET_MTCBA 0x7b1
40#define CM9825_VERB_SET_OTP 0x7b2
41#define CM9825_VERB_SET_OCP 0x7b3
42#define CM9825_VERB_SET_GAD 0x7b4
43#define CM9825_VERB_SET_TMOD 0x7b5
44#define CM9825_VERB_SET_SNR 0x7b6
45
46struct cmi_spec {
47 struct hda_gen_spec gen;
48 const struct hda_verb *chip_d0_verbs;
49 const struct hda_verb *chip_d3_verbs;
50 const struct hda_verb *chip_hp_present_verbs;
51 const struct hda_verb *chip_hp_remove_verbs;
52 struct hda_codec *codec;
53 struct delayed_work unsol_hp_work;
54 int quirk;
55};
56
57static const struct hda_verb cm9825_std_d3_verbs[] = {
58 /* chip sleep verbs */
59 {0x43, CM9825_VERB_SET_D2S, 0x62}, /* depop */
60 {0x43, CM9825_VERB_SET_PLL, 0x01}, /* PLL set */
61 {0x43, CM9825_VERB_SET_NEG, 0xc2}, /* NEG set */
62 {0x43, CM9825_VERB_SET_ADCL, 0x00}, /* ADC */
63 {0x43, CM9825_VERB_SET_DACL, 0x02}, /* DACL */
64 {0x43, CM9825_VERB_SET_VNEG, 0x50}, /* VOL NEG */
65 {0x43, CM9825_VERB_SET_MBIAS, 0x00}, /* MBIAS */
66 {0x43, CM9825_VERB_SET_PDNEG, 0x04}, /* SEL OSC */
67 {0x43, CM9825_VERB_SET_CDALR, 0xf6}, /* Class D */
68 {0x43, CM9825_VERB_SET_OTP, 0xcd}, /* OTP set */
69 {0}
70};
71
72static const struct hda_verb cm9825_std_d0_verbs[] = {
73 /* chip init verbs */
74 {0x34, AC_VERB_SET_EAPD_BTLENABLE, 0x02}, /* EAPD set */
75 {0x43, CM9825_VERB_SET_SNR, 0x30}, /* SNR set */
76 {0x43, CM9825_VERB_SET_PLL, 0x00}, /* PLL set */
77 {0x43, CM9825_VERB_SET_ADCL, 0x00}, /* ADC */
78 {0x43, CM9825_VERB_SET_DACL, 0x02}, /* DACL */
79 {0x43, CM9825_VERB_SET_MBIAS, 0x00}, /* MBIAS */
80 {0x43, CM9825_VERB_SET_VNEG, 0x56}, /* VOL NEG */
81 {0x43, CM9825_VERB_SET_D2S, 0x62}, /* depop */
82 {0x43, CM9825_VERB_SET_DACTRL, 0x00}, /* DACTRL set */
83 {0x43, CM9825_VERB_SET_PDNEG, 0x0c}, /* SEL OSC */
84 {0x43, CM9825_VERB_SET_VDO, 0x80}, /* VDO set */
85 {0x43, CM9825_VERB_SET_CDALR, 0xf4}, /* Class D */
86 {0x43, CM9825_VERB_SET_OTP, 0xcd}, /* OTP set */
87 {0x43, CM9825_VERB_SET_MTCBA, 0x61}, /* SR set */
88 {0x43, CM9825_VERB_SET_OCP, 0x33}, /* OTP set */
89 {0x43, CM9825_VERB_SET_GAD, 0x07}, /* ADC -3db */
90 {0x43, CM9825_VERB_SET_TMOD, 0x26}, /* Class D clk */
91 {0x3C, AC_VERB_SET_AMP_GAIN_MUTE |
92 AC_AMP_SET_OUTPUT | AC_AMP_SET_RIGHT, 0x2d}, /* Gain set */
93 {0x3C, AC_VERB_SET_AMP_GAIN_MUTE |
94 AC_AMP_SET_OUTPUT | AC_AMP_SET_LEFT, 0x2d}, /* Gain set */
95 {0x43, CM9825_VERB_SET_HPF_1, 0x40}, /* HPF set */
96 {0x43, CM9825_VERB_SET_HPF_2, 0x40}, /* HPF set */
97 {0}
98};
99
100static const struct hda_verb cm9825_hp_present_verbs[] = {
101 {0x42, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00}, /* PIN off */
102 {0x43, CM9825_VERB_SET_ADCL, 0x88}, /* ADC */
103 {0x43, CM9825_VERB_SET_DACL, 0xaa}, /* DACL */
104 {0x43, CM9825_VERB_SET_MBIAS, 0x10}, /* MBIAS */
105 {0x43, CM9825_VERB_SET_D2S, 0xf2}, /* depop */
106 {0x43, CM9825_VERB_SET_DACTRL, 0x00}, /* DACTRL set */
107 {0x43, CM9825_VERB_SET_VDO, 0xc4}, /* VDO set */
108 {0}
109};
110
111static const struct hda_verb cm9825_hp_remove_verbs[] = {
112 {0x43, CM9825_VERB_SET_ADCL, 0x00}, /* ADC */
113 {0x43, CM9825_VERB_SET_DACL, 0x56}, /* DACL */
114 {0x43, CM9825_VERB_SET_MBIAS, 0x00}, /* MBIAS */
115 {0x43, CM9825_VERB_SET_D2S, 0x62}, /* depop */
116 {0x43, CM9825_VERB_SET_DACTRL, 0xe0}, /* DACTRL set */
117 {0x43, CM9825_VERB_SET_VDO, 0x80}, /* VDO set */
118 {0x42, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40}, /* PIN on */
119 {0}
120};
121
122static void cm9825_unsol_hp_delayed(struct work_struct *work)
123{
124 struct cmi_spec *spec =
125 container_of(to_delayed_work(work), struct cmi_spec, unsol_hp_work);
126 struct hda_jack_tbl *jack;
127 hda_nid_t hp_pin = spec->gen.autocfg.hp_pins[0];
128 bool hp_jack_plugin = false;
129 int err = 0;
130
131 hp_jack_plugin = snd_hda_jack_detect(spec->codec, hp_pin);
132
133 codec_dbg(spec->codec, "hp_jack_plugin %d, hp_pin 0x%X\n",
134 (int)hp_jack_plugin, hp_pin);
135
136 if (!hp_jack_plugin) {
137 err =
138 snd_hda_codec_write(spec->codec, 0x42, 0,
139 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
140 if (err)
141 codec_dbg(spec->codec, "codec_write err %d\n", err);
142
143 snd_hda_sequence_write(spec->codec, spec->chip_hp_remove_verbs);
144 } else {
145 snd_hda_sequence_write(spec->codec,
146 spec->chip_hp_present_verbs);
147 }
148
149 jack = snd_hda_jack_tbl_get(spec->codec, hp_pin);
150 if (jack) {
151 jack->block_report = 0;
152 snd_hda_jack_report_sync(spec->codec);
153 }
154}
155
156static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
157{
158 struct cmi_spec *spec = codec->spec;
159 struct hda_jack_tbl *tbl;
160
161 /* Delay enabling the HP amp, to let the mic-detection
162 * state machine run.
163 */
164
165 codec_dbg(spec->codec, "cb->nid 0x%X\n", cb->nid);
166
167 tbl = snd_hda_jack_tbl_get(codec, cb->nid);
168 if (tbl)
169 tbl->block_report = 1;
170 schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(200));
171}
172
173static void cm9825_setup_unsol(struct hda_codec *codec)
174{
175 struct cmi_spec *spec = codec->spec;
176
177 hda_nid_t hp_pin = spec->gen.autocfg.hp_pins[0];
178
179 snd_hda_jack_detect_enable_callback(codec, hp_pin, hp_callback);
180}
181
182static int cm9825_init(struct hda_codec *codec)
183{
184 snd_hda_gen_init(codec);
185 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
186
187 return 0;
188}
189
190static void cm9825_free(struct hda_codec *codec)
191{
192 struct cmi_spec *spec = codec->spec;
193
194 cancel_delayed_work_sync(&spec->unsol_hp_work);
195 snd_hda_gen_free(codec);
196}
197
198static int cm9825_suspend(struct hda_codec *codec)
199{
200 struct cmi_spec *spec = codec->spec;
201
202 cancel_delayed_work_sync(&spec->unsol_hp_work);
203
204 snd_hda_sequence_write(codec, spec->chip_d3_verbs);
205
206 return 0;
207}
208
209static int cm9825_resume(struct hda_codec *codec)
210{
211 struct cmi_spec *spec = codec->spec;
212 hda_nid_t hp_pin = 0;
213 bool hp_jack_plugin = false;
214 int err;
215
216 err =
217 snd_hda_codec_write(spec->codec, 0x42, 0,
218 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
219 if (err)
220 codec_dbg(codec, "codec_write err %d\n", err);
221
222 msleep(150); /* for depop noise */
223
224 codec->patch_ops.init(codec);
225
226 hp_pin = spec->gen.autocfg.hp_pins[0];
227 hp_jack_plugin = snd_hda_jack_detect(spec->codec, hp_pin);
228
229 codec_dbg(spec->codec, "hp_jack_plugin %d, hp_pin 0x%X\n",
230 (int)hp_jack_plugin, hp_pin);
231
232 if (!hp_jack_plugin) {
233 err =
234 snd_hda_codec_write(spec->codec, 0x42, 0,
235 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
236
237 if (err)
238 codec_dbg(codec, "codec_write err %d\n", err);
239
240 snd_hda_sequence_write(codec, cm9825_hp_remove_verbs);
241 }
242
243 snd_hda_regmap_sync(codec);
244 hda_call_check_power_status(codec, 0x01);
245
246 return 0;
247}
248
249/*
250 * stuff for auto-parser
251 */
252static const struct hda_codec_ops cmi_auto_patch_ops = {
253 .build_controls = snd_hda_gen_build_controls,
254 .build_pcms = snd_hda_gen_build_pcms,
255 .init = snd_hda_gen_init,
256 .free = snd_hda_gen_free,
257 .unsol_event = snd_hda_jack_unsol_event,
258};
259
260static int patch_cm9825(struct hda_codec *codec)
261{
262 struct cmi_spec *spec;
263 struct auto_pin_cfg *cfg;
264 int err;
265
266 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
267 if (spec == NULL)
268 return -ENOMEM;
269
270 INIT_DELAYED_WORK(&spec->unsol_hp_work, cm9825_unsol_hp_delayed);
271 codec->spec = spec;
272 spec->codec = codec;
273 codec->patch_ops = cmi_auto_patch_ops;
274 codec->patch_ops.init = cm9825_init;
275 codec->patch_ops.suspend = cm9825_suspend;
276 codec->patch_ops.resume = cm9825_resume;
277 codec->patch_ops.free = cm9825_free;
278 codec->patch_ops.check_power_status = snd_hda_gen_check_power_status;
279 cfg = &spec->gen.autocfg;
280 snd_hda_gen_spec_init(&spec->gen);
281 spec->chip_d0_verbs = cm9825_std_d0_verbs;
282 spec->chip_d3_verbs = cm9825_std_d3_verbs;
283 spec->chip_hp_present_verbs = cm9825_hp_present_verbs;
284 spec->chip_hp_remove_verbs = cm9825_hp_remove_verbs;
285
286 snd_hda_sequence_write(codec, spec->chip_d0_verbs);
287
288 err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
289 if (err < 0)
290 goto error;
291 err = snd_hda_gen_parse_auto_config(codec, cfg);
292 if (err < 0)
293 goto error;
294
295 cm9825_setup_unsol(codec);
296
297 return 0;
298
299 error:
300 cm9825_free(codec);
301
302 codec_info(codec, "Enter err %d\n", err);
303
304 return err;
305}
306
307static int patch_cmi9880(struct hda_codec *codec)
308{
309 struct cmi_spec *spec;
310 struct auto_pin_cfg *cfg;
311 int err;
312
313 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
314 if (spec == NULL)
315 return -ENOMEM;
316
317 codec->spec = spec;
318 codec->patch_ops = cmi_auto_patch_ops;
319 cfg = &spec->gen.autocfg;
320 snd_hda_gen_spec_init(&spec->gen);
321
322 err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
323 if (err < 0)
324 goto error;
325 err = snd_hda_gen_parse_auto_config(codec, cfg);
326 if (err < 0)
327 goto error;
328
329 return 0;
330
331 error:
332 snd_hda_gen_free(codec);
333 return err;
334}
335
336static int patch_cmi8888(struct hda_codec *codec)
337{
338 struct cmi_spec *spec;
339 struct auto_pin_cfg *cfg;
340 int err;
341
342 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
343 if (!spec)
344 return -ENOMEM;
345
346 codec->spec = spec;
347 codec->patch_ops = cmi_auto_patch_ops;
348 cfg = &spec->gen.autocfg;
349 snd_hda_gen_spec_init(&spec->gen);
350
351 /* mask NID 0x10 from the playback volume selection;
352 * it's a headphone boost volume handled manually below
353 */
354 spec->gen.out_vol_mask = (1ULL << 0x10);
355
356 err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0);
357 if (err < 0)
358 goto error;
359 err = snd_hda_gen_parse_auto_config(codec, cfg);
360 if (err < 0)
361 goto error;
362
363 if (get_defcfg_device(snd_hda_codec_get_pincfg(codec, 0x10)) ==
364 AC_JACK_HP_OUT) {
365 static const struct snd_kcontrol_new amp_kctl =
366 HDA_CODEC_VOLUME("Headphone Amp Playback Volume",
367 0x10, 0, HDA_OUTPUT);
368 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &amp_kctl)) {
369 err = -ENOMEM;
370 goto error;
371 }
372 }
373
374 return 0;
375
376 error:
377 snd_hda_gen_free(codec);
378 return err;
379}
380
381/*
382 * patch entries
383 */
384static const struct hda_device_id snd_hda_id_cmedia[] = {
385 HDA_CODEC_ENTRY(0x13f68888, "CMI8888", patch_cmi8888),
386 HDA_CODEC_ENTRY(0x13f69880, "CMI9880", patch_cmi9880),
387 HDA_CODEC_ENTRY(0x434d4980, "CMI9880", patch_cmi9880),
388 HDA_CODEC_ENTRY(0x13f69825, "CM9825", patch_cm9825),
389 {0} /* terminator */
390};
391MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cmedia);
392
393MODULE_LICENSE("GPL");
394MODULE_DESCRIPTION("C-Media HD-audio codec");
395
396static struct hda_codec_driver cmedia_driver = {
397 .id = snd_hda_id_cmedia,
398};
399
400module_hda_codec_driver(cmedia_driver);
Note: See TracBrowser for help on using the repository browser.