patch_cirrus.c 52 KB

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