patch_cirrus.c 50 KB

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