patch_cirrus.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * HD audio interface patch for Cirrus Logic CS420x chip
  3. *
  4. * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This driver is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This driver is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <sound/core.h>
  25. #include "hda_codec.h"
  26. #include "hda_local.h"
  27. /*
  28. */
  29. struct cs_spec {
  30. int board_config;
  31. struct auto_pin_cfg autocfg;
  32. struct hda_multi_out multiout;
  33. struct snd_kcontrol *vmaster_sw;
  34. struct snd_kcontrol *vmaster_vol;
  35. hda_nid_t dac_nid[AUTO_CFG_MAX_OUTS];
  36. hda_nid_t slave_dig_outs[2];
  37. unsigned int input_idx[AUTO_PIN_LAST];
  38. unsigned int capsrc_idx[AUTO_PIN_LAST];
  39. hda_nid_t adc_nid[AUTO_PIN_LAST];
  40. unsigned int adc_idx[AUTO_PIN_LAST];
  41. unsigned int num_inputs;
  42. unsigned int cur_input;
  43. unsigned int automic_idx;
  44. hda_nid_t cur_adc;
  45. unsigned int cur_adc_stream_tag;
  46. unsigned int cur_adc_format;
  47. hda_nid_t dig_in;
  48. const struct hda_bind_ctls *capture_bind[2];
  49. unsigned int gpio_mask;
  50. unsigned int gpio_dir;
  51. unsigned int gpio_data;
  52. struct hda_pcm pcm_rec[2]; /* PCM information */
  53. unsigned int hp_detect:1;
  54. unsigned int mic_detect:1;
  55. };
  56. /* available models */
  57. enum {
  58. CS420X_MBP53,
  59. CS420X_MBP55,
  60. CS420X_IMAC27,
  61. CS420X_AUTO,
  62. CS420X_MODELS
  63. };
  64. /* Vendor-specific processing widget */
  65. #define CS420X_VENDOR_NID 0x11
  66. #define CS_DIG_OUT1_PIN_NID 0x10
  67. #define CS_DIG_OUT2_PIN_NID 0x15
  68. #define CS_DMIC1_PIN_NID 0x12
  69. #define CS_DMIC2_PIN_NID 0x0e
  70. /* coef indices */
  71. #define IDX_SPDIF_STAT 0x0000
  72. #define IDX_SPDIF_CTL 0x0001
  73. #define IDX_ADC_CFG 0x0002
  74. /* SZC bitmask, 4 modes below:
  75. * 0 = immediate,
  76. * 1 = digital immediate, analog zero-cross
  77. * 2 = digtail & analog soft-ramp
  78. * 3 = digital soft-ramp, analog zero-cross
  79. */
  80. #define CS_COEF_ADC_SZC_MASK (3 << 0)
  81. #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
  82. #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
  83. /* PGA mode: 0 = differential, 1 = signle-ended */
  84. #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
  85. #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
  86. #define IDX_DAC_CFG 0x0003
  87. /* SZC bitmask, 4 modes below:
  88. * 0 = Immediate
  89. * 1 = zero-cross
  90. * 2 = soft-ramp
  91. * 3 = soft-ramp on zero-cross
  92. */
  93. #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
  94. #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
  95. #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
  96. #define IDX_BEEP_CFG 0x0004
  97. /* 0x0008 - test reg key */
  98. /* 0x0009 - 0x0014 -> 12 test regs */
  99. /* 0x0015 - visibility reg */
  100. static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
  101. {
  102. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  103. AC_VERB_SET_COEF_INDEX, idx);
  104. return snd_hda_codec_read(codec, CS420X_VENDOR_NID, 0,
  105. AC_VERB_GET_PROC_COEF, 0);
  106. }
  107. static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  108. unsigned int coef)
  109. {
  110. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  111. AC_VERB_SET_COEF_INDEX, idx);
  112. snd_hda_codec_write(codec, CS420X_VENDOR_NID, 0,
  113. AC_VERB_SET_PROC_COEF, coef);
  114. }
  115. #define HP_EVENT 1
  116. #define MIC_EVENT 2
  117. /*
  118. * PCM callbacks
  119. */
  120. static int cs_playback_pcm_open(struct hda_pcm_stream *hinfo,
  121. struct hda_codec *codec,
  122. struct snd_pcm_substream *substream)
  123. {
  124. struct cs_spec *spec = codec->spec;
  125. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  126. hinfo);
  127. }
  128. static int cs_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  129. struct hda_codec *codec,
  130. unsigned int stream_tag,
  131. unsigned int format,
  132. struct snd_pcm_substream *substream)
  133. {
  134. struct cs_spec *spec = codec->spec;
  135. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  136. stream_tag, format, substream);
  137. }
  138. static int cs_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  139. struct hda_codec *codec,
  140. struct snd_pcm_substream *substream)
  141. {
  142. struct cs_spec *spec = codec->spec;
  143. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  144. }
  145. /*
  146. * Digital out
  147. */
  148. static int cs_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  149. struct hda_codec *codec,
  150. struct snd_pcm_substream *substream)
  151. {
  152. struct cs_spec *spec = codec->spec;
  153. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  154. }
  155. static int cs_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  156. struct hda_codec *codec,
  157. struct snd_pcm_substream *substream)
  158. {
  159. struct cs_spec *spec = codec->spec;
  160. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  161. }
  162. static int cs_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  163. struct hda_codec *codec,
  164. unsigned int stream_tag,
  165. unsigned int format,
  166. struct snd_pcm_substream *substream)
  167. {
  168. struct cs_spec *spec = codec->spec;
  169. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  170. format, substream);
  171. }
  172. static int cs_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  173. struct hda_codec *codec,
  174. struct snd_pcm_substream *substream)
  175. {
  176. struct cs_spec *spec = codec->spec;
  177. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  178. }
  179. /*
  180. * Analog capture
  181. */
  182. static int cs_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  183. struct hda_codec *codec,
  184. unsigned int stream_tag,
  185. unsigned int format,
  186. struct snd_pcm_substream *substream)
  187. {
  188. struct cs_spec *spec = codec->spec;
  189. spec->cur_adc = spec->adc_nid[spec->cur_input];
  190. spec->cur_adc_stream_tag = stream_tag;
  191. spec->cur_adc_format = format;
  192. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  193. return 0;
  194. }
  195. static int cs_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  196. struct hda_codec *codec,
  197. struct snd_pcm_substream *substream)
  198. {
  199. struct cs_spec *spec = codec->spec;
  200. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  201. spec->cur_adc = 0;
  202. return 0;
  203. }
  204. /*
  205. */
  206. static const struct hda_pcm_stream cs_pcm_analog_playback = {
  207. .substreams = 1,
  208. .channels_min = 2,
  209. .channels_max = 2,
  210. .ops = {
  211. .open = cs_playback_pcm_open,
  212. .prepare = cs_playback_pcm_prepare,
  213. .cleanup = cs_playback_pcm_cleanup
  214. },
  215. };
  216. static const struct hda_pcm_stream cs_pcm_analog_capture = {
  217. .substreams = 1,
  218. .channels_min = 2,
  219. .channels_max = 2,
  220. .ops = {
  221. .prepare = cs_capture_pcm_prepare,
  222. .cleanup = cs_capture_pcm_cleanup
  223. },
  224. };
  225. static const struct hda_pcm_stream cs_pcm_digital_playback = {
  226. .substreams = 1,
  227. .channels_min = 2,
  228. .channels_max = 2,
  229. .ops = {
  230. .open = cs_dig_playback_pcm_open,
  231. .close = cs_dig_playback_pcm_close,
  232. .prepare = cs_dig_playback_pcm_prepare,
  233. .cleanup = cs_dig_playback_pcm_cleanup
  234. },
  235. };
  236. static const struct hda_pcm_stream cs_pcm_digital_capture = {
  237. .substreams = 1,
  238. .channels_min = 2,
  239. .channels_max = 2,
  240. };
  241. static int cs_build_pcms(struct hda_codec *codec)
  242. {
  243. struct cs_spec *spec = codec->spec;
  244. struct hda_pcm *info = spec->pcm_rec;
  245. codec->pcm_info = info;
  246. codec->num_pcms = 0;
  247. info->name = "Cirrus Analog";
  248. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cs_pcm_analog_playback;
  249. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dac_nid[0];
  250. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  251. spec->multiout.max_channels;
  252. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cs_pcm_analog_capture;
  253. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  254. spec->adc_nid[spec->cur_input];
  255. codec->num_pcms++;
  256. if (!spec->multiout.dig_out_nid && !spec->dig_in)
  257. return 0;
  258. info++;
  259. info->name = "Cirrus Digital";
  260. info->pcm_type = spec->autocfg.dig_out_type[0];
  261. if (!info->pcm_type)
  262. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  263. if (spec->multiout.dig_out_nid) {
  264. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  265. cs_pcm_digital_playback;
  266. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  267. spec->multiout.dig_out_nid;
  268. }
  269. if (spec->dig_in) {
  270. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  271. cs_pcm_digital_capture;
  272. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  273. }
  274. codec->num_pcms++;
  275. return 0;
  276. }
  277. /*
  278. * parse codec topology
  279. */
  280. static hda_nid_t get_dac(struct hda_codec *codec, hda_nid_t pin)
  281. {
  282. hda_nid_t dac;
  283. if (!pin)
  284. return 0;
  285. if (snd_hda_get_connections(codec, pin, &dac, 1) != 1)
  286. return 0;
  287. return dac;
  288. }
  289. static int is_ext_mic(struct hda_codec *codec, unsigned int idx)
  290. {
  291. struct cs_spec *spec = codec->spec;
  292. struct auto_pin_cfg *cfg = &spec->autocfg;
  293. hda_nid_t pin = cfg->inputs[idx].pin;
  294. unsigned int val;
  295. if (!is_jack_detectable(codec, pin))
  296. return 0;
  297. val = snd_hda_codec_get_pincfg(codec, pin);
  298. return (snd_hda_get_input_pin_attr(val) != INPUT_PIN_ATTR_INT);
  299. }
  300. static hda_nid_t get_adc(struct hda_codec *codec, hda_nid_t pin,
  301. unsigned int *idxp)
  302. {
  303. int i;
  304. hda_nid_t nid;
  305. nid = codec->start_nid;
  306. for (i = 0; i < codec->num_nodes; i++, nid++) {
  307. unsigned int type;
  308. int idx;
  309. type = get_wcaps_type(get_wcaps(codec, nid));
  310. if (type != AC_WID_AUD_IN)
  311. continue;
  312. idx = snd_hda_get_conn_index(codec, nid, pin, 0);
  313. if (idx >= 0) {
  314. *idxp = idx;
  315. return nid;
  316. }
  317. }
  318. return 0;
  319. }
  320. static int is_active_pin(struct hda_codec *codec, hda_nid_t nid)
  321. {
  322. unsigned int val;
  323. val = snd_hda_codec_get_pincfg(codec, nid);
  324. return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
  325. }
  326. static int parse_output(struct hda_codec *codec)
  327. {
  328. struct cs_spec *spec = codec->spec;
  329. struct auto_pin_cfg *cfg = &spec->autocfg;
  330. int i, extra_nids;
  331. hda_nid_t dac;
  332. for (i = 0; i < cfg->line_outs; i++) {
  333. dac = get_dac(codec, cfg->line_out_pins[i]);
  334. if (!dac)
  335. break;
  336. spec->dac_nid[i] = dac;
  337. }
  338. spec->multiout.num_dacs = i;
  339. spec->multiout.dac_nids = spec->dac_nid;
  340. spec->multiout.max_channels = i * 2;
  341. /* add HP and speakers */
  342. extra_nids = 0;
  343. for (i = 0; i < cfg->hp_outs; i++) {
  344. dac = get_dac(codec, cfg->hp_pins[i]);
  345. if (!dac)
  346. break;
  347. if (!i)
  348. spec->multiout.hp_nid = dac;
  349. else
  350. spec->multiout.extra_out_nid[extra_nids++] = dac;
  351. }
  352. for (i = 0; i < cfg->speaker_outs; i++) {
  353. dac = get_dac(codec, cfg->speaker_pins[i]);
  354. if (!dac)
  355. break;
  356. spec->multiout.extra_out_nid[extra_nids++] = dac;
  357. }
  358. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  359. cfg->speaker_outs = cfg->line_outs;
  360. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  361. sizeof(cfg->speaker_pins));
  362. cfg->line_outs = 0;
  363. }
  364. return 0;
  365. }
  366. static int parse_input(struct hda_codec *codec)
  367. {
  368. struct cs_spec *spec = codec->spec;
  369. struct auto_pin_cfg *cfg = &spec->autocfg;
  370. int i;
  371. for (i = 0; i < cfg->num_inputs; i++) {
  372. hda_nid_t pin = cfg->inputs[i].pin;
  373. spec->input_idx[spec->num_inputs] = i;
  374. spec->capsrc_idx[i] = spec->num_inputs++;
  375. spec->cur_input = i;
  376. spec->adc_nid[i] = get_adc(codec, pin, &spec->adc_idx[i]);
  377. }
  378. if (!spec->num_inputs)
  379. return 0;
  380. /* check whether the automatic mic switch is available */
  381. if (spec->num_inputs == 2 &&
  382. cfg->inputs[0].type == AUTO_PIN_MIC &&
  383. cfg->inputs[1].type == AUTO_PIN_MIC) {
  384. if (is_ext_mic(codec, cfg->inputs[0].pin)) {
  385. if (!is_ext_mic(codec, cfg->inputs[1].pin)) {
  386. spec->mic_detect = 1;
  387. spec->automic_idx = 0;
  388. }
  389. } else {
  390. if (is_ext_mic(codec, cfg->inputs[1].pin)) {
  391. spec->mic_detect = 1;
  392. spec->automic_idx = 1;
  393. }
  394. }
  395. }
  396. return 0;
  397. }
  398. static int parse_digital_output(struct hda_codec *codec)
  399. {
  400. struct cs_spec *spec = codec->spec;
  401. struct auto_pin_cfg *cfg = &spec->autocfg;
  402. hda_nid_t nid;
  403. if (!cfg->dig_outs)
  404. return 0;
  405. if (snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) < 1)
  406. return 0;
  407. spec->multiout.dig_out_nid = nid;
  408. spec->multiout.share_spdif = 1;
  409. if (cfg->dig_outs > 1 &&
  410. snd_hda_get_connections(codec, cfg->dig_out_pins[1], &nid, 1) > 0) {
  411. spec->slave_dig_outs[0] = nid;
  412. codec->slave_dig_outs = spec->slave_dig_outs;
  413. }
  414. return 0;
  415. }
  416. static int parse_digital_input(struct hda_codec *codec)
  417. {
  418. struct cs_spec *spec = codec->spec;
  419. struct auto_pin_cfg *cfg = &spec->autocfg;
  420. int idx;
  421. if (cfg->dig_in_pin)
  422. spec->dig_in = get_adc(codec, cfg->dig_in_pin, &idx);
  423. return 0;
  424. }
  425. /*
  426. * create mixer controls
  427. */
  428. static const char * const dir_sfx[2] = { "Playback", "Capture" };
  429. static int add_mute(struct hda_codec *codec, const char *name, int index,
  430. unsigned int pval, int dir, struct snd_kcontrol **kctlp)
  431. {
  432. char tmp[44];
  433. struct snd_kcontrol_new knew =
  434. HDA_CODEC_MUTE_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  435. knew.private_value = pval;
  436. snprintf(tmp, sizeof(tmp), "%s %s Switch", name, dir_sfx[dir]);
  437. *kctlp = snd_ctl_new1(&knew, codec);
  438. (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
  439. return snd_hda_ctl_add(codec, 0, *kctlp);
  440. }
  441. static int add_volume(struct hda_codec *codec, const char *name,
  442. int index, unsigned int pval, int dir,
  443. struct snd_kcontrol **kctlp)
  444. {
  445. char tmp[32];
  446. struct snd_kcontrol_new knew =
  447. HDA_CODEC_VOLUME_IDX(tmp, index, 0, 0, HDA_OUTPUT);
  448. knew.private_value = pval;
  449. snprintf(tmp, sizeof(tmp), "%s %s Volume", name, dir_sfx[dir]);
  450. *kctlp = snd_ctl_new1(&knew, codec);
  451. (*kctlp)->id.subdevice = HDA_SUBDEV_AMP_FLAG;
  452. return snd_hda_ctl_add(codec, 0, *kctlp);
  453. }
  454. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  455. {
  456. unsigned int caps;
  457. /* set the upper-limit for mixer amp to 0dB */
  458. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  459. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  460. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  461. << AC_AMPCAP_NUM_STEPS_SHIFT;
  462. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  463. }
  464. static int add_vmaster(struct hda_codec *codec, hda_nid_t dac)
  465. {
  466. struct cs_spec *spec = codec->spec;
  467. unsigned int tlv[4];
  468. int err;
  469. spec->vmaster_sw =
  470. snd_ctl_make_virtual_master("Master Playback Switch", NULL);
  471. err = snd_hda_ctl_add(codec, dac, spec->vmaster_sw);
  472. if (err < 0)
  473. return err;
  474. snd_hda_set_vmaster_tlv(codec, dac, HDA_OUTPUT, tlv);
  475. spec->vmaster_vol =
  476. snd_ctl_make_virtual_master("Master Playback Volume", tlv);
  477. err = snd_hda_ctl_add(codec, dac, spec->vmaster_vol);
  478. if (err < 0)
  479. return err;
  480. return 0;
  481. }
  482. static int add_output(struct hda_codec *codec, hda_nid_t dac, int idx,
  483. int num_ctls, int type)
  484. {
  485. struct cs_spec *spec = codec->spec;
  486. const char *name;
  487. int err, index;
  488. struct snd_kcontrol *kctl;
  489. static const char * const speakers[] = {
  490. "Front Speaker", "Surround Speaker", "Bass Speaker"
  491. };
  492. static const char * const line_outs[] = {
  493. "Front Line-Out", "Surround Line-Out", "Bass Line-Out"
  494. };
  495. fix_volume_caps(codec, dac);
  496. if (!spec->vmaster_sw) {
  497. err = add_vmaster(codec, dac);
  498. if (err < 0)
  499. return err;
  500. }
  501. index = 0;
  502. switch (type) {
  503. case AUTO_PIN_HP_OUT:
  504. name = "Headphone";
  505. index = idx;
  506. break;
  507. case AUTO_PIN_SPEAKER_OUT:
  508. if (num_ctls > 1)
  509. name = speakers[idx];
  510. else
  511. name = "Speaker";
  512. break;
  513. default:
  514. if (num_ctls > 1)
  515. name = line_outs[idx];
  516. else
  517. name = "Line-Out";
  518. break;
  519. }
  520. err = add_mute(codec, name, index,
  521. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  522. if (err < 0)
  523. return err;
  524. err = snd_ctl_add_slave(spec->vmaster_sw, kctl);
  525. if (err < 0)
  526. return err;
  527. err = add_volume(codec, name, index,
  528. HDA_COMPOSE_AMP_VAL(dac, 3, 0, HDA_OUTPUT), 0, &kctl);
  529. if (err < 0)
  530. return err;
  531. err = snd_ctl_add_slave(spec->vmaster_vol, kctl);
  532. if (err < 0)
  533. return err;
  534. return 0;
  535. }
  536. static int build_output(struct hda_codec *codec)
  537. {
  538. struct cs_spec *spec = codec->spec;
  539. struct auto_pin_cfg *cfg = &spec->autocfg;
  540. int i, err;
  541. for (i = 0; i < cfg->line_outs; i++) {
  542. err = add_output(codec, get_dac(codec, cfg->line_out_pins[i]),
  543. i, cfg->line_outs, cfg->line_out_type);
  544. if (err < 0)
  545. return err;
  546. }
  547. for (i = 0; i < cfg->hp_outs; i++) {
  548. err = add_output(codec, get_dac(codec, cfg->hp_pins[i]),
  549. i, cfg->hp_outs, AUTO_PIN_HP_OUT);
  550. if (err < 0)
  551. return err;
  552. }
  553. for (i = 0; i < cfg->speaker_outs; i++) {
  554. err = add_output(codec, get_dac(codec, cfg->speaker_pins[i]),
  555. i, cfg->speaker_outs, AUTO_PIN_SPEAKER_OUT);
  556. if (err < 0)
  557. return err;
  558. }
  559. return 0;
  560. }
  561. /*
  562. */
  563. static const struct snd_kcontrol_new cs_capture_ctls[] = {
  564. HDA_BIND_SW("Capture Switch", 0),
  565. HDA_BIND_VOL("Capture Volume", 0),
  566. };
  567. static int change_cur_input(struct hda_codec *codec, unsigned int idx,
  568. int force)
  569. {
  570. struct cs_spec *spec = codec->spec;
  571. if (spec->cur_input == idx && !force)
  572. return 0;
  573. if (spec->cur_adc && spec->cur_adc != spec->adc_nid[idx]) {
  574. /* stream is running, let's swap the current ADC */
  575. __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
  576. spec->cur_adc = spec->adc_nid[idx];
  577. snd_hda_codec_setup_stream(codec, spec->cur_adc,
  578. spec->cur_adc_stream_tag, 0,
  579. spec->cur_adc_format);
  580. }
  581. snd_hda_codec_write(codec, spec->cur_adc, 0,
  582. AC_VERB_SET_CONNECT_SEL,
  583. spec->adc_idx[idx]);
  584. spec->cur_input = idx;
  585. return 1;
  586. }
  587. static int cs_capture_source_info(struct snd_kcontrol *kcontrol,
  588. struct snd_ctl_elem_info *uinfo)
  589. {
  590. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  591. struct cs_spec *spec = codec->spec;
  592. struct auto_pin_cfg *cfg = &spec->autocfg;
  593. unsigned int idx;
  594. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  595. uinfo->count = 1;
  596. uinfo->value.enumerated.items = spec->num_inputs;
  597. if (uinfo->value.enumerated.item >= spec->num_inputs)
  598. uinfo->value.enumerated.item = spec->num_inputs - 1;
  599. idx = spec->input_idx[uinfo->value.enumerated.item];
  600. strcpy(uinfo->value.enumerated.name,
  601. hda_get_input_pin_label(codec, cfg->inputs[idx].pin, 1));
  602. return 0;
  603. }
  604. static int cs_capture_source_get(struct snd_kcontrol *kcontrol,
  605. struct snd_ctl_elem_value *ucontrol)
  606. {
  607. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  608. struct cs_spec *spec = codec->spec;
  609. ucontrol->value.enumerated.item[0] = spec->capsrc_idx[spec->cur_input];
  610. return 0;
  611. }
  612. static int cs_capture_source_put(struct snd_kcontrol *kcontrol,
  613. struct snd_ctl_elem_value *ucontrol)
  614. {
  615. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  616. struct cs_spec *spec = codec->spec;
  617. unsigned int idx = ucontrol->value.enumerated.item[0];
  618. if (idx >= spec->num_inputs)
  619. return -EINVAL;
  620. idx = spec->input_idx[idx];
  621. return change_cur_input(codec, idx, 0);
  622. }
  623. static const struct snd_kcontrol_new cs_capture_source = {
  624. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  625. .name = "Capture Source",
  626. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  627. .info = cs_capture_source_info,
  628. .get = cs_capture_source_get,
  629. .put = cs_capture_source_put,
  630. };
  631. static const struct hda_bind_ctls *make_bind_capture(struct hda_codec *codec,
  632. struct hda_ctl_ops *ops)
  633. {
  634. struct cs_spec *spec = codec->spec;
  635. struct hda_bind_ctls *bind;
  636. int i, n;
  637. bind = kzalloc(sizeof(*bind) + sizeof(long) * (spec->num_inputs + 1),
  638. GFP_KERNEL);
  639. if (!bind)
  640. return NULL;
  641. bind->ops = ops;
  642. n = 0;
  643. for (i = 0; i < AUTO_PIN_LAST; i++) {
  644. if (!spec->adc_nid[i])
  645. continue;
  646. bind->values[n++] =
  647. HDA_COMPOSE_AMP_VAL(spec->adc_nid[i], 3,
  648. spec->adc_idx[i], HDA_INPUT);
  649. }
  650. return bind;
  651. }
  652. /* add a (input-boost) volume control to the given input pin */
  653. static int add_input_volume_control(struct hda_codec *codec,
  654. struct auto_pin_cfg *cfg,
  655. int item)
  656. {
  657. hda_nid_t pin = cfg->inputs[item].pin;
  658. u32 caps;
  659. const char *label;
  660. struct snd_kcontrol *kctl;
  661. if (!(get_wcaps(codec, pin) & AC_WCAP_IN_AMP))
  662. return 0;
  663. caps = query_amp_caps(codec, pin, HDA_INPUT);
  664. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  665. if (caps <= 1)
  666. return 0;
  667. label = hda_get_autocfg_input_label(codec, cfg, item);
  668. return add_volume(codec, label, 0,
  669. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT), 1, &kctl);
  670. }
  671. static int build_input(struct hda_codec *codec)
  672. {
  673. struct cs_spec *spec = codec->spec;
  674. int i, err;
  675. if (!spec->num_inputs)
  676. return 0;
  677. /* make bind-capture */
  678. spec->capture_bind[0] = make_bind_capture(codec, &snd_hda_bind_sw);
  679. spec->capture_bind[1] = make_bind_capture(codec, &snd_hda_bind_vol);
  680. for (i = 0; i < 2; i++) {
  681. struct snd_kcontrol *kctl;
  682. int n;
  683. if (!spec->capture_bind[i])
  684. return -ENOMEM;
  685. kctl = snd_ctl_new1(&cs_capture_ctls[i], codec);
  686. if (!kctl)
  687. return -ENOMEM;
  688. kctl->private_value = (long)spec->capture_bind[i];
  689. err = snd_hda_ctl_add(codec, 0, kctl);
  690. if (err < 0)
  691. return err;
  692. for (n = 0; n < AUTO_PIN_LAST; n++) {
  693. if (!spec->adc_nid[n])
  694. continue;
  695. err = snd_hda_add_nid(codec, kctl, 0, spec->adc_nid[n]);
  696. if (err < 0)
  697. return err;
  698. }
  699. }
  700. if (spec->num_inputs > 1 && !spec->mic_detect) {
  701. err = snd_hda_ctl_add(codec, 0,
  702. snd_ctl_new1(&cs_capture_source, codec));
  703. if (err < 0)
  704. return err;
  705. }
  706. for (i = 0; i < spec->num_inputs; i++) {
  707. err = add_input_volume_control(codec, &spec->autocfg, i);
  708. if (err < 0)
  709. return err;
  710. }
  711. return 0;
  712. }
  713. /*
  714. */
  715. static int build_digital_output(struct hda_codec *codec)
  716. {
  717. struct cs_spec *spec = codec->spec;
  718. int err;
  719. if (!spec->multiout.dig_out_nid)
  720. return 0;
  721. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid,
  722. spec->multiout.dig_out_nid);
  723. if (err < 0)
  724. return err;
  725. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  726. if (err < 0)
  727. return err;
  728. return 0;
  729. }
  730. static int build_digital_input(struct hda_codec *codec)
  731. {
  732. struct cs_spec *spec = codec->spec;
  733. if (spec->dig_in)
  734. return snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  735. return 0;
  736. }
  737. /*
  738. * auto-mute and auto-mic switching
  739. */
  740. static void cs_automute(struct hda_codec *codec)
  741. {
  742. struct cs_spec *spec = codec->spec;
  743. struct auto_pin_cfg *cfg = &spec->autocfg;
  744. unsigned int hp_present;
  745. hda_nid_t nid;
  746. int i;
  747. hp_present = 0;
  748. for (i = 0; i < cfg->hp_outs; i++) {
  749. nid = cfg->hp_pins[i];
  750. if (!is_jack_detectable(codec, nid))
  751. continue;
  752. hp_present = snd_hda_jack_detect(codec, nid);
  753. if (hp_present)
  754. break;
  755. }
  756. for (i = 0; i < cfg->speaker_outs; i++) {
  757. nid = cfg->speaker_pins[i];
  758. snd_hda_codec_write(codec, nid, 0,
  759. AC_VERB_SET_PIN_WIDGET_CONTROL,
  760. hp_present ? 0 : PIN_OUT);
  761. }
  762. if (spec->board_config == CS420X_MBP53 ||
  763. spec->board_config == CS420X_MBP55 ||
  764. spec->board_config == CS420X_IMAC27) {
  765. unsigned int gpio = hp_present ? 0x02 : 0x08;
  766. snd_hda_codec_write(codec, 0x01, 0,
  767. AC_VERB_SET_GPIO_DATA, gpio);
  768. }
  769. }
  770. static void cs_automic(struct hda_codec *codec)
  771. {
  772. struct cs_spec *spec = codec->spec;
  773. struct auto_pin_cfg *cfg = &spec->autocfg;
  774. hda_nid_t nid;
  775. unsigned int present;
  776. nid = cfg->inputs[spec->automic_idx].pin;
  777. present = snd_hda_jack_detect(codec, nid);
  778. if (present)
  779. change_cur_input(codec, spec->automic_idx, 0);
  780. else
  781. change_cur_input(codec, !spec->automic_idx, 0);
  782. }
  783. /*
  784. */
  785. static void init_output(struct hda_codec *codec)
  786. {
  787. struct cs_spec *spec = codec->spec;
  788. struct auto_pin_cfg *cfg = &spec->autocfg;
  789. int i;
  790. /* mute first */
  791. for (i = 0; i < spec->multiout.num_dacs; i++)
  792. snd_hda_codec_write(codec, spec->multiout.dac_nids[i], 0,
  793. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  794. if (spec->multiout.hp_nid)
  795. snd_hda_codec_write(codec, spec->multiout.hp_nid, 0,
  796. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  797. for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
  798. if (!spec->multiout.extra_out_nid[i])
  799. break;
  800. snd_hda_codec_write(codec, spec->multiout.extra_out_nid[i], 0,
  801. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  802. }
  803. /* set appropriate pin controls */
  804. for (i = 0; i < cfg->line_outs; i++)
  805. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  806. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  807. for (i = 0; i < cfg->hp_outs; i++) {
  808. hda_nid_t nid = cfg->hp_pins[i];
  809. snd_hda_codec_write(codec, nid, 0,
  810. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  811. if (!cfg->speaker_outs)
  812. continue;
  813. if (is_jack_detectable(codec, nid)) {
  814. snd_hda_codec_write(codec, nid, 0,
  815. AC_VERB_SET_UNSOLICITED_ENABLE,
  816. AC_USRSP_EN | HP_EVENT);
  817. spec->hp_detect = 1;
  818. }
  819. }
  820. for (i = 0; i < cfg->speaker_outs; i++)
  821. snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
  822. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  823. if (spec->hp_detect)
  824. cs_automute(codec);
  825. }
  826. static void init_input(struct hda_codec *codec)
  827. {
  828. struct cs_spec *spec = codec->spec;
  829. struct auto_pin_cfg *cfg = &spec->autocfg;
  830. unsigned int coef;
  831. int i;
  832. for (i = 0; i < cfg->num_inputs; i++) {
  833. unsigned int ctl;
  834. hda_nid_t pin = cfg->inputs[i].pin;
  835. if (!spec->adc_nid[i])
  836. continue;
  837. /* set appropriate pin control and mute first */
  838. ctl = PIN_IN;
  839. if (cfg->inputs[i].type == AUTO_PIN_MIC) {
  840. unsigned int caps = snd_hda_query_pin_caps(codec, pin);
  841. caps >>= AC_PINCAP_VREF_SHIFT;
  842. if (caps & AC_PINCAP_VREF_80)
  843. ctl = PIN_VREF80;
  844. }
  845. snd_hda_codec_write(codec, pin, 0,
  846. AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
  847. snd_hda_codec_write(codec, spec->adc_nid[i], 0,
  848. AC_VERB_SET_AMP_GAIN_MUTE,
  849. AMP_IN_MUTE(spec->adc_idx[i]));
  850. if (spec->mic_detect && spec->automic_idx == i)
  851. snd_hda_codec_write(codec, pin, 0,
  852. AC_VERB_SET_UNSOLICITED_ENABLE,
  853. AC_USRSP_EN | MIC_EVENT);
  854. }
  855. change_cur_input(codec, spec->cur_input, 1);
  856. if (spec->mic_detect)
  857. cs_automic(codec);
  858. coef = 0x000a; /* ADC1/2 - Digital and Analog Soft Ramp */
  859. if (is_active_pin(codec, CS_DMIC2_PIN_NID))
  860. coef |= 0x0500; /* DMIC2 enable 2 channels, disable GPIO1 */
  861. if (is_active_pin(codec, CS_DMIC1_PIN_NID))
  862. coef |= 0x1800; /* DMIC1 enable 2 channels, disable GPIO0
  863. * No effect if SPDIF_OUT2 is selected in
  864. * IDX_SPDIF_CTL.
  865. */
  866. cs_vendor_coef_set(codec, IDX_ADC_CFG, coef);
  867. }
  868. static const struct hda_verb cs_coef_init_verbs[] = {
  869. {0x11, AC_VERB_SET_PROC_STATE, 1},
  870. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  871. {0x11, AC_VERB_SET_PROC_COEF,
  872. (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
  873. | 0x0040 /* Mute DACs on FIFO error */
  874. | 0x1000 /* Enable DACs High Pass Filter */
  875. | 0x0400 /* Disable Coefficient Auto increment */
  876. )},
  877. /* Beep */
  878. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  879. {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
  880. {} /* terminator */
  881. };
  882. /* Errata: CS4207 rev C0/C1/C2 Silicon
  883. *
  884. * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
  885. *
  886. * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
  887. * may be excessive (up to an additional 200 μA), which is most easily
  888. * observed while the part is being held in reset (RESET# active low).
  889. *
  890. * Root Cause: At initial powerup of the device, the logic that drives
  891. * the clock and write enable to the S/PDIF SRC RAMs is not properly
  892. * initialized.
  893. * Certain random patterns will cause a steady leakage current in those
  894. * RAM cells. The issue will resolve once the SRCs are used (turned on).
  895. *
  896. * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
  897. * blocks, which will alleviate the issue.
  898. */
  899. static const struct hda_verb cs_errata_init_verbs[] = {
  900. {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
  901. {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  902. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  903. {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
  904. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  905. {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
  906. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  907. {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
  908. {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
  909. {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
  910. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  911. {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
  912. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  913. {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
  914. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  915. {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
  916. {0x11, AC_VERB_SET_PROC_STATE, 0x00},
  917. #if 0 /* Don't to set to D3 as we are in power-up sequence */
  918. {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
  919. {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
  920. /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
  921. #endif
  922. {} /* terminator */
  923. };
  924. /* SPDIF setup */
  925. static void init_digital(struct hda_codec *codec)
  926. {
  927. unsigned int coef;
  928. coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
  929. coef |= 0x0008; /* Replace with mute on error */
  930. if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
  931. coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
  932. * SPDIF_OUT2 is shared with GPIO1 and
  933. * DMIC_SDA2.
  934. */
  935. cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
  936. }
  937. static int cs_init(struct hda_codec *codec)
  938. {
  939. struct cs_spec *spec = codec->spec;
  940. /* init_verb sequence for C0/C1/C2 errata*/
  941. snd_hda_sequence_write(codec, cs_errata_init_verbs);
  942. snd_hda_sequence_write(codec, cs_coef_init_verbs);
  943. if (spec->gpio_mask) {
  944. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  945. spec->gpio_mask);
  946. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  947. spec->gpio_dir);
  948. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  949. spec->gpio_data);
  950. }
  951. init_output(codec);
  952. init_input(codec);
  953. init_digital(codec);
  954. return 0;
  955. }
  956. static int cs_build_controls(struct hda_codec *codec)
  957. {
  958. int err;
  959. err = build_output(codec);
  960. if (err < 0)
  961. return err;
  962. err = build_input(codec);
  963. if (err < 0)
  964. return err;
  965. err = build_digital_output(codec);
  966. if (err < 0)
  967. return err;
  968. err = build_digital_input(codec);
  969. if (err < 0)
  970. return err;
  971. return cs_init(codec);
  972. }
  973. static void cs_free(struct hda_codec *codec)
  974. {
  975. struct cs_spec *spec = codec->spec;
  976. kfree(spec->capture_bind[0]);
  977. kfree(spec->capture_bind[1]);
  978. kfree(codec->spec);
  979. }
  980. static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
  981. {
  982. switch ((res >> 26) & 0x7f) {
  983. case HP_EVENT:
  984. cs_automute(codec);
  985. break;
  986. case MIC_EVENT:
  987. cs_automic(codec);
  988. break;
  989. }
  990. }
  991. static const struct hda_codec_ops cs_patch_ops = {
  992. .build_controls = cs_build_controls,
  993. .build_pcms = cs_build_pcms,
  994. .init = cs_init,
  995. .free = cs_free,
  996. .unsol_event = cs_unsol_event,
  997. };
  998. static int cs_parse_auto_config(struct hda_codec *codec)
  999. {
  1000. struct cs_spec *spec = codec->spec;
  1001. int err;
  1002. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  1003. if (err < 0)
  1004. return err;
  1005. err = parse_output(codec);
  1006. if (err < 0)
  1007. return err;
  1008. err = parse_input(codec);
  1009. if (err < 0)
  1010. return err;
  1011. err = parse_digital_output(codec);
  1012. if (err < 0)
  1013. return err;
  1014. err = parse_digital_input(codec);
  1015. if (err < 0)
  1016. return err;
  1017. return 0;
  1018. }
  1019. static const char * const cs420x_models[CS420X_MODELS] = {
  1020. [CS420X_MBP53] = "mbp53",
  1021. [CS420X_MBP55] = "mbp55",
  1022. [CS420X_IMAC27] = "imac27",
  1023. [CS420X_AUTO] = "auto",
  1024. };
  1025. static const struct snd_pci_quirk cs420x_cfg_tbl[] = {
  1026. SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
  1027. SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
  1028. SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
  1029. SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
  1030. SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),
  1031. {} /* terminator */
  1032. };
  1033. struct cs_pincfg {
  1034. hda_nid_t nid;
  1035. u32 val;
  1036. };
  1037. static const struct cs_pincfg mbp53_pincfgs[] = {
  1038. { 0x09, 0x012b4050 },
  1039. { 0x0a, 0x90100141 },
  1040. { 0x0b, 0x90100140 },
  1041. { 0x0c, 0x018b3020 },
  1042. { 0x0d, 0x90a00110 },
  1043. { 0x0e, 0x400000f0 },
  1044. { 0x0f, 0x01cbe030 },
  1045. { 0x10, 0x014be060 },
  1046. { 0x12, 0x400000f0 },
  1047. { 0x15, 0x400000f0 },
  1048. {} /* terminator */
  1049. };
  1050. static const struct cs_pincfg mbp55_pincfgs[] = {
  1051. { 0x09, 0x012b4030 },
  1052. { 0x0a, 0x90100121 },
  1053. { 0x0b, 0x90100120 },
  1054. { 0x0c, 0x400000f0 },
  1055. { 0x0d, 0x90a00110 },
  1056. { 0x0e, 0x400000f0 },
  1057. { 0x0f, 0x400000f0 },
  1058. { 0x10, 0x014be040 },
  1059. { 0x12, 0x400000f0 },
  1060. { 0x15, 0x400000f0 },
  1061. {} /* terminator */
  1062. };
  1063. static const struct cs_pincfg imac27_pincfgs[] = {
  1064. { 0x09, 0x012b4050 },
  1065. { 0x0a, 0x90100140 },
  1066. { 0x0b, 0x90100142 },
  1067. { 0x0c, 0x018b3020 },
  1068. { 0x0d, 0x90a00110 },
  1069. { 0x0e, 0x400000f0 },
  1070. { 0x0f, 0x01cbe030 },
  1071. { 0x10, 0x014be060 },
  1072. { 0x12, 0x01ab9070 },
  1073. { 0x15, 0x400000f0 },
  1074. {} /* terminator */
  1075. };
  1076. static const struct cs_pincfg *cs_pincfgs[CS420X_MODELS] = {
  1077. [CS420X_MBP53] = mbp53_pincfgs,
  1078. [CS420X_MBP55] = mbp55_pincfgs,
  1079. [CS420X_IMAC27] = imac27_pincfgs,
  1080. };
  1081. static void fix_pincfg(struct hda_codec *codec, int model)
  1082. {
  1083. const struct cs_pincfg *cfg = cs_pincfgs[model];
  1084. if (!cfg)
  1085. return;
  1086. for (; cfg->nid; cfg++)
  1087. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  1088. }
  1089. static int patch_cs420x(struct hda_codec *codec)
  1090. {
  1091. struct cs_spec *spec;
  1092. int err;
  1093. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1094. if (!spec)
  1095. return -ENOMEM;
  1096. codec->spec = spec;
  1097. spec->board_config =
  1098. snd_hda_check_board_config(codec, CS420X_MODELS,
  1099. cs420x_models, cs420x_cfg_tbl);
  1100. if (spec->board_config >= 0)
  1101. fix_pincfg(codec, spec->board_config);
  1102. switch (spec->board_config) {
  1103. case CS420X_IMAC27:
  1104. case CS420X_MBP53:
  1105. case CS420X_MBP55:
  1106. /* GPIO1 = headphones */
  1107. /* GPIO3 = speakers */
  1108. spec->gpio_mask = 0x0a;
  1109. spec->gpio_dir = 0x0a;
  1110. break;
  1111. }
  1112. err = cs_parse_auto_config(codec);
  1113. if (err < 0)
  1114. goto error;
  1115. codec->patch_ops = cs_patch_ops;
  1116. return 0;
  1117. error:
  1118. kfree(codec->spec);
  1119. codec->spec = NULL;
  1120. return err;
  1121. }
  1122. /*
  1123. * patch entries
  1124. */
  1125. static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
  1126. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  1127. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  1128. {} /* terminator */
  1129. };
  1130. MODULE_ALIAS("snd-hda-codec-id:10134206");
  1131. MODULE_ALIAS("snd-hda-codec-id:10134207");
  1132. MODULE_LICENSE("GPL");
  1133. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  1134. static struct hda_codec_preset_list cirrus_list = {
  1135. .preset = snd_hda_preset_cirrus,
  1136. .owner = THIS_MODULE,
  1137. };
  1138. static int __init patch_cirrus_init(void)
  1139. {
  1140. return snd_hda_add_codec_preset(&cirrus_list);
  1141. }
  1142. static void __exit patch_cirrus_exit(void)
  1143. {
  1144. snd_hda_delete_codec_preset(&cirrus_list);
  1145. }
  1146. module_init(patch_cirrus_init)
  1147. module_exit(patch_cirrus_exit)