patch_cirrus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. spec->gpio_data = 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, spec->gpio_data);
  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. spec->gen.automute_hook = cs_automute;
  444. snd_hda_pick_fixup(codec, cs420x_models, cs420x_fixup_tbl,
  445. cs420x_fixups);
  446. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  447. err = cs_parse_auto_config(codec);
  448. if (err < 0)
  449. goto error;
  450. codec->patch_ops = cs_patch_ops;
  451. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  452. return 0;
  453. error:
  454. cs_free(codec);
  455. return err;
  456. }
  457. /*
  458. * Cirrus Logic CS4210
  459. *
  460. * 1 DAC => HP(sense) / Speakers,
  461. * 1 ADC <= LineIn(sense) / MicIn / DMicIn,
  462. * 1 SPDIF OUT => SPDIF Trasmitter(sense)
  463. */
  464. /* CS4210 board names */
  465. static const struct hda_model_fixup cs421x_models[] = {
  466. { .id = CS421X_CDB4210, .name = "cdb4210" },
  467. {}
  468. };
  469. static const struct snd_pci_quirk cs421x_fixup_tbl[] = {
  470. /* Test Intel board + CDB2410 */
  471. SND_PCI_QUIRK(0x8086, 0x5001, "DP45SG/CDB4210", CS421X_CDB4210),
  472. {} /* terminator */
  473. };
  474. /* CS4210 board pinconfigs */
  475. /* Default CS4210 (CDB4210)*/
  476. static const struct hda_pintbl cdb4210_pincfgs[] = {
  477. { 0x05, 0x0321401f },
  478. { 0x06, 0x90170010 },
  479. { 0x07, 0x03813031 },
  480. { 0x08, 0xb7a70037 },
  481. { 0x09, 0xb7a6003e },
  482. { 0x0a, 0x034510f0 },
  483. {} /* terminator */
  484. };
  485. /* Setup GPIO/SENSE for each board (if used) */
  486. static void cs421x_fixup_sense_b(struct hda_codec *codec,
  487. const struct hda_fixup *fix, int action)
  488. {
  489. struct cs_spec *spec = codec->spec;
  490. if (action == HDA_FIXUP_ACT_PRE_PROBE)
  491. spec->sense_b = 1;
  492. }
  493. static const struct hda_fixup cs421x_fixups[] = {
  494. [CS421X_CDB4210] = {
  495. .type = HDA_FIXUP_PINS,
  496. .v.pins = cdb4210_pincfgs,
  497. .chained = true,
  498. .chain_id = CS421X_SENSE_B,
  499. },
  500. [CS421X_SENSE_B] = {
  501. .type = HDA_FIXUP_FUNC,
  502. .v.func = cs421x_fixup_sense_b,
  503. }
  504. };
  505. static const struct hda_verb cs421x_coef_init_verbs[] = {
  506. {0x0B, AC_VERB_SET_PROC_STATE, 1},
  507. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DEV_CFG},
  508. /*
  509. Disable Coefficient Index Auto-Increment(DAI)=1,
  510. PDREF=0
  511. */
  512. {0x0B, AC_VERB_SET_PROC_COEF, 0x0001 },
  513. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_ADC_CFG},
  514. /* ADC SZCMode = Digital Soft Ramp */
  515. {0x0B, AC_VERB_SET_PROC_COEF, 0x0002 },
  516. {0x0B, AC_VERB_SET_COEF_INDEX, CS421X_IDX_DAC_CFG},
  517. {0x0B, AC_VERB_SET_PROC_COEF,
  518. (0x0002 /* DAC SZCMode = Digital Soft Ramp */
  519. | 0x0004 /* Mute DAC on FIFO error */
  520. | 0x0008 /* Enable DAC High Pass Filter */
  521. )},
  522. {} /* terminator */
  523. };
  524. /* Errata: CS4210 rev A1 Silicon
  525. *
  526. * http://www.cirrus.com/en/pubs/errata/
  527. *
  528. * Description:
  529. * 1. Performance degredation is present in the ADC.
  530. * 2. Speaker output is not completely muted upon HP detect.
  531. * 3. Noise is present when clipping occurs on the amplified
  532. * speaker outputs.
  533. *
  534. * Workaround:
  535. * The following verb sequence written to the registers during
  536. * initialization will correct the issues listed above.
  537. */
  538. static const struct hda_verb cs421x_coef_init_verbs_A1_silicon_fixes[] = {
  539. {0x0B, AC_VERB_SET_PROC_STATE, 0x01}, /* VPW: processing on */
  540. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0006},
  541. {0x0B, AC_VERB_SET_PROC_COEF, 0x9999}, /* Test mode: on */
  542. {0x0B, AC_VERB_SET_COEF_INDEX, 0x000A},
  543. {0x0B, AC_VERB_SET_PROC_COEF, 0x14CB}, /* Chop double */
  544. {0x0B, AC_VERB_SET_COEF_INDEX, 0x0011},
  545. {0x0B, AC_VERB_SET_PROC_COEF, 0xA2D0}, /* Increase ADC current */
  546. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001A},
  547. {0x0B, AC_VERB_SET_PROC_COEF, 0x02A9}, /* Mute speaker */
  548. {0x0B, AC_VERB_SET_COEF_INDEX, 0x001B},
  549. {0x0B, AC_VERB_SET_PROC_COEF, 0X1006}, /* Remove noise */
  550. {} /* terminator */
  551. };
  552. /* Speaker Amp Gain is controlled by the vendor widget's coef 4 */
  553. static const DECLARE_TLV_DB_SCALE(cs421x_speaker_boost_db_scale, 900, 300, 0);
  554. static int cs421x_boost_vol_info(struct snd_kcontrol *kcontrol,
  555. struct snd_ctl_elem_info *uinfo)
  556. {
  557. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  558. uinfo->count = 1;
  559. uinfo->value.integer.min = 0;
  560. uinfo->value.integer.max = 3;
  561. return 0;
  562. }
  563. static int cs421x_boost_vol_get(struct snd_kcontrol *kcontrol,
  564. struct snd_ctl_elem_value *ucontrol)
  565. {
  566. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  567. ucontrol->value.integer.value[0] =
  568. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL) & 0x0003;
  569. return 0;
  570. }
  571. static int cs421x_boost_vol_put(struct snd_kcontrol *kcontrol,
  572. struct snd_ctl_elem_value *ucontrol)
  573. {
  574. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  575. unsigned int vol = ucontrol->value.integer.value[0];
  576. unsigned int coef =
  577. cs_vendor_coef_get(codec, CS421X_IDX_SPK_CTL);
  578. unsigned int original_coef = coef;
  579. coef &= ~0x0003;
  580. coef |= (vol & 0x0003);
  581. if (original_coef == coef)
  582. return 0;
  583. else {
  584. cs_vendor_coef_set(codec, CS421X_IDX_SPK_CTL, coef);
  585. return 1;
  586. }
  587. }
  588. static const struct snd_kcontrol_new cs421x_speaker_boost_ctl = {
  589. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  590. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  591. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  592. .name = "Speaker Boost Playback Volume",
  593. .info = cs421x_boost_vol_info,
  594. .get = cs421x_boost_vol_get,
  595. .put = cs421x_boost_vol_put,
  596. .tlv = { .p = cs421x_speaker_boost_db_scale },
  597. };
  598. static void cs4210_pinmux_init(struct hda_codec *codec)
  599. {
  600. struct cs_spec *spec = codec->spec;
  601. unsigned int def_conf, coef;
  602. /* GPIO, DMIC_SCL, DMIC_SDA and SENSE_B are multiplexed */
  603. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  604. if (spec->gpio_mask)
  605. coef |= 0x0008; /* B1,B2 are GPIOs */
  606. else
  607. coef &= ~0x0008;
  608. if (spec->sense_b)
  609. coef |= 0x0010; /* B2 is SENSE_B, not inverted */
  610. else
  611. coef &= ~0x0010;
  612. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  613. if ((spec->gpio_mask || spec->sense_b) &&
  614. is_active_pin(codec, CS421X_DMIC_PIN_NID)) {
  615. /*
  616. GPIO or SENSE_B forced - disconnect the DMIC pin.
  617. */
  618. def_conf = snd_hda_codec_get_pincfg(codec, CS421X_DMIC_PIN_NID);
  619. def_conf &= ~AC_DEFCFG_PORT_CONN;
  620. def_conf |= (AC_JACK_PORT_NONE << AC_DEFCFG_PORT_CONN_SHIFT);
  621. snd_hda_codec_set_pincfg(codec, CS421X_DMIC_PIN_NID, def_conf);
  622. }
  623. }
  624. static void cs4210_spdif_automute(struct hda_codec *codec,
  625. struct hda_jack_tbl *tbl)
  626. {
  627. struct cs_spec *spec = codec->spec;
  628. bool spdif_present = false;
  629. hda_nid_t spdif_pin = spec->gen.autocfg.dig_out_pins[0];
  630. /* detect on spdif is specific to CS4210 */
  631. if (!spec->spdif_detect ||
  632. spec->vendor_nid != CS4210_VENDOR_NID)
  633. return;
  634. spdif_present = snd_hda_jack_detect(codec, spdif_pin);
  635. if (spdif_present == spec->spdif_present)
  636. return;
  637. spec->spdif_present = spdif_present;
  638. /* SPDIF TX on/off */
  639. if (spdif_present)
  640. snd_hda_set_pin_ctl(codec, spdif_pin,
  641. spdif_present ? PIN_OUT : 0);
  642. cs_automute(codec);
  643. }
  644. static void parse_cs421x_digital(struct hda_codec *codec)
  645. {
  646. struct cs_spec *spec = codec->spec;
  647. struct auto_pin_cfg *cfg = &spec->gen.autocfg;
  648. int i;
  649. for (i = 0; i < cfg->dig_outs; i++) {
  650. hda_nid_t nid = cfg->dig_out_pins[i];
  651. if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) {
  652. spec->spdif_detect = 1;
  653. snd_hda_jack_detect_enable_callback(codec, nid,
  654. SPDIF_EVENT,
  655. cs4210_spdif_automute);
  656. }
  657. }
  658. }
  659. static int cs421x_init(struct hda_codec *codec)
  660. {
  661. struct cs_spec *spec = codec->spec;
  662. if (spec->vendor_nid == CS4210_VENDOR_NID) {
  663. snd_hda_sequence_write(codec, cs421x_coef_init_verbs);
  664. snd_hda_sequence_write(codec, cs421x_coef_init_verbs_A1_silicon_fixes);
  665. cs4210_pinmux_init(codec);
  666. }
  667. snd_hda_gen_init(codec);
  668. if (spec->gpio_mask) {
  669. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
  670. spec->gpio_mask);
  671. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
  672. spec->gpio_dir);
  673. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
  674. spec->gpio_data);
  675. }
  676. init_input_coef(codec);
  677. cs4210_spdif_automute(codec, NULL);
  678. return 0;
  679. }
  680. static int cs421x_build_controls(struct hda_codec *codec)
  681. {
  682. struct cs_spec *spec = codec->spec;
  683. int err;
  684. err = snd_hda_gen_build_controls(codec);
  685. if (err < 0)
  686. return err;
  687. if (spec->gen.autocfg.speaker_outs &&
  688. spec->vendor_nid == CS4210_VENDOR_NID) {
  689. err = snd_hda_ctl_add(codec, 0,
  690. snd_ctl_new1(&cs421x_speaker_boost_ctl, codec));
  691. if (err < 0)
  692. return err;
  693. }
  694. return 0;
  695. }
  696. static void fix_volume_caps(struct hda_codec *codec, hda_nid_t dac)
  697. {
  698. unsigned int caps;
  699. /* set the upper-limit for mixer amp to 0dB */
  700. caps = query_amp_caps(codec, dac, HDA_OUTPUT);
  701. caps &= ~(0x7f << AC_AMPCAP_NUM_STEPS_SHIFT);
  702. caps |= ((caps >> AC_AMPCAP_OFFSET_SHIFT) & 0x7f)
  703. << AC_AMPCAP_NUM_STEPS_SHIFT;
  704. snd_hda_override_amp_caps(codec, dac, HDA_OUTPUT, caps);
  705. }
  706. static int cs421x_parse_auto_config(struct hda_codec *codec)
  707. {
  708. struct cs_spec *spec = codec->spec;
  709. hda_nid_t dac = CS4210_DAC_NID;
  710. int err;
  711. fix_volume_caps(codec, dac);
  712. err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
  713. if (err < 0)
  714. return err;
  715. err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
  716. if (err < 0)
  717. return err;
  718. parse_cs421x_digital(codec);
  719. return 0;
  720. }
  721. #ifdef CONFIG_PM
  722. /*
  723. Manage PDREF, when transitioning to D3hot
  724. (DAC,ADC) -> D3, PDREF=1, AFG->D3
  725. */
  726. static int cs421x_suspend(struct hda_codec *codec)
  727. {
  728. struct cs_spec *spec = codec->spec;
  729. unsigned int coef;
  730. snd_hda_shutup_pins(codec);
  731. snd_hda_codec_write(codec, CS4210_DAC_NID, 0,
  732. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  733. snd_hda_codec_write(codec, CS4210_ADC_NID, 0,
  734. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  735. if (spec->vendor_nid == CS4210_VENDOR_NID) {
  736. coef = cs_vendor_coef_get(codec, CS421X_IDX_DEV_CFG);
  737. coef |= 0x0004; /* PDREF */
  738. cs_vendor_coef_set(codec, CS421X_IDX_DEV_CFG, coef);
  739. }
  740. return 0;
  741. }
  742. #endif
  743. static const struct hda_codec_ops cs421x_patch_ops = {
  744. .build_controls = cs421x_build_controls,
  745. .build_pcms = snd_hda_gen_build_pcms,
  746. .init = cs421x_init,
  747. .free = cs_free,
  748. .unsol_event = snd_hda_jack_unsol_event,
  749. #ifdef CONFIG_PM
  750. .suspend = cs421x_suspend,
  751. #endif
  752. };
  753. static int patch_cs4210(struct hda_codec *codec)
  754. {
  755. struct cs_spec *spec;
  756. int err;
  757. spec = cs_alloc_spec(codec, CS4210_VENDOR_NID);
  758. if (!spec)
  759. return -ENOMEM;
  760. spec->gen.automute_hook = cs_automute;
  761. snd_hda_pick_fixup(codec, cs421x_models, cs421x_fixup_tbl,
  762. cs421x_fixups);
  763. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  764. /*
  765. Update the GPIO/DMIC/SENSE_B pinmux before the configuration
  766. is auto-parsed. If GPIO or SENSE_B is forced, DMIC input
  767. is disabled.
  768. */
  769. cs4210_pinmux_init(codec);
  770. err = cs421x_parse_auto_config(codec);
  771. if (err < 0)
  772. goto error;
  773. codec->patch_ops = cs421x_patch_ops;
  774. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  775. return 0;
  776. error:
  777. cs_free(codec);
  778. return err;
  779. }
  780. static int patch_cs4213(struct hda_codec *codec)
  781. {
  782. struct cs_spec *spec;
  783. int err;
  784. spec = cs_alloc_spec(codec, CS4213_VENDOR_NID);
  785. if (!spec)
  786. return -ENOMEM;
  787. err = cs421x_parse_auto_config(codec);
  788. if (err < 0)
  789. goto error;
  790. codec->patch_ops = cs421x_patch_ops;
  791. return 0;
  792. error:
  793. cs_free(codec);
  794. return err;
  795. }
  796. /*
  797. * patch entries
  798. */
  799. static const struct hda_codec_preset snd_hda_preset_cirrus[] = {
  800. { .id = 0x10134206, .name = "CS4206", .patch = patch_cs420x },
  801. { .id = 0x10134207, .name = "CS4207", .patch = patch_cs420x },
  802. { .id = 0x10134210, .name = "CS4210", .patch = patch_cs4210 },
  803. { .id = 0x10134213, .name = "CS4213", .patch = patch_cs4213 },
  804. {} /* terminator */
  805. };
  806. MODULE_ALIAS("snd-hda-codec-id:10134206");
  807. MODULE_ALIAS("snd-hda-codec-id:10134207");
  808. MODULE_ALIAS("snd-hda-codec-id:10134210");
  809. MODULE_ALIAS("snd-hda-codec-id:10134213");
  810. MODULE_LICENSE("GPL");
  811. MODULE_DESCRIPTION("Cirrus Logic HD-audio codec");
  812. static struct hda_codec_preset_list cirrus_list = {
  813. .preset = snd_hda_preset_cirrus,
  814. .owner = THIS_MODULE,
  815. };
  816. static int __init patch_cirrus_init(void)
  817. {
  818. return snd_hda_add_codec_preset(&cirrus_list);
  819. }
  820. static void __exit patch_cirrus_exit(void)
  821. {
  822. snd_hda_delete_codec_preset(&cirrus_list);
  823. }
  824. module_init(patch_cirrus_init)
  825. module_exit(patch_cirrus_exit)