patch_cirrus.c 50 KB

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