patch_cirrus.c 52 KB

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