patch_cirrus.c 51 KB

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