patch_cirrus.c 52 KB

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