patch_cirrus.c 51 KB

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