patch_cirrus.c 53 KB

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