patch_cirrus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. #define HP_EVENT 1
  53. #define MIC_EVENT 2
  54. /*
  55. * PCM callbacks
  56. */
  57. static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
  58. struct hda_codec *codec,
  59. struct snd_pcm_substream *substream)
  60. {
  61. struct cs_spec *spec = codec->spec;
  62. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  63. hinfo);
  64. }
  65. static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  66. struct hda_codec *codec,
  67. unsigned int stream_tag,
  68. unsigned int format,
  69. struct snd_pcm_substream *substream)
  70. {
  71. struct cs_spec *spec = codec->spec;
  72. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  73. stream_tag, format, substream);
  74. }
  75. static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  76. struct hda_codec *codec,
  77. struct snd_pcm_substream *substream)
  78. {
  79. struct cs_spec *spec = codec->spec;
  80. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  81. }
  82. /*
  83. * Digital out
  84. */
  85. static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  86. struct hda_codec *codec,
  87. struct snd_pcm_substream *substream)
  88. {
  89. struct cs_spec *spec = codec->spec;
  90. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  91. }
  92. static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  93. struct hda_codec *codec,
  94. struct snd_pcm_substream *substream)
  95. {
  96. struct cs_spec *spec = codec->spec;
  97. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  98. }
  99. static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  100. struct hda_codec *codec,
  101. unsigned int stream_tag,
  102. unsigned int format,
  103. struct snd_pcm_substream *substream)
  104. {
  105. struct cs_spec *spec = codec->spec;
  106. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  107. format, substream);
  108. }
  109. static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  110. struct hda_codec *codec,
  111. struct snd_pcm_substream *substream)
  112. {
  113. struct cs_spec *spec = codec->spec;
  114. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  115. }
  116. /*
  117. * Analog capture
  118. */
  119. static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  120. struct hda_codec *codec,
  121. unsigned int stream_tag,
  122. unsigned int format,
  123. struct snd_pcm_substream *substream)
  124. {
  125. struct cs_spec *spec = codec->spec;
  126. spec->cur_adc = spec->adc_nid[spec->cur_input];
  127. spec->cur_adc_stream_tag = stream_tag;
  128. spec->cur_adc_format = format;
  129. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  130. return 0;
  131. }
  132. static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  133. struct hda_codec *codec,
  134. struct snd_pcm_substream *substream)
  135. {
  136. struct cs_spec *spec = codec->spec;
  137. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  138. spec->cur_adc = 0;
  139. return 0;
  140. }
  141. /*
  142. */
  143. static struct hda_pcm_stream cs_pcm_analog_playback = {
  144. .substreams = 1,
  145. .channels_min = 2,
  146. .channels_max = 2,
  147. .ops = {
  148. .open = cs_playback_pcm_open,
  149. .prepare = cs_playback_pcm_prepare,
  150. .cleanup = cs_playback_pcm_cleanup
  151. },
  152. };
  153. static struct hda_pcm_stream cs_pcm_analog_capture = {
  154. .substreams = 1,
  155. .channels_min = 2,
  156. .channels_max = 2,
  157. .ops = {
  158. .prepare = cs_capture_pcm_prepare,
  159. .cleanup = cs_capture_pcm_cleanup
  160. },
  161. };
  162. static struct hda_pcm_stream cs_pcm_digital_playback = {
  163. .substreams = 1,
  164. .channels_min = 2,
  165. .channels_max = 2,
  166. .ops = {
  167. .open = cs_dig_playback_pcm_open,
  168. .close = cs_dig_playback_pcm_close,
  169. .prepare = cs_dig_playback_pcm_prepare,
  170. .cleanup = cs_dig_playback_pcm_cleanup
  171. },
  172. };
  173. static struct hda_pcm_stream cs_pcm_digital_capture = {
  174. .substreams = 1,
  175. .channels_min = 2,
  176. .channels_max = 2,
  177. };
  178. static int cs_build_pcms(struct hda_codec *codec)
  179. {
  180. struct cs_spec *spec = codec->spec;
  181. struct hda_pcm *info = spec->pcm_rec;
  182. codec->pcm_info = info;
  183. codec->num_pcms = 0;
  184. info->name = "Cirrus Analog";
  185. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
  186. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
  187. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  188. spec->multiout.max_channels;
  189. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
  190. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  191. spec->adc_nid[spec->cur_input];
  192. codec->num_pcms++;
  193. if (!spec->multiout.dig_out_nid && !spec->dig_in)
  194. return 0;
  195. info++;
  196. info->name = "Cirrus Digital";
  197. info->pcm_type = spec->autocfg.dig_out_type[0];
  198. if (!info->pcm_type)
  199. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  200. if (spec->multiout.dig_out_nid) {
  201. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  202. cs_pcm_digital_playback;
  203. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  204. spec->multiout.dig_out_nid;
  205. }
  206. if (spec->dig_in) {
  207. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  208. cs_pcm_digital_capture;
  209. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  210. }
  211. codec->num_pcms++;
  212. return 0;
  213. }
  214. /*
  215. * parse codec topology
  216. */
  217. static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
  218. {
  219. hda_nid_t dac;
  220. if (!pin)
  221. return 0;
  222. if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
  223. return 0;
  224. return dac;
  225. }
  226. static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
  227. {
  228. struct cs_spec *spec = codec->spec;
  229. struct auto_pin_cfg *cfg = &spec->autocfg;
  230. hda_nid_t pin = cfg->input_pins[idx];
  231. unsigned int val = snd_hda_query_pin_caps(codec, pin);
  232. if (!(val & AC_PINCAP_PRES_DETECT))
  233. return 0;
  234. val = snd_hda_codec_get_pincfg(codec, pin);
  235. return (get_defcfg_connect(val) == AC_JACK_PORT_COMPLEX);
  236. }
  237. static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
  238. unsigned int *idxp)
  239. {
  240. int i;
  241. hda_nid_t nid;
  242. nid = codec->start_nid;
  243. for (i = 0; i < codec->num_nodes; i++, nid++) {
  244. hda_nid_t pins[2];
  245. unsigned int type;
  246. int j, nums;
  247. type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
  248. >> AC_WCAP_TYPE_SHIFT;
  249. if (type != AC_WID_AUD_IN)
  250. continue;
  251. nums = snd_hda_get_connections(codec, nid, pins,
  252. ARRAY_SIZE(pins));
  253. if (nums <= 0)
  254. continue;
  255. for (j = 0; j < nums; j++) {
  256. if (pins[j] == pin) {
  257. *idxp = j;
  258. return nid;
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264. static int parse_output(struct hda_codec *codec)
  265. {
  266. struct cs_spec *spec = codec->spec;
  267. struct auto_pin_cfg *cfg = &spec->autocfg;
  268. int i, err, extra_nids;
  269. hda_nid_t dac;
  270. for (i = 0; i < cfg->line_outs; i++) {
  271. dac = get_dac(codec, cfg->line_out_pins[i]);
  272. if (!dac)
  273. break;
  274. spec->dac_nid[i] = dac;
  275. }
  276. spec->multiout.num_dacs = i;
  277. spec->multiout.dac_nids = spec->dac_nid;
  278. spec->multiout.max_channels = i * 2;
  279. /* add HP and speakers */
  280. extra_nids = 0;
  281. for (i = 0; i < cfg->hp_outs; i++) {
  282. dac = get_dac(codec, cfg->hp_pins[i]);
  283. if (!dac)
  284. break;
  285. if (!i)
  286. spec->multiout.hp_nid = dac;
  287. else
  288. spec->multiout.extra_out_nid[extra_nids++] = dac;
  289. }
  290. for (i = 0; i < cfg->speaker_outs; i++) {
  291. dac = get_dac(codec, cfg->speaker_pins[i]);
  292. if (!dac)
  293. break;
  294. spec->multiout.extra_out_nid[extra_nids++] = dac;
  295. }
  296. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  297. cfg->speaker_outs = cfg->line_outs;
  298. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  299. sizeof(cfg->speaker_pins));
  300. cfg->line_outs = 0;
  301. }
  302. return 0;
  303. }
  304. static int parse_input(struct hda_codec *codec)
  305. {
  306. struct cs_spec *spec = codec->spec;
  307. struct auto_pin_cfg *cfg = &spec->autocfg;
  308. int i, n, err;
  309. for (i = 0; i < AUTO_PIN_LAST; i++) {
  310. hda_nid_t pin = cfg->input_pins[i];
  311. struct snd_kcontrol *kctl;
  312. if (!pin)
  313. continue;
  314. spec->input_idx[spec->num_inputs] = i;
  315. spec->capsrc_idx[i] = spec->num_inputs++;
  316. spec->cur_input = i;
  317. spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
  318. }
  319. if (!spec->num_inputs)
  320. return 0;
  321. /* check whether the automatic mic switch is available */
  322. if (spec->num_inputs == 2 &&
  323. spec->adc_nid[AUTO_PIN_MIC] && spec->adc_nid[AUTO_PIN_FRONT_MIC]) {
  324. if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_FRONT_MIC])) {
  325. if (!is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
  326. spec->mic_detect = 1;
  327. spec->automic_idx = AUTO_PIN_FRONT_MIC;
  328. }
  329. } else {
  330. if (is_ext_mic(codec, cfg->input_pins[AUTO_PIN_MIC])) {
  331. spec->mic_detect = 1;
  332. spec->automic_idx = AUTO_PIN_MIC;
  333. }
  334. }
  335. }
  336. return 0;
  337. }
  338. static int parse_digital_output(struct hda_codec *codec)
  339. {
  340. struct cs_spec *spec = codec->spec;
  341. struct auto_pin_cfg *cfg = &spec->autocfg;
  342. hda_nid_t nid;
  343. int err;
  344. if (!cfg->dig_outs)
  345. return 0;
  346. if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
  347. return 0;
  348. spec->multiout.dig_out_nid = nid;
  349. spec->multiout.share_spdif = 1;
  350. if (cfg->dig_outs > 1 &&
  351. snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
  352. spec->slave_dig_outs[0] = nid;
  353. codec->slave_dig_outs = spec->slave_dig_outs;
  354. }
  355. return 0;
  356. }
  357. static int parse_digital_input(struct hda_codec *codec)
  358. {
  359. struct cs_spec *spec = codec->spec;
  360. struct auto_pin_cfg *cfg = &spec->autocfg;
  361. int idx;
  362. if (!cfg->dig_in_pin)
  363. return 0;
  364. spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
  365. if (!spec->dig_in)
  366. return 0;
  367. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  368. }
  369. /*
  370. * create mixer controls
  371. */
  372. static const char *dir_sfx[2] = { "Playback", "Capture" };
  373. static int add_mute(struct hda_codec *codec, const char *name, int index,
  374. unsigned int pval, int dir, struct snd_kcontrol **kctlp)
  375. {
  376. char tmp[32];
  377. struct snd_kcontrol_new knew =
  378. HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  379. knew.private_value = pval;
  380. snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
  381. *kctlp = snd_ctl_new1(&knew, codec);
  382. return snd_hda_ctl_add(codec, *kctlp);
  383. }
  384. static int add_volume(struct hda_codec *codec, const char *name,
  385. int index, unsigned int pval, int dir,
  386. struct snd_kcontrol **kctlp)
  387. {
  388. char tmp[32];
  389. struct snd_kcontrol_new knew =
  390. HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  391. knew.private_value = pval;
  392. snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
  393. *kctlp = snd_ctl_new1(&knew, codec);
  394. return snd_hda_ctl_add(codec, *kctlp);
  395. }
  396. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  397. {
  398. unsigned int caps;
  399. /* set the upper-limit for mixer amp to 0dB */
  400. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  401. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  402. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  403. << AC_AMPCAP_NUM_STEPS_SHIFT;
  404. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  405. }
  406. static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
  407. {
  408. struct cs_spec *spec = codec->spec;
  409. unsigned int tlv[4];
  410. int err;
  411. spec->vmaster_sw =
  412. snd_ctl_make_virtual_master("Master Playback Switch", NULL);
  413. err = snd_hda_ctl_add(codec, spec->vmaster_sw);
  414. if (err < 0)
  415. return err;
  416. snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
  417. spec->vmaster_vol =
  418. snd_ctl_make_virtual_master("Master Playback Volume", tlv);
  419. err = snd_hda_ctl_add(codec, spec->vmaster_vol);
  420. if (err < 0)
  421. return err;
  422. return 0;
  423. }
  424. static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
  425. int num_ctls, int type)
  426. {
  427. struct cs_spec *spec = codec->spec;
  428. const char *name;
  429. int err, index;
  430. struct snd_kcontrol *kctl;
  431. static char *speakers[] = {
  432. "Front Speaker", "Surround Speaker", "Bass Speaker"
  433. };
  434. static char *line_outs[] = {
  435. "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
  436. };
  437. fix_volume_caps(codec, dac);
  438. if (!spec->vmaster_sw) {
  439. err = add_vmaster(codec, dac);
  440. if (err < 0)
  441. return err;
  442. }
  443. index = 0;
  444. switch (type) {
  445. case AUTO_PIN_HP_OUT:
  446. name = "Headphone";
  447. index = idx;
  448. break;
  449. case AUTO_PIN_SPEAKER_OUT:
  450. if (num_ctls > 1)
  451. name = speakers[idx];
  452. else
  453. name = "Speaker";
  454. break;
  455. default:
  456. if (num_ctls > 1)
  457. name = line_outs[idx];
  458. else
  459. name = "Line-Out";
  460. break;
  461. }
  462. err = add_mute(codec, name, index,
  463. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  464. if (err < 0)
  465. return err;
  466. err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
  467. if (err < 0)
  468. return err;
  469. err = add_volume(codec, name, index,
  470. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  471. if (err < 0)
  472. return err;
  473. err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
  474. if (err < 0)
  475. return err;
  476. return 0;
  477. }
  478. static int build_output(struct hda_codec *codec)
  479. {
  480. struct cs_spec *spec = codec->spec;
  481. struct auto_pin_cfg *cfg = &spec->autocfg;
  482. int i, err;
  483. for (i = 0; i < cfg->line_outs; i++) {
  484. err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
  485. i, cfg->line_outs, cfg->line_out_type);
  486. if (err < 0)
  487. return err;
  488. }
  489. for (i = 0; i < cfg->hp_outs; i++) {
  490. err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
  491. i, cfg->hp_outs, AUTO_PIN_HP_OUT);
  492. if (err < 0)
  493. return err;
  494. }
  495. for (i = 0; i < cfg->speaker_outs; i++) {
  496. err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
  497. i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
  498. if (err < 0)
  499. return err;
  500. }
  501. return 0;
  502. }
  503. /*
  504. */
  505. static struct snd_kcontrol_new cs_capture_ctls[] = {
  506. HDA_BIND_SW("Capture Switch", 0),
  507. HDA_BIND_VOL("Capture Volume", 0),
  508. };
  509. static int change_cur_input(struct hda_codec *codec, unsigned int idx)
  510. {
  511. struct cs_spec *spec = codec->spec;
  512. struct auto_pin_cfg *cfg = &spec->autocfg;
  513. if (spec->cur_input == idx)
  514. return 0;
  515. if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
  516. /* stream is running, let's swap the current ADC */
  517. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  518. spec->cur_adc = spec->adc_nid[idx];
  519. snd_hda_codec_setup_stream(codec, spec->cur_adc,
  520. spec->cur_adc_stream_tag, 0,
  521. spec->cur_adc_format);
  522. }
  523. snd_hda_codec_write(codec, spec->cur_adc, 0,
  524. AC_VERB_SET_CONNECT_SEL,
  525. spec->adc_idx[idx]);
  526. spec->cur_input = idx;
  527. return 1;
  528. }
  529. static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
  530. struct snd_ctl_elem_info *uinfo)
  531. {
  532. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  533. struct cs_spec *spec = codec->spec;
  534. unsigned int idx;
  535. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  536. uinfo->count = 1;
  537. uinfo->value.enumerated.items = spec->num_inputs;
  538. if (uinfo->value.enumerated.item >= spec->num_inputs)
  539. uinfo->value.enumerated.item = spec->num_inputs - 1;
  540. idx = spec->input_idx[uinfo->value.enumerated.item];
  541. strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
  542. return 0;
  543. }
  544. static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
  545. struct snd_ctl_elem_value *ucontrol)
  546. {
  547. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  548. struct cs_spec *spec = codec->spec;
  549. ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
  550. return 0;
  551. }
  552. static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
  553. struct snd_ctl_elem_value *ucontrol)
  554. {
  555. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  556. struct cs_spec *spec = codec->spec;
  557. unsigned int idx = ucontrol->value.enumerated.item[0];
  558. if (idx >= spec->num_inputs)
  559. return -EINVAL;
  560. idx = spec->input_idx[idx];
  561. return change_cur_input(codec, idx);
  562. }
  563. static struct snd_kcontrol_new cs_capture_source = {
  564. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  565. .name = "Capture Source",
  566. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  567. .info = cs_capture_source_info,
  568. .get = cs_capture_source_get,
  569. .put = cs_capture_source_put,
  570. };
  571. static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
  572. struct hda_ctl_ops *ops)
  573. {
  574. struct cs_spec *spec = codec->spec;
  575. struct hda_bind_ctls *bind;
  576. int i, n;
  577. bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
  578. GFP_KERNEL);
  579. if (!bind)
  580. return NULL;
  581. bind->ops = ops;
  582. n = 0;
  583. for (i = 0; i < AUTO_PIN_LAST; i++) {
  584. if (!spec->adc_nid[i])
  585. continue;
  586. bind->values[n++] =
  587. HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
  588. spec->adc_idx[i], HDA_INPUT);
  589. }
  590. return bind;
  591. }
  592. static int build_input(struct hda_codec *codec)
  593. {
  594. struct cs_spec *spec = codec->spec;
  595. int i, err;
  596. if (!spec->num_inputs)
  597. return 0;
  598. /* make bind-capture */
  599. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  600. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  601. for (i = 0; i < 2; i++) {
  602. struct snd_kcontrol *kctl;
  603. if (!spec->capture_bind[i])
  604. return -ENOMEM;
  605. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  606. if (!kctl)
  607. return -ENOMEM;
  608. kctl->private_value = (long)spec->capture_bind[i];
  609. err = snd_hda_ctl_add(codec, kctl);
  610. if (err < 0)
  611. return err;
  612. }
  613. if (spec->num_inputs > 1 && !spec->mic_detect) {
  614. err = snd_hda_ctl_add(codec,
  615. snd_ctl_new1(&cs_capture_source, codec));
  616. if (err < 0)
  617. return err;
  618. }
  619. return 0;
  620. }
  621. /*
  622. */
  623. static int build_digital_output(struct hda_codec *codec)
  624. {
  625. struct cs_spec *spec = codec->spec;
  626. int err;
  627. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  628. if (err < 0)
  629. return err;
  630. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  631. if (err < 0)
  632. return err;
  633. return 0;
  634. }
  635. static int build_digital_input(struct hda_codec *codec)
  636. {
  637. struct cs_spec *spec = codec->spec;
  638. if (spec->dig_in)
  639. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  640. return 0;
  641. }
  642. /*
  643. * auto-mute and auto-mic switching
  644. */
  645. static void cs_automute(struct hda_codec *codec)
  646. {
  647. struct cs_spec *spec = codec->spec;
  648. struct auto_pin_cfg *cfg = &spec->autocfg;
  649. unsigned int caps, present, hp_present;
  650. hda_nid_t nid;
  651. int i;
  652. hp_present = 0;
  653. for (i = 0; i < cfg->hp_outs; i++) {
  654. nid = cfg->hp_pins[i];
  655. caps = snd_hda_query_pin_caps(codec, nid);
  656. if (!(caps & AC_PINCAP_PRES_DETECT))
  657. continue;
  658. if (caps & AC_PINCAP_TRIG_REQ)
  659. snd_hda_codec_read(codec, nid, 0,
  660. AC_VERB_SET_PIN_SENSE, 0);
  661. present = snd_hda_codec_read(codec, nid, 0,
  662. AC_VERB_GET_PIN_SENSE, 0);
  663. hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
  664. if (hp_present)
  665. break;
  666. }
  667. for (i = 0; i < cfg->speaker_outs; i++) {
  668. nid = cfg->speaker_pins[i];
  669. snd_hda_codec_write(codec, nid, 0,
  670. AC_VERB_SET_PIN_WIDGET_CONTROL,
  671. hp_present ? 0 : PIN_OUT);
  672. }
  673. }
  674. static void cs_automic(struct hda_codec *codec)
  675. {
  676. struct cs_spec *spec = codec->spec;
  677. struct auto_pin_cfg *cfg = &spec->autocfg;
  678. hda_nid_t nid;
  679. unsigned int caps, present;
  680. nid = cfg->input_pins[spec->automic_idx];
  681. caps = snd_hda_query_pin_caps(codec, nid);
  682. if (caps & AC_PINCAP_TRIG_REQ)
  683. snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
  684. present = snd_hda_codec_read(codec, nid, 0,
  685. AC_VERB_GET_PIN_SENSE, 0);
  686. if (present & AC_PINSENSE_PRESENCE)
  687. change_cur_input(codec, spec->automic_idx);
  688. else {
  689. unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
  690. AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
  691. change_cur_input(codec, imic);
  692. }
  693. }
  694. /*
  695. */
  696. static void init_output(struct hda_codec *codec)
  697. {
  698. struct cs_spec *spec = codec->spec;
  699. struct auto_pin_cfg *cfg = &spec->autocfg;
  700. int i;
  701. /* mute first */
  702. for (i = 0; i < spec->multiout.num_dacs; i++)
  703. snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
  704. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  705. if (spec->multiout.hp_nid)
  706. snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
  707. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  708. for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
  709. if (!spec->multiout.extra_out_nid[i])
  710. break;
  711. snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
  712. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  713. }
  714. /* set appropriate pin controls */
  715. for (i = 0; i < cfg->line_outs; i++)
  716. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  717. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  718. for (i = 0; i < cfg->hp_outs; i++) {
  719. hda_nid_t nid = cfg->hp_pins[i];
  720. snd_hda_codec_write(codec, nid, 0,
  721. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  722. if (!cfg->speaker_outs)
  723. continue;
  724. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  725. snd_hda_codec_write(codec, nid, 0,
  726. AC_VERB_SET_UNSOLICITED_ENABLE,
  727. AC_USRSP_EN | HP_EVENT);
  728. spec->hp_detect = 1;
  729. }
  730. }
  731. for (i = 0; i < cfg->speaker_outs; i++)
  732. snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
  733. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  734. if (spec->hp_detect)
  735. cs_automute(codec);
  736. }
  737. static void init_input(struct hda_codec *codec)
  738. {
  739. struct cs_spec *spec = codec->spec;
  740. struct auto_pin_cfg *cfg = &spec->autocfg;
  741. int i;
  742. for (i = 0; i < AUTO_PIN_LAST; i++) {
  743. unsigned int ctl;
  744. hda_nid_t pin = cfg->input_pins[i];
  745. if (!pin || !spec->adc_nid[i])
  746. continue;
  747. /* set appropriate pin control and mute first */
  748. ctl = PIN_IN;
  749. if (i <= AUTO_PIN_FRONT_MIC) {
  750. unsigned int caps = snd_hda_query_pin_caps(codec, pin);
  751. caps >>= AC_PINCAP_VREF_SHIFT;
  752. if (caps & AC_PINCAP_VREF_80)
  753. ctl = PIN_VREF80;
  754. }
  755. snd_hda_codec_write(codec, pin, 0,
  756. AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
  757. snd_hda_codec_write(codec, spec->adc_nid[i], 0,
  758. AC_VERB_SET_AMP_GAIN_MUTE,
  759. AMP_IN_MUTE(spec->adc_idx[i]));
  760. if (spec->mic_detect && spec->automic_idx == i)
  761. snd_hda_codec_write(codec, pin, 0,
  762. AC_VERB_SET_UNSOLICITED_ENABLE,
  763. AC_USRSP_EN | MIC_EVENT);
  764. }
  765. if (spec->mic_detect)
  766. cs_automic(codec);
  767. }
  768. static int cs_init(struct hda_codec *codec)
  769. {
  770. struct cs_spec *spec = codec->spec;
  771. init_output(codec);
  772. init_input(codec);
  773. return 0;
  774. }
  775. static int cs_build_controls(struct hda_codec *codec)
  776. {
  777. struct cs_spec *spec = codec->spec;
  778. int err;
  779. err = build_output(codec);
  780. if (err < 0)
  781. return err;
  782. err = build_input(codec);
  783. if (err < 0)
  784. return err;
  785. err = build_digital_output(codec);
  786. if (err < 0)
  787. return err;
  788. err = build_digital_input(codec);
  789. if (err < 0)
  790. return err;
  791. return cs_init(codec);
  792. }
  793. static void cs_free(struct hda_codec *codec)
  794. {
  795. struct cs_spec *spec = codec->spec;
  796. kfree(spec->capture_bind[0]);
  797. kfree(spec->capture_bind[1]);
  798. kfree(codec->spec);
  799. }
  800. static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
  801. {
  802. switch ((res >> 26) & 0x7f) {
  803. case HP_EVENT:
  804. cs_automute(codec);
  805. break;
  806. case MIC_EVENT:
  807. cs_automic(codec);
  808. break;
  809. }
  810. }
  811. static struct hda_codec_ops cs_patch_ops = {
  812. .build_controls = cs_build_controls,
  813. .build_pcms = cs_build_pcms,
  814. .init = cs_init,
  815. .free = cs_free,
  816. .unsol_event = cs_unsol_event,
  817. };
  818. static int cs_parse_auto_config(struct hda_codec *codec)
  819. {
  820. struct cs_spec *spec = codec->spec;
  821. int err;
  822. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  823. if (err < 0)
  824. return err;
  825. return 0;
  826. }
  827. static int patch_cs420x(struct hda_codec *codec)
  828. {
  829. struct cs_spec *spec;
  830. int err;
  831. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  832. if (!spec)
  833. return -ENOMEM;
  834. codec->spec = spec;
  835. err = cs_parse_auto_config(codec);
  836. if (err < 0)
  837. goto error;
  838. err = parse_output(codec);
  839. if (err < 0)
  840. goto error;
  841. err = parse_input(codec);
  842. if (err < 0)
  843. goto error;
  844. err = parse_digital_output(codec);
  845. if (err < 0)
  846. goto error;
  847. err = parse_digital_input(codec);
  848. if (err < 0)
  849. goto error;
  850. codec->patch_ops = cs_patch_ops;
  851. return 0;
  852. error:
  853. kfree(codec->spec);
  854. codec->spec = NULL;
  855. return err;
  856. }
  857. /*
  858. * patch entries
  859. */
  860. static struct hda_codec_preset snd_hda_preset_cirrus[] = {
  861. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  862. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  863. {} /* terminator */
  864. };
  865. MODULE_ALIAS("snd-hda-codec-id:10134206");
  866. MODULE_ALIAS("snd-hda-codec-id:10134207");
  867. MODULE_LICENSE("GPL");
  868. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  869. static struct hda_codec_preset_list cirrus_list = {
  870. .preset = snd_hda_preset_cirrus,
  871. .owner = THIS_MODULE,
  872. };
  873. static int __init patch_cirrus_init(void)
  874. {
  875. return snd_hda_add_codec_preset(&cirrus_list);
  876. }
  877. static void __exit patch_cirrus_exit(void)
  878. {
  879. snd_hda_delete_codec_preset(&cirrus_list);
  880. }
  881. module_init(patch_cirrus_init)
  882. module_exit(patch_cirrus_exit)