patch_sigmatel.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for SigmaTel STAC92xx
  5. *
  6. * Copyright (c) 2005 Embedded Alley Solutions, Inc.
  7. * Matt Porter <mporter@embeddedalley.com>
  8. *
  9. * Based on patch_cmedia.c and patch_realtek.c
  10. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  11. *
  12. * This driver is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This driver is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <sound/driver.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/pci.h>
  31. #include <sound/core.h>
  32. #include <sound/asoundef.h>
  33. #include "hda_codec.h"
  34. #include "hda_local.h"
  35. #define NUM_CONTROL_ALLOC 32
  36. #define STAC_HP_EVENT 0x37
  37. #define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
  38. #define STAC_REF 0
  39. #define STAC_D945GTP3 1
  40. #define STAC_D945GTP5 2
  41. #define STAC_MACMINI 3
  42. #define STAC_D965_2112 4
  43. #define STAC_D965_284B 5
  44. #define STAC_922X_MODELS 6 /* number of 922x models */
  45. struct sigmatel_spec {
  46. struct snd_kcontrol_new *mixers[4];
  47. unsigned int num_mixers;
  48. int board_config;
  49. unsigned int surr_switch: 1;
  50. unsigned int line_switch: 1;
  51. unsigned int mic_switch: 1;
  52. unsigned int alt_switch: 1;
  53. unsigned int hp_detect: 1;
  54. unsigned int gpio_mute: 1;
  55. /* playback */
  56. struct hda_multi_out multiout;
  57. hda_nid_t dac_nids[5];
  58. /* capture */
  59. hda_nid_t *adc_nids;
  60. unsigned int num_adcs;
  61. hda_nid_t *mux_nids;
  62. unsigned int num_muxes;
  63. hda_nid_t dig_in_nid;
  64. /* pin widgets */
  65. hda_nid_t *pin_nids;
  66. unsigned int num_pins;
  67. unsigned int *pin_configs;
  68. /* codec specific stuff */
  69. struct hda_verb *init;
  70. struct snd_kcontrol_new *mixer;
  71. /* capture source */
  72. struct hda_input_mux *input_mux;
  73. unsigned int cur_mux[3];
  74. /* i/o switches */
  75. unsigned int io_switch[2];
  76. struct hda_pcm pcm_rec[2]; /* PCM information */
  77. /* dynamic controls and input_mux */
  78. struct auto_pin_cfg autocfg;
  79. unsigned int num_kctl_alloc, num_kctl_used;
  80. struct snd_kcontrol_new *kctl_alloc;
  81. struct hda_input_mux private_imux;
  82. };
  83. static hda_nid_t stac9200_adc_nids[1] = {
  84. 0x03,
  85. };
  86. static hda_nid_t stac9200_mux_nids[1] = {
  87. 0x0c,
  88. };
  89. static hda_nid_t stac9200_dac_nids[1] = {
  90. 0x02,
  91. };
  92. static hda_nid_t stac922x_adc_nids[2] = {
  93. 0x06, 0x07,
  94. };
  95. static hda_nid_t stac9227_adc_nids[2] = {
  96. 0x07, 0x08,
  97. };
  98. #if 0
  99. static hda_nid_t d965_2112_dac_nids[3] = {
  100. 0x02, 0x03, 0x05,
  101. };
  102. #endif
  103. static hda_nid_t stac922x_mux_nids[2] = {
  104. 0x12, 0x13,
  105. };
  106. static hda_nid_t stac9227_mux_nids[2] = {
  107. 0x15, 0x16,
  108. };
  109. static hda_nid_t stac927x_adc_nids[3] = {
  110. 0x07, 0x08, 0x09
  111. };
  112. static hda_nid_t stac927x_mux_nids[3] = {
  113. 0x15, 0x16, 0x17
  114. };
  115. static hda_nid_t stac9200_pin_nids[8] = {
  116. 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  117. };
  118. static hda_nid_t stac922x_pin_nids[10] = {
  119. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  120. 0x0f, 0x10, 0x11, 0x15, 0x1b,
  121. };
  122. static hda_nid_t stac927x_pin_nids[14] = {
  123. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  124. 0x0f, 0x10, 0x11, 0x12, 0x13,
  125. 0x14, 0x21, 0x22, 0x23,
  126. };
  127. static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  128. {
  129. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  130. struct sigmatel_spec *spec = codec->spec;
  131. return snd_hda_input_mux_info(spec->input_mux, uinfo);
  132. }
  133. static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  134. {
  135. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  136. struct sigmatel_spec *spec = codec->spec;
  137. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  138. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  139. return 0;
  140. }
  141. static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  142. {
  143. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  144. struct sigmatel_spec *spec = codec->spec;
  145. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  146. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  147. spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
  148. }
  149. static struct hda_verb stac9200_core_init[] = {
  150. /* set dac0mux for dac converter */
  151. { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
  152. {}
  153. };
  154. static struct hda_verb stac922x_core_init[] = {
  155. /* set master volume and direct control */
  156. { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
  157. {}
  158. };
  159. static struct hda_verb stac9227_core_init[] = {
  160. /* set master volume and direct control */
  161. { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
  162. /* unmute node 0x1b */
  163. { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
  164. {}
  165. };
  166. static struct hda_verb d965_2112_core_init[] = {
  167. /* set master volume and direct control */
  168. { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
  169. /* unmute node 0x1b */
  170. { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
  171. /* select node 0x03 as DAC */
  172. { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
  173. {}
  174. };
  175. static struct hda_verb stac927x_core_init[] = {
  176. /* set master volume and direct control */
  177. { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
  178. {}
  179. };
  180. static struct snd_kcontrol_new stac9200_mixer[] = {
  181. HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
  182. HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
  183. {
  184. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  185. .name = "Input Source",
  186. .count = 1,
  187. .info = stac92xx_mux_enum_info,
  188. .get = stac92xx_mux_enum_get,
  189. .put = stac92xx_mux_enum_put,
  190. },
  191. HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
  192. HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
  193. HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
  194. { } /* end */
  195. };
  196. /* This needs to be generated dynamically based on sequence */
  197. static struct snd_kcontrol_new stac922x_mixer[] = {
  198. {
  199. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  200. .name = "Input Source",
  201. .count = 1,
  202. .info = stac92xx_mux_enum_info,
  203. .get = stac92xx_mux_enum_get,
  204. .put = stac92xx_mux_enum_put,
  205. },
  206. HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
  207. HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
  208. HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
  209. { } /* end */
  210. };
  211. /* This needs to be generated dynamically based on sequence */
  212. static struct snd_kcontrol_new stac9227_mixer[] = {
  213. {
  214. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  215. .name = "Input Source",
  216. .count = 1,
  217. .info = stac92xx_mux_enum_info,
  218. .get = stac92xx_mux_enum_get,
  219. .put = stac92xx_mux_enum_put,
  220. },
  221. HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
  222. HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
  223. { } /* end */
  224. };
  225. static snd_kcontrol_new_t stac927x_mixer[] = {
  226. {
  227. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  228. .name = "Input Source",
  229. .count = 1,
  230. .info = stac92xx_mux_enum_info,
  231. .get = stac92xx_mux_enum_get,
  232. .put = stac92xx_mux_enum_put,
  233. },
  234. HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
  235. HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
  236. HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
  237. { } /* end */
  238. };
  239. static int stac92xx_build_controls(struct hda_codec *codec)
  240. {
  241. struct sigmatel_spec *spec = codec->spec;
  242. int err;
  243. int i;
  244. err = snd_hda_add_new_ctls(codec, spec->mixer);
  245. if (err < 0)
  246. return err;
  247. for (i = 0; i < spec->num_mixers; i++) {
  248. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  249. if (err < 0)
  250. return err;
  251. }
  252. if (spec->multiout.dig_out_nid) {
  253. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  254. if (err < 0)
  255. return err;
  256. }
  257. if (spec->dig_in_nid) {
  258. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  259. if (err < 0)
  260. return err;
  261. }
  262. return 0;
  263. }
  264. static unsigned int ref9200_pin_configs[8] = {
  265. 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
  266. 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
  267. };
  268. static unsigned int *stac9200_brd_tbl[] = {
  269. ref9200_pin_configs,
  270. };
  271. static struct hda_board_config stac9200_cfg_tbl[] = {
  272. { .modelname = "ref",
  273. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  274. .pci_subdevice = 0x2668, /* DFI LanParty */
  275. .config = STAC_REF },
  276. {} /* terminator */
  277. };
  278. static unsigned int ref922x_pin_configs[10] = {
  279. 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
  280. 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
  281. 0x40000100, 0x40000100,
  282. };
  283. static unsigned int d945gtp3_pin_configs[10] = {
  284. 0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
  285. 0x40000100, 0x40000100, 0x40000100, 0x40000100,
  286. 0x02a19120, 0x40000100,
  287. };
  288. static unsigned int d945gtp5_pin_configs[10] = {
  289. 0x0221401f, 0x01011012, 0x01813024, 0x01014010,
  290. 0x01a19021, 0x01016011, 0x01452130, 0x40000100,
  291. 0x02a19320, 0x40000100,
  292. };
  293. static unsigned int d965_2112_pin_configs[10] = {
  294. 0x0221401f, 0x40000100, 0x40000100, 0x01014011,
  295. 0x01a19021, 0x01813024, 0x01452130, 0x40000100,
  296. 0x02a19320, 0x40000100,
  297. };
  298. static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
  299. [STAC_REF] = ref922x_pin_configs,
  300. [STAC_D945GTP3] = d945gtp3_pin_configs,
  301. [STAC_D945GTP5] = d945gtp5_pin_configs,
  302. [STAC_MACMINI] = d945gtp5_pin_configs,
  303. [STAC_D965_2112] = d965_2112_pin_configs,
  304. };
  305. static struct hda_board_config stac922x_cfg_tbl[] = {
  306. { .modelname = "ref",
  307. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  308. .pci_subdevice = 0x2668, /* DFI LanParty */
  309. .config = STAC_REF }, /* SigmaTel reference board */
  310. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  311. .pci_subdevice = 0x0101,
  312. .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
  313. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  314. .pci_subdevice = 0x0202,
  315. .config = STAC_D945GTP3 }, /* Intel D945GNT - 3 Stack, 9221 A1 */
  316. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  317. .pci_subdevice = 0x0b0b,
  318. .config = STAC_D945GTP3 }, /* Intel D945PSN - 3 Stack, 9221 A1 */
  319. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  320. .pci_subdevice = 0x0707,
  321. .config = STAC_D945GTP5 }, /* Intel D945PSV - 5 Stack */
  322. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  323. .pci_subdevice = 0x0404,
  324. .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
  325. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  326. .pci_subdevice = 0x0303,
  327. .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
  328. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  329. .pci_subdevice = 0x0013,
  330. .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
  331. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  332. .pci_subdevice = 0x0417,
  333. .config = STAC_D945GTP5 }, /* Intel D975XBK - 5 Stack */
  334. { .pci_subvendor = 0x8384,
  335. .pci_subdevice = 0x7680,
  336. .config = STAC_MACMINI }, /* Apple Mac Mini (early 2006) */
  337. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  338. .pci_subdevice = 0x2112,
  339. .config = STAC_D965_2112 },
  340. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  341. .pci_subdevice = 0x284b,
  342. .config = STAC_D965_284B },
  343. {} /* terminator */
  344. };
  345. static unsigned int ref927x_pin_configs[14] = {
  346. 0x01813122, 0x01a19021, 0x01014010, 0x01016011,
  347. 0x01012012, 0x01011014, 0x40000100, 0x40000100,
  348. 0x40000100, 0x40000100, 0x40000100, 0x01441030,
  349. 0x01c41030, 0x40000100,
  350. };
  351. static unsigned int *stac927x_brd_tbl[] = {
  352. ref927x_pin_configs,
  353. };
  354. static struct hda_board_config stac927x_cfg_tbl[] = {
  355. { .modelname = "ref",
  356. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  357. .pci_subdevice = 0x2668, /* DFI LanParty */
  358. .config = STAC_REF }, /* SigmaTel reference board */
  359. {} /* terminator */
  360. };
  361. static void stac92xx_set_config_regs(struct hda_codec *codec)
  362. {
  363. int i;
  364. struct sigmatel_spec *spec = codec->spec;
  365. unsigned int pin_cfg;
  366. for (i=0; i < spec->num_pins; i++) {
  367. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  368. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
  369. spec->pin_configs[i] & 0x000000ff);
  370. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  371. AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
  372. (spec->pin_configs[i] & 0x0000ff00) >> 8);
  373. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  374. AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
  375. (spec->pin_configs[i] & 0x00ff0000) >> 16);
  376. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  377. AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
  378. spec->pin_configs[i] >> 24);
  379. pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
  380. AC_VERB_GET_CONFIG_DEFAULT,
  381. 0x00);
  382. snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
  383. }
  384. }
  385. /*
  386. * Analog playback callbacks
  387. */
  388. static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
  389. struct hda_codec *codec,
  390. struct snd_pcm_substream *substream)
  391. {
  392. struct sigmatel_spec *spec = codec->spec;
  393. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  394. }
  395. static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  396. struct hda_codec *codec,
  397. unsigned int stream_tag,
  398. unsigned int format,
  399. struct snd_pcm_substream *substream)
  400. {
  401. struct sigmatel_spec *spec = codec->spec;
  402. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
  403. }
  404. static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  405. struct hda_codec *codec,
  406. struct snd_pcm_substream *substream)
  407. {
  408. struct sigmatel_spec *spec = codec->spec;
  409. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  410. }
  411. /*
  412. * Digital playback callbacks
  413. */
  414. static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  415. struct hda_codec *codec,
  416. struct snd_pcm_substream *substream)
  417. {
  418. struct sigmatel_spec *spec = codec->spec;
  419. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  420. }
  421. static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  422. struct hda_codec *codec,
  423. struct snd_pcm_substream *substream)
  424. {
  425. struct sigmatel_spec *spec = codec->spec;
  426. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  427. }
  428. /*
  429. * Analog capture callbacks
  430. */
  431. static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  432. struct hda_codec *codec,
  433. unsigned int stream_tag,
  434. unsigned int format,
  435. struct snd_pcm_substream *substream)
  436. {
  437. struct sigmatel_spec *spec = codec->spec;
  438. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  439. stream_tag, 0, format);
  440. return 0;
  441. }
  442. static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  443. struct hda_codec *codec,
  444. struct snd_pcm_substream *substream)
  445. {
  446. struct sigmatel_spec *spec = codec->spec;
  447. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  448. return 0;
  449. }
  450. static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
  451. .substreams = 1,
  452. .channels_min = 2,
  453. .channels_max = 2,
  454. /* NID is set in stac92xx_build_pcms */
  455. .ops = {
  456. .open = stac92xx_dig_playback_pcm_open,
  457. .close = stac92xx_dig_playback_pcm_close
  458. },
  459. };
  460. static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
  461. .substreams = 1,
  462. .channels_min = 2,
  463. .channels_max = 2,
  464. /* NID is set in stac92xx_build_pcms */
  465. };
  466. static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
  467. .substreams = 1,
  468. .channels_min = 2,
  469. .channels_max = 8,
  470. .nid = 0x02, /* NID to query formats and rates */
  471. .ops = {
  472. .open = stac92xx_playback_pcm_open,
  473. .prepare = stac92xx_playback_pcm_prepare,
  474. .cleanup = stac92xx_playback_pcm_cleanup
  475. },
  476. };
  477. static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
  478. .substreams = 1,
  479. .channels_min = 2,
  480. .channels_max = 2,
  481. .nid = 0x06, /* NID to query formats and rates */
  482. .ops = {
  483. .open = stac92xx_playback_pcm_open,
  484. .prepare = stac92xx_playback_pcm_prepare,
  485. .cleanup = stac92xx_playback_pcm_cleanup
  486. },
  487. };
  488. static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
  489. .substreams = 2,
  490. .channels_min = 2,
  491. .channels_max = 2,
  492. /* NID is set in stac92xx_build_pcms */
  493. .ops = {
  494. .prepare = stac92xx_capture_pcm_prepare,
  495. .cleanup = stac92xx_capture_pcm_cleanup
  496. },
  497. };
  498. static int stac92xx_build_pcms(struct hda_codec *codec)
  499. {
  500. struct sigmatel_spec *spec = codec->spec;
  501. struct hda_pcm *info = spec->pcm_rec;
  502. codec->num_pcms = 1;
  503. codec->pcm_info = info;
  504. info->name = "STAC92xx Analog";
  505. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
  506. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
  507. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
  508. if (spec->alt_switch) {
  509. codec->num_pcms++;
  510. info++;
  511. info->name = "STAC92xx Analog Alt";
  512. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
  513. }
  514. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  515. codec->num_pcms++;
  516. info++;
  517. info->name = "STAC92xx Digital";
  518. if (spec->multiout.dig_out_nid) {
  519. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
  520. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  521. }
  522. if (spec->dig_in_nid) {
  523. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
  524. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  525. }
  526. }
  527. return 0;
  528. }
  529. static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
  530. {
  531. unsigned int pincap = snd_hda_param_read(codec, nid,
  532. AC_PAR_PIN_CAP);
  533. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  534. if (pincap & AC_PINCAP_VREF_100)
  535. return AC_PINCTL_VREF_100;
  536. if (pincap & AC_PINCAP_VREF_80)
  537. return AC_PINCTL_VREF_80;
  538. if (pincap & AC_PINCAP_VREF_50)
  539. return AC_PINCTL_VREF_50;
  540. if (pincap & AC_PINCAP_VREF_GRD)
  541. return AC_PINCTL_VREF_GRD;
  542. return 0;
  543. }
  544. static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
  545. {
  546. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
  547. }
  548. static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  549. {
  550. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  551. uinfo->count = 1;
  552. uinfo->value.integer.min = 0;
  553. uinfo->value.integer.max = 1;
  554. return 0;
  555. }
  556. static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  557. {
  558. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  559. struct sigmatel_spec *spec = codec->spec;
  560. int io_idx = kcontrol-> private_value & 0xff;
  561. ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
  562. return 0;
  563. }
  564. static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  565. {
  566. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  567. struct sigmatel_spec *spec = codec->spec;
  568. hda_nid_t nid = kcontrol->private_value >> 8;
  569. int io_idx = kcontrol-> private_value & 0xff;
  570. unsigned short val = ucontrol->value.integer.value[0];
  571. spec->io_switch[io_idx] = val;
  572. if (val)
  573. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  574. else {
  575. unsigned int pinctl = AC_PINCTL_IN_EN;
  576. if (io_idx) /* set VREF for mic */
  577. pinctl |= stac92xx_get_vref(codec, nid);
  578. stac92xx_auto_set_pinctl(codec, nid, pinctl);
  579. }
  580. return 1;
  581. }
  582. #define STAC_CODEC_IO_SWITCH(xname, xpval) \
  583. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  584. .name = xname, \
  585. .index = 0, \
  586. .info = stac92xx_io_switch_info, \
  587. .get = stac92xx_io_switch_get, \
  588. .put = stac92xx_io_switch_put, \
  589. .private_value = xpval, \
  590. }
  591. enum {
  592. STAC_CTL_WIDGET_VOL,
  593. STAC_CTL_WIDGET_MUTE,
  594. STAC_CTL_WIDGET_IO_SWITCH,
  595. };
  596. static struct snd_kcontrol_new stac92xx_control_templates[] = {
  597. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  598. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  599. STAC_CODEC_IO_SWITCH(NULL, 0),
  600. };
  601. /* add dynamic controls */
  602. static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
  603. {
  604. struct snd_kcontrol_new *knew;
  605. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  606. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  607. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
  608. if (! knew)
  609. return -ENOMEM;
  610. if (spec->kctl_alloc) {
  611. memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
  612. kfree(spec->kctl_alloc);
  613. }
  614. spec->kctl_alloc = knew;
  615. spec->num_kctl_alloc = num;
  616. }
  617. knew = &spec->kctl_alloc[spec->num_kctl_used];
  618. *knew = stac92xx_control_templates[type];
  619. knew->name = kstrdup(name, GFP_KERNEL);
  620. if (! knew->name)
  621. return -ENOMEM;
  622. knew->private_value = val;
  623. spec->num_kctl_used++;
  624. return 0;
  625. }
  626. /* flag inputs as additional dynamic lineouts */
  627. static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  628. {
  629. struct sigmatel_spec *spec = codec->spec;
  630. switch (cfg->line_outs) {
  631. case 3:
  632. /* add line-in as side */
  633. if (cfg->input_pins[AUTO_PIN_LINE]) {
  634. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
  635. spec->line_switch = 1;
  636. cfg->line_outs++;
  637. }
  638. break;
  639. case 2:
  640. /* add line-in as clfe and mic as side */
  641. if (cfg->input_pins[AUTO_PIN_LINE]) {
  642. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
  643. spec->line_switch = 1;
  644. cfg->line_outs++;
  645. }
  646. if (cfg->input_pins[AUTO_PIN_MIC]) {
  647. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
  648. spec->mic_switch = 1;
  649. cfg->line_outs++;
  650. }
  651. break;
  652. case 1:
  653. /* add line-in as surr and mic as clfe */
  654. if (cfg->input_pins[AUTO_PIN_LINE]) {
  655. cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
  656. spec->line_switch = 1;
  657. cfg->line_outs++;
  658. }
  659. if (cfg->input_pins[AUTO_PIN_MIC]) {
  660. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
  661. spec->mic_switch = 1;
  662. cfg->line_outs++;
  663. }
  664. break;
  665. }
  666. return 0;
  667. }
  668. /*
  669. * XXX The line_out pin widget connection list may not be set to the
  670. * desired DAC nid. This is the case on 927x where ports A and B can
  671. * be routed to several DACs.
  672. *
  673. * This requires an analysis of the line-out/hp pin configuration
  674. * to provide a best fit for pin/DAC configurations that are routable.
  675. * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
  676. * A and B is not supported.
  677. */
  678. /* fill in the dac_nids table from the parsed pin configuration */
  679. static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
  680. const struct auto_pin_cfg *cfg)
  681. {
  682. struct sigmatel_spec *spec = codec->spec;
  683. hda_nid_t nid;
  684. int i;
  685. /* check the pins hardwired to audio widget */
  686. for (i = 0; i < cfg->line_outs; i++) {
  687. nid = cfg->line_out_pins[i];
  688. spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
  689. AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  690. }
  691. spec->multiout.num_dacs = cfg->line_outs;
  692. return 0;
  693. }
  694. /* add playback controls from the parsed DAC table */
  695. static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
  696. const struct auto_pin_cfg *cfg)
  697. {
  698. char name[32];
  699. static const char *chname[4] = {
  700. "Front", "Surround", NULL /*CLFE*/, "Side"
  701. };
  702. hda_nid_t nid;
  703. int i, err;
  704. for (i = 0; i < cfg->line_outs; i++) {
  705. if (!spec->multiout.dac_nids[i])
  706. continue;
  707. nid = spec->multiout.dac_nids[i];
  708. if (i == 2) {
  709. /* Center/LFE */
  710. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
  711. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  712. return err;
  713. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
  714. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  715. return err;
  716. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
  717. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  718. return err;
  719. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
  720. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  721. return err;
  722. } else {
  723. sprintf(name, "%s Playback Volume", chname[i]);
  724. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
  725. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  726. return err;
  727. sprintf(name, "%s Playback Switch", chname[i]);
  728. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
  729. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  730. return err;
  731. }
  732. }
  733. if (spec->line_switch)
  734. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
  735. return err;
  736. if (spec->mic_switch)
  737. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
  738. return err;
  739. return 0;
  740. }
  741. /* add playback controls for HP output */
  742. static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  743. {
  744. struct sigmatel_spec *spec = codec->spec;
  745. hda_nid_t pin = cfg->hp_pin;
  746. hda_nid_t nid;
  747. int i, err;
  748. unsigned int wid_caps;
  749. if (! pin)
  750. return 0;
  751. wid_caps = get_wcaps(codec, pin);
  752. if (wid_caps & AC_WCAP_UNSOL_CAP)
  753. spec->hp_detect = 1;
  754. nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  755. for (i = 0; i < cfg->line_outs; i++) {
  756. if (! spec->multiout.dac_nids[i])
  757. continue;
  758. if (spec->multiout.dac_nids[i] == nid)
  759. return 0;
  760. }
  761. spec->multiout.hp_nid = nid;
  762. /* control HP volume/switch on the output mixer amp */
  763. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
  764. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  765. return err;
  766. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
  767. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  768. return err;
  769. return 0;
  770. }
  771. /* create playback/capture controls for input pins */
  772. static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  773. {
  774. struct sigmatel_spec *spec = codec->spec;
  775. struct hda_input_mux *imux = &spec->private_imux;
  776. hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
  777. int i, j, k;
  778. for (i = 0; i < AUTO_PIN_LAST; i++) {
  779. int index = -1;
  780. if (cfg->input_pins[i]) {
  781. imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
  782. for (j=0; j<spec->num_muxes; j++) {
  783. int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
  784. for (k=0; k<num_cons; k++)
  785. if (con_lst[k] == cfg->input_pins[i]) {
  786. index = k;
  787. break;
  788. }
  789. if (index >= 0)
  790. break;
  791. }
  792. imux->items[imux->num_items].index = index;
  793. imux->num_items++;
  794. }
  795. }
  796. if (imux->num_items == 1) {
  797. /*
  798. * Set the current input for the muxes.
  799. * The STAC9221 has two input muxes with identical source
  800. * NID lists. Hopefully this won't get confused.
  801. */
  802. for (i = 0; i < spec->num_muxes; i++) {
  803. snd_hda_codec_write(codec, spec->mux_nids[i], 0,
  804. AC_VERB_SET_CONNECT_SEL,
  805. imux->items[0].index);
  806. }
  807. }
  808. return 0;
  809. }
  810. static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
  811. {
  812. struct sigmatel_spec *spec = codec->spec;
  813. int i;
  814. for (i = 0; i < spec->autocfg.line_outs; i++) {
  815. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  816. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  817. }
  818. }
  819. static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
  820. {
  821. struct sigmatel_spec *spec = codec->spec;
  822. hda_nid_t pin;
  823. pin = spec->autocfg.hp_pin;
  824. if (pin) /* connect to front */
  825. stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  826. }
  827. static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
  828. {
  829. struct sigmatel_spec *spec = codec->spec;
  830. int err;
  831. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  832. return err;
  833. if (! spec->autocfg.line_outs)
  834. return 0; /* can't find valid pin config */
  835. if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
  836. return err;
  837. if (spec->multiout.num_dacs == 0)
  838. if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
  839. return err;
  840. if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
  841. (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
  842. (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  843. return err;
  844. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  845. if (spec->multiout.max_channels > 2)
  846. spec->surr_switch = 1;
  847. if (spec->autocfg.dig_out_pin)
  848. spec->multiout.dig_out_nid = dig_out;
  849. if (spec->autocfg.dig_in_pin)
  850. spec->dig_in_nid = dig_in;
  851. if (spec->kctl_alloc)
  852. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  853. spec->input_mux = &spec->private_imux;
  854. return 1;
  855. }
  856. /* add playback controls for HP output */
  857. static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
  858. struct auto_pin_cfg *cfg)
  859. {
  860. struct sigmatel_spec *spec = codec->spec;
  861. hda_nid_t pin = cfg->hp_pin;
  862. unsigned int wid_caps;
  863. if (! pin)
  864. return 0;
  865. wid_caps = get_wcaps(codec, pin);
  866. if (wid_caps & AC_WCAP_UNSOL_CAP)
  867. spec->hp_detect = 1;
  868. return 0;
  869. }
  870. static int stac9200_parse_auto_config(struct hda_codec *codec)
  871. {
  872. struct sigmatel_spec *spec = codec->spec;
  873. int err;
  874. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  875. return err;
  876. if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  877. return err;
  878. if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
  879. return err;
  880. if (spec->autocfg.dig_out_pin)
  881. spec->multiout.dig_out_nid = 0x05;
  882. if (spec->autocfg.dig_in_pin)
  883. spec->dig_in_nid = 0x04;
  884. if (spec->kctl_alloc)
  885. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  886. spec->input_mux = &spec->private_imux;
  887. return 1;
  888. }
  889. /*
  890. * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
  891. * funky external mute control using GPIO pins.
  892. */
  893. static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
  894. {
  895. unsigned int gpiostate, gpiomask, gpiodir;
  896. gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
  897. AC_VERB_GET_GPIO_DATA, 0);
  898. if (!muted)
  899. gpiostate |= (1 << pin);
  900. else
  901. gpiostate &= ~(1 << pin);
  902. gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
  903. AC_VERB_GET_GPIO_MASK, 0);
  904. gpiomask |= (1 << pin);
  905. gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
  906. AC_VERB_GET_GPIO_DIRECTION, 0);
  907. gpiodir |= (1 << pin);
  908. /* AppleHDA seems to do this -- WTF is this verb?? */
  909. snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
  910. snd_hda_codec_write(codec, codec->afg, 0,
  911. AC_VERB_SET_GPIO_MASK, gpiomask);
  912. snd_hda_codec_write(codec, codec->afg, 0,
  913. AC_VERB_SET_GPIO_DIRECTION, gpiodir);
  914. msleep(1);
  915. snd_hda_codec_write(codec, codec->afg, 0,
  916. AC_VERB_SET_GPIO_DATA, gpiostate);
  917. }
  918. static int stac92xx_init(struct hda_codec *codec)
  919. {
  920. struct sigmatel_spec *spec = codec->spec;
  921. struct auto_pin_cfg *cfg = &spec->autocfg;
  922. int i;
  923. snd_hda_sequence_write(codec, spec->init);
  924. /* set up pins */
  925. if (spec->hp_detect) {
  926. /* Enable unsolicited responses on the HP widget */
  927. snd_hda_codec_write(codec, cfg->hp_pin, 0,
  928. AC_VERB_SET_UNSOLICITED_ENABLE,
  929. STAC_UNSOL_ENABLE);
  930. /* fake event to set up pins */
  931. codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
  932. } else {
  933. stac92xx_auto_init_multi_out(codec);
  934. stac92xx_auto_init_hp_out(codec);
  935. }
  936. for (i = 0; i < AUTO_PIN_LAST; i++) {
  937. hda_nid_t nid = cfg->input_pins[i];
  938. if (nid) {
  939. unsigned int pinctl = AC_PINCTL_IN_EN;
  940. if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
  941. pinctl |= stac92xx_get_vref(codec, nid);
  942. stac92xx_auto_set_pinctl(codec, nid, pinctl);
  943. }
  944. }
  945. if (cfg->dig_out_pin)
  946. stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
  947. AC_PINCTL_OUT_EN);
  948. if (cfg->dig_in_pin)
  949. stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
  950. AC_PINCTL_IN_EN);
  951. if (spec->gpio_mute) {
  952. stac922x_gpio_mute(codec, 0, 0);
  953. stac922x_gpio_mute(codec, 1, 0);
  954. }
  955. return 0;
  956. }
  957. static void stac92xx_free(struct hda_codec *codec)
  958. {
  959. struct sigmatel_spec *spec = codec->spec;
  960. int i;
  961. if (! spec)
  962. return;
  963. if (spec->kctl_alloc) {
  964. for (i = 0; i < spec->num_kctl_used; i++)
  965. kfree(spec->kctl_alloc[i].name);
  966. kfree(spec->kctl_alloc);
  967. }
  968. kfree(spec);
  969. }
  970. static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
  971. unsigned int flag)
  972. {
  973. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  974. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  975. snd_hda_codec_write(codec, nid, 0,
  976. AC_VERB_SET_PIN_WIDGET_CONTROL,
  977. pin_ctl | flag);
  978. }
  979. static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
  980. unsigned int flag)
  981. {
  982. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  983. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  984. snd_hda_codec_write(codec, nid, 0,
  985. AC_VERB_SET_PIN_WIDGET_CONTROL,
  986. pin_ctl & ~flag);
  987. }
  988. static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
  989. {
  990. struct sigmatel_spec *spec = codec->spec;
  991. struct auto_pin_cfg *cfg = &spec->autocfg;
  992. int i, presence;
  993. if ((res >> 26) != STAC_HP_EVENT)
  994. return;
  995. presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
  996. AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
  997. if (presence) {
  998. /* disable lineouts, enable hp */
  999. for (i = 0; i < cfg->line_outs; i++)
  1000. stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
  1001. AC_PINCTL_OUT_EN);
  1002. stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  1003. } else {
  1004. /* enable lineouts, disable hp */
  1005. for (i = 0; i < cfg->line_outs; i++)
  1006. stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
  1007. AC_PINCTL_OUT_EN);
  1008. stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  1009. }
  1010. }
  1011. #ifdef CONFIG_PM
  1012. static int stac92xx_resume(struct hda_codec *codec)
  1013. {
  1014. struct sigmatel_spec *spec = codec->spec;
  1015. int i;
  1016. stac92xx_init(codec);
  1017. for (i = 0; i < spec->num_mixers; i++)
  1018. snd_hda_resume_ctls(codec, spec->mixers[i]);
  1019. if (spec->multiout.dig_out_nid)
  1020. snd_hda_resume_spdif_out(codec);
  1021. if (spec->dig_in_nid)
  1022. snd_hda_resume_spdif_in(codec);
  1023. return 0;
  1024. }
  1025. #endif
  1026. static struct hda_codec_ops stac92xx_patch_ops = {
  1027. .build_controls = stac92xx_build_controls,
  1028. .build_pcms = stac92xx_build_pcms,
  1029. .init = stac92xx_init,
  1030. .free = stac92xx_free,
  1031. .unsol_event = stac92xx_unsol_event,
  1032. #ifdef CONFIG_PM
  1033. .resume = stac92xx_resume,
  1034. #endif
  1035. };
  1036. static int patch_stac9200(struct hda_codec *codec)
  1037. {
  1038. struct sigmatel_spec *spec;
  1039. int err;
  1040. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1041. if (spec == NULL)
  1042. return -ENOMEM;
  1043. codec->spec = spec;
  1044. spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
  1045. if (spec->board_config < 0)
  1046. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
  1047. else {
  1048. spec->num_pins = 8;
  1049. spec->pin_nids = stac9200_pin_nids;
  1050. spec->pin_configs = stac9200_brd_tbl[spec->board_config];
  1051. stac92xx_set_config_regs(codec);
  1052. }
  1053. spec->multiout.max_channels = 2;
  1054. spec->multiout.num_dacs = 1;
  1055. spec->multiout.dac_nids = stac9200_dac_nids;
  1056. spec->adc_nids = stac9200_adc_nids;
  1057. spec->mux_nids = stac9200_mux_nids;
  1058. spec->num_muxes = 1;
  1059. spec->init = stac9200_core_init;
  1060. spec->mixer = stac9200_mixer;
  1061. err = stac9200_parse_auto_config(codec);
  1062. if (err < 0) {
  1063. stac92xx_free(codec);
  1064. return err;
  1065. }
  1066. codec->patch_ops = stac92xx_patch_ops;
  1067. return 0;
  1068. }
  1069. static int patch_stac922x(struct hda_codec *codec)
  1070. {
  1071. struct sigmatel_spec *spec;
  1072. int err;
  1073. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1074. if (spec == NULL)
  1075. return -ENOMEM;
  1076. codec->spec = spec;
  1077. spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
  1078. if (spec->board_config < 0)
  1079. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
  1080. "using BIOS defaults\n");
  1081. else if (stac922x_brd_tbl[spec->board_config] != NULL) {
  1082. spec->num_pins = 10;
  1083. spec->pin_nids = stac922x_pin_nids;
  1084. spec->pin_configs = stac922x_brd_tbl[spec->board_config];
  1085. stac92xx_set_config_regs(codec);
  1086. }
  1087. spec->adc_nids = stac922x_adc_nids;
  1088. spec->mux_nids = stac922x_mux_nids;
  1089. spec->num_muxes = 2;
  1090. spec->init = stac922x_core_init;
  1091. spec->mixer = stac922x_mixer;
  1092. spec->multiout.dac_nids = spec->dac_nids;
  1093. switch (spec->board_config) {
  1094. case STAC_D965_2112:
  1095. spec->adc_nids = stac9227_adc_nids;
  1096. spec->mux_nids = stac9227_mux_nids;
  1097. #if 0
  1098. spec->multiout.dac_nids = d965_2112_dac_nids;
  1099. spec->multiout.num_dacs = ARRAY_SIZE(d965_2112_dac_nids);
  1100. #endif
  1101. spec->init = d965_2112_core_init;
  1102. spec->mixer = stac9227_mixer;
  1103. break;
  1104. case STAC_D965_284B:
  1105. spec->adc_nids = stac9227_adc_nids;
  1106. spec->mux_nids = stac9227_mux_nids;
  1107. spec->init = stac9227_core_init;
  1108. spec->mixer = stac9227_mixer;
  1109. break;
  1110. }
  1111. err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
  1112. if (err < 0) {
  1113. stac92xx_free(codec);
  1114. return err;
  1115. }
  1116. if (spec->board_config == STAC_MACMINI)
  1117. spec->gpio_mute = 1;
  1118. codec->patch_ops = stac92xx_patch_ops;
  1119. return 0;
  1120. }
  1121. static int patch_stac927x(struct hda_codec *codec)
  1122. {
  1123. struct sigmatel_spec *spec;
  1124. int err;
  1125. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1126. if (spec == NULL)
  1127. return -ENOMEM;
  1128. codec->spec = spec;
  1129. spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
  1130. if (spec->board_config < 0)
  1131. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
  1132. else {
  1133. spec->num_pins = 14;
  1134. spec->pin_nids = stac927x_pin_nids;
  1135. spec->pin_configs = stac927x_brd_tbl[spec->board_config];
  1136. stac92xx_set_config_regs(codec);
  1137. }
  1138. spec->adc_nids = stac927x_adc_nids;
  1139. spec->mux_nids = stac927x_mux_nids;
  1140. spec->num_muxes = 3;
  1141. spec->init = stac927x_core_init;
  1142. spec->mixer = stac927x_mixer;
  1143. spec->multiout.dac_nids = spec->dac_nids;
  1144. err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
  1145. if (err < 0) {
  1146. stac92xx_free(codec);
  1147. return err;
  1148. }
  1149. codec->patch_ops = stac92xx_patch_ops;
  1150. return 0;
  1151. }
  1152. /*
  1153. * STAC 7661(?) hack
  1154. */
  1155. /* static config for Sony VAIO FE550G */
  1156. static hda_nid_t vaio_dacs[] = { 0x2 };
  1157. #define VAIO_HP_DAC 0x5
  1158. static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
  1159. static hda_nid_t vaio_mux_nids[] = { 0x15 };
  1160. static struct hda_input_mux vaio_mux = {
  1161. .num_items = 2,
  1162. .items = {
  1163. /* { "HP", 0x0 }, */
  1164. { "Line", 0x1 },
  1165. { "Mic", 0x2 },
  1166. { "PCM", 0x3 },
  1167. }
  1168. };
  1169. static struct hda_verb vaio_init[] = {
  1170. {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
  1171. {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
  1172. {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
  1173. {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
  1174. {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
  1175. {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
  1176. {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
  1177. {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
  1178. {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
  1179. {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
  1180. {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
  1181. {}
  1182. };
  1183. /* bind volumes of both NID 0x02 and 0x05 */
  1184. static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
  1185. struct snd_ctl_elem_value *ucontrol)
  1186. {
  1187. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1188. long *valp = ucontrol->value.integer.value;
  1189. int change;
  1190. change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
  1191. 0x7f, valp[0] & 0x7f);
  1192. change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
  1193. 0x7f, valp[1] & 0x7f);
  1194. snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
  1195. 0x7f, valp[0] & 0x7f);
  1196. snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
  1197. 0x7f, valp[1] & 0x7f);
  1198. return change;
  1199. }
  1200. /* bind volumes of both NID 0x02 and 0x05 */
  1201. static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
  1202. struct snd_ctl_elem_value *ucontrol)
  1203. {
  1204. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1205. long *valp = ucontrol->value.integer.value;
  1206. int change;
  1207. change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
  1208. 0x80, (valp[0] ? 0 : 0x80));
  1209. change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
  1210. 0x80, (valp[1] ? 0 : 0x80));
  1211. snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
  1212. 0x80, (valp[0] ? 0 : 0x80));
  1213. snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
  1214. 0x80, (valp[1] ? 0 : 0x80));
  1215. return change;
  1216. }
  1217. static struct snd_kcontrol_new vaio_mixer[] = {
  1218. {
  1219. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1220. .name = "Master Playback Volume",
  1221. .info = snd_hda_mixer_amp_volume_info,
  1222. .get = snd_hda_mixer_amp_volume_get,
  1223. .put = vaio_master_vol_put,
  1224. .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
  1225. },
  1226. {
  1227. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1228. .name = "Master Playback Switch",
  1229. .info = snd_hda_mixer_amp_switch_info,
  1230. .get = snd_hda_mixer_amp_switch_get,
  1231. .put = vaio_master_sw_put,
  1232. .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
  1233. },
  1234. /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
  1235. HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
  1236. HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
  1237. {
  1238. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1239. .name = "Capture Source",
  1240. .count = 1,
  1241. .info = stac92xx_mux_enum_info,
  1242. .get = stac92xx_mux_enum_get,
  1243. .put = stac92xx_mux_enum_put,
  1244. },
  1245. {}
  1246. };
  1247. static struct hda_codec_ops stac7661_patch_ops = {
  1248. .build_controls = stac92xx_build_controls,
  1249. .build_pcms = stac92xx_build_pcms,
  1250. .init = stac92xx_init,
  1251. .free = stac92xx_free,
  1252. #ifdef CONFIG_PM
  1253. .resume = stac92xx_resume,
  1254. #endif
  1255. };
  1256. enum { STAC7661_VAIO };
  1257. static struct hda_board_config stac7661_cfg_tbl[] = {
  1258. { .modelname = "vaio", .config = STAC7661_VAIO },
  1259. { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
  1260. .config = STAC7661_VAIO },
  1261. { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
  1262. .config = STAC7661_VAIO },
  1263. {}
  1264. };
  1265. static int patch_stac7661(struct hda_codec *codec)
  1266. {
  1267. struct sigmatel_spec *spec;
  1268. int board_config;
  1269. board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
  1270. if (board_config < 0)
  1271. /* unknown config, let generic-parser do its job... */
  1272. return snd_hda_parse_generic_codec(codec);
  1273. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1274. if (spec == NULL)
  1275. return -ENOMEM;
  1276. codec->spec = spec;
  1277. switch (board_config) {
  1278. case STAC7661_VAIO:
  1279. spec->mixer = vaio_mixer;
  1280. spec->init = vaio_init;
  1281. spec->multiout.max_channels = 2;
  1282. spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
  1283. spec->multiout.dac_nids = vaio_dacs;
  1284. spec->multiout.hp_nid = VAIO_HP_DAC;
  1285. spec->num_adcs = ARRAY_SIZE(vaio_adcs);
  1286. spec->adc_nids = vaio_adcs;
  1287. spec->input_mux = &vaio_mux;
  1288. spec->mux_nids = vaio_mux_nids;
  1289. break;
  1290. }
  1291. codec->patch_ops = stac7661_patch_ops;
  1292. return 0;
  1293. }
  1294. /*
  1295. * patch entries
  1296. */
  1297. struct hda_codec_preset snd_hda_preset_sigmatel[] = {
  1298. { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
  1299. { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
  1300. { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
  1301. { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
  1302. { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
  1303. { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
  1304. { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
  1305. { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac922x },
  1306. { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac922x },
  1307. { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac922x },
  1308. { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac922x },
  1309. { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac922x },
  1310. { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac922x },
  1311. { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
  1312. { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
  1313. { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
  1314. { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
  1315. { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
  1316. { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
  1317. { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
  1318. { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
  1319. { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
  1320. { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
  1321. { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
  1322. {} /* terminator */
  1323. };