patch_cirrus.c 25 KB

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