patch_cirrus.c 30 KB

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