patch_cirrus.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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. #include <sound/tlv.h>
  28. /*
  29. */
  30. struct cs_spec {
  31. int board_config;
  32. struct auto_pin_cfg autocfg;
  33. struct hda_multi_out multiout;
  34. struct snd_kcontrol *vmaster_sw;
  35. struct snd_kcontrol *vmaster_vol;
  36. hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
  37. hda_nid_t slave_dig_outs[2];
  38. unsigned int input_idx[AUTO_PIN_LAST];
  39. unsigned int capsrc_idx[AUTO_PIN_LAST];
  40. hda_nid_t adc_nid[AUTO_PIN_LAST];
  41. unsigned int adc_idx[AUTO_PIN_LAST];
  42. unsigned int num_inputs;
  43. unsigned int cur_input;
  44. unsigned int automic_idx;
  45. hda_nid_t cur_adc;
  46. unsigned int cur_adc_stream_tag;
  47. unsigned int cur_adc_format;
  48. hda_nid_t dig_in;
  49. const struct hda_bind_ctls *capture_bind[2];
  50. unsigned int gpio_mask;
  51. unsigned int gpio_dir;
  52. unsigned int gpio_data;
  53. struct hda_pcm pcm_rec[2]; /* PCM information */
  54. unsigned int hp_detect:1;
  55. unsigned int mic_detect:1;
  56. /* CS421x */
  57. unsigned int spdif_detect:1;
  58. unsigned int sense_b:1;
  59. hda_nid_t vendor_nid;
  60. struct hda_input_mux input_mux;
  61. unsigned int last_input;
  62. };
  63. /* available models with CS420x */
  64. enum {
  65. CS420X_MBP53,
  66. CS420X_MBP55,
  67. CS420X_IMAC27,
  68. CS420X_AUTO,
  69. CS420X_MODELS
  70. };
  71. /* CS421x boards */
  72. enum {
  73. CS421X_CDB4210,
  74. CS421X_MODELS
  75. };
  76. /* Vendor-specific processing widget */
  77. #define CS420X_VENDOR_NID 0x11
  78. #define CS_DIG_OUT1_PIN_NID 0x10
  79. #define CS_DIG_OUT2_PIN_NID 0x15
  80. #define CS_DMIC1_PIN_NID 0x12
  81. #define CS_DMIC2_PIN_NID 0x0e
  82. /* coef indices */
  83. #define IDX_SPDIF_STAT 0x0000
  84. #define IDX_SPDIF_CTL 0x0001
  85. #define IDX_ADC_CFG 0x0002
  86. /* SZC bitmask, 4 modes below:
  87. * 0 = immediate,
  88. * 1 = digital immediate, analog zero-cross
  89. * 2 = digtail & analog soft-ramp
  90. * 3 = digital soft-ramp, analog zero-cross
  91. */
  92. #define CS_COEF_ADC_SZC_MASK (3 << 0)
  93. #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
  94. #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
  95. /* PGA mode: 0 = differential, 1 = signle-ended */
  96. #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
  97. #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
  98. #define IDX_DAC_CFG 0x0003
  99. /* SZC bitmask, 4 modes below:
  100. * 0 = Immediate
  101. * 1 = zero-cross
  102. * 2 = soft-ramp
  103. * 3 = soft-ramp on zero-cross
  104. */
  105. #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
  106. #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
  107. #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
  108. #define IDX_BEEP_CFG 0x0004
  109. /* 0x0008 - test reg key */
  110. /* 0x0009 - 0x0014 -> 12 test regs */
  111. /* 0x0015 - visibility reg */
  112. /*
  113. * Cirrus Logic CS4210
  114. *
  115. * 1 DAC => HP(sense) / Speakers,
  116. * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
  117. * 1 SPDIF OUT => SPDIF Trasmitter(sense)
  118. */
  119. #define CS4210_DAC_NID 0x02
  120. #define CS4210_ADC_NID 0x03
  121. #define CS421X_VENDOR_NID 0x0B
  122. #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
  123. #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
  124. #define CS421X_IDX_DEV_CFG 0x01
  125. #define CS421X_IDX_ADC_CFG 0x02
  126. #define CS421X_IDX_DAC_CFG 0x03
  127. #define CS421X_IDX_SPK_CTL 0x04
  128. #define SPDIF_EVENT 0x04
  129. static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
  130. {
  131. struct cs_spec *spec = codec->spec;
  132. snd_hda_codec_write(codec, spec->vendor_nid, 0,
  133. AC_VERB_SET_COEF_INDEX, idx);
  134. return snd_hda_codec_read(codec, spec->vendor_nid, 0,
  135. AC_VERB_GET_PROC_COEF, 0);
  136. }
  137. static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  138. unsigned int coef)
  139. {
  140. struct cs_spec *spec = codec->spec;
  141. snd_hda_codec_write(codec, spec->vendor_nid, 0,
  142. AC_VERB_SET_COEF_INDEX, idx);
  143. snd_hda_codec_write(codec, spec->vendor_nid, 0,
  144. AC_VERB_SET_PROC_COEF, coef);
  145. }
  146. #define HP_EVENT 1
  147. #define MIC_EVENT 2
  148. /*
  149. * PCM callbacks
  150. */
  151. static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
  152. struct hda_codec *codec,
  153. struct snd_pcm_substream *substream)
  154. {
  155. struct cs_spec *spec = codec->spec;
  156. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  157. hinfo);
  158. }
  159. static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  160. struct hda_codec *codec,
  161. unsigned int stream_tag,
  162. unsigned int format,
  163. struct snd_pcm_substream *substream)
  164. {
  165. struct cs_spec *spec = codec->spec;
  166. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  167. stream_tag, format, substream);
  168. }
  169. static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  170. struct hda_codec *codec,
  171. struct snd_pcm_substream *substream)
  172. {
  173. struct cs_spec *spec = codec->spec;
  174. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  175. }
  176. /*
  177. * Digital out
  178. */
  179. static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  180. struct hda_codec *codec,
  181. struct snd_pcm_substream *substream)
  182. {
  183. struct cs_spec *spec = codec->spec;
  184. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  185. }
  186. static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  187. struct hda_codec *codec,
  188. struct snd_pcm_substream *substream)
  189. {
  190. struct cs_spec *spec = codec->spec;
  191. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  192. }
  193. static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  194. struct hda_codec *codec,
  195. unsigned int stream_tag,
  196. unsigned int format,
  197. struct snd_pcm_substream *substream)
  198. {
  199. struct cs_spec *spec = codec->spec;
  200. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  201. format, substream);
  202. }
  203. static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  204. struct hda_codec *codec,
  205. struct snd_pcm_substream *substream)
  206. {
  207. struct cs_spec *spec = codec->spec;
  208. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  209. }
  210. /*
  211. * Analog capture
  212. */
  213. static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  214. struct hda_codec *codec,
  215. unsigned int stream_tag,
  216. unsigned int format,
  217. struct snd_pcm_substream *substream)
  218. {
  219. struct cs_spec *spec = codec->spec;
  220. spec->cur_adc = spec->adc_nid[spec->cur_input];
  221. spec->cur_adc_stream_tag = stream_tag;
  222. spec->cur_adc_format = format;
  223. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  224. return 0;
  225. }
  226. static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  227. struct hda_codec *codec,
  228. struct snd_pcm_substream *substream)
  229. {
  230. struct cs_spec *spec = codec->spec;
  231. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  232. spec->cur_adc = 0;
  233. return 0;
  234. }
  235. /*
  236. */
  237. static const struct hda_pcm_stream cs_pcm_analog_playback = {
  238. .substreams = 1,
  239. .channels_min = 2,
  240. .channels_max = 2,
  241. .ops = {
  242. .open = cs_playback_pcm_open,
  243. .prepare = cs_playback_pcm_prepare,
  244. .cleanup = cs_playback_pcm_cleanup
  245. },
  246. };
  247. static const struct hda_pcm_stream cs_pcm_analog_capture = {
  248. .substreams = 1,
  249. .channels_min = 2,
  250. .channels_max = 2,
  251. .ops = {
  252. .prepare = cs_capture_pcm_prepare,
  253. .cleanup = cs_capture_pcm_cleanup
  254. },
  255. };
  256. static const struct hda_pcm_stream cs_pcm_digital_playback = {
  257. .substreams = 1,
  258. .channels_min = 2,
  259. .channels_max = 2,
  260. .ops = {
  261. .open = cs_dig_playback_pcm_open,
  262. .close = cs_dig_playback_pcm_close,
  263. .prepare = cs_dig_playback_pcm_prepare,
  264. .cleanup = cs_dig_playback_pcm_cleanup
  265. },
  266. };
  267. static const struct hda_pcm_stream cs_pcm_digital_capture = {
  268. .substreams = 1,
  269. .channels_min = 2,
  270. .channels_max = 2,
  271. };
  272. static int cs_build_pcms(struct hda_codec *codec)
  273. {
  274. struct cs_spec *spec = codec->spec;
  275. struct hda_pcm *info = spec->pcm_rec;
  276. codec->pcm_info = info;
  277. codec->num_pcms = 0;
  278. info->name = "Cirrus Analog";
  279. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
  280. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
  281. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  282. spec->multiout.max_channels;
  283. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
  284. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  285. spec->adc_nid[spec->cur_input];
  286. codec->num_pcms++;
  287. if (!spec->multiout.dig_out_nid && !spec->dig_in)
  288. return 0;
  289. info++;
  290. info->name = "Cirrus Digital";
  291. info->pcm_type = spec->autocfg.dig_out_type[0];
  292. if (!info->pcm_type)
  293. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  294. if (spec->multiout.dig_out_nid) {
  295. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  296. cs_pcm_digital_playback;
  297. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  298. spec->multiout.dig_out_nid;
  299. }
  300. if (spec->dig_in) {
  301. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  302. cs_pcm_digital_capture;
  303. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  304. }
  305. codec->num_pcms++;
  306. return 0;
  307. }
  308. /*
  309. * parse codec topology
  310. */
  311. static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
  312. {
  313. hda_nid_t dac;
  314. if (!pin)
  315. return 0;
  316. if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
  317. return 0;
  318. return dac;
  319. }
  320. static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
  321. {
  322. struct cs_spec *spec = codec->spec;
  323. struct auto_pin_cfg *cfg = &spec->autocfg;
  324. hda_nid_t pin = cfg->inputs[idx].pin;
  325. unsigned int val;
  326. if (!is_jack_detectable(codec, pin))
  327. return 0;
  328. val = snd_hda_codec_get_pincfg(codec, pin);
  329. return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
  330. }
  331. static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
  332. unsigned int *idxp)
  333. {
  334. int i, idx;
  335. hda_nid_t nid;
  336. nid = codec->start_nid;
  337. for (i = 0; i < codec->num_nodes; i++, nid++) {
  338. unsigned int type;
  339. type = get_wcaps_type(get_wcaps(codec, nid));
  340. if (type != AC_WID_AUD_IN)
  341. continue;
  342. idx = snd_hda_get_conn_index(codec, nid, pin, false);
  343. if (idx >= 0) {
  344. *idxp = idx;
  345. return nid;
  346. }
  347. }
  348. return 0;
  349. }
  350. static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
  351. {
  352. unsigned int val;
  353. val = snd_hda_codec_get_pincfg(codec, nid);
  354. return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
  355. }
  356. static int parse_output(struct hda_codec *codec)
  357. {
  358. struct cs_spec *spec = codec->spec;
  359. struct auto_pin_cfg *cfg = &spec->autocfg;
  360. int i, extra_nids;
  361. hda_nid_t dac;
  362. for (i = 0; i < cfg->line_outs; i++) {
  363. dac = get_dac(codec, cfg->line_out_pins[i]);
  364. if (!dac)
  365. break;
  366. spec->dac_nid[i] = dac;
  367. }
  368. spec->multiout.num_dacs = i;
  369. spec->multiout.dac_nids = spec->dac_nid;
  370. spec->multiout.max_channels = i * 2;
  371. /* add HP and speakers */
  372. extra_nids = 0;
  373. for (i = 0; i < cfg->hp_outs; i++) {
  374. dac = get_dac(codec, cfg->hp_pins[i]);
  375. if (!dac)
  376. break;
  377. if (!i)
  378. spec->multiout.hp_nid = dac;
  379. else
  380. spec->multiout.extra_out_nid[extra_nids++] = dac;
  381. }
  382. for (i = 0; i < cfg->speaker_outs; i++) {
  383. dac = get_dac(codec, cfg->speaker_pins[i]);
  384. if (!dac)
  385. break;
  386. spec->multiout.extra_out_nid[extra_nids++] = dac;
  387. }
  388. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  389. cfg->speaker_outs = cfg->line_outs;
  390. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  391. sizeof(cfg->speaker_pins));
  392. cfg->line_outs = 0;
  393. }
  394. return 0;
  395. }
  396. static int parse_input(struct hda_codec *codec)
  397. {
  398. struct cs_spec *spec = codec->spec;
  399. struct auto_pin_cfg *cfg = &spec->autocfg;
  400. int i;
  401. for (i = 0; i < cfg->num_inputs; i++) {
  402. hda_nid_t pin = cfg->inputs[i].pin;
  403. spec->input_idx[spec->num_inputs] = i;
  404. spec->capsrc_idx[i] = spec->num_inputs++;
  405. spec->cur_input = i;
  406. spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
  407. }
  408. if (!spec->num_inputs)
  409. return 0;
  410. /* check whether the automatic mic switch is available */
  411. if (spec->num_inputs == 2 &&
  412. cfg->inputs[0].type == AUTO_PIN_MIC &&
  413. cfg->inputs[1].type == AUTO_PIN_MIC) {
  414. if (is_ext_mic(codec, cfg->inputs[0].pin)) {
  415. if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
  416. spec->mic_detect = 1;
  417. spec->automic_idx = 0;
  418. }
  419. } else {
  420. if (is_ext_mic(codec, cfg->inputs[1].pin)) {
  421. spec->mic_detect = 1;
  422. spec->automic_idx = 1;
  423. }
  424. }
  425. }
  426. return 0;
  427. }
  428. static int parse_digital_output(struct hda_codec *codec)
  429. {
  430. struct cs_spec *spec = codec->spec;
  431. struct auto_pin_cfg *cfg = &spec->autocfg;
  432. hda_nid_t nid;
  433. if (!cfg->dig_outs)
  434. return 0;
  435. if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
  436. return 0;
  437. spec->multiout.dig_out_nid = nid;
  438. spec->multiout.share_spdif = 1;
  439. if (cfg->dig_outs > 1 &&
  440. snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
  441. spec->slave_dig_outs[0] = nid;
  442. codec->slave_dig_outs = spec->slave_dig_outs;
  443. }
  444. return 0;
  445. }
  446. static int parse_digital_input(struct hda_codec *codec)
  447. {
  448. struct cs_spec *spec = codec->spec;
  449. struct auto_pin_cfg *cfg = &spec->autocfg;
  450. int idx;
  451. if (cfg->dig_in_pin)
  452. spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
  453. return 0;
  454. }
  455. /*
  456. * create mixer controls
  457. */
  458. static const char * const dir_sfx[2] = { "Playback", "Capture" };
  459. static int add_mute(struct hda_codec *codec, const char *name, int index,
  460. unsigned int pval, int dir, struct snd_kcontrol **kctlp)
  461. {
  462. char tmp[44];
  463. struct snd_kcontrol_new knew =
  464. HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  465. knew.private_value = pval;
  466. snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
  467. *kctlp = snd_ctl_new1(&knew, codec);
  468. (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
  469. return snd_hda_ctl_add(codec, 0, *kctlp);
  470. }
  471. static int add_volume(struct hda_codec *codec, const char *name,
  472. int index, unsigned int pval, int dir,
  473. struct snd_kcontrol **kctlp)
  474. {
  475. char tmp[44];
  476. struct snd_kcontrol_new knew =
  477. HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  478. knew.private_value = pval;
  479. snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
  480. *kctlp = snd_ctl_new1(&knew, codec);
  481. (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
  482. return snd_hda_ctl_add(codec, 0, *kctlp);
  483. }
  484. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  485. {
  486. unsigned int caps;
  487. /* set the upper-limit for mixer amp to 0dB */
  488. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  489. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  490. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  491. << AC_AMPCAP_NUM_STEPS_SHIFT;
  492. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  493. }
  494. static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
  495. {
  496. struct cs_spec *spec = codec->spec;
  497. unsigned int tlv[4];
  498. int err;
  499. spec->vmaster_sw =
  500. snd_ctl_make_virtual_master("Master Playback Switch", NULL);
  501. err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
  502. if (err < 0)
  503. return err;
  504. snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
  505. spec->vmaster_vol =
  506. snd_ctl_make_virtual_master("Master Playback Volume", tlv);
  507. err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
  508. if (err < 0)
  509. return err;
  510. return 0;
  511. }
  512. static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
  513. int num_ctls, int type)
  514. {
  515. struct cs_spec *spec = codec->spec;
  516. const char *name;
  517. int err, index;
  518. struct snd_kcontrol *kctl;
  519. static const char * const speakers[] = {
  520. "Front Speaker", "Surround Speaker", "Bass Speaker"
  521. };
  522. static const char * const line_outs[] = {
  523. "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
  524. };
  525. fix_volume_caps(codec, dac);
  526. if (!spec->vmaster_sw) {
  527. err = add_vmaster(codec, dac);
  528. if (err < 0)
  529. return err;
  530. }
  531. index = 0;
  532. switch (type) {
  533. case AUTO_PIN_HP_OUT:
  534. name = "Headphone";
  535. index = idx;
  536. break;
  537. case AUTO_PIN_SPEAKER_OUT:
  538. if (num_ctls > 1)
  539. name = speakers[idx];
  540. else
  541. name = "Speaker";
  542. break;
  543. default:
  544. if (num_ctls > 1)
  545. name = line_outs[idx];
  546. else
  547. name = "Line-Out";
  548. break;
  549. }
  550. err = add_mute(codec, name, index,
  551. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  552. if (err < 0)
  553. return err;
  554. err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
  555. if (err < 0)
  556. return err;
  557. err = add_volume(codec, name, index,
  558. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  559. if (err < 0)
  560. return err;
  561. err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
  562. if (err < 0)
  563. return err;
  564. return 0;
  565. }
  566. static int build_output(struct hda_codec *codec)
  567. {
  568. struct cs_spec *spec = codec->spec;
  569. struct auto_pin_cfg *cfg = &spec->autocfg;
  570. int i, err;
  571. for (i = 0; i < cfg->line_outs; i++) {
  572. err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
  573. i, cfg->line_outs, cfg->line_out_type);
  574. if (err < 0)
  575. return err;
  576. }
  577. for (i = 0; i < cfg->hp_outs; i++) {
  578. err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
  579. i, cfg->hp_outs, AUTO_PIN_HP_OUT);
  580. if (err < 0)
  581. return err;
  582. }
  583. for (i = 0; i < cfg->speaker_outs; i++) {
  584. err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
  585. i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
  586. if (err < 0)
  587. return err;
  588. }
  589. return 0;
  590. }
  591. /*
  592. */
  593. static const struct snd_kcontrol_new cs_capture_ctls[] = {
  594. HDA_BIND_SW("Capture Switch", 0),
  595. HDA_BIND_VOL("Capture Volume", 0),
  596. };
  597. static int change_cur_input(struct hda_codec *codec, unsigned int idx,
  598. int force)
  599. {
  600. struct cs_spec *spec = codec->spec;
  601. if (spec->cur_input == idx && !force)
  602. return 0;
  603. if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
  604. /* stream is running, let's swap the current ADC */
  605. __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
  606. spec->cur_adc = spec->adc_nid[idx];
  607. snd_hda_codec_setup_stream(codec, spec->cur_adc,
  608. spec->cur_adc_stream_tag, 0,
  609. spec->cur_adc_format);
  610. }
  611. snd_hda_codec_write(codec, spec->cur_adc, 0,
  612. AC_VERB_SET_CONNECT_SEL,
  613. spec->adc_idx[idx]);
  614. spec->cur_input = idx;
  615. return 1;
  616. }
  617. static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
  618. struct snd_ctl_elem_info *uinfo)
  619. {
  620. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  621. struct cs_spec *spec = codec->spec;
  622. struct auto_pin_cfg *cfg = &spec->autocfg;
  623. unsigned int idx;
  624. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  625. uinfo->count = 1;
  626. uinfo->value.enumerated.items = spec->num_inputs;
  627. if (uinfo->value.enumerated.item >= spec->num_inputs)
  628. uinfo->value.enumerated.item = spec->num_inputs - 1;
  629. idx = spec->input_idx[uinfo->value.enumerated.item];
  630. strcpy(uinfo->value.enumerated.name,
  631. hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
  632. return 0;
  633. }
  634. static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
  635. struct snd_ctl_elem_value *ucontrol)
  636. {
  637. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  638. struct cs_spec *spec = codec->spec;
  639. ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
  640. return 0;
  641. }
  642. static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
  643. struct snd_ctl_elem_value *ucontrol)
  644. {
  645. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  646. struct cs_spec *spec = codec->spec;
  647. unsigned int idx = ucontrol->value.enumerated.item[0];
  648. if (idx >= spec->num_inputs)
  649. return -EINVAL;
  650. idx = spec->input_idx[idx];
  651. return change_cur_input(codec, idx, 0);
  652. }
  653. static const struct snd_kcontrol_new cs_capture_source = {
  654. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  655. .name = "Capture Source",
  656. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  657. .info = cs_capture_source_info,
  658. .get = cs_capture_source_get,
  659. .put = cs_capture_source_put,
  660. };
  661. static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
  662. struct hda_ctl_ops *ops)
  663. {
  664. struct cs_spec *spec = codec->spec;
  665. struct hda_bind_ctls *bind;
  666. int i, n;
  667. bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
  668. GFP_KERNEL);
  669. if (!bind)
  670. return NULL;
  671. bind->ops = ops;
  672. n = 0;
  673. for (i = 0; i < AUTO_PIN_LAST; i++) {
  674. if (!spec->adc_nid[i])
  675. continue;
  676. bind->values[n++] =
  677. HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
  678. spec->adc_idx[i], HDA_INPUT);
  679. }
  680. return bind;
  681. }
  682. /* add a (input-boost) volume control to the given input pin */
  683. static int add_input_volume_control(struct hda_codec *codec,
  684. struct auto_pin_cfg *cfg,
  685. int item)
  686. {
  687. hda_nid_t pin = cfg->inputs[item].pin;
  688. u32 caps;
  689. const char *label;
  690. struct snd_kcontrol *kctl;
  691. if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
  692. return 0;
  693. caps = query_amp_caps(codec, pin, HDA_INPUT);
  694. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  695. if (caps <= 1)
  696. return 0;
  697. label = hda_get_autocfg_input_label(codec, cfg, item);
  698. return add_volume(codec, label, 0,
  699. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
  700. }
  701. static int build_input(struct hda_codec *codec)
  702. {
  703. struct cs_spec *spec = codec->spec;
  704. int i, err;
  705. if (!spec->num_inputs)
  706. return 0;
  707. /* make bind-capture */
  708. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  709. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  710. for (i = 0; i < 2; i++) {
  711. struct snd_kcontrol *kctl;
  712. int n;
  713. if (!spec->capture_bind[i])
  714. return -ENOMEM;
  715. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  716. if (!kctl)
  717. return -ENOMEM;
  718. kctl->private_value = (long)spec->capture_bind[i];
  719. err = snd_hda_ctl_add(codec, 0, kctl);
  720. if (err < 0)
  721. return err;
  722. for (n = 0; n < AUTO_PIN_LAST; n++) {
  723. if (!spec->adc_nid[n])
  724. continue;
  725. err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
  726. if (err < 0)
  727. return err;
  728. }
  729. }
  730. if (spec->num_inputs > 1 && !spec->mic_detect) {
  731. err = snd_hda_ctl_add(codec, 0,
  732. snd_ctl_new1(&cs_capture_source, codec));
  733. if (err < 0)
  734. return err;
  735. }
  736. for (i = 0; i < spec->num_inputs; i++) {
  737. err = add_input_volume_control(codec, &spec->autocfg, i);
  738. if (err < 0)
  739. return err;
  740. }
  741. return 0;
  742. }
  743. /*
  744. */
  745. static int build_digital_output(struct hda_codec *codec)
  746. {
  747. struct cs_spec *spec = codec->spec;
  748. int err;
  749. if (!spec->multiout.dig_out_nid)
  750. return 0;
  751. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
  752. spec->multiout.dig_out_nid);
  753. if (err < 0)
  754. return err;
  755. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  756. if (err < 0)
  757. return err;
  758. return 0;
  759. }
  760. static int build_digital_input(struct hda_codec *codec)
  761. {
  762. struct cs_spec *spec = codec->spec;
  763. if (spec->dig_in)
  764. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  765. return 0;
  766. }
  767. /*
  768. * auto-mute and auto-mic switching
  769. * CS421x auto-output redirecting
  770. * HP/SPK/SPDIF
  771. */
  772. static void cs_automute(struct hda_codec *codec)
  773. {
  774. struct cs_spec *spec = codec->spec;
  775. struct auto_pin_cfg *cfg = &spec->autocfg;
  776. unsigned int hp_present;
  777. unsigned int spdif_present;
  778. hda_nid_t nid;
  779. int i;
  780. spdif_present = 0;
  781. if (cfg->dig_outs) {
  782. nid = cfg->dig_out_pins[0];
  783. if (is_jack_detectable(codec, nid)) {
  784. /*
  785. TODO: SPDIF output redirect when SENSE_B is enabled.
  786. Shared (SENSE_A) jack (e.g HP/mini-TOSLINK)
  787. assumed.
  788. */
  789. if (snd_hda_jack_detect(codec, nid)
  790. /* && spec->sense_b */)
  791. spdif_present = 1;
  792. }
  793. }
  794. hp_present = 0;
  795. for (i = 0; i < cfg->hp_outs; i++) {
  796. nid = cfg->hp_pins[i];
  797. if (!is_jack_detectable(codec, nid))
  798. continue;
  799. hp_present = snd_hda_jack_detect(codec, nid);
  800. if (hp_present)
  801. break;
  802. }
  803. /* mute speakers if spdif or hp jack is plugged in */
  804. for (i = 0; i < cfg->speaker_outs; i++) {
  805. nid = cfg->speaker_pins[i];
  806. snd_hda_codec_write(codec, nid, 0,
  807. AC_VERB_SET_PIN_WIDGET_CONTROL,
  808. hp_present ? 0 : PIN_OUT);
  809. /* detect on spdif is specific to CS421x */
  810. if (spec->vendor_nid == CS421X_VENDOR_NID) {
  811. snd_hda_codec_write(codec, nid, 0,
  812. AC_VERB_SET_PIN_WIDGET_CONTROL,
  813. spdif_present ? 0 : PIN_OUT);
  814. }
  815. }
  816. if (spec->board_config == CS420X_MBP53 ||
  817. spec->board_config == CS420X_MBP55 ||
  818. spec->board_config == CS420X_IMAC27) {
  819. unsigned int gpio = hp_present ? 0x02 : 0x08;
  820. snd_hda_codec_write(codec, 0x01, 0,
  821. AC_VERB_SET_GPIO_DATA, gpio);
  822. }
  823. /* specific to CS421x */
  824. if (spec->vendor_nid == CS421X_VENDOR_NID) {
  825. /* mute HPs if spdif jack (SENSE_B) is present */
  826. for (i = 0; i < cfg->hp_outs; i++) {
  827. nid = cfg->hp_pins[i];
  828. snd_hda_codec_write(codec, nid, 0,
  829. AC_VERB_SET_PIN_WIDGET_CONTROL,
  830. (spdif_present && spec->sense_b) ? 0 : PIN_HP);
  831. }
  832. /* SPDIF TX on/off */
  833. if (cfg->dig_outs) {
  834. nid = cfg->dig_out_pins[0];
  835. snd_hda_codec_write(codec, nid, 0,
  836. AC_VERB_SET_PIN_WIDGET_CONTROL,
  837. spdif_present ? PIN_OUT : 0);
  838. }
  839. /* Update board GPIOs if neccessary ... */
  840. }
  841. }
  842. /*
  843. * Auto-input redirect for CS421x
  844. * Switch max 3 inputs of a single ADC (nid 3)
  845. */
  846. static void cs_automic(struct hda_codec *codec)
  847. {
  848. struct cs_spec *spec = codec->spec;
  849. struct auto_pin_cfg *cfg = &spec->autocfg;
  850. hda_nid_t nid;
  851. unsigned int present;
  852. nid = cfg->inputs[spec->automic_idx].pin;
  853. present = snd_hda_jack_detect(codec, nid);
  854. /* specific to CS421x, single ADC */
  855. if (spec->vendor_nid == CS421X_VENDOR_NID) {
  856. if (present) {
  857. spec->last_input = spec->cur_input;
  858. spec->cur_input = spec->automic_idx;
  859. } else {
  860. spec->cur_input = spec->last_input;
  861. }
  862. snd_hda_codec_write_cache(codec, spec->cur_adc, 0,
  863. AC_VERB_SET_CONNECT_SEL,
  864. spec->adc_idx[spec->cur_input]);
  865. } else {
  866. if (present)
  867. change_cur_input(codec, spec->automic_idx, 0);
  868. else
  869. change_cur_input(codec, !spec->automic_idx, 0);
  870. }
  871. }
  872. /*
  873. */
  874. static void init_output(struct hda_codec *codec)
  875. {
  876. struct cs_spec *spec = codec->spec;
  877. struct auto_pin_cfg *cfg = &spec->autocfg;
  878. int i;
  879. /* mute first */
  880. for (i = 0; i < spec->multiout.num_dacs; i++)
  881. snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
  882. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  883. if (spec->multiout.hp_nid)
  884. snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
  885. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  886. for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
  887. if (!spec->multiout.extra_out_nid[i])
  888. break;
  889. snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
  890. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  891. }
  892. /* set appropriate pin controls */
  893. for (i = 0; i < cfg->line_outs; i++)
  894. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  895. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  896. /* HP */
  897. for (i = 0; i < cfg->hp_outs; i++) {
  898. hda_nid_t nid = cfg->hp_pins[i];
  899. snd_hda_codec_write(codec, nid, 0,
  900. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  901. if (!cfg->speaker_outs)
  902. continue;
  903. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  904. snd_hda_codec_write(codec, nid, 0,
  905. AC_VERB_SET_UNSOLICITED_ENABLE,
  906. AC_USRSP_EN | HP_EVENT);
  907. spec->hp_detect = 1;
  908. }
  909. }
  910. /* Speaker */
  911. for (i = 0; i < cfg->speaker_outs; i++)
  912. snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
  913. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  914. /* SPDIF is enabled on presence detect for CS421x */
  915. if (spec->hp_detect || spec->spdif_detect)
  916. cs_automute(codec);
  917. }
  918. static void init_input(struct hda_codec *codec)
  919. {
  920. struct cs_spec *spec = codec->spec;
  921. struct auto_pin_cfg *cfg = &spec->autocfg;
  922. unsigned int coef;
  923. int i;
  924. for (i = 0; i < cfg->num_inputs; i++) {
  925. unsigned int ctl;
  926. hda_nid_t pin = cfg->inputs[i].pin;
  927. if (!spec->adc_nid[i])
  928. continue;
  929. /* set appropriate pin control and mute first */
  930. ctl = PIN_IN;
  931. if (cfg->inputs[i].type == AUTO_PIN_MIC) {
  932. unsigned int caps = snd_hda_query_pin_caps(codec, pin);
  933. caps >>= AC_PINCAP_VREF_SHIFT;
  934. if (caps & AC_PINCAP_VREF_80)
  935. ctl = PIN_VREF80;
  936. }
  937. snd_hda_codec_write(codec, pin, 0,
  938. AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
  939. snd_hda_codec_write(codec, spec->adc_nid[i], 0,
  940. AC_VERB_SET_AMP_GAIN_MUTE,
  941. AMP_IN_MUTE(spec->adc_idx[i]));
  942. if (spec->mic_detect && spec->automic_idx == i)
  943. snd_hda_codec_write(codec, pin, 0,
  944. AC_VERB_SET_UNSOLICITED_ENABLE,
  945. AC_USRSP_EN | MIC_EVENT);
  946. }
  947. /* specific to CS421x */
  948. if (spec->vendor_nid == CS421X_VENDOR_NID) {
  949. if (spec->mic_detect)
  950. cs_automic(codec);
  951. else {
  952. spec->cur_adc = spec->adc_nid[spec->cur_input];
  953. snd_hda_codec_write(codec, spec->cur_adc, 0,
  954. AC_VERB_SET_CONNECT_SEL,
  955. spec->adc_idx[spec->cur_input]);
  956. }
  957. } else {
  958. change_cur_input(codec, spec->cur_input, 1);
  959. if (spec->mic_detect)
  960. cs_automic(codec);
  961. coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
  962. if (is_active_pin(codec, CS_DMIC2_PIN_NID))
  963. coef |= 0x0500; /* DMIC2 2 chan on, GPIO1 off */
  964. if (is_active_pin(codec, CS_DMIC1_PIN_NID))
  965. coef |= 0x1800; /* DMIC1 2 chan on, GPIO0 off
  966. * No effect if SPDIF_OUT2 is
  967. * selected in IDX_SPDIF_CTL.
  968. */
  969. cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
  970. }
  971. }
  972. static const struct hda_verb cs_coef_init_verbs[] = {
  973. {0x11, AC_VERB_SET_PROC_STATE, 1},
  974. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  975. {0x11, AC_VERB_SET_PROC_COEF,
  976. (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
  977. | 0x0040 /* Mute DACs on FIFO error */
  978. | 0x1000 /* Enable DACs High Pass Filter */
  979. | 0x0400 /* Disable Coefficient Auto increment */
  980. )},
  981. /* Beep */
  982. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  983. {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
  984. {} /* terminator */
  985. };
  986. /* Errata: CS4207 rev C0/C1/C2 Silicon
  987. *
  988. * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
  989. *
  990. * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
  991. * may be excessive (up to an additional 200 μA), which is most easily
  992. * observed while the part is being held in reset (RESET# active low).
  993. *
  994. * Root Cause: At initial powerup of the device, the logic that drives
  995. * the clock and write enable to the S/PDIF SRC RAMs is not properly
  996. * initialized.
  997. * Certain random patterns will cause a steady leakage current in those
  998. * RAM cells. The issue will resolve once the SRCs are used (turned on).
  999. *
  1000. * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
  1001. * blocks, which will alleviate the issue.
  1002. */
  1003. static const struct hda_verb cs_errata_init_verbs[] = {
  1004. {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
  1005. {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  1006. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  1007. {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
  1008. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  1009. {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
  1010. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  1011. {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
  1012. {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
  1013. {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
  1014. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  1015. {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
  1016. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  1017. {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
  1018. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  1019. {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
  1020. {0x11, AC_VERB_SET_PROC_STATE, 0x00},
  1021. #if 0 /* Don't to set to D3 as we are in power-up sequence */
  1022. {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
  1023. {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
  1024. /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
  1025. #endif
  1026. {} /* terminator */
  1027. };
  1028. /* SPDIF setup */
  1029. static void init_digital(struct hda_codec *codec)
  1030. {
  1031. unsigned int coef;
  1032. coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
  1033. coef |= 0x0008; /* Replace with mute on error */
  1034. if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
  1035. coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
  1036. * SPDIF_OUT2 is shared with GPIO1 and
  1037. * DMIC_SDA2.
  1038. */
  1039. cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
  1040. }
  1041. static int cs_init(struct hda_codec *codec)
  1042. {
  1043. struct cs_spec *spec = codec->spec;
  1044. /* init_verb sequence for C0/C1/C2 errata*/
  1045. snd_hda_sequence_write(codec, cs_errata_init_verbs);
  1046. snd_hda_sequence_write(codec, cs_coef_init_verbs);
  1047. if (spec->gpio_mask) {
  1048. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  1049. spec->gpio_mask);
  1050. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  1051. spec->gpio_dir);
  1052. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  1053. spec->gpio_data);
  1054. }
  1055. init_output(codec);
  1056. init_input(codec);
  1057. init_digital(codec);
  1058. return 0;
  1059. }
  1060. static int cs_build_controls(struct hda_codec *codec)
  1061. {
  1062. int err;
  1063. err = build_output(codec);
  1064. if (err < 0)
  1065. return err;
  1066. err = build_input(codec);
  1067. if (err < 0)
  1068. return err;
  1069. err = build_digital_output(codec);
  1070. if (err < 0)
  1071. return err;
  1072. err = build_digital_input(codec);
  1073. if (err < 0)
  1074. return err;
  1075. return cs_init(codec);
  1076. }
  1077. static void cs_free(struct hda_codec *codec)
  1078. {
  1079. struct cs_spec *spec = codec->spec;
  1080. kfree(spec->capture_bind[0]);
  1081. kfree(spec->capture_bind[1]);
  1082. kfree(codec->spec);
  1083. }
  1084. static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
  1085. {
  1086. switch ((res >> 26) & 0x7f) {
  1087. case HP_EVENT:
  1088. cs_automute(codec);
  1089. break;
  1090. case MIC_EVENT:
  1091. cs_automic(codec);
  1092. break;
  1093. }
  1094. }
  1095. static const struct hda_codec_ops cs_patch_ops = {
  1096. .build_controls = cs_build_controls,
  1097. .build_pcms = cs_build_pcms,
  1098. .init = cs_init,
  1099. .free = cs_free,
  1100. .unsol_event = cs_unsol_event,
  1101. };
  1102. static int cs_parse_auto_config(struct hda_codec *codec)
  1103. {
  1104. struct cs_spec *spec = codec->spec;
  1105. int err;
  1106. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  1107. if (err < 0)
  1108. return err;
  1109. err = parse_output(codec);
  1110. if (err < 0)
  1111. return err;
  1112. err = parse_input(codec);
  1113. if (err < 0)
  1114. return err;
  1115. err = parse_digital_output(codec);
  1116. if (err < 0)
  1117. return err;
  1118. err = parse_digital_input(codec);
  1119. if (err < 0)
  1120. return err;
  1121. return 0;
  1122. }
  1123. static const char * const cs420x_models[CS420X_MODELS] = {
  1124. [CS420X_MBP53] = "mbp53",
  1125. [CS420X_MBP55] = "mbp55",
  1126. [CS420X_IMAC27] = "imac27",
  1127. [CS420X_AUTO] = "auto",
  1128. };
  1129. static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
  1130. SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
  1131. SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
  1132. SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
  1133. SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
  1134. SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
  1135. {} /* terminator */
  1136. };
  1137. struct cs_pincfg {
  1138. hda_nid_t nid;
  1139. u32 val;
  1140. };
  1141. static const struct cs_pincfg mbp53_pincfgs[] = {
  1142. { 0x09, 0x012b4050 },
  1143. { 0x0a, 0x90100141 },
  1144. { 0x0b, 0x90100140 },
  1145. { 0x0c, 0x018b3020 },
  1146. { 0x0d, 0x90a00110 },
  1147. { 0x0e, 0x400000f0 },
  1148. { 0x0f, 0x01cbe030 },
  1149. { 0x10, 0x014be060 },
  1150. { 0x12, 0x400000f0 },
  1151. { 0x15, 0x400000f0 },
  1152. {} /* terminator */
  1153. };
  1154. static const struct cs_pincfg mbp55_pincfgs[] = {
  1155. { 0x09, 0x012b4030 },
  1156. { 0x0a, 0x90100121 },
  1157. { 0x0b, 0x90100120 },
  1158. { 0x0c, 0x400000f0 },
  1159. { 0x0d, 0x90a00110 },
  1160. { 0x0e, 0x400000f0 },
  1161. { 0x0f, 0x400000f0 },
  1162. { 0x10, 0x014be040 },
  1163. { 0x12, 0x400000f0 },
  1164. { 0x15, 0x400000f0 },
  1165. {} /* terminator */
  1166. };
  1167. static const struct cs_pincfg imac27_pincfgs[] = {
  1168. { 0x09, 0x012b4050 },
  1169. { 0x0a, 0x90100140 },
  1170. { 0x0b, 0x90100142 },
  1171. { 0x0c, 0x018b3020 },
  1172. { 0x0d, 0x90a00110 },
  1173. { 0x0e, 0x400000f0 },
  1174. { 0x0f, 0x01cbe030 },
  1175. { 0x10, 0x014be060 },
  1176. { 0x12, 0x01ab9070 },
  1177. { 0x15, 0x400000f0 },
  1178. {} /* terminator */
  1179. };
  1180. static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
  1181. [CS420X_MBP53] = mbp53_pincfgs,
  1182. [CS420X_MBP55] = mbp55_pincfgs,
  1183. [CS420X_IMAC27] = imac27_pincfgs,
  1184. };
  1185. static void fix_pincfg(struct hda_codec *codec, int model,
  1186. const struct cs_pincfg **pin_configs)
  1187. {
  1188. const struct cs_pincfg *cfg = pin_configs[model];
  1189. if (!cfg)
  1190. return;
  1191. for (; cfg->nid; cfg++)
  1192. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  1193. }
  1194. static int patch_cs420x(struct hda_codec *codec)
  1195. {
  1196. struct cs_spec *spec;
  1197. int err;
  1198. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1199. if (!spec)
  1200. return -ENOMEM;
  1201. codec->spec = spec;
  1202. spec->vendor_nid = CS420X_VENDOR_NID;
  1203. spec->board_config =
  1204. snd_hda_check_board_config(codec, CS420X_MODELS,
  1205. cs420x_models, cs420x_cfg_tbl);
  1206. if (spec->board_config >= 0)
  1207. fix_pincfg(codec, spec->board_config, cs_pincfgs);
  1208. switch (spec->board_config) {
  1209. case CS420X_IMAC27:
  1210. case CS420X_MBP53:
  1211. case CS420X_MBP55:
  1212. /* GPIO1 = headphones */
  1213. /* GPIO3 = speakers */
  1214. spec->gpio_mask = 0x0a;
  1215. spec->gpio_dir = 0x0a;
  1216. break;
  1217. }
  1218. err = cs_parse_auto_config(codec);
  1219. if (err < 0)
  1220. goto error;
  1221. codec->patch_ops = cs_patch_ops;
  1222. return 0;
  1223. error:
  1224. kfree(codec->spec);
  1225. codec->spec = NULL;
  1226. return err;
  1227. }
  1228. /*
  1229. * Cirrus Logic CS4210
  1230. *
  1231. * 1 DAC => HP(sense) / Speakers,
  1232. * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
  1233. * 1 SPDIF OUT => SPDIF Trasmitter(sense)
  1234. */
  1235. /* CS4210 board names */
  1236. static const char *cs421x_models[CS421X_MODELS] = {
  1237. [CS421X_CDB4210] = "cdb4210",
  1238. };
  1239. static const struct snd_pci_quirk cs421x_cfg_tbl[] = {
  1240. /* Test Intel board + CDB2410 */
  1241. SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
  1242. {} /* terminator */
  1243. };
  1244. /* CS4210 board pinconfigs */
  1245. /* Default CS4210 (CDB4210)*/
  1246. static const struct cs_pincfg cdb4210_pincfgs[] = {
  1247. { 0x05, 0x0321401f },
  1248. { 0x06, 0x90170010 },
  1249. { 0x07, 0x03813031 },
  1250. { 0x08, 0xb7a70037 },
  1251. { 0x09, 0xb7a6003e },
  1252. { 0x0a, 0x034510f0 },
  1253. {} /* terminator */
  1254. };
  1255. static const struct cs_pincfg *cs421x_pincfgs[CS421X_MODELS] = {
  1256. [CS421X_CDB4210] = cdb4210_pincfgs,
  1257. };
  1258. static const struct hda_verb cs421x_coef_init_verbs[] = {
  1259. {0x0B, AC_VERB_SET_PROC_STATE, 1},
  1260. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
  1261. /*
  1262. Disable Coefficient Index Auto-Increment(DAI)=1,
  1263. PDREF=0
  1264. */
  1265. {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
  1266. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
  1267. /* ADC SZCMode = Digital Soft Ramp */
  1268. {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
  1269. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
  1270. {0x0B, AC_VERB_SET_PROC_COEF,
  1271. (0x0002 /* DAC SZCMode = Digital Soft Ramp */
  1272. | 0x0004 /* Mute DAC on FIFO error */
  1273. | 0x0008 /* Enable DAC High Pass Filter */
  1274. )},
  1275. {} /* terminator */
  1276. };
  1277. /* Errata: CS4210 rev A1 Silicon
  1278. *
  1279. * http://www.cirrus.com/en/pubs/errata/
  1280. *
  1281. * Description:
  1282. * 1. Performance degredation is present in the ADC.
  1283. * 2. Speaker output is not completely muted upon HP detect.
  1284. * 3. Noise is present when clipping occurs on the amplified
  1285. * speaker outputs.
  1286. *
  1287. * Workaround:
  1288. * The following verb sequence written to the registers during
  1289. * initialization will correct the issues listed above.
  1290. */
  1291. static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
  1292. {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  1293. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
  1294. {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
  1295. {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
  1296. {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
  1297. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
  1298. {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
  1299. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
  1300. {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
  1301. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
  1302. {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
  1303. {} /* terminator */
  1304. };
  1305. /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
  1306. static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
  1307. static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
  1308. struct snd_ctl_elem_info *uinfo)
  1309. {
  1310. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1311. uinfo->count = 1;
  1312. uinfo->value.integer.min = 0;
  1313. uinfo->value.integer.max = 3;
  1314. return 0;
  1315. }
  1316. static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
  1317. struct snd_ctl_elem_value *ucontrol)
  1318. {
  1319. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1320. ucontrol->value.integer.value[0] =
  1321. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
  1322. return 0;
  1323. }
  1324. static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
  1325. struct snd_ctl_elem_value *ucontrol)
  1326. {
  1327. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1328. unsigned int vol = ucontrol->value.integer.value[0];
  1329. unsigned int coef =
  1330. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
  1331. unsigned int original_coef = coef;
  1332. coef &= ~0x0003;
  1333. coef |= (vol & 0x0003);
  1334. if (original_coef == coef)
  1335. return 0;
  1336. else {
  1337. cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
  1338. return 1;
  1339. }
  1340. }
  1341. static const struct snd_kcontrol_new cs421x_speaker_bost_ctl = {
  1342. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1343. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1344. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  1345. .name = "Speaker Boost Playback Volume",
  1346. .info = cs421x_boost_vol_info,
  1347. .get = cs421x_boost_vol_get,
  1348. .put = cs421x_boost_vol_put,
  1349. .tlv = { .p = cs421x_speaker_boost_db_scale },
  1350. };
  1351. static void cs421x_pinmux_init(struct hda_codec *codec)
  1352. {
  1353. struct cs_spec *spec = codec->spec;
  1354. unsigned int def_conf, coef;
  1355. /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
  1356. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  1357. if (spec->gpio_mask)
  1358. coef |= 0x0008; /* B1,B2 are GPIOs */
  1359. else
  1360. coef &= ~0x0008;
  1361. if (spec->sense_b)
  1362. coef |= 0x0010; /* B2 is SENSE_B, not inverted */
  1363. else
  1364. coef &= ~0x0010;
  1365. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  1366. if ((spec->gpio_mask || spec->sense_b) &&
  1367. is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
  1368. /*
  1369. GPIO or SENSE_B forced - disconnect the DMIC pin.
  1370. */
  1371. def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
  1372. def_conf &= ~AC_DEFCFG_PORT_CONN;
  1373. def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
  1374. snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
  1375. }
  1376. }
  1377. static void init_cs421x_digital(struct hda_codec *codec)
  1378. {
  1379. struct cs_spec *spec = codec->spec;
  1380. struct auto_pin_cfg *cfg = &spec->autocfg;
  1381. int i;
  1382. for (i = 0; i < cfg->dig_outs; i++) {
  1383. hda_nid_t nid = cfg->dig_out_pins[i];
  1384. if (!cfg->speaker_outs)
  1385. continue;
  1386. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  1387. snd_hda_codec_write(codec, nid, 0,
  1388. AC_VERB_SET_UNSOLICITED_ENABLE,
  1389. AC_USRSP_EN | SPDIF_EVENT);
  1390. spec->spdif_detect = 1;
  1391. }
  1392. }
  1393. }
  1394. static int cs421x_init(struct hda_codec *codec)
  1395. {
  1396. struct cs_spec *spec = codec->spec;
  1397. snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
  1398. snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
  1399. cs421x_pinmux_init(codec);
  1400. if (spec->gpio_mask) {
  1401. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  1402. spec->gpio_mask);
  1403. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  1404. spec->gpio_dir);
  1405. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  1406. spec->gpio_data);
  1407. }
  1408. init_output(codec);
  1409. init_input(codec);
  1410. init_cs421x_digital(codec);
  1411. return 0;
  1412. }
  1413. /*
  1414. * CS4210 Input MUX (1 ADC)
  1415. */
  1416. static int cs421x_mux_enum_info(struct snd_kcontrol *kcontrol,
  1417. struct snd_ctl_elem_info *uinfo)
  1418. {
  1419. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1420. struct cs_spec *spec = codec->spec;
  1421. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  1422. }
  1423. static int cs421x_mux_enum_get(struct snd_kcontrol *kcontrol,
  1424. struct snd_ctl_elem_value *ucontrol)
  1425. {
  1426. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1427. struct cs_spec *spec = codec->spec;
  1428. ucontrol->value.enumerated.item[0] = spec->cur_input;
  1429. return 0;
  1430. }
  1431. static int cs421x_mux_enum_put(struct snd_kcontrol *kcontrol,
  1432. struct snd_ctl_elem_value *ucontrol)
  1433. {
  1434. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1435. struct cs_spec *spec = codec->spec;
  1436. return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
  1437. spec->adc_nid[0], &spec->cur_input);
  1438. }
  1439. static struct snd_kcontrol_new cs421x_capture_source = {
  1440. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1441. .name = "Capture Source",
  1442. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1443. .info = cs421x_mux_enum_info,
  1444. .get = cs421x_mux_enum_get,
  1445. .put = cs421x_mux_enum_put,
  1446. };
  1447. static int cs421x_add_input_volume_control(struct hda_codec *codec, int item)
  1448. {
  1449. struct cs_spec *spec = codec->spec;
  1450. struct auto_pin_cfg *cfg = &spec->autocfg;
  1451. const struct hda_input_mux *imux = &spec->input_mux;
  1452. hda_nid_t pin = cfg->inputs[item].pin;
  1453. struct snd_kcontrol *kctl;
  1454. u32 caps;
  1455. if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
  1456. return 0;
  1457. caps = query_amp_caps(codec, pin, HDA_INPUT);
  1458. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1459. if (caps <= 1)
  1460. return 0;
  1461. return add_volume(codec, imux->items[item].label, 0,
  1462. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
  1463. }
  1464. /* add a (input-boost) volume control to the given input pin */
  1465. static int build_cs421x_input(struct hda_codec *codec)
  1466. {
  1467. struct cs_spec *spec = codec->spec;
  1468. struct auto_pin_cfg *cfg = &spec->autocfg;
  1469. struct hda_input_mux *imux = &spec->input_mux;
  1470. int i, err, type_idx;
  1471. const char *label;
  1472. if (!spec->num_inputs)
  1473. return 0;
  1474. /* make bind-capture */
  1475. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  1476. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  1477. for (i = 0; i < 2; i++) {
  1478. struct snd_kcontrol *kctl;
  1479. int n;
  1480. if (!spec->capture_bind[i])
  1481. return -ENOMEM;
  1482. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  1483. if (!kctl)
  1484. return -ENOMEM;
  1485. kctl->private_value = (long)spec->capture_bind[i];
  1486. err = snd_hda_ctl_add(codec, 0, kctl);
  1487. if (err < 0)
  1488. return err;
  1489. for (n = 0; n < AUTO_PIN_LAST; n++) {
  1490. if (!spec->adc_nid[n])
  1491. continue;
  1492. err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
  1493. if (err < 0)
  1494. return err;
  1495. }
  1496. }
  1497. /* Add Input MUX Items + Capture Volume/Switch */
  1498. for (i = 0; i < spec->num_inputs; i++) {
  1499. label = hda_get_autocfg_input_label(codec, cfg, i);
  1500. snd_hda_add_imux_item(imux, label, spec->adc_idx[i], &type_idx);
  1501. err = cs421x_add_input_volume_control(codec, i);
  1502. if (err < 0)
  1503. return err;
  1504. }
  1505. /*
  1506. Add 'Capture Source' Switch if
  1507. * 2 inputs and no mic detec
  1508. * 3 inputs
  1509. */
  1510. if ((spec->num_inputs == 2 && !spec->mic_detect) ||
  1511. (spec->num_inputs == 3)) {
  1512. err = snd_hda_ctl_add(codec, spec->adc_nid[0],
  1513. snd_ctl_new1(&cs421x_capture_source, codec));
  1514. if (err < 0)
  1515. return err;
  1516. }
  1517. return 0;
  1518. }
  1519. /* Single DAC (Mute/Gain) */
  1520. static int build_cs421x_output(struct hda_codec *codec)
  1521. {
  1522. hda_nid_t dac = CS4210_DAC_NID;
  1523. struct cs_spec *spec = codec->spec;
  1524. struct auto_pin_cfg *cfg = &spec->autocfg;
  1525. struct snd_kcontrol *kctl;
  1526. int err;
  1527. char *name = "HP/Speakers";
  1528. fix_volume_caps(codec, dac);
  1529. if (!spec->vmaster_sw) {
  1530. err = add_vmaster(codec, dac);
  1531. if (err < 0)
  1532. return err;
  1533. }
  1534. err = add_mute(codec, name, 0,
  1535. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  1536. if (err < 0)
  1537. return err;
  1538. err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
  1539. if (err < 0)
  1540. return err;
  1541. err = add_volume(codec, name, 0,
  1542. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  1543. if (err < 0)
  1544. return err;
  1545. err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
  1546. if (err < 0)
  1547. return err;
  1548. if (cfg->speaker_outs) {
  1549. err = snd_hda_ctl_add(codec, 0,
  1550. snd_ctl_new1(&cs421x_speaker_bost_ctl, codec));
  1551. if (err < 0)
  1552. return err;
  1553. }
  1554. return err;
  1555. }
  1556. static int cs421x_build_controls(struct hda_codec *codec)
  1557. {
  1558. int err;
  1559. err = build_cs421x_output(codec);
  1560. if (err < 0)
  1561. return err;
  1562. err = build_cs421x_input(codec);
  1563. if (err < 0)
  1564. return err;
  1565. err = build_digital_output(codec);
  1566. if (err < 0)
  1567. return err;
  1568. return cs421x_init(codec);
  1569. }
  1570. static void cs421x_unsol_event(struct hda_codec *codec, unsigned int res)
  1571. {
  1572. switch ((res >> 26) & 0x3f) {
  1573. case HP_EVENT:
  1574. case SPDIF_EVENT:
  1575. cs_automute(codec);
  1576. break;
  1577. case MIC_EVENT:
  1578. cs_automic(codec);
  1579. break;
  1580. }
  1581. }
  1582. static int parse_cs421x_input(struct hda_codec *codec)
  1583. {
  1584. struct cs_spec *spec = codec->spec;
  1585. struct auto_pin_cfg *cfg = &spec->autocfg;
  1586. int i;
  1587. for (i = 0; i < cfg->num_inputs; i++) {
  1588. hda_nid_t pin = cfg->inputs[i].pin;
  1589. spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
  1590. spec->cur_input = spec->last_input = i;
  1591. spec->num_inputs++;
  1592. /* check whether the automatic mic switch is available */
  1593. if (is_ext_mic(codec, i) && cfg->num_inputs >= 2) {
  1594. spec->mic_detect = 1;
  1595. spec->automic_idx = i;
  1596. }
  1597. }
  1598. return 0;
  1599. }
  1600. static int cs421x_parse_auto_config(struct hda_codec *codec)
  1601. {
  1602. struct cs_spec *spec = codec->spec;
  1603. int err;
  1604. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  1605. if (err < 0)
  1606. return err;
  1607. err = parse_output(codec);
  1608. if (err < 0)
  1609. return err;
  1610. err = parse_cs421x_input(codec);
  1611. if (err < 0)
  1612. return err;
  1613. err = parse_digital_output(codec);
  1614. if (err < 0)
  1615. return err;
  1616. return 0;
  1617. }
  1618. #ifdef CONFIG_PM
  1619. /*
  1620. Manage PDREF, when transitioning to D3hot
  1621. (DAC,ADC) -> D3, PDREF=1, AFG->D3
  1622. */
  1623. static int cs421x_suspend(struct hda_codec *codec, pm_message_t state)
  1624. {
  1625. unsigned int coef;
  1626. snd_hda_shutup_pins(codec);
  1627. snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
  1628. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  1629. snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
  1630. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  1631. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  1632. coef |= 0x0004; /* PDREF */
  1633. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  1634. return 0;
  1635. }
  1636. #endif
  1637. static struct hda_codec_ops cs4210_patch_ops = {
  1638. .build_controls = cs421x_build_controls,
  1639. .build_pcms = cs_build_pcms,
  1640. .init = cs421x_init,
  1641. .free = cs_free,
  1642. .unsol_event = cs421x_unsol_event,
  1643. #ifdef CONFIG_PM
  1644. .suspend = cs421x_suspend,
  1645. #endif
  1646. };
  1647. static int patch_cs421x(struct hda_codec *codec)
  1648. {
  1649. struct cs_spec *spec;
  1650. int err;
  1651. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1652. if (!spec)
  1653. return -ENOMEM;
  1654. codec->spec = spec;
  1655. spec->vendor_nid = CS421X_VENDOR_NID;
  1656. spec->board_config =
  1657. snd_hda_check_board_config(codec, CS421X_MODELS,
  1658. cs421x_models, cs421x_cfg_tbl);
  1659. if (spec->board_config >= 0)
  1660. fix_pincfg(codec, spec->board_config, cs421x_pincfgs);
  1661. /*
  1662. Setup GPIO/SENSE for each board (if used)
  1663. */
  1664. switch (spec->board_config) {
  1665. case CS421X_CDB4210:
  1666. snd_printd("CS4210 board: %s\n",
  1667. cs421x_models[spec->board_config]);
  1668. /* spec->gpio_mask = 3;
  1669. spec->gpio_dir = 3;
  1670. spec->gpio_data = 3;
  1671. */
  1672. spec->sense_b = 1;
  1673. break;
  1674. }
  1675. /*
  1676. Update the GPIO/DMIC/SENSE_B pinmux before the configuration
  1677. is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
  1678. is disabled.
  1679. */
  1680. cs421x_pinmux_init(codec);
  1681. err = cs421x_parse_auto_config(codec);
  1682. if (err < 0)
  1683. goto error;
  1684. codec->patch_ops = cs4210_patch_ops;
  1685. return 0;
  1686. error:
  1687. kfree(codec->spec);
  1688. codec->spec = NULL;
  1689. return err;
  1690. }
  1691. /*
  1692. * patch entries
  1693. */
  1694. static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
  1695. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  1696. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  1697. { .id = 0x10134210, .name = "CS4210", .patch = patch_cs421x },
  1698. {} /* terminator */
  1699. };
  1700. MODULE_ALIAS("snd-hda-codec-id:10134206");
  1701. MODULE_ALIAS("snd-hda-codec-id:10134207");
  1702. MODULE_ALIAS("snd-hda-codec-id:10134210");
  1703. MODULE_LICENSE("GPL");
  1704. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  1705. static struct hda_codec_preset_list cirrus_list = {
  1706. .preset = snd_hda_preset_cirrus,
  1707. .owner = THIS_MODULE,
  1708. };
  1709. static int __init patch_cirrus_init(void)
  1710. {
  1711. return snd_hda_add_codec_preset(&cirrus_list);
  1712. }
  1713. static void __exit patch_cirrus_exit(void)
  1714. {
  1715. snd_hda_delete_codec_preset(&cirrus_list);
  1716. }
  1717. module_init(patch_cirrus_init)
  1718. module_exit(patch_cirrus_exit)