patch_cirrus.c 51 KB

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