patch_cirrus.c 26 KB

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