patch_sigmatel.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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_D965_2112] = d965_2112_pin_configs,
  303. };
  304. static struct hda_board_config stac922x_cfg_tbl[] = {
  305. { .modelname = "ref",
  306. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  307. .pci_subdevice = 0x2668, /* DFI LanParty */
  308. .config = STAC_REF }, /* SigmaTel reference board */
  309. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  310. .pci_subdevice = 0x0101,
  311. .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
  312. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  313. .pci_subdevice = 0x0202,
  314. .config = STAC_D945GTP3 }, /* Intel D945GNT - 3 Stack, 9221 A1 */
  315. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  316. .pci_subdevice = 0x0b0b,
  317. .config = STAC_D945GTP3 }, /* Intel D945PSN - 3 Stack, 9221 A1 */
  318. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  319. .pci_subdevice = 0x0707,
  320. .config = STAC_D945GTP5 }, /* Intel D945PSV - 5 Stack */
  321. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  322. .pci_subdevice = 0x0404,
  323. .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
  324. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  325. .pci_subdevice = 0x0303,
  326. .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
  327. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  328. .pci_subdevice = 0x0013,
  329. .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
  330. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  331. .pci_subdevice = 0x0417,
  332. .config = STAC_D945GTP5 }, /* Intel D975XBK - 5 Stack */
  333. { .pci_subvendor = 0x8384,
  334. .pci_subdevice = 0x7680,
  335. .config = STAC_MACMINI }, /* Apple Mac Mini (early 2006) */
  336. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  337. .pci_subdevice = 0x2112,
  338. .config = STAC_D965_2112 },
  339. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  340. .pci_subdevice = 0x284b,
  341. .config = STAC_D965_284B },
  342. {} /* terminator */
  343. };
  344. static unsigned int ref927x_pin_configs[14] = {
  345. 0x01813122, 0x01a19021, 0x01014010, 0x01016011,
  346. 0x01012012, 0x01011014, 0x40000100, 0x40000100,
  347. 0x40000100, 0x40000100, 0x40000100, 0x01441030,
  348. 0x01c41030, 0x40000100,
  349. };
  350. static unsigned int *stac927x_brd_tbl[] = {
  351. ref927x_pin_configs,
  352. };
  353. static struct hda_board_config stac927x_cfg_tbl[] = {
  354. { .modelname = "ref",
  355. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  356. .pci_subdevice = 0x2668, /* DFI LanParty */
  357. .config = STAC_REF }, /* SigmaTel reference board */
  358. {} /* terminator */
  359. };
  360. static void stac92xx_set_config_regs(struct hda_codec *codec)
  361. {
  362. int i;
  363. struct sigmatel_spec *spec = codec->spec;
  364. unsigned int pin_cfg;
  365. for (i=0; i < spec->num_pins; i++) {
  366. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  367. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
  368. spec->pin_configs[i] & 0x000000ff);
  369. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  370. AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
  371. (spec->pin_configs[i] & 0x0000ff00) >> 8);
  372. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  373. AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
  374. (spec->pin_configs[i] & 0x00ff0000) >> 16);
  375. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  376. AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
  377. spec->pin_configs[i] >> 24);
  378. pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
  379. AC_VERB_GET_CONFIG_DEFAULT,
  380. 0x00);
  381. snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
  382. }
  383. }
  384. /*
  385. * Analog playback callbacks
  386. */
  387. static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
  388. struct hda_codec *codec,
  389. struct snd_pcm_substream *substream)
  390. {
  391. struct sigmatel_spec *spec = codec->spec;
  392. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  393. }
  394. static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  395. struct hda_codec *codec,
  396. unsigned int stream_tag,
  397. unsigned int format,
  398. struct snd_pcm_substream *substream)
  399. {
  400. struct sigmatel_spec *spec = codec->spec;
  401. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
  402. }
  403. static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  404. struct hda_codec *codec,
  405. struct snd_pcm_substream *substream)
  406. {
  407. struct sigmatel_spec *spec = codec->spec;
  408. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  409. }
  410. /*
  411. * Digital playback callbacks
  412. */
  413. static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  414. struct hda_codec *codec,
  415. struct snd_pcm_substream *substream)
  416. {
  417. struct sigmatel_spec *spec = codec->spec;
  418. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  419. }
  420. static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  421. struct hda_codec *codec,
  422. struct snd_pcm_substream *substream)
  423. {
  424. struct sigmatel_spec *spec = codec->spec;
  425. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  426. }
  427. /*
  428. * Analog capture callbacks
  429. */
  430. static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  431. struct hda_codec *codec,
  432. unsigned int stream_tag,
  433. unsigned int format,
  434. struct snd_pcm_substream *substream)
  435. {
  436. struct sigmatel_spec *spec = codec->spec;
  437. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  438. stream_tag, 0, format);
  439. return 0;
  440. }
  441. static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  442. struct hda_codec *codec,
  443. struct snd_pcm_substream *substream)
  444. {
  445. struct sigmatel_spec *spec = codec->spec;
  446. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  447. return 0;
  448. }
  449. static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
  450. .substreams = 1,
  451. .channels_min = 2,
  452. .channels_max = 2,
  453. /* NID is set in stac92xx_build_pcms */
  454. .ops = {
  455. .open = stac92xx_dig_playback_pcm_open,
  456. .close = stac92xx_dig_playback_pcm_close
  457. },
  458. };
  459. static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
  460. .substreams = 1,
  461. .channels_min = 2,
  462. .channels_max = 2,
  463. /* NID is set in stac92xx_build_pcms */
  464. };
  465. static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
  466. .substreams = 1,
  467. .channels_min = 2,
  468. .channels_max = 8,
  469. .nid = 0x02, /* NID to query formats and rates */
  470. .ops = {
  471. .open = stac92xx_playback_pcm_open,
  472. .prepare = stac92xx_playback_pcm_prepare,
  473. .cleanup = stac92xx_playback_pcm_cleanup
  474. },
  475. };
  476. static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
  477. .substreams = 1,
  478. .channels_min = 2,
  479. .channels_max = 2,
  480. .nid = 0x06, /* NID to query formats and rates */
  481. .ops = {
  482. .open = stac92xx_playback_pcm_open,
  483. .prepare = stac92xx_playback_pcm_prepare,
  484. .cleanup = stac92xx_playback_pcm_cleanup
  485. },
  486. };
  487. static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
  488. .substreams = 2,
  489. .channels_min = 2,
  490. .channels_max = 2,
  491. /* NID is set in stac92xx_build_pcms */
  492. .ops = {
  493. .prepare = stac92xx_capture_pcm_prepare,
  494. .cleanup = stac92xx_capture_pcm_cleanup
  495. },
  496. };
  497. static int stac92xx_build_pcms(struct hda_codec *codec)
  498. {
  499. struct sigmatel_spec *spec = codec->spec;
  500. struct hda_pcm *info = spec->pcm_rec;
  501. codec->num_pcms = 1;
  502. codec->pcm_info = info;
  503. info->name = "STAC92xx Analog";
  504. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
  505. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
  506. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
  507. if (spec->alt_switch) {
  508. codec->num_pcms++;
  509. info++;
  510. info->name = "STAC92xx Analog Alt";
  511. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
  512. }
  513. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  514. codec->num_pcms++;
  515. info++;
  516. info->name = "STAC92xx Digital";
  517. if (spec->multiout.dig_out_nid) {
  518. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
  519. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  520. }
  521. if (spec->dig_in_nid) {
  522. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
  523. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  524. }
  525. }
  526. return 0;
  527. }
  528. static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
  529. {
  530. unsigned int pincap = snd_hda_param_read(codec, nid,
  531. AC_PAR_PIN_CAP);
  532. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  533. if (pincap & AC_PINCAP_VREF_100)
  534. return AC_PINCTL_VREF_100;
  535. if (pincap & AC_PINCAP_VREF_80)
  536. return AC_PINCTL_VREF_80;
  537. if (pincap & AC_PINCAP_VREF_50)
  538. return AC_PINCTL_VREF_50;
  539. if (pincap & AC_PINCAP_VREF_GRD)
  540. return AC_PINCTL_VREF_GRD;
  541. return 0;
  542. }
  543. static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
  544. {
  545. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
  546. }
  547. static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  548. {
  549. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  550. uinfo->count = 1;
  551. uinfo->value.integer.min = 0;
  552. uinfo->value.integer.max = 1;
  553. return 0;
  554. }
  555. static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  556. {
  557. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  558. struct sigmatel_spec *spec = codec->spec;
  559. int io_idx = kcontrol-> private_value & 0xff;
  560. ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
  561. return 0;
  562. }
  563. static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  564. {
  565. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  566. struct sigmatel_spec *spec = codec->spec;
  567. hda_nid_t nid = kcontrol->private_value >> 8;
  568. int io_idx = kcontrol-> private_value & 0xff;
  569. unsigned short val = ucontrol->value.integer.value[0];
  570. spec->io_switch[io_idx] = val;
  571. if (val)
  572. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  573. else {
  574. unsigned int pinctl = AC_PINCTL_IN_EN;
  575. if (io_idx) /* set VREF for mic */
  576. pinctl |= stac92xx_get_vref(codec, nid);
  577. stac92xx_auto_set_pinctl(codec, nid, pinctl);
  578. }
  579. return 1;
  580. }
  581. #define STAC_CODEC_IO_SWITCH(xname, xpval) \
  582. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  583. .name = xname, \
  584. .index = 0, \
  585. .info = stac92xx_io_switch_info, \
  586. .get = stac92xx_io_switch_get, \
  587. .put = stac92xx_io_switch_put, \
  588. .private_value = xpval, \
  589. }
  590. enum {
  591. STAC_CTL_WIDGET_VOL,
  592. STAC_CTL_WIDGET_MUTE,
  593. STAC_CTL_WIDGET_IO_SWITCH,
  594. };
  595. static struct snd_kcontrol_new stac92xx_control_templates[] = {
  596. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  597. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  598. STAC_CODEC_IO_SWITCH(NULL, 0),
  599. };
  600. /* add dynamic controls */
  601. static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
  602. {
  603. struct snd_kcontrol_new *knew;
  604. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  605. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  606. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
  607. if (! knew)
  608. return -ENOMEM;
  609. if (spec->kctl_alloc) {
  610. memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
  611. kfree(spec->kctl_alloc);
  612. }
  613. spec->kctl_alloc = knew;
  614. spec->num_kctl_alloc = num;
  615. }
  616. knew = &spec->kctl_alloc[spec->num_kctl_used];
  617. *knew = stac92xx_control_templates[type];
  618. knew->name = kstrdup(name, GFP_KERNEL);
  619. if (! knew->name)
  620. return -ENOMEM;
  621. knew->private_value = val;
  622. spec->num_kctl_used++;
  623. return 0;
  624. }
  625. /* flag inputs as additional dynamic lineouts */
  626. static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  627. {
  628. struct sigmatel_spec *spec = codec->spec;
  629. switch (cfg->line_outs) {
  630. case 3:
  631. /* add line-in as side */
  632. if (cfg->input_pins[AUTO_PIN_LINE]) {
  633. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
  634. spec->line_switch = 1;
  635. cfg->line_outs++;
  636. }
  637. break;
  638. case 2:
  639. /* add line-in as clfe and mic as side */
  640. if (cfg->input_pins[AUTO_PIN_LINE]) {
  641. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
  642. spec->line_switch = 1;
  643. cfg->line_outs++;
  644. }
  645. if (cfg->input_pins[AUTO_PIN_MIC]) {
  646. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
  647. spec->mic_switch = 1;
  648. cfg->line_outs++;
  649. }
  650. break;
  651. case 1:
  652. /* add line-in as surr and mic as clfe */
  653. if (cfg->input_pins[AUTO_PIN_LINE]) {
  654. cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
  655. spec->line_switch = 1;
  656. cfg->line_outs++;
  657. }
  658. if (cfg->input_pins[AUTO_PIN_MIC]) {
  659. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
  660. spec->mic_switch = 1;
  661. cfg->line_outs++;
  662. }
  663. break;
  664. }
  665. return 0;
  666. }
  667. /*
  668. * XXX The line_out pin widget connection list may not be set to the
  669. * desired DAC nid. This is the case on 927x where ports A and B can
  670. * be routed to several DACs.
  671. *
  672. * This requires an analysis of the line-out/hp pin configuration
  673. * to provide a best fit for pin/DAC configurations that are routable.
  674. * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
  675. * A and B is not supported.
  676. */
  677. /* fill in the dac_nids table from the parsed pin configuration */
  678. static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
  679. const struct auto_pin_cfg *cfg)
  680. {
  681. struct sigmatel_spec *spec = codec->spec;
  682. hda_nid_t nid;
  683. int i;
  684. /* check the pins hardwired to audio widget */
  685. for (i = 0; i < cfg->line_outs; i++) {
  686. nid = cfg->line_out_pins[i];
  687. spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
  688. AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  689. }
  690. spec->multiout.num_dacs = cfg->line_outs;
  691. return 0;
  692. }
  693. /* add playback controls from the parsed DAC table */
  694. static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
  695. const struct auto_pin_cfg *cfg)
  696. {
  697. char name[32];
  698. static const char *chname[4] = {
  699. "Front", "Surround", NULL /*CLFE*/, "Side"
  700. };
  701. hda_nid_t nid;
  702. int i, err;
  703. for (i = 0; i < cfg->line_outs; i++) {
  704. if (!spec->multiout.dac_nids[i])
  705. continue;
  706. nid = spec->multiout.dac_nids[i];
  707. if (i == 2) {
  708. /* Center/LFE */
  709. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
  710. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  711. return err;
  712. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
  713. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  714. return err;
  715. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
  716. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  717. return err;
  718. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
  719. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  720. return err;
  721. } else {
  722. sprintf(name, "%s Playback Volume", chname[i]);
  723. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
  724. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  725. return err;
  726. sprintf(name, "%s Playback Switch", chname[i]);
  727. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
  728. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  729. return err;
  730. }
  731. }
  732. if (spec->line_switch)
  733. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
  734. return err;
  735. if (spec->mic_switch)
  736. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
  737. return err;
  738. return 0;
  739. }
  740. /* add playback controls for HP output */
  741. static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  742. {
  743. struct sigmatel_spec *spec = codec->spec;
  744. hda_nid_t pin = cfg->hp_pin;
  745. hda_nid_t nid;
  746. int i, err;
  747. unsigned int wid_caps;
  748. if (! pin)
  749. return 0;
  750. wid_caps = get_wcaps(codec, pin);
  751. if (wid_caps & AC_WCAP_UNSOL_CAP)
  752. spec->hp_detect = 1;
  753. nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  754. for (i = 0; i < cfg->line_outs; i++) {
  755. if (! spec->multiout.dac_nids[i])
  756. continue;
  757. if (spec->multiout.dac_nids[i] == nid)
  758. return 0;
  759. }
  760. spec->multiout.hp_nid = nid;
  761. /* control HP volume/switch on the output mixer amp */
  762. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
  763. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  764. return err;
  765. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
  766. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  767. return err;
  768. return 0;
  769. }
  770. /* create playback/capture controls for input pins */
  771. static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  772. {
  773. struct sigmatel_spec *spec = codec->spec;
  774. struct hda_input_mux *imux = &spec->private_imux;
  775. hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
  776. int i, j, k;
  777. for (i = 0; i < AUTO_PIN_LAST; i++) {
  778. int index = -1;
  779. if (cfg->input_pins[i]) {
  780. imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
  781. for (j=0; j<spec->num_muxes; j++) {
  782. int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
  783. for (k=0; k<num_cons; k++)
  784. if (con_lst[k] == cfg->input_pins[i]) {
  785. index = k;
  786. break;
  787. }
  788. if (index >= 0)
  789. break;
  790. }
  791. imux->items[imux->num_items].index = index;
  792. imux->num_items++;
  793. }
  794. }
  795. if (imux->num_items == 1) {
  796. /*
  797. * Set the current input for the muxes.
  798. * The STAC9221 has two input muxes with identical source
  799. * NID lists. Hopefully this won't get confused.
  800. */
  801. for (i = 0; i < spec->num_muxes; i++) {
  802. snd_hda_codec_write(codec, spec->mux_nids[i], 0,
  803. AC_VERB_SET_CONNECT_SEL,
  804. imux->items[0].index);
  805. }
  806. }
  807. return 0;
  808. }
  809. static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
  810. {
  811. struct sigmatel_spec *spec = codec->spec;
  812. int i;
  813. for (i = 0; i < spec->autocfg.line_outs; i++) {
  814. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  815. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  816. }
  817. }
  818. static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
  819. {
  820. struct sigmatel_spec *spec = codec->spec;
  821. hda_nid_t pin;
  822. pin = spec->autocfg.hp_pin;
  823. if (pin) /* connect to front */
  824. stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  825. }
  826. static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
  827. {
  828. struct sigmatel_spec *spec = codec->spec;
  829. int err;
  830. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  831. return err;
  832. if (! spec->autocfg.line_outs)
  833. return 0; /* can't find valid pin config */
  834. if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
  835. return err;
  836. if (spec->multiout.num_dacs == 0)
  837. if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
  838. return err;
  839. if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
  840. (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
  841. (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  842. return err;
  843. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  844. if (spec->multiout.max_channels > 2)
  845. spec->surr_switch = 1;
  846. if (spec->autocfg.dig_out_pin)
  847. spec->multiout.dig_out_nid = dig_out;
  848. if (spec->autocfg.dig_in_pin)
  849. spec->dig_in_nid = dig_in;
  850. if (spec->kctl_alloc)
  851. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  852. spec->input_mux = &spec->private_imux;
  853. return 1;
  854. }
  855. /* add playback controls for HP output */
  856. static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
  857. struct auto_pin_cfg *cfg)
  858. {
  859. struct sigmatel_spec *spec = codec->spec;
  860. hda_nid_t pin = cfg->hp_pin;
  861. unsigned int wid_caps;
  862. if (! pin)
  863. return 0;
  864. wid_caps = get_wcaps(codec, pin);
  865. if (wid_caps & AC_WCAP_UNSOL_CAP)
  866. spec->hp_detect = 1;
  867. return 0;
  868. }
  869. static int stac9200_parse_auto_config(struct hda_codec *codec)
  870. {
  871. struct sigmatel_spec *spec = codec->spec;
  872. int err;
  873. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  874. return err;
  875. if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  876. return err;
  877. if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
  878. return err;
  879. if (spec->autocfg.dig_out_pin)
  880. spec->multiout.dig_out_nid = 0x05;
  881. if (spec->autocfg.dig_in_pin)
  882. spec->dig_in_nid = 0x04;
  883. if (spec->kctl_alloc)
  884. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  885. spec->input_mux = &spec->private_imux;
  886. return 1;
  887. }
  888. /*
  889. * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
  890. * funky external mute control using GPIO pins.
  891. */
  892. static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
  893. {
  894. unsigned int gpiostate, gpiomask, gpiodir;
  895. gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
  896. AC_VERB_GET_GPIO_DATA, 0);
  897. if (!muted)
  898. gpiostate |= (1 << pin);
  899. else
  900. gpiostate &= ~(1 << pin);
  901. gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
  902. AC_VERB_GET_GPIO_MASK, 0);
  903. gpiomask |= (1 << pin);
  904. gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
  905. AC_VERB_GET_GPIO_DIRECTION, 0);
  906. gpiodir |= (1 << pin);
  907. /* AppleHDA seems to do this -- WTF is this verb?? */
  908. snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
  909. snd_hda_codec_write(codec, codec->afg, 0,
  910. AC_VERB_SET_GPIO_MASK, gpiomask);
  911. snd_hda_codec_write(codec, codec->afg, 0,
  912. AC_VERB_SET_GPIO_DIRECTION, gpiodir);
  913. msleep(1);
  914. snd_hda_codec_write(codec, codec->afg, 0,
  915. AC_VERB_SET_GPIO_DATA, gpiostate);
  916. }
  917. static int stac92xx_init(struct hda_codec *codec)
  918. {
  919. struct sigmatel_spec *spec = codec->spec;
  920. struct auto_pin_cfg *cfg = &spec->autocfg;
  921. int i;
  922. snd_hda_sequence_write(codec, spec->init);
  923. /* set up pins */
  924. if (spec->hp_detect) {
  925. /* Enable unsolicited responses on the HP widget */
  926. snd_hda_codec_write(codec, cfg->hp_pin, 0,
  927. AC_VERB_SET_UNSOLICITED_ENABLE,
  928. STAC_UNSOL_ENABLE);
  929. /* fake event to set up pins */
  930. codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
  931. } else {
  932. stac92xx_auto_init_multi_out(codec);
  933. stac92xx_auto_init_hp_out(codec);
  934. }
  935. for (i = 0; i < AUTO_PIN_LAST; i++) {
  936. hda_nid_t nid = cfg->input_pins[i];
  937. if (nid) {
  938. unsigned int pinctl = AC_PINCTL_IN_EN;
  939. if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
  940. pinctl |= stac92xx_get_vref(codec, nid);
  941. stac92xx_auto_set_pinctl(codec, nid, pinctl);
  942. }
  943. }
  944. if (cfg->dig_out_pin)
  945. stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
  946. AC_PINCTL_OUT_EN);
  947. if (cfg->dig_in_pin)
  948. stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
  949. AC_PINCTL_IN_EN);
  950. if (spec->gpio_mute) {
  951. stac922x_gpio_mute(codec, 0, 0);
  952. stac922x_gpio_mute(codec, 1, 0);
  953. }
  954. return 0;
  955. }
  956. static void stac92xx_free(struct hda_codec *codec)
  957. {
  958. struct sigmatel_spec *spec = codec->spec;
  959. int i;
  960. if (! spec)
  961. return;
  962. if (spec->kctl_alloc) {
  963. for (i = 0; i < spec->num_kctl_used; i++)
  964. kfree(spec->kctl_alloc[i].name);
  965. kfree(spec->kctl_alloc);
  966. }
  967. kfree(spec);
  968. }
  969. static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
  970. unsigned int flag)
  971. {
  972. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  973. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  974. snd_hda_codec_write(codec, nid, 0,
  975. AC_VERB_SET_PIN_WIDGET_CONTROL,
  976. pin_ctl | flag);
  977. }
  978. static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
  979. unsigned int flag)
  980. {
  981. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  982. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  983. snd_hda_codec_write(codec, nid, 0,
  984. AC_VERB_SET_PIN_WIDGET_CONTROL,
  985. pin_ctl & ~flag);
  986. }
  987. static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
  988. {
  989. struct sigmatel_spec *spec = codec->spec;
  990. struct auto_pin_cfg *cfg = &spec->autocfg;
  991. int i, presence;
  992. if ((res >> 26) != STAC_HP_EVENT)
  993. return;
  994. presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
  995. AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
  996. if (presence) {
  997. /* disable lineouts, enable hp */
  998. for (i = 0; i < cfg->line_outs; i++)
  999. stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
  1000. AC_PINCTL_OUT_EN);
  1001. stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  1002. } else {
  1003. /* enable lineouts, disable hp */
  1004. for (i = 0; i < cfg->line_outs; i++)
  1005. stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
  1006. AC_PINCTL_OUT_EN);
  1007. stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  1008. }
  1009. }
  1010. #ifdef CONFIG_PM
  1011. static int stac92xx_resume(struct hda_codec *codec)
  1012. {
  1013. struct sigmatel_spec *spec = codec->spec;
  1014. int i;
  1015. stac92xx_init(codec);
  1016. for (i = 0; i < spec->num_mixers; i++)
  1017. snd_hda_resume_ctls(codec, spec->mixers[i]);
  1018. if (spec->multiout.dig_out_nid)
  1019. snd_hda_resume_spdif_out(codec);
  1020. if (spec->dig_in_nid)
  1021. snd_hda_resume_spdif_in(codec);
  1022. return 0;
  1023. }
  1024. #endif
  1025. static struct hda_codec_ops stac92xx_patch_ops = {
  1026. .build_controls = stac92xx_build_controls,
  1027. .build_pcms = stac92xx_build_pcms,
  1028. .init = stac92xx_init,
  1029. .free = stac92xx_free,
  1030. .unsol_event = stac92xx_unsol_event,
  1031. #ifdef CONFIG_PM
  1032. .resume = stac92xx_resume,
  1033. #endif
  1034. };
  1035. static int patch_stac9200(struct hda_codec *codec)
  1036. {
  1037. struct sigmatel_spec *spec;
  1038. int err;
  1039. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1040. if (spec == NULL)
  1041. return -ENOMEM;
  1042. codec->spec = spec;
  1043. spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
  1044. if (spec->board_config < 0)
  1045. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
  1046. else {
  1047. spec->num_pins = 8;
  1048. spec->pin_nids = stac9200_pin_nids;
  1049. spec->pin_configs = stac9200_brd_tbl[spec->board_config];
  1050. stac92xx_set_config_regs(codec);
  1051. }
  1052. spec->multiout.max_channels = 2;
  1053. spec->multiout.num_dacs = 1;
  1054. spec->multiout.dac_nids = stac9200_dac_nids;
  1055. spec->adc_nids = stac9200_adc_nids;
  1056. spec->mux_nids = stac9200_mux_nids;
  1057. spec->num_muxes = 1;
  1058. spec->init = stac9200_core_init;
  1059. spec->mixer = stac9200_mixer;
  1060. err = stac9200_parse_auto_config(codec);
  1061. if (err < 0) {
  1062. stac92xx_free(codec);
  1063. return err;
  1064. }
  1065. codec->patch_ops = stac92xx_patch_ops;
  1066. return 0;
  1067. }
  1068. static int patch_stac922x(struct hda_codec *codec)
  1069. {
  1070. struct sigmatel_spec *spec;
  1071. int err;
  1072. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1073. if (spec == NULL)
  1074. return -ENOMEM;
  1075. codec->spec = spec;
  1076. spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
  1077. if (spec->board_config < 0)
  1078. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
  1079. "using BIOS defaults\n");
  1080. else if (stac922x_brd_tbl[spec->board_config] != NULL) {
  1081. spec->num_pins = 10;
  1082. spec->pin_nids = stac922x_pin_nids;
  1083. spec->pin_configs = stac922x_brd_tbl[spec->board_config];
  1084. stac92xx_set_config_regs(codec);
  1085. }
  1086. spec->adc_nids = stac922x_adc_nids;
  1087. spec->mux_nids = stac922x_mux_nids;
  1088. spec->num_muxes = 2;
  1089. spec->init = stac922x_core_init;
  1090. spec->mixer = stac922x_mixer;
  1091. spec->multiout.dac_nids = spec->dac_nids;
  1092. switch (spec->board_config) {
  1093. case STAC_D965_2112:
  1094. spec->adc_nids = stac9227_adc_nids;
  1095. spec->mux_nids = stac9227_mux_nids;
  1096. #if 0
  1097. spec->multiout.dac_nids = d965_2112_dac_nids;
  1098. spec->multiout.num_dacs = ARRAY_SIZE(d965_2112_dac_nids);
  1099. #endif
  1100. spec->init = d965_2112_core_init;
  1101. spec->mixer = stac9227_mixer;
  1102. break;
  1103. case STAC_D965_284B:
  1104. spec->adc_nids = stac9227_adc_nids;
  1105. spec->mux_nids = stac9227_mux_nids;
  1106. spec->init = stac9227_core_init;
  1107. spec->mixer = stac9227_mixer;
  1108. break;
  1109. }
  1110. err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
  1111. if (err < 0) {
  1112. stac92xx_free(codec);
  1113. return err;
  1114. }
  1115. if (spec->board_config == STAC_MACMINI)
  1116. spec->gpio_mute = 1;
  1117. codec->patch_ops = stac92xx_patch_ops;
  1118. return 0;
  1119. }
  1120. static int patch_stac927x(struct hda_codec *codec)
  1121. {
  1122. struct sigmatel_spec *spec;
  1123. int err;
  1124. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1125. if (spec == NULL)
  1126. return -ENOMEM;
  1127. codec->spec = spec;
  1128. spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
  1129. if (spec->board_config < 0)
  1130. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
  1131. else {
  1132. spec->num_pins = 14;
  1133. spec->pin_nids = stac927x_pin_nids;
  1134. spec->pin_configs = stac927x_brd_tbl[spec->board_config];
  1135. stac92xx_set_config_regs(codec);
  1136. }
  1137. spec->adc_nids = stac927x_adc_nids;
  1138. spec->mux_nids = stac927x_mux_nids;
  1139. spec->num_muxes = 3;
  1140. spec->init = stac927x_core_init;
  1141. spec->mixer = stac927x_mixer;
  1142. spec->multiout.dac_nids = spec->dac_nids;
  1143. err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
  1144. if (err < 0) {
  1145. stac92xx_free(codec);
  1146. return err;
  1147. }
  1148. codec->patch_ops = stac92xx_patch_ops;
  1149. return 0;
  1150. }
  1151. /*
  1152. * STAC 7661(?) hack
  1153. */
  1154. /* static config for Sony VAIO FE550G */
  1155. static hda_nid_t vaio_dacs[] = { 0x2 };
  1156. #define VAIO_HP_DAC 0x5
  1157. static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
  1158. static hda_nid_t vaio_mux_nids[] = { 0x15 };
  1159. static struct hda_input_mux vaio_mux = {
  1160. .num_items = 2,
  1161. .items = {
  1162. /* { "HP", 0x0 }, */
  1163. { "Line", 0x1 },
  1164. { "Mic", 0x2 },
  1165. { "PCM", 0x3 },
  1166. }
  1167. };
  1168. static struct hda_verb vaio_init[] = {
  1169. {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
  1170. {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
  1171. {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
  1172. {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
  1173. {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
  1174. {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
  1175. {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
  1176. {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
  1177. {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
  1178. {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
  1179. {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
  1180. {}
  1181. };
  1182. /* bind volumes of both NID 0x02 and 0x05 */
  1183. static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
  1184. struct snd_ctl_elem_value *ucontrol)
  1185. {
  1186. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1187. long *valp = ucontrol->value.integer.value;
  1188. int change;
  1189. change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
  1190. 0x7f, valp[0] & 0x7f);
  1191. change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
  1192. 0x7f, valp[1] & 0x7f);
  1193. snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
  1194. 0x7f, valp[0] & 0x7f);
  1195. snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
  1196. 0x7f, valp[1] & 0x7f);
  1197. return change;
  1198. }
  1199. /* bind volumes of both NID 0x02 and 0x05 */
  1200. static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
  1201. struct snd_ctl_elem_value *ucontrol)
  1202. {
  1203. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1204. long *valp = ucontrol->value.integer.value;
  1205. int change;
  1206. change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
  1207. 0x80, (valp[0] ? 0 : 0x80));
  1208. change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
  1209. 0x80, (valp[1] ? 0 : 0x80));
  1210. snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
  1211. 0x80, (valp[0] ? 0 : 0x80));
  1212. snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
  1213. 0x80, (valp[1] ? 0 : 0x80));
  1214. return change;
  1215. }
  1216. static struct snd_kcontrol_new vaio_mixer[] = {
  1217. {
  1218. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1219. .name = "Master Playback Volume",
  1220. .info = snd_hda_mixer_amp_volume_info,
  1221. .get = snd_hda_mixer_amp_volume_get,
  1222. .put = vaio_master_vol_put,
  1223. .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
  1224. },
  1225. {
  1226. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1227. .name = "Master Playback Switch",
  1228. .info = snd_hda_mixer_amp_switch_info,
  1229. .get = snd_hda_mixer_amp_switch_get,
  1230. .put = vaio_master_sw_put,
  1231. .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
  1232. },
  1233. /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
  1234. HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
  1235. HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
  1236. {
  1237. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1238. .name = "Capture Source",
  1239. .count = 1,
  1240. .info = stac92xx_mux_enum_info,
  1241. .get = stac92xx_mux_enum_get,
  1242. .put = stac92xx_mux_enum_put,
  1243. },
  1244. {}
  1245. };
  1246. static struct hda_codec_ops stac7661_patch_ops = {
  1247. .build_controls = stac92xx_build_controls,
  1248. .build_pcms = stac92xx_build_pcms,
  1249. .init = stac92xx_init,
  1250. .free = stac92xx_free,
  1251. #ifdef CONFIG_PM
  1252. .resume = stac92xx_resume,
  1253. #endif
  1254. };
  1255. enum { STAC7661_VAIO };
  1256. static struct hda_board_config stac7661_cfg_tbl[] = {
  1257. { .modelname = "vaio", .config = STAC7661_VAIO },
  1258. { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
  1259. .config = STAC7661_VAIO },
  1260. { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
  1261. .config = STAC7661_VAIO },
  1262. {}
  1263. };
  1264. static int patch_stac7661(struct hda_codec *codec)
  1265. {
  1266. struct sigmatel_spec *spec;
  1267. int board_config;
  1268. board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
  1269. if (board_config < 0)
  1270. /* unknown config, let generic-parser do its job... */
  1271. return snd_hda_parse_generic_codec(codec);
  1272. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1273. if (spec == NULL)
  1274. return -ENOMEM;
  1275. codec->spec = spec;
  1276. switch (board_config) {
  1277. case STAC7661_VAIO:
  1278. spec->mixer = vaio_mixer;
  1279. spec->init = vaio_init;
  1280. spec->multiout.max_channels = 2;
  1281. spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
  1282. spec->multiout.dac_nids = vaio_dacs;
  1283. spec->multiout.hp_nid = VAIO_HP_DAC;
  1284. spec->num_adcs = ARRAY_SIZE(vaio_adcs);
  1285. spec->adc_nids = vaio_adcs;
  1286. spec->input_mux = &vaio_mux;
  1287. spec->mux_nids = vaio_mux_nids;
  1288. break;
  1289. }
  1290. codec->patch_ops = stac7661_patch_ops;
  1291. return 0;
  1292. }
  1293. /*
  1294. * patch entries
  1295. */
  1296. struct hda_codec_preset snd_hda_preset_sigmatel[] = {
  1297. { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
  1298. { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
  1299. { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
  1300. { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
  1301. { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
  1302. { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
  1303. { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
  1304. { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac922x },
  1305. { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac922x },
  1306. { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac922x },
  1307. { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac922x },
  1308. { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac922x },
  1309. { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac922x },
  1310. { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
  1311. { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
  1312. { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
  1313. { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
  1314. { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
  1315. { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
  1316. { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
  1317. { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
  1318. { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
  1319. { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
  1320. { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
  1321. {} /* terminator */
  1322. };