patch_cirrus.c 50 KB

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