patch_cirrus.c 53 KB

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