patch_cirrus.c 50 KB

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