patch_cirrus.c 52 KB

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