patch_cirrus.c 33 KB

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