patch_cirrus.c 28 KB

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