patch_cirrus.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * HD audio interface patch for Cirrus Logic CS420x chip
  3. *
  4. * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This driver is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This driver is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <sound/core.h>
  25. #include "hda_codec.h"
  26. #include "hda_local.h"
  27. /*
  28. */
  29. struct cs_spec {
  30. struct auto_pin_cfg autocfg;
  31. struct hda_multi_out multiout;
  32. struct snd_kcontrol *vmaster_sw;
  33. struct snd_kcontrol *vmaster_vol;
  34. hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
  35. hda_nid_t slave_dig_outs[2];
  36. unsigned int input_idx[AUTO_PIN_LAST];
  37. unsigned int capsrc_idx[AUTO_PIN_LAST];
  38. hda_nid_t adc_nid[AUTO_PIN_LAST];
  39. unsigned int adc_idx[AUTO_PIN_LAST];
  40. unsigned int num_inputs;
  41. unsigned int cur_input;
  42. unsigned int automic_idx;
  43. hda_nid_t cur_adc;
  44. unsigned int cur_adc_stream_tag;
  45. unsigned int cur_adc_format;
  46. hda_nid_t dig_in;
  47. struct hda_bind_ctls *capture_bind[2];
  48. struct hda_pcm pcm_rec[2]; /* PCM information */
  49. unsigned int hp_detect:1;
  50. unsigned int mic_detect:1;
  51. };
  52. /* Vendor-specific processing widget */
  53. #define CS420X_VENDOR_NID 0x11
  54. #define CS_DIG_OUT1_PIN_NID 0x10
  55. #define CS_DIG_OUT2_PIN_NID 0x15
  56. #define CS_DMIC1_PIN_NID 0x12
  57. #define CS_DMIC2_PIN_NID 0x0e
  58. /* coef indices */
  59. #define IDX_SPDIF_STAT 0x0000
  60. #define IDX_SPDIF_CTL 0x0001
  61. #define IDX_ADC_CFG 0x0002
  62. /* SZC bitmask, 4 modes below:
  63. * 0 = immediate,
  64. * 1 = digital immediate, analog zero-cross
  65. * 2 = digtail & analog soft-ramp
  66. * 3 = digital soft-ramp, analog zero-cross
  67. */
  68. #define CS_COEF_ADC_SZC_MASK (3 << 0)
  69. #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
  70. #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
  71. /* PGA mode: 0 = differential, 1 = signle-ended */
  72. #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
  73. #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
  74. #define IDX_DAC_CFG 0x0003
  75. /* SZC bitmask, 4 modes below:
  76. * 0 = Immediate
  77. * 1 = zero-cross
  78. * 2 = soft-ramp
  79. * 3 = soft-ramp on zero-cross
  80. */
  81. #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
  82. #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
  83. #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
  84. #define IDX_BEEP_CFG 0x0004
  85. /* 0x0008 - test reg key */
  86. /* 0x0009 - 0x0014 -> 12 test regs */
  87. /* 0x0015 - visibility reg */
  88. static int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
  89. {
  90. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  91. AC_VERB_SET_COEF_INDEX, idx);
  92. return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
  93. AC_VERB_GET_PROC_COEF, 0);
  94. }
  95. static void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  96. unsigned int coef)
  97. {
  98. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  99. AC_VERB_SET_COEF_INDEX, idx);
  100. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  101. AC_VERB_SET_PROC_COEF, coef);
  102. }
  103. #define HP_EVENT 1
  104. #define MIC_EVENT 2
  105. /*
  106. * PCM callbacks
  107. */
  108. static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
  109. struct hda_codec *codec,
  110. struct snd_pcm_substream *substream)
  111. {
  112. struct cs_spec *spec = codec->spec;
  113. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  114. hinfo);
  115. }
  116. static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  117. struct hda_codec *codec,
  118. unsigned int stream_tag,
  119. unsigned int format,
  120. struct snd_pcm_substream *substream)
  121. {
  122. struct cs_spec *spec = codec->spec;
  123. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  124. stream_tag, format, substream);
  125. }
  126. static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  127. struct hda_codec *codec,
  128. struct snd_pcm_substream *substream)
  129. {
  130. struct cs_spec *spec = codec->spec;
  131. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  132. }
  133. /*
  134. * Digital out
  135. */
  136. static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  137. struct hda_codec *codec,
  138. struct snd_pcm_substream *substream)
  139. {
  140. struct cs_spec *spec = codec->spec;
  141. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  142. }
  143. static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  144. struct hda_codec *codec,
  145. struct snd_pcm_substream *substream)
  146. {
  147. struct cs_spec *spec = codec->spec;
  148. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  149. }
  150. static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  151. struct hda_codec *codec,
  152. unsigned int stream_tag,
  153. unsigned int format,
  154. struct snd_pcm_substream *substream)
  155. {
  156. struct cs_spec *spec = codec->spec;
  157. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  158. format, substream);
  159. }
  160. static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  161. struct hda_codec *codec,
  162. struct snd_pcm_substream *substream)
  163. {
  164. struct cs_spec *spec = codec->spec;
  165. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  166. }
  167. /*
  168. * Analog capture
  169. */
  170. static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  171. struct hda_codec *codec,
  172. unsigned int stream_tag,
  173. unsigned int format,
  174. struct snd_pcm_substream *substream)
  175. {
  176. struct cs_spec *spec = codec->spec;
  177. spec->cur_adc = spec->adc_nid[spec->cur_input];
  178. spec->cur_adc_stream_tag = stream_tag;
  179. spec->cur_adc_format = format;
  180. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  181. return 0;
  182. }
  183. static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  184. struct hda_codec *codec,
  185. struct snd_pcm_substream *substream)
  186. {
  187. struct cs_spec *spec = codec->spec;
  188. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  189. spec->cur_adc = 0;
  190. return 0;
  191. }
  192. /*
  193. */
  194. static struct hda_pcm_stream cs_pcm_analog_playback = {
  195. .substreams = 1,
  196. .channels_min = 2,
  197. .channels_max = 2,
  198. .ops = {
  199. .open = cs_playback_pcm_open,
  200. .prepare = cs_playback_pcm_prepare,
  201. .cleanup = cs_playback_pcm_cleanup
  202. },
  203. };
  204. static struct hda_pcm_stream cs_pcm_analog_capture = {
  205. .substreams = 1,
  206. .channels_min = 2,
  207. .channels_max = 2,
  208. .ops = {
  209. .prepare = cs_capture_pcm_prepare,
  210. .cleanup = cs_capture_pcm_cleanup
  211. },
  212. };
  213. static struct hda_pcm_stream cs_pcm_digital_playback = {
  214. .substreams = 1,
  215. .channels_min = 2,
  216. .channels_max = 2,
  217. .ops = {
  218. .open = cs_dig_playback_pcm_open,
  219. .close = cs_dig_playback_pcm_close,
  220. .prepare = cs_dig_playback_pcm_prepare,
  221. .cleanup = cs_dig_playback_pcm_cleanup
  222. },
  223. };
  224. static struct hda_pcm_stream cs_pcm_digital_capture = {
  225. .substreams = 1,
  226. .channels_min = 2,
  227. .channels_max = 2,
  228. };
  229. static int cs_build_pcms(struct hda_codec *codec)
  230. {
  231. struct cs_spec *spec = codec->spec;
  232. struct hda_pcm *info = spec->pcm_rec;
  233. codec->pcm_info = info;
  234. codec->num_pcms = 0;
  235. info->name = "Cirrus Analog";
  236. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
  237. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
  238. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  239. spec->multiout.max_channels;
  240. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
  241. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  242. spec->adc_nid[spec->cur_input];
  243. codec->num_pcms++;
  244. if (!spec->multiout.dig_out_nid && !spec->dig_in)
  245. return 0;
  246. info++;
  247. info->name = "Cirrus Digital";
  248. info->pcm_type = spec->autocfg.dig_out_type[0];
  249. if (!info->pcm_type)
  250. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  251. if (spec->multiout.dig_out_nid) {
  252. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  253. cs_pcm_digital_playback;
  254. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  255. spec->multiout.dig_out_nid;
  256. }
  257. if (spec->dig_in) {
  258. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  259. cs_pcm_digital_capture;
  260. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  261. }
  262. codec->num_pcms++;
  263. return 0;
  264. }
  265. /*
  266. * parse codec topology
  267. */
  268. static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
  269. {
  270. hda_nid_t dac;
  271. if (!pin)
  272. return 0;
  273. if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
  274. return 0;
  275. return dac;
  276. }
  277. static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
  278. {
  279. struct cs_spec *spec = codec->spec;
  280. struct auto_pin_cfg *cfg = &spec->autocfg;
  281. hda_nid_t pin = cfg->input_pins[idx];
  282. unsigned int val = snd_hda_query_pin_caps(codec, pin);
  283. if (!(val & AC_PINCAP_PRES_DETECT))
  284. return 0;
  285. val = snd_hda_codec_get_pincfg(codec, pin);
  286. return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
  287. }
  288. static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
  289. unsigned int *idxp)
  290. {
  291. int i;
  292. hda_nid_t nid;
  293. nid = codec->start_nid;
  294. for (i = 0; i < codec->num_nodes; i++, nid++) {
  295. hda_nid_t pins[2];
  296. unsigned int type;
  297. int j, nums;
  298. type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
  299. >> AC_WCAP_TYPE_SHIFT;
  300. if (type != AC_WID_AUD_IN)
  301. continue;
  302. nums = snd_hda_get_connections(codec, nid, pins,
  303. ARRAY_SIZE(pins));
  304. if (nums <= 0)
  305. continue;
  306. for (j = 0; j < nums; j++) {
  307. if (pins[j] == pin) {
  308. *idxp = j;
  309. return nid;
  310. }
  311. }
  312. }
  313. return 0;
  314. }
  315. static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
  316. {
  317. struct cs_spec *spec = codec->spec;
  318. unsigned int val;
  319. val = snd_hda_codec_get_pincfg(codec, nid);
  320. return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
  321. }
  322. static int parse_output(struct hda_codec *codec)
  323. {
  324. struct cs_spec *spec = codec->spec;
  325. struct auto_pin_cfg *cfg = &spec->autocfg;
  326. int i, err, extra_nids;
  327. hda_nid_t dac;
  328. for (i = 0; i < cfg->line_outs; i++) {
  329. dac = get_dac(codec, cfg->line_out_pins[i]);
  330. if (!dac)
  331. break;
  332. spec->dac_nid[i] = dac;
  333. }
  334. spec->multiout.num_dacs = i;
  335. spec->multiout.dac_nids = spec->dac_nid;
  336. spec->multiout.max_channels = i * 2;
  337. /* add HP and speakers */
  338. extra_nids = 0;
  339. for (i = 0; i < cfg->hp_outs; i++) {
  340. dac = get_dac(codec, cfg->hp_pins[i]);
  341. if (!dac)
  342. break;
  343. if (!i)
  344. spec->multiout.hp_nid = dac;
  345. else
  346. spec->multiout.extra_out_nid[extra_nids++] = dac;
  347. }
  348. for (i = 0; i < cfg->speaker_outs; i++) {
  349. dac = get_dac(codec, cfg->speaker_pins[i]);
  350. if (!dac)
  351. break;
  352. spec->multiout.extra_out_nid[extra_nids++] = dac;
  353. }
  354. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  355. cfg->speaker_outs = cfg->line_outs;
  356. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  357. sizeof(cfg->speaker_pins));
  358. cfg->line_outs = 0;
  359. }
  360. return 0;
  361. }
  362. static int parse_input(struct hda_codec *codec)
  363. {
  364. struct cs_spec *spec = codec->spec;
  365. struct auto_pin_cfg *cfg = &spec->autocfg;
  366. int i, n, err;
  367. for (i = 0; i < AUTO_PIN_LAST; i++) {
  368. hda_nid_t pin = cfg->input_pins[i];
  369. struct snd_kcontrol *kctl;
  370. if (!pin)
  371. continue;
  372. spec->input_idx[spec->num_inputs] = i;
  373. spec->capsrc_idx[i] = spec->num_inputs++;
  374. spec->cur_input = i;
  375. spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
  376. }
  377. if (!spec->num_inputs)
  378. return 0;
  379. /* check whether the automatic mic switch is available */
  380. if (spec->num_inputs == 2 &&
  381. spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
  382. if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
  383. if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
  384. spec->mic_detect = 1;
  385. spec->automic_idx = AUTO_PIN_FRONT_MIC;
  386. }
  387. } else {
  388. if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
  389. spec->mic_detect = 1;
  390. spec->automic_idx = AUTO_PIN_MIC;
  391. }
  392. }
  393. }
  394. return 0;
  395. }
  396. static int parse_digital_output(struct hda_codec *codec)
  397. {
  398. struct cs_spec *spec = codec->spec;
  399. struct auto_pin_cfg *cfg = &spec->autocfg;
  400. hda_nid_t nid;
  401. int err;
  402. if (!cfg->dig_outs)
  403. return 0;
  404. if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
  405. return 0;
  406. spec->multiout.dig_out_nid = nid;
  407. spec->multiout.share_spdif = 1;
  408. if (cfg->dig_outs > 1 &&
  409. snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
  410. spec->slave_dig_outs[0] = nid;
  411. codec->slave_dig_outs = spec->slave_dig_outs;
  412. }
  413. return 0;
  414. }
  415. static int parse_digital_input(struct hda_codec *codec)
  416. {
  417. struct cs_spec *spec = codec->spec;
  418. struct auto_pin_cfg *cfg = &spec->autocfg;
  419. int idx;
  420. if (cfg->dig_in_pin)
  421. spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
  422. return 0;
  423. }
  424. /*
  425. * create mixer controls
  426. */
  427. static const char *dir_sfx[2] = { "Playback", "Capture" };
  428. static int add_mute(struct hda_codec *codec, const char *name, int index,
  429. unsigned int pval, int dir, struct snd_kcontrol **kctlp)
  430. {
  431. char tmp[32];
  432. struct snd_kcontrol_new knew =
  433. HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  434. knew.private_value = pval;
  435. snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
  436. *kctlp = snd_ctl_new1(&knew, codec);
  437. return snd_hda_ctl_add(codec, *kctlp);
  438. }
  439. static int add_volume(struct hda_codec *codec, const char *name,
  440. int index, unsigned int pval, int dir,
  441. struct snd_kcontrol **kctlp)
  442. {
  443. char tmp[32];
  444. struct snd_kcontrol_new knew =
  445. HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  446. knew.private_value = pval;
  447. snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
  448. *kctlp = snd_ctl_new1(&knew, codec);
  449. return snd_hda_ctl_add(codec, *kctlp);
  450. }
  451. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  452. {
  453. unsigned int caps;
  454. /* set the upper-limit for mixer amp to 0dB */
  455. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  456. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  457. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  458. << AC_AMPCAP_NUM_STEPS_SHIFT;
  459. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  460. }
  461. static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
  462. {
  463. struct cs_spec *spec = codec->spec;
  464. unsigned int tlv[4];
  465. int err;
  466. spec->vmaster_sw =
  467. snd_ctl_make_virtual_master("Master Playback Switch", NULL);
  468. err = snd_hda_ctl_add(codec, spec->vmaster_sw);
  469. if (err < 0)
  470. return err;
  471. snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
  472. spec->vmaster_vol =
  473. snd_ctl_make_virtual_master("Master Playback Volume", tlv);
  474. err = snd_hda_ctl_add(codec, spec->vmaster_vol);
  475. if (err < 0)
  476. return err;
  477. return 0;
  478. }
  479. static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
  480. int num_ctls, int type)
  481. {
  482. struct cs_spec *spec = codec->spec;
  483. const char *name;
  484. int err, index;
  485. struct snd_kcontrol *kctl;
  486. static char *speakers[] = {
  487. "Front Speaker", "Surround Speaker", "Bass Speaker"
  488. };
  489. static char *line_outs[] = {
  490. "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
  491. };
  492. fix_volume_caps(codec, dac);
  493. if (!spec->vmaster_sw) {
  494. err = add_vmaster(codec, dac);
  495. if (err < 0)
  496. return err;
  497. }
  498. index = 0;
  499. switch (type) {
  500. case AUTO_PIN_HP_OUT:
  501. name = "Headphone";
  502. index = idx;
  503. break;
  504. case AUTO_PIN_SPEAKER_OUT:
  505. if (num_ctls > 1)
  506. name = speakers[idx];
  507. else
  508. name = "Speaker";
  509. break;
  510. default:
  511. if (num_ctls > 1)
  512. name = line_outs[idx];
  513. else
  514. name = "Line-Out";
  515. break;
  516. }
  517. err = add_mute(codec, name, index,
  518. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  519. if (err < 0)
  520. return err;
  521. err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
  522. if (err < 0)
  523. return err;
  524. err = add_volume(codec, name, index,
  525. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  526. if (err < 0)
  527. return err;
  528. err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
  529. if (err < 0)
  530. return err;
  531. return 0;
  532. }
  533. static int build_output(struct hda_codec *codec)
  534. {
  535. struct cs_spec *spec = codec->spec;
  536. struct auto_pin_cfg *cfg = &spec->autocfg;
  537. int i, err;
  538. for (i = 0; i < cfg->line_outs; i++) {
  539. err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
  540. i, cfg->line_outs, cfg->line_out_type);
  541. if (err < 0)
  542. return err;
  543. }
  544. for (i = 0; i < cfg->hp_outs; i++) {
  545. err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
  546. i, cfg->hp_outs, AUTO_PIN_HP_OUT);
  547. if (err < 0)
  548. return err;
  549. }
  550. for (i = 0; i < cfg->speaker_outs; i++) {
  551. err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
  552. i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
  553. if (err < 0)
  554. return err;
  555. }
  556. return 0;
  557. }
  558. /*
  559. */
  560. static struct snd_kcontrol_new cs_capture_ctls[] = {
  561. HDA_BIND_SW("Capture Switch", 0),
  562. HDA_BIND_VOL("Capture Volume", 0),
  563. };
  564. static int change_cur_input(struct hda_codec *codec, unsigned int idx,
  565. int force)
  566. {
  567. struct cs_spec *spec = codec->spec;
  568. struct auto_pin_cfg *cfg = &spec->autocfg;
  569. if (spec->cur_input == idx && !force)
  570. return 0;
  571. if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
  572. /* stream is running, let's swap the current ADC */
  573. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  574. spec->cur_adc = spec->adc_nid[idx];
  575. snd_hda_codec_setup_stream(codec, spec->cur_adc,
  576. spec->cur_adc_stream_tag, 0,
  577. spec->cur_adc_format);
  578. }
  579. snd_hda_codec_write(codec, spec->cur_adc, 0,
  580. AC_VERB_SET_CONNECT_SEL,
  581. spec->adc_idx[idx]);
  582. spec->cur_input = idx;
  583. return 1;
  584. }
  585. static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
  586. struct snd_ctl_elem_info *uinfo)
  587. {
  588. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  589. struct cs_spec *spec = codec->spec;
  590. unsigned int idx;
  591. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  592. uinfo->count = 1;
  593. uinfo->value.enumerated.items = spec->num_inputs;
  594. if (uinfo->value.enumerated.item >= spec->num_inputs)
  595. uinfo->value.enumerated.item = spec->num_inputs - 1;
  596. idx = spec->input_idx[uinfo->value.enumerated.item];
  597. strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
  598. return 0;
  599. }
  600. static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
  601. struct snd_ctl_elem_value *ucontrol)
  602. {
  603. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  604. struct cs_spec *spec = codec->spec;
  605. ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
  606. return 0;
  607. }
  608. static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
  609. struct snd_ctl_elem_value *ucontrol)
  610. {
  611. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  612. struct cs_spec *spec = codec->spec;
  613. unsigned int idx = ucontrol->value.enumerated.item[0];
  614. if (idx >= spec->num_inputs)
  615. return -EINVAL;
  616. idx = spec->input_idx[idx];
  617. return change_cur_input(codec, idx, 0);
  618. }
  619. static struct snd_kcontrol_new cs_capture_source = {
  620. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  621. .name = "Capture Source",
  622. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  623. .info = cs_capture_source_info,
  624. .get = cs_capture_source_get,
  625. .put = cs_capture_source_put,
  626. };
  627. static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
  628. struct hda_ctl_ops *ops)
  629. {
  630. struct cs_spec *spec = codec->spec;
  631. struct hda_bind_ctls *bind;
  632. int i, n;
  633. bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
  634. GFP_KERNEL);
  635. if (!bind)
  636. return NULL;
  637. bind->ops = ops;
  638. n = 0;
  639. for (i = 0; i < AUTO_PIN_LAST; i++) {
  640. if (!spec->adc_nid[i])
  641. continue;
  642. bind->values[n++] =
  643. HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
  644. spec->adc_idx[i], HDA_INPUT);
  645. }
  646. return bind;
  647. }
  648. static int build_input(struct hda_codec *codec)
  649. {
  650. struct cs_spec *spec = codec->spec;
  651. int i, err;
  652. if (!spec->num_inputs)
  653. return 0;
  654. /* make bind-capture */
  655. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  656. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  657. for (i = 0; i < 2; i++) {
  658. struct snd_kcontrol *kctl;
  659. if (!spec->capture_bind[i])
  660. return -ENOMEM;
  661. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  662. if (!kctl)
  663. return -ENOMEM;
  664. kctl->private_value = (long)spec->capture_bind[i];
  665. err = snd_hda_ctl_add(codec, kctl);
  666. if (err < 0)
  667. return err;
  668. }
  669. if (spec->num_inputs > 1 && !spec->mic_detect) {
  670. err = snd_hda_ctl_add(codec,
  671. snd_ctl_new1(&cs_capture_source, codec));
  672. if (err < 0)
  673. return err;
  674. }
  675. return 0;
  676. }
  677. /*
  678. */
  679. static int build_digital_output(struct hda_codec *codec)
  680. {
  681. struct cs_spec *spec = codec->spec;
  682. int err;
  683. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  684. if (err < 0)
  685. return err;
  686. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  687. if (err < 0)
  688. return err;
  689. return 0;
  690. }
  691. static int build_digital_input(struct hda_codec *codec)
  692. {
  693. struct cs_spec *spec = codec->spec;
  694. if (spec->dig_in)
  695. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  696. return 0;
  697. }
  698. /*
  699. * auto-mute and auto-mic switching
  700. */
  701. static void cs_automute(struct hda_codec *codec)
  702. {
  703. struct cs_spec *spec = codec->spec;
  704. struct auto_pin_cfg *cfg = &spec->autocfg;
  705. unsigned int caps, present, hp_present;
  706. hda_nid_t nid;
  707. int i;
  708. hp_present = 0;
  709. for (i = 0; i < cfg->hp_outs; i++) {
  710. nid = cfg->hp_pins[i];
  711. caps = snd_hda_query_pin_caps(codec, nid);
  712. if (!(caps & AC_PINCAP_PRES_DETECT))
  713. continue;
  714. if (caps & AC_PINCAP_TRIG_REQ)
  715. snd_hda_codec_read(codec, nid, 0,
  716. AC_VERB_SET_PIN_SENSE, 0);
  717. present = snd_hda_codec_read(codec, nid, 0,
  718. AC_VERB_GET_PIN_SENSE, 0);
  719. hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
  720. if (hp_present)
  721. break;
  722. }
  723. for (i = 0; i < cfg->speaker_outs; i++) {
  724. nid = cfg->speaker_pins[i];
  725. snd_hda_codec_write(codec, nid, 0,
  726. AC_VERB_SET_PIN_WIDGET_CONTROL,
  727. hp_present ? 0 : PIN_OUT);
  728. }
  729. }
  730. static void cs_automic(struct hda_codec *codec)
  731. {
  732. struct cs_spec *spec = codec->spec;
  733. struct auto_pin_cfg *cfg = &spec->autocfg;
  734. hda_nid_t nid;
  735. unsigned int caps, present;
  736. nid = cfg->input_pins[spec->automic_idx];
  737. caps = snd_hda_query_pin_caps(codec, nid);
  738. if (caps & AC_PINCAP_TRIG_REQ)
  739. snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
  740. present = snd_hda_codec_read(codec, nid, 0,
  741. AC_VERB_GET_PIN_SENSE, 0);
  742. if (present & AC_PINSENSE_PRESENCE)
  743. change_cur_input(codec, spec->automic_idx, 0);
  744. else {
  745. unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
  746. AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
  747. change_cur_input(codec, imic, 0);
  748. }
  749. }
  750. /*
  751. */
  752. static void init_output(struct hda_codec *codec)
  753. {
  754. struct cs_spec *spec = codec->spec;
  755. struct auto_pin_cfg *cfg = &spec->autocfg;
  756. int i;
  757. /* mute first */
  758. for (i = 0; i < spec->multiout.num_dacs; i++)
  759. snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
  760. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  761. if (spec->multiout.hp_nid)
  762. snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
  763. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  764. for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
  765. if (!spec->multiout.extra_out_nid[i])
  766. break;
  767. snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
  768. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  769. }
  770. /* set appropriate pin controls */
  771. for (i = 0; i < cfg->line_outs; i++)
  772. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  773. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  774. for (i = 0; i < cfg->hp_outs; i++) {
  775. hda_nid_t nid = cfg->hp_pins[i];
  776. snd_hda_codec_write(codec, nid, 0,
  777. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  778. if (!cfg->speaker_outs)
  779. continue;
  780. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  781. snd_hda_codec_write(codec, nid, 0,
  782. AC_VERB_SET_UNSOLICITED_ENABLE,
  783. AC_USRSP_EN | HP_EVENT);
  784. spec->hp_detect = 1;
  785. }
  786. }
  787. for (i = 0; i < cfg->speaker_outs; i++)
  788. snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
  789. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  790. if (spec->hp_detect)
  791. cs_automute(codec);
  792. }
  793. static void init_input(struct hda_codec *codec)
  794. {
  795. struct cs_spec *spec = codec->spec;
  796. struct auto_pin_cfg *cfg = &spec->autocfg;
  797. unsigned int coef;
  798. int i;
  799. for (i = 0; i < AUTO_PIN_LAST; i++) {
  800. unsigned int ctl;
  801. hda_nid_t pin = cfg->input_pins[i];
  802. if (!pin || !spec->adc_nid[i])
  803. continue;
  804. /* set appropriate pin control and mute first */
  805. ctl = PIN_IN;
  806. if (i <= AUTO_PIN_FRONT_MIC) {
  807. unsigned int caps = snd_hda_query_pin_caps(codec, pin);
  808. caps >>= AC_PINCAP_VREF_SHIFT;
  809. if (caps & AC_PINCAP_VREF_80)
  810. ctl = PIN_VREF80;
  811. }
  812. snd_hda_codec_write(codec, pin, 0,
  813. AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
  814. snd_hda_codec_write(codec, spec->adc_nid[i], 0,
  815. AC_VERB_SET_AMP_GAIN_MUTE,
  816. AMP_IN_MUTE(spec->adc_idx[i]));
  817. if (spec->mic_detect && spec->automic_idx == i)
  818. snd_hda_codec_write(codec, pin, 0,
  819. AC_VERB_SET_UNSOLICITED_ENABLE,
  820. AC_USRSP_EN | MIC_EVENT);
  821. }
  822. change_cur_input(codec, spec->cur_input, 1);
  823. if (spec->mic_detect)
  824. cs_automic(codec);
  825. coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
  826. if (is_active_pin(codec, CS_DMIC2_PIN_NID))
  827. coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
  828. if (is_active_pin(codec, CS_DMIC1_PIN_NID))
  829. coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
  830. * No effect if SPDIF_OUT2 is slected in
  831. * IDX_SPDIF_CTL.
  832. */
  833. cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
  834. }
  835. static struct hda_verb cs_coef_init_verbs[] = {
  836. {0x11, AC_VERB_SET_PROC_STATE, 1},
  837. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  838. {0x11, AC_VERB_SET_PROC_COEF,
  839. (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
  840. | 0x0040 /* Mute DACs on FIFO error */
  841. | 0x1000 /* Enable DACs High Pass Filter */
  842. | 0x0400 /* Disable Coefficient Auto increment */
  843. )},
  844. /* Beep */
  845. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  846. {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
  847. {} /* terminator */
  848. };
  849. /* SPDIF setup */
  850. static void init_digital(struct hda_codec *codec)
  851. {
  852. unsigned int coef;
  853. coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
  854. coef |= 0x0008; /* Replace with mute on error */
  855. if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
  856. coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
  857. * SPDIF_OUT2 is shared with GPIO1 and
  858. * DMIC_SDA2.
  859. */
  860. cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
  861. }
  862. static int cs_init(struct hda_codec *codec)
  863. {
  864. struct cs_spec *spec = codec->spec;
  865. snd_hda_sequence_write(codec, cs_coef_init_verbs);
  866. init_output(codec);
  867. init_input(codec);
  868. init_digital(codec);
  869. return 0;
  870. }
  871. static int cs_build_controls(struct hda_codec *codec)
  872. {
  873. struct cs_spec *spec = codec->spec;
  874. int err;
  875. err = build_output(codec);
  876. if (err < 0)
  877. return err;
  878. err = build_input(codec);
  879. if (err < 0)
  880. return err;
  881. err = build_digital_output(codec);
  882. if (err < 0)
  883. return err;
  884. err = build_digital_input(codec);
  885. if (err < 0)
  886. return err;
  887. return cs_init(codec);
  888. }
  889. static void cs_free(struct hda_codec *codec)
  890. {
  891. struct cs_spec *spec = codec->spec;
  892. kfree(spec->capture_bind[0]);
  893. kfree(spec->capture_bind[1]);
  894. kfree(codec->spec);
  895. }
  896. static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
  897. {
  898. switch ((res >> 26) & 0x7f) {
  899. case HP_EVENT:
  900. cs_automute(codec);
  901. break;
  902. case MIC_EVENT:
  903. cs_automic(codec);
  904. break;
  905. }
  906. }
  907. static struct hda_codec_ops cs_patch_ops = {
  908. .build_controls = cs_build_controls,
  909. .build_pcms = cs_build_pcms,
  910. .init = cs_init,
  911. .free = cs_free,
  912. .unsol_event = cs_unsol_event,
  913. };
  914. static int cs_parse_auto_config(struct hda_codec *codec)
  915. {
  916. struct cs_spec *spec = codec->spec;
  917. int err;
  918. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  919. if (err < 0)
  920. return err;
  921. return 0;
  922. }
  923. static int patch_cs420x(struct hda_codec *codec)
  924. {
  925. struct cs_spec *spec;
  926. int err;
  927. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  928. if (!spec)
  929. return -ENOMEM;
  930. codec->spec = spec;
  931. err = cs_parse_auto_config(codec);
  932. if (err < 0)
  933. goto error;
  934. err = parse_output(codec);
  935. if (err < 0)
  936. goto error;
  937. err = parse_input(codec);
  938. if (err < 0)
  939. goto error;
  940. err = parse_digital_output(codec);
  941. if (err < 0)
  942. goto error;
  943. err = parse_digital_input(codec);
  944. if (err < 0)
  945. goto error;
  946. codec->patch_ops = cs_patch_ops;
  947. return 0;
  948. error:
  949. kfree(codec->spec);
  950. codec->spec = NULL;
  951. return err;
  952. }
  953. /*
  954. * patch entries
  955. */
  956. static struct hda_codec_preset snd_hda_preset_cirrus[] = {
  957. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  958. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  959. {} /* terminator */
  960. };
  961. MODULE_ALIAS("snd-hda-codec-id:10134206");
  962. MODULE_ALIAS("snd-hda-codec-id:10134207");
  963. MODULE_LICENSE("GPL");
  964. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  965. static struct hda_codec_preset_list cirrus_list = {
  966. .preset = snd_hda_preset_cirrus,
  967. .owner = THIS_MODULE,
  968. };
  969. static int __init patch_cirrus_init(void)
  970. {
  971. return snd_hda_add_codec_preset(&cirrus_list);
  972. }
  973. static void __exit patch_cirrus_exit(void)
  974. {
  975. snd_hda_delete_codec_preset(&cirrus_list);
  976. }
  977. module_init(patch_cirrus_init)
  978. module_exit(patch_cirrus_exit)