patch_cirrus.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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/slab.h>
  22. #include <linux/module.h>
  23. #include <sound/core.h>
  24. #include <sound/tlv.h>
  25. #include "hda_codec.h"
  26. #include "hda_local.h"
  27. #include "hda_auto_parser.h"
  28. #include "hda_jack.h"
  29. #include "hda_generic.h"
  30. /*
  31. */
  32. struct cs_spec {
  33. struct hda_gen_spec gen;
  34. unsigned int gpio_mask;
  35. unsigned int gpio_dir;
  36. unsigned int gpio_data;
  37. unsigned int gpio_eapd_hp; /* EAPD GPIO bit for headphones */
  38. unsigned int gpio_eapd_speaker; /* EAPD GPIO bit for speakers */
  39. /* CS421x */
  40. unsigned int spdif_detect:1;
  41. unsigned int spdif_present:1;
  42. unsigned int sense_b:1;
  43. hda_nid_t vendor_nid;
  44. /* for MBP SPDIF control */
  45. int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
  46. struct snd_ctl_elem_value *ucontrol);
  47. };
  48. /* available models with CS420x */
  49. enum {
  50. CS420X_MBP53,
  51. CS420X_MBP55,
  52. CS420X_IMAC27,
  53. CS420X_GPIO_13,
  54. CS420X_GPIO_23,
  55. CS420X_MBP101,
  56. CS420X_MBP81,
  57. CS420X_MBA42,
  58. CS420X_AUTO,
  59. /* aliases */
  60. CS420X_IMAC27_122 = CS420X_GPIO_23,
  61. CS420X_APPLE = CS420X_GPIO_13,
  62. };
  63. /* CS421x boards */
  64. enum {
  65. CS421X_CDB4210,
  66. CS421X_SENSE_B,
  67. CS421X_STUMPY,
  68. };
  69. /* Vendor-specific processing widget */
  70. #define CS420X_VENDOR_NID 0x11
  71. #define CS_DIG_OUT1_PIN_NID 0x10
  72. #define CS_DIG_OUT2_PIN_NID 0x15
  73. #define CS_DMIC1_PIN_NID 0x0e
  74. #define CS_DMIC2_PIN_NID 0x12
  75. /* coef indices */
  76. #define IDX_SPDIF_STAT 0x0000
  77. #define IDX_SPDIF_CTL 0x0001
  78. #define IDX_ADC_CFG 0x0002
  79. /* SZC bitmask, 4 modes below:
  80. * 0 = immediate,
  81. * 1 = digital immediate, analog zero-cross
  82. * 2 = digtail & analog soft-ramp
  83. * 3 = digital soft-ramp, analog zero-cross
  84. */
  85. #define CS_COEF_ADC_SZC_MASK (3 << 0)
  86. #define CS_COEF_ADC_MIC_SZC_MODE (3 << 0) /* SZC setup for mic */
  87. #define CS_COEF_ADC_LI_SZC_MODE (3 << 0) /* SZC setup for line-in */
  88. /* PGA mode: 0 = differential, 1 = signle-ended */
  89. #define CS_COEF_ADC_MIC_PGA_MODE (1 << 5) /* PGA setup for mic */
  90. #define CS_COEF_ADC_LI_PGA_MODE (1 << 6) /* PGA setup for line-in */
  91. #define IDX_DAC_CFG 0x0003
  92. /* SZC bitmask, 4 modes below:
  93. * 0 = Immediate
  94. * 1 = zero-cross
  95. * 2 = soft-ramp
  96. * 3 = soft-ramp on zero-cross
  97. */
  98. #define CS_COEF_DAC_HP_SZC_MODE (3 << 0) /* nid 0x02 */
  99. #define CS_COEF_DAC_LO_SZC_MODE (3 << 2) /* nid 0x03 */
  100. #define CS_COEF_DAC_SPK_SZC_MODE (3 << 4) /* nid 0x04 */
  101. #define IDX_BEEP_CFG 0x0004
  102. /* 0x0008 - test reg key */
  103. /* 0x0009 - 0x0014 -> 12 test regs */
  104. /* 0x0015 - visibility reg */
  105. /* Cirrus Logic CS4208 */
  106. #define CS4208_VENDOR_NID 0x24
  107. /*
  108. * Cirrus Logic CS4210
  109. *
  110. * 1 DAC => HP(sense) / Speakers,
  111. * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
  112. * 1 SPDIF OUT => SPDIF Trasmitter(sense)
  113. */
  114. #define CS4210_DAC_NID 0x02
  115. #define CS4210_ADC_NID 0x03
  116. #define CS4210_VENDOR_NID 0x0B
  117. #define CS421X_DMIC_PIN_NID 0x09 /* Port E */
  118. #define CS421X_SPDIF_PIN_NID 0x0A /* Port H */
  119. #define CS421X_IDX_DEV_CFG 0x01
  120. #define CS421X_IDX_ADC_CFG 0x02
  121. #define CS421X_IDX_DAC_CFG 0x03
  122. #define CS421X_IDX_SPK_CTL 0x04
  123. /* Cirrus Logic CS4213 is like CS4210 but does not have SPDIF input/output */
  124. #define CS4213_VENDOR_NID 0x09
  125. static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
  126. {
  127. struct cs_spec *spec = codec->spec;
  128. snd_hda_codec_write(codec, spec->vendor_nid, 0,
  129. AC_VERB_SET_COEF_INDEX, idx);
  130. return snd_hda_codec_read(codec, spec->vendor_nid, 0,
  131. AC_VERB_GET_PROC_COEF, 0);
  132. }
  133. static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  134. unsigned int coef)
  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. snd_hda_codec_write(codec, spec->vendor_nid, 0,
  140. AC_VERB_SET_PROC_COEF, coef);
  141. }
  142. /*
  143. * auto-mute and auto-mic switching
  144. * CS421x auto-output redirecting
  145. * HP/SPK/SPDIF
  146. */
  147. static void cs_automute(struct hda_codec *codec)
  148. {
  149. struct cs_spec *spec = codec->spec;
  150. /* mute HPs if spdif jack (SENSE_B) is present */
  151. spec->gen.master_mute = !!(spec->spdif_present && spec->sense_b);
  152. snd_hda_gen_update_outputs(codec);
  153. if (spec->gpio_eapd_hp || spec->gpio_eapd_speaker) {
  154. if (spec->gen.automute_speaker)
  155. spec->gpio_data = spec->gen.hp_jack_present ?
  156. spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
  157. else
  158. spec->gpio_data =
  159. spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
  160. snd_hda_codec_write(codec, 0x01, 0,
  161. AC_VERB_SET_GPIO_DATA, spec->gpio_data);
  162. }
  163. }
  164. static bool is_active_pin(struct hda_codec *codec, hda_nid_t nid)
  165. {
  166. unsigned int val;
  167. val = snd_hda_codec_get_pincfg(codec, nid);
  168. return (get_defcfg_connect(val) != AC_JACK_PORT_NONE);
  169. }
  170. static void init_input_coef(struct hda_codec *codec)
  171. {
  172. struct cs_spec *spec = codec->spec;
  173. unsigned int coef;
  174. /* CS420x has multiple ADC, CS421x has single ADC */
  175. if (spec->vendor_nid == CS420X_VENDOR_NID) {
  176. coef = cs_vendor_coef_get(codec, IDX_BEEP_CFG);
  177. if (is_active_pin(codec, CS_DMIC2_PIN_NID))
  178. coef |= 1 << 4; /* DMIC2 2 chan on, GPIO1 off */
  179. if (is_active_pin(codec, CS_DMIC1_PIN_NID))
  180. coef |= 1 << 3; /* DMIC1 2 chan on, GPIO0 off
  181. * No effect if SPDIF_OUT2 is
  182. * selected in IDX_SPDIF_CTL.
  183. */
  184. cs_vendor_coef_set(codec, IDX_BEEP_CFG, coef);
  185. }
  186. }
  187. static const struct hda_verb cs_coef_init_verbs[] = {
  188. {0x11, AC_VERB_SET_PROC_STATE, 1},
  189. {0x11, AC_VERB_SET_COEF_INDEX, IDX_DAC_CFG},
  190. {0x11, AC_VERB_SET_PROC_COEF,
  191. (0x002a /* DAC1/2/3 SZCMode Soft Ramp */
  192. | 0x0040 /* Mute DACs on FIFO error */
  193. | 0x1000 /* Enable DACs High Pass Filter */
  194. | 0x0400 /* Disable Coefficient Auto increment */
  195. )},
  196. /* ADC1/2 - Digital and Analog Soft Ramp */
  197. {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
  198. {0x11, AC_VERB_SET_PROC_COEF, 0x000a},
  199. /* Beep */
  200. {0x11, AC_VERB_SET_COEF_INDEX, IDX_BEEP_CFG},
  201. {0x11, AC_VERB_SET_PROC_COEF, 0x0007}, /* Enable Beep thru DAC1/2/3 */
  202. {} /* terminator */
  203. };
  204. static const struct hda_verb cs4208_coef_init_verbs[] = {
  205. {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
  206. {0x24, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  207. {0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
  208. {0x24, AC_VERB_SET_PROC_COEF, 0x0001}, /* A1 ICS */
  209. {0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
  210. {0x24, AC_VERB_SET_PROC_COEF, 0x1C01}, /* A1 Enable, A Thresh = 300mV */
  211. {} /* terminator */
  212. };
  213. /* Errata: CS4207 rev C0/C1/C2 Silicon
  214. *
  215. * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
  216. *
  217. * 6. At high temperature (TA > +85°C), the digital supply current (IVD)
  218. * may be excessive (up to an additional 200 μA), which is most easily
  219. * observed while the part is being held in reset (RESET# active low).
  220. *
  221. * Root Cause: At initial powerup of the device, the logic that drives
  222. * the clock and write enable to the S/PDIF SRC RAMs is not properly
  223. * initialized.
  224. * Certain random patterns will cause a steady leakage current in those
  225. * RAM cells. The issue will resolve once the SRCs are used (turned on).
  226. *
  227. * Workaround: The following verb sequence briefly turns on the S/PDIF SRC
  228. * blocks, which will alleviate the issue.
  229. */
  230. static const struct hda_verb cs_errata_init_verbs[] = {
  231. {0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
  232. {0x11, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  233. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  234. {0x11, AC_VERB_SET_PROC_COEF, 0x9999},
  235. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  236. {0x11, AC_VERB_SET_PROC_COEF, 0xa412},
  237. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  238. {0x11, AC_VERB_SET_PROC_COEF, 0x0009},
  239. {0x07, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Rx: D0 */
  240. {0x08, AC_VERB_SET_POWER_STATE, 0x00}, /* S/PDIF Tx: D0 */
  241. {0x11, AC_VERB_SET_COEF_INDEX, 0x0017},
  242. {0x11, AC_VERB_SET_PROC_COEF, 0x2412},
  243. {0x11, AC_VERB_SET_COEF_INDEX, 0x0008},
  244. {0x11, AC_VERB_SET_PROC_COEF, 0x0000},
  245. {0x11, AC_VERB_SET_COEF_INDEX, 0x0001},
  246. {0x11, AC_VERB_SET_PROC_COEF, 0x0008},
  247. {0x11, AC_VERB_SET_PROC_STATE, 0x00},
  248. #if 0 /* Don't to set to D3 as we are in power-up sequence */
  249. {0x07, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Rx: D3 */
  250. {0x08, AC_VERB_SET_POWER_STATE, 0x03}, /* S/PDIF Tx: D3 */
  251. /*{0x01, AC_VERB_SET_POWER_STATE, 0x03},*/ /* AFG: D3 This is already handled */
  252. #endif
  253. {} /* terminator */
  254. };
  255. /* SPDIF setup */
  256. static void init_digital_coef(struct hda_codec *codec)
  257. {
  258. unsigned int coef;
  259. coef = 0x0002; /* SRC_MUTE soft-mute on SPDIF (if no lock) */
  260. coef |= 0x0008; /* Replace with mute on error */
  261. if (is_active_pin(codec, CS_DIG_OUT2_PIN_NID))
  262. coef |= 0x4000; /* RX to TX1 or TX2 Loopthru / SPDIF2
  263. * SPDIF_OUT2 is shared with GPIO1 and
  264. * DMIC_SDA2.
  265. */
  266. cs_vendor_coef_set(codec, IDX_SPDIF_CTL, coef);
  267. }
  268. static int cs_init(struct hda_codec *codec)
  269. {
  270. struct cs_spec *spec = codec->spec;
  271. if (spec->vendor_nid == CS420X_VENDOR_NID) {
  272. /* init_verb sequence for C0/C1/C2 errata*/
  273. snd_hda_sequence_write(codec, cs_errata_init_verbs);
  274. snd_hda_sequence_write(codec, cs_coef_init_verbs);
  275. } else if (spec->vendor_nid == CS4208_VENDOR_NID) {
  276. snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
  277. }
  278. snd_hda_gen_init(codec);
  279. if (spec->gpio_mask) {
  280. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  281. spec->gpio_mask);
  282. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  283. spec->gpio_dir);
  284. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  285. spec->gpio_data);
  286. }
  287. if (spec->vendor_nid == CS420X_VENDOR_NID) {
  288. init_input_coef(codec);
  289. init_digital_coef(codec);
  290. }
  291. return 0;
  292. }
  293. static int cs_build_controls(struct hda_codec *codec)
  294. {
  295. int err;
  296. err = snd_hda_gen_build_controls(codec);
  297. if (err < 0)
  298. return err;
  299. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
  300. return 0;
  301. }
  302. #define cs_free snd_hda_gen_free
  303. static const struct hda_codec_ops cs_patch_ops = {
  304. .build_controls = cs_build_controls,
  305. .build_pcms = snd_hda_gen_build_pcms,
  306. .init = cs_init,
  307. .free = cs_free,
  308. .unsol_event = snd_hda_jack_unsol_event,
  309. };
  310. static int cs_parse_auto_config(struct hda_codec *codec)
  311. {
  312. struct cs_spec *spec = codec->spec;
  313. int err;
  314. int i;
  315. err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
  316. if (err < 0)
  317. return err;
  318. err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
  319. if (err < 0)
  320. return err;
  321. /* keep the ADCs powered up when it's dynamically switchable */
  322. if (spec->gen.dyn_adc_switch) {
  323. unsigned int done = 0;
  324. for (i = 0; i < spec->gen.input_mux.num_items; i++) {
  325. int idx = spec->gen.dyn_adc_idx[i];
  326. if (done & (1 << idx))
  327. continue;
  328. snd_hda_gen_fix_pin_power(codec,
  329. spec->gen.adc_nids[idx]);
  330. done |= 1 << idx;
  331. }
  332. }
  333. return 0;
  334. }
  335. static const struct hda_model_fixup cs420x_models[] = {
  336. { .id = CS420X_MBP53, .name = "mbp53" },
  337. { .id = CS420X_MBP55, .name = "mbp55" },
  338. { .id = CS420X_IMAC27, .name = "imac27" },
  339. { .id = CS420X_IMAC27_122, .name = "imac27_122" },
  340. { .id = CS420X_APPLE, .name = "apple" },
  341. { .id = CS420X_MBP101, .name = "mbp101" },
  342. { .id = CS420X_MBP81, .name = "mbp81" },
  343. { .id = CS420X_MBA42, .name = "mba42" },
  344. {}
  345. };
  346. static const struct snd_pci_quirk cs420x_fixup_tbl[] = {
  347. SND_PCI_QUIRK(0x10de, 0x0ac0, "MacBookPro 5,3", CS420X_MBP53),
  348. SND_PCI_QUIRK(0x10de, 0x0d94, "MacBookAir 3,1(2)", CS420X_MBP55),
  349. SND_PCI_QUIRK(0x10de, 0xcb79, "MacBookPro 5,5", CS420X_MBP55),
  350. SND_PCI_QUIRK(0x10de, 0xcb89, "MacBookPro 7,1", CS420X_MBP55),
  351. /* this conflicts with too many other models */
  352. /*SND_PCI_QUIRK(0x8086, 0x7270, "IMac 27 Inch", CS420X_IMAC27),*/
  353. /* codec SSID */
  354. SND_PCI_QUIRK(0x106b, 0x1c00, "MacBookPro 8,1", CS420X_MBP81),
  355. SND_PCI_QUIRK(0x106b, 0x2000, "iMac 12,2", CS420X_IMAC27_122),
  356. SND_PCI_QUIRK(0x106b, 0x2800, "MacBookPro 10,1", CS420X_MBP101),
  357. SND_PCI_QUIRK(0x106b, 0x5600, "MacBookAir 5,2", CS420X_MBP81),
  358. SND_PCI_QUIRK(0x106b, 0x5b00, "MacBookAir 4,2", CS420X_MBA42),
  359. SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS420X_APPLE),
  360. {} /* terminator */
  361. };
  362. static const struct hda_pintbl mbp53_pincfgs[] = {
  363. { 0x09, 0x012b4050 },
  364. { 0x0a, 0x90100141 },
  365. { 0x0b, 0x90100140 },
  366. { 0x0c, 0x018b3020 },
  367. { 0x0d, 0x90a00110 },
  368. { 0x0e, 0x400000f0 },
  369. { 0x0f, 0x01cbe030 },
  370. { 0x10, 0x014be060 },
  371. { 0x12, 0x400000f0 },
  372. { 0x15, 0x400000f0 },
  373. {} /* terminator */
  374. };
  375. static const struct hda_pintbl mbp55_pincfgs[] = {
  376. { 0x09, 0x012b4030 },
  377. { 0x0a, 0x90100121 },
  378. { 0x0b, 0x90100120 },
  379. { 0x0c, 0x400000f0 },
  380. { 0x0d, 0x90a00110 },
  381. { 0x0e, 0x400000f0 },
  382. { 0x0f, 0x400000f0 },
  383. { 0x10, 0x014be040 },
  384. { 0x12, 0x400000f0 },
  385. { 0x15, 0x400000f0 },
  386. {} /* terminator */
  387. };
  388. static const struct hda_pintbl imac27_pincfgs[] = {
  389. { 0x09, 0x012b4050 },
  390. { 0x0a, 0x90100140 },
  391. { 0x0b, 0x90100142 },
  392. { 0x0c, 0x018b3020 },
  393. { 0x0d, 0x90a00110 },
  394. { 0x0e, 0x400000f0 },
  395. { 0x0f, 0x01cbe030 },
  396. { 0x10, 0x014be060 },
  397. { 0x12, 0x01ab9070 },
  398. { 0x15, 0x400000f0 },
  399. {} /* terminator */
  400. };
  401. static const struct hda_pintbl mbp101_pincfgs[] = {
  402. { 0x0d, 0x40ab90f0 },
  403. { 0x0e, 0x90a600f0 },
  404. { 0x12, 0x50a600f0 },
  405. {} /* terminator */
  406. };
  407. static const struct hda_pintbl mba42_pincfgs[] = {
  408. { 0x09, 0x012b4030 }, /* HP */
  409. { 0x0a, 0x400000f0 },
  410. { 0x0b, 0x90100120 }, /* speaker */
  411. { 0x0c, 0x400000f0 },
  412. { 0x0d, 0x90a00110 }, /* mic */
  413. { 0x0e, 0x400000f0 },
  414. { 0x0f, 0x400000f0 },
  415. { 0x10, 0x400000f0 },
  416. { 0x12, 0x400000f0 },
  417. { 0x15, 0x400000f0 },
  418. {} /* terminator */
  419. };
  420. static const struct hda_pintbl mba6_pincfgs[] = {
  421. { 0x10, 0x032120f0 }, /* HP */
  422. { 0x11, 0x500000f0 },
  423. { 0x12, 0x90100010 }, /* Speaker */
  424. { 0x13, 0x500000f0 },
  425. { 0x14, 0x500000f0 },
  426. { 0x15, 0x770000f0 },
  427. { 0x16, 0x770000f0 },
  428. { 0x17, 0x430000f0 },
  429. { 0x18, 0x43ab9030 }, /* Mic */
  430. { 0x19, 0x770000f0 },
  431. { 0x1a, 0x770000f0 },
  432. { 0x1b, 0x770000f0 },
  433. { 0x1c, 0x90a00090 },
  434. { 0x1d, 0x500000f0 },
  435. { 0x1e, 0x500000f0 },
  436. { 0x1f, 0x500000f0 },
  437. { 0x20, 0x500000f0 },
  438. { 0x21, 0x430000f0 },
  439. { 0x22, 0x430000f0 },
  440. {} /* terminator */
  441. };
  442. static void cs420x_fixup_gpio_13(struct hda_codec *codec,
  443. const struct hda_fixup *fix, int action)
  444. {
  445. if (action == HDA_FIXUP_ACT_PRE_PROBE) {
  446. struct cs_spec *spec = codec->spec;
  447. spec->gpio_eapd_hp = 2; /* GPIO1 = headphones */
  448. spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
  449. spec->gpio_mask = spec->gpio_dir =
  450. spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
  451. }
  452. }
  453. static void cs420x_fixup_gpio_23(struct hda_codec *codec,
  454. const struct hda_fixup *fix, int action)
  455. {
  456. if (action == HDA_FIXUP_ACT_PRE_PROBE) {
  457. struct cs_spec *spec = codec->spec;
  458. spec->gpio_eapd_hp = 4; /* GPIO2 = headphones */
  459. spec->gpio_eapd_speaker = 8; /* GPIO3 = speakers */
  460. spec->gpio_mask = spec->gpio_dir =
  461. spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
  462. }
  463. }
  464. static const struct hda_fixup cs420x_fixups[] = {
  465. [CS420X_MBP53] = {
  466. .type = HDA_FIXUP_PINS,
  467. .v.pins = mbp53_pincfgs,
  468. .chained = true,
  469. .chain_id = CS420X_APPLE,
  470. },
  471. [CS420X_MBP55] = {
  472. .type = HDA_FIXUP_PINS,
  473. .v.pins = mbp55_pincfgs,
  474. .chained = true,
  475. .chain_id = CS420X_GPIO_13,
  476. },
  477. [CS420X_IMAC27] = {
  478. .type = HDA_FIXUP_PINS,
  479. .v.pins = imac27_pincfgs,
  480. .chained = true,
  481. .chain_id = CS420X_GPIO_13,
  482. },
  483. [CS420X_GPIO_13] = {
  484. .type = HDA_FIXUP_FUNC,
  485. .v.func = cs420x_fixup_gpio_13,
  486. },
  487. [CS420X_GPIO_23] = {
  488. .type = HDA_FIXUP_FUNC,
  489. .v.func = cs420x_fixup_gpio_23,
  490. },
  491. [CS420X_MBP101] = {
  492. .type = HDA_FIXUP_PINS,
  493. .v.pins = mbp101_pincfgs,
  494. .chained = true,
  495. .chain_id = CS420X_GPIO_13,
  496. },
  497. [CS420X_MBP81] = {
  498. .type = HDA_FIXUP_VERBS,
  499. .v.verbs = (const struct hda_verb[]) {
  500. /* internal mic ADC2: right only, single ended */
  501. {0x11, AC_VERB_SET_COEF_INDEX, IDX_ADC_CFG},
  502. {0x11, AC_VERB_SET_PROC_COEF, 0x102a},
  503. {}
  504. },
  505. .chained = true,
  506. .chain_id = CS420X_GPIO_13,
  507. },
  508. [CS420X_MBA42] = {
  509. .type = HDA_FIXUP_PINS,
  510. .v.pins = mba42_pincfgs,
  511. .chained = true,
  512. .chain_id = CS420X_GPIO_13,
  513. },
  514. };
  515. static struct cs_spec *cs_alloc_spec(struct hda_codec *codec, int vendor_nid)
  516. {
  517. struct cs_spec *spec;
  518. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  519. if (!spec)
  520. return NULL;
  521. codec->spec = spec;
  522. spec->vendor_nid = vendor_nid;
  523. codec->power_save_node = 1;
  524. snd_hda_gen_spec_init(&spec->gen);
  525. return spec;
  526. }
  527. static int patch_cs420x(struct hda_codec *codec)
  528. {
  529. struct cs_spec *spec;
  530. int err;
  531. spec = cs_alloc_spec(codec, CS420X_VENDOR_NID);
  532. if (!spec)
  533. return -ENOMEM;
  534. codec->patch_ops = cs_patch_ops;
  535. spec->gen.automute_hook = cs_automute;
  536. codec->single_adc_amp = 1;
  537. snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
  538. cs420x_fixups);
  539. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  540. err = cs_parse_auto_config(codec);
  541. if (err < 0)
  542. goto error;
  543. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  544. return 0;
  545. error:
  546. cs_free(codec);
  547. return err;
  548. }
  549. /*
  550. * CS4208 support:
  551. * Its layout is no longer compatible with CS4206/CS4207
  552. */
  553. enum {
  554. CS4208_MAC_AUTO,
  555. CS4208_MBA6,
  556. CS4208_MBP11,
  557. CS4208_MACMINI,
  558. CS4208_GPIO0,
  559. };
  560. static const struct hda_model_fixup cs4208_models[] = {
  561. { .id = CS4208_GPIO0, .name = "gpio0" },
  562. { .id = CS4208_MBA6, .name = "mba6" },
  563. { .id = CS4208_MBP11, .name = "mbp11" },
  564. { .id = CS4208_MACMINI, .name = "macmini" },
  565. {}
  566. };
  567. static const struct snd_pci_quirk cs4208_fixup_tbl[] = {
  568. SND_PCI_QUIRK_VENDOR(0x106b, "Apple", CS4208_MAC_AUTO),
  569. {} /* terminator */
  570. };
  571. /* codec SSID matching */
  572. static const struct snd_pci_quirk cs4208_mac_fixup_tbl[] = {
  573. SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
  574. SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
  575. SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
  576. SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
  577. SND_PCI_QUIRK(0x106b, 0x7b00, "MacBookPro 12,1", CS4208_MBP11),
  578. {} /* terminator */
  579. };
  580. static void cs4208_fixup_gpio0(struct hda_codec *codec,
  581. const struct hda_fixup *fix, int action)
  582. {
  583. if (action == HDA_FIXUP_ACT_PRE_PROBE) {
  584. struct cs_spec *spec = codec->spec;
  585. spec->gpio_eapd_hp = 0;
  586. spec->gpio_eapd_speaker = 1;
  587. spec->gpio_mask = spec->gpio_dir =
  588. spec->gpio_eapd_hp | spec->gpio_eapd_speaker;
  589. }
  590. }
  591. static const struct hda_fixup cs4208_fixups[];
  592. /* remap the fixup from codec SSID and apply it */
  593. static void cs4208_fixup_mac(struct hda_codec *codec,
  594. const struct hda_fixup *fix, int action)
  595. {
  596. if (action != HDA_FIXUP_ACT_PRE_PROBE)
  597. return;
  598. codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
  599. snd_hda_pick_fixup(codec, NULL, cs4208_mac_fixup_tbl, cs4208_fixups);
  600. if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET)
  601. codec->fixup_id = CS4208_GPIO0; /* default fixup */
  602. snd_hda_apply_fixup(codec, action);
  603. }
  604. /* MacMini 7,1 has the inverted jack detection */
  605. static void cs4208_fixup_macmini(struct hda_codec *codec,
  606. const struct hda_fixup *fix, int action)
  607. {
  608. static const struct hda_pintbl pincfgs[] = {
  609. { 0x18, 0x00ab9150 }, /* mic (audio-in) jack: disable detect */
  610. { 0x21, 0x004be140 }, /* SPDIF: disable detect */
  611. { }
  612. };
  613. if (action == HDA_FIXUP_ACT_PRE_PROBE) {
  614. /* HP pin (0x10) has an inverted detection */
  615. codec->inv_jack_detect = 1;
  616. /* disable the bogus Mic and SPDIF jack detections */
  617. snd_hda_apply_pincfgs(codec, pincfgs);
  618. }
  619. }
  620. static int cs4208_spdif_sw_put(struct snd_kcontrol *kcontrol,
  621. struct snd_ctl_elem_value *ucontrol)
  622. {
  623. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  624. struct cs_spec *spec = codec->spec;
  625. hda_nid_t pin = spec->gen.autocfg.dig_out_pins[0];
  626. int pinctl = ucontrol->value.integer.value[0] ? PIN_OUT : 0;
  627. snd_hda_set_pin_ctl_cache(codec, pin, pinctl);
  628. return spec->spdif_sw_put(kcontrol, ucontrol);
  629. }
  630. /* hook the SPDIF switch */
  631. static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
  632. const struct hda_fixup *fix, int action)
  633. {
  634. if (action == HDA_FIXUP_ACT_BUILD) {
  635. struct cs_spec *spec = codec->spec;
  636. struct snd_kcontrol *kctl;
  637. if (!spec->gen.autocfg.dig_out_pins[0])
  638. return;
  639. kctl = snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch");
  640. if (!kctl)
  641. return;
  642. spec->spdif_sw_put = kctl->put;
  643. kctl->put = cs4208_spdif_sw_put;
  644. }
  645. }
  646. static const struct hda_fixup cs4208_fixups[] = {
  647. [CS4208_MBA6] = {
  648. .type = HDA_FIXUP_PINS,
  649. .v.pins = mba6_pincfgs,
  650. .chained = true,
  651. .chain_id = CS4208_GPIO0,
  652. },
  653. [CS4208_MBP11] = {
  654. .type = HDA_FIXUP_FUNC,
  655. .v.func = cs4208_fixup_spdif_switch,
  656. .chained = true,
  657. .chain_id = CS4208_GPIO0,
  658. },
  659. [CS4208_MACMINI] = {
  660. .type = HDA_FIXUP_FUNC,
  661. .v.func = cs4208_fixup_macmini,
  662. .chained = true,
  663. .chain_id = CS4208_GPIO0,
  664. },
  665. [CS4208_GPIO0] = {
  666. .type = HDA_FIXUP_FUNC,
  667. .v.func = cs4208_fixup_gpio0,
  668. },
  669. [CS4208_MAC_AUTO] = {
  670. .type = HDA_FIXUP_FUNC,
  671. .v.func = cs4208_fixup_mac,
  672. },
  673. };
  674. /* correct the 0dB offset of input pins */
  675. static void cs4208_fix_amp_caps(struct hda_codec *codec, hda_nid_t adc)
  676. {
  677. unsigned int caps;
  678. caps = query_amp_caps(codec, adc, HDA_INPUT);
  679. caps &= ~(AC_AMPCAP_OFFSET);
  680. caps |= 0x02;
  681. snd_hda_override_amp_caps(codec, adc, HDA_INPUT, caps);
  682. }
  683. static int patch_cs4208(struct hda_codec *codec)
  684. {
  685. struct cs_spec *spec;
  686. int err;
  687. spec = cs_alloc_spec(codec, CS4208_VENDOR_NID);
  688. if (!spec)
  689. return -ENOMEM;
  690. codec->patch_ops = cs_patch_ops;
  691. spec->gen.automute_hook = cs_automute;
  692. /* exclude NID 0x10 (HP) from output volumes due to different steps */
  693. spec->gen.out_vol_mask = 1ULL << 0x10;
  694. snd_hda_pick_fixup(codec, cs4208_models, cs4208_fixup_tbl,
  695. cs4208_fixups);
  696. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  697. snd_hda_override_wcaps(codec, 0x18,
  698. get_wcaps(codec, 0x18) | AC_WCAP_STEREO);
  699. cs4208_fix_amp_caps(codec, 0x18);
  700. cs4208_fix_amp_caps(codec, 0x1b);
  701. cs4208_fix_amp_caps(codec, 0x1c);
  702. err = cs_parse_auto_config(codec);
  703. if (err < 0)
  704. goto error;
  705. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  706. return 0;
  707. error:
  708. cs_free(codec);
  709. return err;
  710. }
  711. /*
  712. * Cirrus Logic CS4210
  713. *
  714. * 1 DAC => HP(sense) / Speakers,
  715. * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
  716. * 1 SPDIF OUT => SPDIF Trasmitter(sense)
  717. */
  718. /* CS4210 board names */
  719. static const struct hda_model_fixup cs421x_models[] = {
  720. { .id = CS421X_CDB4210, .name = "cdb4210" },
  721. { .id = CS421X_STUMPY, .name = "stumpy" },
  722. {}
  723. };
  724. static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
  725. /* Test Intel board + CDB2410 */
  726. SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
  727. {} /* terminator */
  728. };
  729. /* CS4210 board pinconfigs */
  730. /* Default CS4210 (CDB4210)*/
  731. static const struct hda_pintbl cdb4210_pincfgs[] = {
  732. { 0x05, 0x0321401f },
  733. { 0x06, 0x90170010 },
  734. { 0x07, 0x03813031 },
  735. { 0x08, 0xb7a70037 },
  736. { 0x09, 0xb7a6003e },
  737. { 0x0a, 0x034510f0 },
  738. {} /* terminator */
  739. };
  740. /* Stumpy ChromeBox */
  741. static const struct hda_pintbl stumpy_pincfgs[] = {
  742. { 0x05, 0x022120f0 },
  743. { 0x06, 0x901700f0 },
  744. { 0x07, 0x02a120f0 },
  745. { 0x08, 0x77a70037 },
  746. { 0x09, 0x77a6003e },
  747. { 0x0a, 0x434510f0 },
  748. {} /* terminator */
  749. };
  750. /* Setup GPIO/SENSE for each board (if used) */
  751. static void cs421x_fixup_sense_b(struct hda_codec *codec,
  752. const struct hda_fixup *fix, int action)
  753. {
  754. struct cs_spec *spec = codec->spec;
  755. if (action == HDA_FIXUP_ACT_PRE_PROBE)
  756. spec->sense_b = 1;
  757. }
  758. static const struct hda_fixup cs421x_fixups[] = {
  759. [CS421X_CDB4210] = {
  760. .type = HDA_FIXUP_PINS,
  761. .v.pins = cdb4210_pincfgs,
  762. .chained = true,
  763. .chain_id = CS421X_SENSE_B,
  764. },
  765. [CS421X_SENSE_B] = {
  766. .type = HDA_FIXUP_FUNC,
  767. .v.func = cs421x_fixup_sense_b,
  768. },
  769. [CS421X_STUMPY] = {
  770. .type = HDA_FIXUP_PINS,
  771. .v.pins = stumpy_pincfgs,
  772. },
  773. };
  774. static const struct hda_verb cs421x_coef_init_verbs[] = {
  775. {0x0B, AC_VERB_SET_PROC_STATE, 1},
  776. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
  777. /*
  778. Disable Coefficient Index Auto-Increment(DAI)=1,
  779. PDREF=0
  780. */
  781. {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
  782. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
  783. /* ADC SZCMode = Digital Soft Ramp */
  784. {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
  785. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
  786. {0x0B, AC_VERB_SET_PROC_COEF,
  787. (0x0002 /* DAC SZCMode = Digital Soft Ramp */
  788. | 0x0004 /* Mute DAC on FIFO error */
  789. | 0x0008 /* Enable DAC High Pass Filter */
  790. )},
  791. {} /* terminator */
  792. };
  793. /* Errata: CS4210 rev A1 Silicon
  794. *
  795. * http://www.cirrus.com/en/pubs/errata/
  796. *
  797. * Description:
  798. * 1. Performance degredation is present in the ADC.
  799. * 2. Speaker output is not completely muted upon HP detect.
  800. * 3. Noise is present when clipping occurs on the amplified
  801. * speaker outputs.
  802. *
  803. * Workaround:
  804. * The following verb sequence written to the registers during
  805. * initialization will correct the issues listed above.
  806. */
  807. static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
  808. {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  809. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
  810. {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
  811. {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
  812. {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
  813. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
  814. {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
  815. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
  816. {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
  817. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
  818. {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
  819. {} /* terminator */
  820. };
  821. /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
  822. static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
  823. static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
  824. struct snd_ctl_elem_info *uinfo)
  825. {
  826. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  827. uinfo->count = 1;
  828. uinfo->value.integer.min = 0;
  829. uinfo->value.integer.max = 3;
  830. return 0;
  831. }
  832. static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
  833. struct snd_ctl_elem_value *ucontrol)
  834. {
  835. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  836. ucontrol->value.integer.value[0] =
  837. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
  838. return 0;
  839. }
  840. static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
  841. struct snd_ctl_elem_value *ucontrol)
  842. {
  843. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  844. unsigned int vol = ucontrol->value.integer.value[0];
  845. unsigned int coef =
  846. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
  847. unsigned int original_coef = coef;
  848. coef &= ~0x0003;
  849. coef |= (vol & 0x0003);
  850. if (original_coef == coef)
  851. return 0;
  852. else {
  853. cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
  854. return 1;
  855. }
  856. }
  857. static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
  858. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  859. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  860. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  861. .name = "Speaker Boost Playback Volume",
  862. .info = cs421x_boost_vol_info,
  863. .get = cs421x_boost_vol_get,
  864. .put = cs421x_boost_vol_put,
  865. .tlv = { .p = cs421x_speaker_boost_db_scale },
  866. };
  867. static void cs4210_pinmux_init(struct hda_codec *codec)
  868. {
  869. struct cs_spec *spec = codec->spec;
  870. unsigned int def_conf, coef;
  871. /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
  872. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  873. if (spec->gpio_mask)
  874. coef |= 0x0008; /* B1,B2 are GPIOs */
  875. else
  876. coef &= ~0x0008;
  877. if (spec->sense_b)
  878. coef |= 0x0010; /* B2 is SENSE_B, not inverted */
  879. else
  880. coef &= ~0x0010;
  881. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  882. if ((spec->gpio_mask || spec->sense_b) &&
  883. is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
  884. /*
  885. GPIO or SENSE_B forced - disconnect the DMIC pin.
  886. */
  887. def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
  888. def_conf &= ~AC_DEFCFG_PORT_CONN;
  889. def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
  890. snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
  891. }
  892. }
  893. static void cs4210_spdif_automute(struct hda_codec *codec,
  894. struct hda_jack_callback *tbl)
  895. {
  896. struct cs_spec *spec = codec->spec;
  897. bool spdif_present = false;
  898. hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
  899. /* detect on spdif is specific to CS4210 */
  900. if (!spec->spdif_detect ||
  901. spec->vendor_nid != CS4210_VENDOR_NID)
  902. return;
  903. spdif_present = snd_hda_jack_detect(codec, spdif_pin);
  904. if (spdif_present == spec->spdif_present)
  905. return;
  906. spec->spdif_present = spdif_present;
  907. /* SPDIF TX on/off */
  908. snd_hda_set_pin_ctl(codec, spdif_pin, spdif_present ? PIN_OUT : 0);
  909. cs_automute(codec);
  910. }
  911. static void parse_cs421x_digital(struct hda_codec *codec)
  912. {
  913. struct cs_spec *spec = codec->spec;
  914. struct auto_pin_cfg *cfg = &spec->gen.autocfg;
  915. int i;
  916. for (i = 0; i < cfg->dig_outs; i++) {
  917. hda_nid_t nid = cfg->dig_out_pins[i];
  918. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  919. spec->spdif_detect = 1;
  920. snd_hda_jack_detect_enable_callback(codec, nid,
  921. cs4210_spdif_automute);
  922. }
  923. }
  924. }
  925. static int cs421x_init(struct hda_codec *codec)
  926. {
  927. struct cs_spec *spec = codec->spec;
  928. if (spec->vendor_nid == CS4210_VENDOR_NID) {
  929. snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
  930. snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
  931. cs4210_pinmux_init(codec);
  932. }
  933. snd_hda_gen_init(codec);
  934. if (spec->gpio_mask) {
  935. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  936. spec->gpio_mask);
  937. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  938. spec->gpio_dir);
  939. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  940. spec->gpio_data);
  941. }
  942. init_input_coef(codec);
  943. cs4210_spdif_automute(codec, NULL);
  944. return 0;
  945. }
  946. static int cs421x_build_controls(struct hda_codec *codec)
  947. {
  948. struct cs_spec *spec = codec->spec;
  949. int err;
  950. err = snd_hda_gen_build_controls(codec);
  951. if (err < 0)
  952. return err;
  953. if (spec->gen.autocfg.speaker_outs &&
  954. spec->vendor_nid == CS4210_VENDOR_NID) {
  955. err = snd_hda_ctl_add(codec, 0,
  956. snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
  957. if (err < 0)
  958. return err;
  959. }
  960. return 0;
  961. }
  962. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  963. {
  964. unsigned int caps;
  965. /* set the upper-limit for mixer amp to 0dB */
  966. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  967. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  968. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  969. << AC_AMPCAP_NUM_STEPS_SHIFT;
  970. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  971. }
  972. static int cs421x_parse_auto_config(struct hda_codec *codec)
  973. {
  974. struct cs_spec *spec = codec->spec;
  975. hda_nid_t dac = CS4210_DAC_NID;
  976. int err;
  977. fix_volume_caps(codec, dac);
  978. err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
  979. if (err < 0)
  980. return err;
  981. err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
  982. if (err < 0)
  983. return err;
  984. parse_cs421x_digital(codec);
  985. return 0;
  986. }
  987. #ifdef CONFIG_PM
  988. /*
  989. Manage PDREF, when transitioning to D3hot
  990. (DAC,ADC) -> D3, PDREF=1, AFG->D3
  991. */
  992. static int cs421x_suspend(struct hda_codec *codec)
  993. {
  994. struct cs_spec *spec = codec->spec;
  995. unsigned int coef;
  996. snd_hda_shutup_pins(codec);
  997. snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
  998. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  999. snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
  1000. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  1001. if (spec->vendor_nid == CS4210_VENDOR_NID) {
  1002. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  1003. coef |= 0x0004; /* PDREF */
  1004. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  1005. }
  1006. return 0;
  1007. }
  1008. #endif
  1009. static const struct hda_codec_ops cs421x_patch_ops = {
  1010. .build_controls = cs421x_build_controls,
  1011. .build_pcms = snd_hda_gen_build_pcms,
  1012. .init = cs421x_init,
  1013. .free = cs_free,
  1014. .unsol_event = snd_hda_jack_unsol_event,
  1015. #ifdef CONFIG_PM
  1016. .suspend = cs421x_suspend,
  1017. #endif
  1018. };
  1019. static int patch_cs4210(struct hda_codec *codec)
  1020. {
  1021. struct cs_spec *spec;
  1022. int err;
  1023. spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
  1024. if (!spec)
  1025. return -ENOMEM;
  1026. codec->patch_ops = cs421x_patch_ops;
  1027. spec->gen.automute_hook = cs_automute;
  1028. snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
  1029. cs421x_fixups);
  1030. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  1031. /*
  1032. Update the GPIO/DMIC/SENSE_B pinmux before the configuration
  1033. is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
  1034. is disabled.
  1035. */
  1036. cs4210_pinmux_init(codec);
  1037. err = cs421x_parse_auto_config(codec);
  1038. if (err < 0)
  1039. goto error;
  1040. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  1041. return 0;
  1042. error:
  1043. cs_free(codec);
  1044. return err;
  1045. }
  1046. static int patch_cs4213(struct hda_codec *codec)
  1047. {
  1048. struct cs_spec *spec;
  1049. int err;
  1050. spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
  1051. if (!spec)
  1052. return -ENOMEM;
  1053. codec->patch_ops = cs421x_patch_ops;
  1054. err = cs421x_parse_auto_config(codec);
  1055. if (err < 0)
  1056. goto error;
  1057. return 0;
  1058. error:
  1059. cs_free(codec);
  1060. return err;
  1061. }
  1062. /*
  1063. * patch entries
  1064. */
  1065. static const struct hda_device_id snd_hda_id_cirrus[] = {
  1066. HDA_CODEC_ENTRY(0x10134206, "CS4206", patch_cs420x),
  1067. HDA_CODEC_ENTRY(0x10134207, "CS4207", patch_cs420x),
  1068. HDA_CODEC_ENTRY(0x10134208, "CS4208", patch_cs4208),
  1069. HDA_CODEC_ENTRY(0x10134210, "CS4210", patch_cs4210),
  1070. HDA_CODEC_ENTRY(0x10134213, "CS4213", patch_cs4213),
  1071. {} /* terminator */
  1072. };
  1073. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cirrus);
  1074. MODULE_LICENSE("GPL");
  1075. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  1076. static struct hda_codec_driver cirrus_driver = {
  1077. .id = snd_hda_id_cirrus,
  1078. };
  1079. module_hda_codec_driver(cirrus_driver);