patch_cirrus.c 32 KB

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