patch_cirrus.c 24 KB

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