patch_cirrus.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. int force)
  511. {
  512. struct cs_spec *spec = codec->spec;
  513. struct auto_pin_cfg *cfg = &spec->autocfg;
  514. if (spec->cur_input == idx && !force)
  515. return 0;
  516. if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
  517. /* stream is running, let's swap the current ADC */
  518. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  519. spec->cur_adc = spec->adc_nid[idx];
  520. snd_hda_codec_setup_stream(codec, spec->cur_adc,
  521. spec->cur_adc_stream_tag, 0,
  522. spec->cur_adc_format);
  523. }
  524. snd_hda_codec_write(codec, spec->cur_adc, 0,
  525. AC_VERB_SET_CONNECT_SEL,
  526. spec->adc_idx[idx]);
  527. spec->cur_input = idx;
  528. return 1;
  529. }
  530. static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
  531. struct snd_ctl_elem_info *uinfo)
  532. {
  533. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  534. struct cs_spec *spec = codec->spec;
  535. unsigned int idx;
  536. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  537. uinfo->count = 1;
  538. uinfo->value.enumerated.items = spec->num_inputs;
  539. if (uinfo->value.enumerated.item >= spec->num_inputs)
  540. uinfo->value.enumerated.item = spec->num_inputs - 1;
  541. idx = spec->input_idx[uinfo->value.enumerated.item];
  542. strcpy(uinfo->value.enumerated.name, auto_pin_cfg_labels[idx]);
  543. return 0;
  544. }
  545. static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
  546. struct snd_ctl_elem_value *ucontrol)
  547. {
  548. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  549. struct cs_spec *spec = codec->spec;
  550. ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
  551. return 0;
  552. }
  553. static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
  554. struct snd_ctl_elem_value *ucontrol)
  555. {
  556. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  557. struct cs_spec *spec = codec->spec;
  558. unsigned int idx = ucontrol->value.enumerated.item[0];
  559. if (idx >= spec->num_inputs)
  560. return -EINVAL;
  561. idx = spec->input_idx[idx];
  562. return change_cur_input(codec, idx, 0);
  563. }
  564. static struct snd_kcontrol_new cs_capture_source = {
  565. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  566. .name = "Capture Source",
  567. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  568. .info = cs_capture_source_info,
  569. .get = cs_capture_source_get,
  570. .put = cs_capture_source_put,
  571. };
  572. static struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
  573. struct hda_ctl_ops *ops)
  574. {
  575. struct cs_spec *spec = codec->spec;
  576. struct hda_bind_ctls *bind;
  577. int i, n;
  578. bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
  579. GFP_KERNEL);
  580. if (!bind)
  581. return NULL;
  582. bind->ops = ops;
  583. n = 0;
  584. for (i = 0; i < AUTO_PIN_LAST; i++) {
  585. if (!spec->adc_nid[i])
  586. continue;
  587. bind->values[n++] =
  588. HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
  589. spec->adc_idx[i], HDA_INPUT);
  590. }
  591. return bind;
  592. }
  593. static int build_input(struct hda_codec *codec)
  594. {
  595. struct cs_spec *spec = codec->spec;
  596. int i, err;
  597. if (!spec->num_inputs)
  598. return 0;
  599. /* make bind-capture */
  600. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  601. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  602. for (i = 0; i < 2; i++) {
  603. struct snd_kcontrol *kctl;
  604. if (!spec->capture_bind[i])
  605. return -ENOMEM;
  606. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  607. if (!kctl)
  608. return -ENOMEM;
  609. kctl->private_value = (long)spec->capture_bind[i];
  610. err = snd_hda_ctl_add(codec, kctl);
  611. if (err < 0)
  612. return err;
  613. }
  614. if (spec->num_inputs > 1 && !spec->mic_detect) {
  615. err = snd_hda_ctl_add(codec,
  616. snd_ctl_new1(&cs_capture_source, codec));
  617. if (err < 0)
  618. return err;
  619. }
  620. return 0;
  621. }
  622. /*
  623. */
  624. static int build_digital_output(struct hda_codec *codec)
  625. {
  626. struct cs_spec *spec = codec->spec;
  627. int err;
  628. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  629. if (err < 0)
  630. return err;
  631. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  632. if (err < 0)
  633. return err;
  634. return 0;
  635. }
  636. static int build_digital_input(struct hda_codec *codec)
  637. {
  638. struct cs_spec *spec = codec->spec;
  639. if (spec->dig_in)
  640. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  641. return 0;
  642. }
  643. /*
  644. * auto-mute and auto-mic switching
  645. */
  646. static void cs_automute(struct hda_codec *codec)
  647. {
  648. struct cs_spec *spec = codec->spec;
  649. struct auto_pin_cfg *cfg = &spec->autocfg;
  650. unsigned int caps, present, hp_present;
  651. hda_nid_t nid;
  652. int i;
  653. hp_present = 0;
  654. for (i = 0; i < cfg->hp_outs; i++) {
  655. nid = cfg->hp_pins[i];
  656. caps = snd_hda_query_pin_caps(codec, nid);
  657. if (!(caps & AC_PINCAP_PRES_DETECT))
  658. continue;
  659. if (caps & AC_PINCAP_TRIG_REQ)
  660. snd_hda_codec_read(codec, nid, 0,
  661. AC_VERB_SET_PIN_SENSE, 0);
  662. present = snd_hda_codec_read(codec, nid, 0,
  663. AC_VERB_GET_PIN_SENSE, 0);
  664. hp_present |= (present & AC_PINSENSE_PRESENCE) != 0;
  665. if (hp_present)
  666. break;
  667. }
  668. for (i = 0; i < cfg->speaker_outs; i++) {
  669. nid = cfg->speaker_pins[i];
  670. snd_hda_codec_write(codec, nid, 0,
  671. AC_VERB_SET_PIN_WIDGET_CONTROL,
  672. hp_present ? 0 : PIN_OUT);
  673. }
  674. }
  675. static void cs_automic(struct hda_codec *codec)
  676. {
  677. struct cs_spec *spec = codec->spec;
  678. struct auto_pin_cfg *cfg = &spec->autocfg;
  679. hda_nid_t nid;
  680. unsigned int caps, present;
  681. nid = cfg->input_pins[spec->automic_idx];
  682. caps = snd_hda_query_pin_caps(codec, nid);
  683. if (caps & AC_PINCAP_TRIG_REQ)
  684. snd_hda_codec_read(codec, nid, 0, AC_VERB_SET_PIN_SENSE, 0);
  685. present = snd_hda_codec_read(codec, nid, 0,
  686. AC_VERB_GET_PIN_SENSE, 0);
  687. if (present & AC_PINSENSE_PRESENCE)
  688. change_cur_input(codec, spec->automic_idx, 0);
  689. else {
  690. unsigned int imic = (spec->automic_idx == AUTO_PIN_MIC) ?
  691. AUTO_PIN_FRONT_MIC : AUTO_PIN_MIC;
  692. change_cur_input(codec, imic, 0);
  693. }
  694. }
  695. /*
  696. */
  697. static void init_output(struct hda_codec *codec)
  698. {
  699. struct cs_spec *spec = codec->spec;
  700. struct auto_pin_cfg *cfg = &spec->autocfg;
  701. int i;
  702. /* mute first */
  703. for (i = 0; i < spec->multiout.num_dacs; i++)
  704. snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
  705. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  706. if (spec->multiout.hp_nid)
  707. snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
  708. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  709. for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
  710. if (!spec->multiout.extra_out_nid[i])
  711. break;
  712. snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
  713. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  714. }
  715. /* set appropriate pin controls */
  716. for (i = 0; i < cfg->line_outs; i++)
  717. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  718. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  719. for (i = 0; i < cfg->hp_outs; i++) {
  720. hda_nid_t nid = cfg->hp_pins[i];
  721. snd_hda_codec_write(codec, nid, 0,
  722. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  723. if (!cfg->speaker_outs)
  724. continue;
  725. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  726. snd_hda_codec_write(codec, nid, 0,
  727. AC_VERB_SET_UNSOLICITED_ENABLE,
  728. AC_USRSP_EN | HP_EVENT);
  729. spec->hp_detect = 1;
  730. }
  731. }
  732. for (i = 0; i < cfg->speaker_outs; i++)
  733. snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
  734. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  735. if (spec->hp_detect)
  736. cs_automute(codec);
  737. }
  738. static void init_input(struct hda_codec *codec)
  739. {
  740. struct cs_spec *spec = codec->spec;
  741. struct auto_pin_cfg *cfg = &spec->autocfg;
  742. int i;
  743. for (i = 0; i < AUTO_PIN_LAST; i++) {
  744. unsigned int ctl;
  745. hda_nid_t pin = cfg->input_pins[i];
  746. if (!pin || !spec->adc_nid[i])
  747. continue;
  748. /* set appropriate pin control and mute first */
  749. ctl = PIN_IN;
  750. if (i <= AUTO_PIN_FRONT_MIC) {
  751. unsigned int caps = snd_hda_query_pin_caps(codec, pin);
  752. caps >>= AC_PINCAP_VREF_SHIFT;
  753. if (caps & AC_PINCAP_VREF_80)
  754. ctl = PIN_VREF80;
  755. }
  756. snd_hda_codec_write(codec, pin, 0,
  757. AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
  758. snd_hda_codec_write(codec, spec->adc_nid[i], 0,
  759. AC_VERB_SET_AMP_GAIN_MUTE,
  760. AMP_IN_MUTE(spec->adc_idx[i]));
  761. if (spec->mic_detect && spec->automic_idx == i)
  762. snd_hda_codec_write(codec, pin, 0,
  763. AC_VERB_SET_UNSOLICITED_ENABLE,
  764. AC_USRSP_EN | MIC_EVENT);
  765. }
  766. change_cur_input(codec, spec->cur_input, 1);
  767. if (spec->mic_detect)
  768. cs_automic(codec);
  769. }
  770. static int cs_init(struct hda_codec *codec)
  771. {
  772. struct cs_spec *spec = codec->spec;
  773. init_output(codec);
  774. init_input(codec);
  775. return 0;
  776. }
  777. static int cs_build_controls(struct hda_codec *codec)
  778. {
  779. struct cs_spec *spec = codec->spec;
  780. int err;
  781. err = build_output(codec);
  782. if (err < 0)
  783. return err;
  784. err = build_input(codec);
  785. if (err < 0)
  786. return err;
  787. err = build_digital_output(codec);
  788. if (err < 0)
  789. return err;
  790. err = build_digital_input(codec);
  791. if (err < 0)
  792. return err;
  793. return cs_init(codec);
  794. }
  795. static void cs_free(struct hda_codec *codec)
  796. {
  797. struct cs_spec *spec = codec->spec;
  798. kfree(spec->capture_bind[0]);
  799. kfree(spec->capture_bind[1]);
  800. kfree(codec->spec);
  801. }
  802. static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
  803. {
  804. switch ((res >> 26) & 0x7f) {
  805. case HP_EVENT:
  806. cs_automute(codec);
  807. break;
  808. case MIC_EVENT:
  809. cs_automic(codec);
  810. break;
  811. }
  812. }
  813. static struct hda_codec_ops cs_patch_ops = {
  814. .build_controls = cs_build_controls,
  815. .build_pcms = cs_build_pcms,
  816. .init = cs_init,
  817. .free = cs_free,
  818. .unsol_event = cs_unsol_event,
  819. };
  820. static int cs_parse_auto_config(struct hda_codec *codec)
  821. {
  822. struct cs_spec *spec = codec->spec;
  823. int err;
  824. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  825. if (err < 0)
  826. return err;
  827. return 0;
  828. }
  829. static int patch_cs420x(struct hda_codec *codec)
  830. {
  831. struct cs_spec *spec;
  832. int err;
  833. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  834. if (!spec)
  835. return -ENOMEM;
  836. codec->spec = spec;
  837. err = cs_parse_auto_config(codec);
  838. if (err < 0)
  839. goto error;
  840. err = parse_output(codec);
  841. if (err < 0)
  842. goto error;
  843. err = parse_input(codec);
  844. if (err < 0)
  845. goto error;
  846. err = parse_digital_output(codec);
  847. if (err < 0)
  848. goto error;
  849. err = parse_digital_input(codec);
  850. if (err < 0)
  851. goto error;
  852. codec->patch_ops = cs_patch_ops;
  853. return 0;
  854. error:
  855. kfree(codec->spec);
  856. codec->spec = NULL;
  857. return err;
  858. }
  859. /*
  860. * patch entries
  861. */
  862. static struct hda_codec_preset snd_hda_preset_cirrus[] = {
  863. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  864. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  865. {} /* terminator */
  866. };
  867. MODULE_ALIAS("snd-hda-codec-id:10134206");
  868. MODULE_ALIAS("snd-hda-codec-id:10134207");
  869. MODULE_LICENSE("GPL");
  870. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  871. static struct hda_codec_preset_list cirrus_list = {
  872. .preset = snd_hda_preset_cirrus,
  873. .owner = THIS_MODULE,
  874. };
  875. static int __init patch_cirrus_init(void)
  876. {
  877. return snd_hda_add_codec_preset(&cirrus_list);
  878. }
  879. static void __exit patch_cirrus_exit(void)
  880. {
  881. snd_hda_delete_codec_preset(&cirrus_list);
  882. }
  883. module_init(patch_cirrus_init)
  884. module_exit(patch_cirrus_exit)