patch_cirrus.c 53 KB

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