patch_cmedia.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for C-Media CMI9880
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <sound/driver.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/pci.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include "hda_local.h"
  31. #define NUM_PINS 11
  32. /* board config type */
  33. enum {
  34. CMI_MINIMAL, /* back 3-jack */
  35. CMI_MIN_FP, /* back 3-jack + front-panel 2-jack */
  36. CMI_FULL, /* back 6-jack + front-panel 2-jack */
  37. CMI_FULL_DIG, /* back 6-jack + front-panel 2-jack + digital I/O */
  38. CMI_ALLOUT, /* back 5-jack + front-panel 2-jack + digital out */
  39. CMI_AUTO, /* let driver guess it */
  40. };
  41. struct cmi_spec {
  42. int board_config;
  43. unsigned int surr_switch: 1; /* switchable line,mic */
  44. unsigned int no_line_in: 1; /* no line-in (5-jack) */
  45. unsigned int front_panel: 1; /* has front-panel 2-jack */
  46. /* playback */
  47. struct hda_multi_out multiout;
  48. hda_nid_t dac_nids[4]; /* NID for each DAC */
  49. /* capture */
  50. hda_nid_t *adc_nids;
  51. hda_nid_t dig_in_nid;
  52. /* capture source */
  53. const struct hda_input_mux *input_mux;
  54. unsigned int cur_mux[2];
  55. /* channel mode */
  56. unsigned int num_ch_modes;
  57. unsigned int cur_ch_mode;
  58. const struct cmi_channel_mode *channel_modes;
  59. struct hda_pcm pcm_rec[2]; /* PCM information */
  60. /* pin deafault configuration */
  61. hda_nid_t pin_nid[NUM_PINS];
  62. unsigned int def_conf[NUM_PINS];
  63. unsigned int pin_def_confs;
  64. /* multichannel pins */
  65. hda_nid_t multich_pin[4]; /* max 8-channel */
  66. struct hda_verb multi_init[9]; /* 2 verbs for each pin + terminator */
  67. };
  68. /*
  69. * input MUX
  70. */
  71. static int cmi_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  72. {
  73. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  74. struct cmi_spec *spec = codec->spec;
  75. return snd_hda_input_mux_info(spec->input_mux, uinfo);
  76. }
  77. static int cmi_mux_enum_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  78. {
  79. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  80. struct cmi_spec *spec = codec->spec;
  81. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  82. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  83. return 0;
  84. }
  85. static int cmi_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  86. {
  87. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  88. struct cmi_spec *spec = codec->spec;
  89. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  90. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  91. spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
  92. }
  93. /*
  94. * shared line-in, mic for surrounds
  95. */
  96. /* 3-stack / 2 channel */
  97. static struct hda_verb cmi9880_ch2_init[] = {
  98. /* set line-in PIN for input */
  99. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
  100. /* set mic PIN for input, also enable vref */
  101. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
  102. /* route front PCM (DAC1) to HP */
  103. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  104. {}
  105. };
  106. /* 3-stack / 6 channel */
  107. static struct hda_verb cmi9880_ch6_init[] = {
  108. /* set line-in PIN for output */
  109. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
  110. /* set mic PIN for output */
  111. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
  112. /* route front PCM (DAC1) to HP */
  113. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  114. {}
  115. };
  116. /* 3-stack+front / 8 channel */
  117. static struct hda_verb cmi9880_ch8_init[] = {
  118. /* set line-in PIN for output */
  119. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
  120. /* set mic PIN for output */
  121. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
  122. /* route rear-surround PCM (DAC4) to HP */
  123. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
  124. {}
  125. };
  126. struct cmi_channel_mode {
  127. unsigned int channels;
  128. const struct hda_verb *sequence;
  129. };
  130. static struct cmi_channel_mode cmi9880_channel_modes[3] = {
  131. { 2, cmi9880_ch2_init },
  132. { 6, cmi9880_ch6_init },
  133. { 8, cmi9880_ch8_init },
  134. };
  135. static int cmi_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  136. {
  137. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  138. struct cmi_spec *spec = codec->spec;
  139. snd_assert(spec->channel_modes, return -EINVAL);
  140. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  141. uinfo->count = 1;
  142. uinfo->value.enumerated.items = spec->num_ch_modes;
  143. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  144. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  145. sprintf(uinfo->value.enumerated.name, "%dch",
  146. spec->channel_modes[uinfo->value.enumerated.item].channels);
  147. return 0;
  148. }
  149. static int cmi_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  150. {
  151. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  152. struct cmi_spec *spec = codec->spec;
  153. ucontrol->value.enumerated.item[0] = spec->cur_ch_mode;
  154. return 0;
  155. }
  156. static int cmi_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  157. {
  158. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  159. struct cmi_spec *spec = codec->spec;
  160. snd_assert(spec->channel_modes, return -EINVAL);
  161. if (ucontrol->value.enumerated.item[0] >= spec->num_ch_modes)
  162. ucontrol->value.enumerated.item[0] = spec->num_ch_modes;
  163. if (ucontrol->value.enumerated.item[0] == spec->cur_ch_mode &&
  164. ! codec->in_resume)
  165. return 0;
  166. spec->cur_ch_mode = ucontrol->value.enumerated.item[0];
  167. snd_hda_sequence_write(codec, spec->channel_modes[spec->cur_ch_mode].sequence);
  168. spec->multiout.max_channels = spec->channel_modes[spec->cur_ch_mode].channels;
  169. return 1;
  170. }
  171. /*
  172. */
  173. static snd_kcontrol_new_t cmi9880_basic_mixer[] = {
  174. /* CMI9880 has no playback volumes! */
  175. HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
  176. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
  177. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
  178. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
  179. HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
  180. {
  181. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  182. /* The multiple "Capture Source" controls confuse alsamixer
  183. * So call somewhat different..
  184. * FIXME: the controls appear in the "playback" view!
  185. */
  186. /* .name = "Capture Source", */
  187. .name = "Input Source",
  188. .count = 2,
  189. .info = cmi_mux_enum_info,
  190. .get = cmi_mux_enum_get,
  191. .put = cmi_mux_enum_put,
  192. },
  193. HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
  194. HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
  195. HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
  196. HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
  197. HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
  198. HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
  199. { } /* end */
  200. };
  201. /*
  202. * shared I/O pins
  203. */
  204. static snd_kcontrol_new_t cmi9880_ch_mode_mixer[] = {
  205. {
  206. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  207. .name = "Channel Mode",
  208. .info = cmi_ch_mode_info,
  209. .get = cmi_ch_mode_get,
  210. .put = cmi_ch_mode_put,
  211. },
  212. { } /* end */
  213. };
  214. /* AUD-in selections:
  215. * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
  216. */
  217. static struct hda_input_mux cmi9880_basic_mux = {
  218. .num_items = 4,
  219. .items = {
  220. { "Front Mic", 0x5 },
  221. { "Rear Mic", 0x2 },
  222. { "Line", 0x1 },
  223. { "CD", 0x7 },
  224. }
  225. };
  226. static struct hda_input_mux cmi9880_no_line_mux = {
  227. .num_items = 3,
  228. .items = {
  229. { "Front Mic", 0x5 },
  230. { "Rear Mic", 0x2 },
  231. { "CD", 0x7 },
  232. }
  233. };
  234. /* front, rear, clfe, rear_surr */
  235. static hda_nid_t cmi9880_dac_nids[4] = {
  236. 0x03, 0x04, 0x05, 0x06
  237. };
  238. /* ADC0, ADC1 */
  239. static hda_nid_t cmi9880_adc_nids[2] = {
  240. 0x08, 0x09
  241. };
  242. #define CMI_DIG_OUT_NID 0x07
  243. #define CMI_DIG_IN_NID 0x0a
  244. /*
  245. */
  246. static struct hda_verb cmi9880_basic_init[] = {
  247. /* port-D for line out (rear panel) */
  248. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  249. /* port-E for HP out (front panel) */
  250. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  251. /* route front PCM to HP */
  252. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  253. /* port-A for surround (rear panel) */
  254. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  255. /* port-G for CLFE (rear panel) */
  256. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  257. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  258. /* port-H for side (rear panel) */
  259. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  260. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  261. /* port-C for line-in (rear panel) */
  262. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
  263. /* port-B for mic-in (rear panel) with vref */
  264. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
  265. /* port-F for mic-in (front panel) with vref */
  266. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
  267. /* CD-in */
  268. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
  269. /* route front mic to ADC1/2 */
  270. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  271. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  272. {} /* terminator */
  273. };
  274. static struct hda_verb cmi9880_allout_init[] = {
  275. /* port-D for line out (rear panel) */
  276. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  277. /* port-E for HP out (front panel) */
  278. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  279. /* route front PCM to HP */
  280. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  281. /* port-A for side (rear panel) */
  282. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  283. /* port-G for CLFE (rear panel) */
  284. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  285. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  286. /* port-H for side (rear panel) */
  287. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  288. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  289. /* port-C for surround (rear panel) */
  290. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
  291. /* port-B for mic-in (rear panel) with vref */
  292. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
  293. /* port-F for mic-in (front panel) with vref */
  294. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
  295. /* CD-in */
  296. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
  297. /* route front mic to ADC1/2 */
  298. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  299. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  300. {} /* terminator */
  301. };
  302. /*
  303. */
  304. static int cmi9880_build_controls(struct hda_codec *codec)
  305. {
  306. struct cmi_spec *spec = codec->spec;
  307. int err;
  308. err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
  309. if (err < 0)
  310. return err;
  311. if (spec->surr_switch) {
  312. err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
  313. if (err < 0)
  314. return err;
  315. }
  316. if (spec->multiout.dig_out_nid) {
  317. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  318. if (err < 0)
  319. return err;
  320. }
  321. if (spec->dig_in_nid) {
  322. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  323. if (err < 0)
  324. return err;
  325. }
  326. return 0;
  327. }
  328. #define get_defcfg_connect(cfg) ((cfg & AC_DEFCFG_PORT_CONN) >> AC_DEFCFG_PORT_CONN_SHIFT)
  329. #define get_defcfg_association(cfg) ((cfg & AC_DEFCFG_DEF_ASSOC) >> AC_DEFCFG_ASSOC_SHIFT)
  330. #define get_defcfg_sequence(cfg) (cfg & AC_DEFCFG_SEQUENCE)
  331. /* get all pin default configuration in def_conf */
  332. static int cmi9880_get_pin_def_config(struct hda_codec *codec)
  333. {
  334. struct cmi_spec *spec = codec->spec;
  335. hda_nid_t nid, nid_start;
  336. int i = 0, nodes;
  337. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  338. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  339. unsigned int wid_caps = snd_hda_param_read(codec, nid,
  340. AC_PAR_AUDIO_WIDGET_CAP);
  341. unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  342. /* read all default configuration for pin complex */
  343. if (wid_type == AC_WID_PIN) {
  344. spec->pin_nid[i] = nid;
  345. spec->def_conf[i] =
  346. snd_hda_codec_read(codec, nid, 0,
  347. AC_VERB_GET_CONFIG_DEFAULT, 0);
  348. i++;
  349. }
  350. }
  351. spec->pin_def_confs = i;
  352. return 0;
  353. }
  354. /* get a pin default configuration of nid in def_conf */
  355. static unsigned int cmi9880_get_def_config(struct hda_codec *codec, hda_nid_t nid)
  356. {
  357. struct cmi_spec *spec = codec->spec;
  358. int i = 0;
  359. while (spec->pin_nid[i] != nid && i < spec->pin_def_confs)
  360. i++;
  361. if (i == spec->pin_def_confs)
  362. return (unsigned int) -1;
  363. else
  364. return spec->def_conf[i];
  365. }
  366. /* decide what pins to use for multichannel playback */
  367. static int cmi9880_get_multich_pins(struct hda_codec *codec)
  368. {
  369. struct cmi_spec *spec = codec->spec;
  370. int i, j, pins, seq[4];
  371. int max_channel = 0;
  372. unsigned int def_conf, sequence;
  373. hda_nid_t nid;
  374. memset(spec->multich_pin, 0, sizeof(spec->multich_pin));
  375. for (pins = 0, i = 0; i < spec->pin_def_confs && pins < 4; i++) {
  376. def_conf = spec->def_conf[i];
  377. /* skip pin not connected */
  378. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  379. continue;
  380. /* get the sequence if association == 1 */
  381. /* the other pins have association = 0, incorrect in spec 1.0 */
  382. if (get_defcfg_association(def_conf) == 1) {
  383. sequence = get_defcfg_sequence(def_conf);
  384. seq[pins] = sequence;
  385. spec->multich_pin[pins] = spec->pin_nid[i];
  386. pins++; // ready for next slot
  387. max_channel += 2;
  388. }
  389. }
  390. /* sort by sequence, data collected here will be for Windows */
  391. for (i = 0; i < pins; i++) {
  392. for (j = i + 1; j < pins; j++) {
  393. if (seq[j] < seq[i]) {
  394. sequence = seq[j];
  395. nid = spec->multich_pin[j];
  396. seq[j] = seq[i];
  397. spec->multich_pin[j] = spec->multich_pin[i];
  398. seq[i] = sequence;
  399. spec->multich_pin[i] = nid;
  400. }
  401. }
  402. }
  403. /* the pin assignment is for front, C/LFE, surround and back */
  404. if (max_channel >= 6) {
  405. hda_nid_t temp;
  406. /* exchange pin of C/LFE and surround */
  407. temp = spec->multich_pin[1];
  408. spec->multich_pin[1] = spec->multich_pin[2];
  409. spec->multich_pin[2] = temp;
  410. }
  411. return max_channel;
  412. }
  413. /* fill in the multi_dac_nids table, which will decide
  414. which audio widget to use for each channel */
  415. static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec)
  416. {
  417. struct cmi_spec *spec = codec->spec;
  418. hda_nid_t nid;
  419. int assigned[4];
  420. int i, j;
  421. /* clear the table, only one c-media dac assumed here */
  422. memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
  423. memset(assigned, 0, sizeof(assigned));
  424. /* check the pins we found */
  425. for (i = 0; i < spec->multiout.max_channels / 2; i++) {
  426. nid = spec->multich_pin[i];
  427. /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
  428. if (nid <= 0x0e && nid >= 0x0b) {
  429. spec->dac_nids[i] = nid - 0x08;
  430. assigned[nid - 0x0b] = 1;
  431. }
  432. }
  433. /* left pin can be connect to any audio widget */
  434. for (i = 0; i < spec->multiout.max_channels / 2; i++) {
  435. if (!assigned[i]) {
  436. /* search for an empty channel */
  437. /* I should also check the pin type */
  438. for (j = 0; j < ARRAY_SIZE(spec->dac_nids); j++)
  439. if (! spec->dac_nids[j]) {
  440. spec->dac_nids[j] = i + 3;
  441. assigned[i] = 1;
  442. break;
  443. }
  444. }
  445. }
  446. return 0;
  447. }
  448. /* create multi_init table, which is used for multichannel initialization */
  449. static int cmi9880_fill_multi_init(struct hda_codec *codec)
  450. {
  451. struct cmi_spec *spec = codec->spec;
  452. hda_nid_t nid;
  453. int i, j, k, len;
  454. /* clear the table, only one c-media dac assumed here */
  455. memset(spec->multi_init, 0, sizeof(spec->multi_init));
  456. for (j = 0, i = 0; i < spec->multiout.max_channels / 2; i++) {
  457. hda_nid_t conn[4];
  458. nid = spec->multich_pin[i];
  459. /* set as output */
  460. spec->multi_init[j].nid = nid;
  461. spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
  462. spec->multi_init[j].param = 0xc0;
  463. j++;
  464. /* nid 0x0f,0x10,0x1f,0x20 are needed to set connection */
  465. switch (nid) {
  466. case 0x0f:
  467. case 0x10:
  468. case 0x1f:
  469. case 0x20:
  470. /* set connection */
  471. spec->multi_init[j].nid = nid;
  472. spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
  473. /* find the index in connect list */
  474. len = snd_hda_get_connections(codec, nid, conn, 4);
  475. for (k = 0; k < len; k++)
  476. if (conn[k] == spec->dac_nids[i])
  477. break;
  478. spec->multi_init[j].param = k < len ? k : 0;
  479. j++;
  480. break;
  481. }
  482. }
  483. return 0;
  484. }
  485. static int cmi9880_init(struct hda_codec *codec)
  486. {
  487. struct cmi_spec *spec = codec->spec;
  488. if (spec->board_config == CMI_ALLOUT)
  489. snd_hda_sequence_write(codec, cmi9880_allout_init);
  490. else
  491. snd_hda_sequence_write(codec, cmi9880_basic_init);
  492. if (spec->board_config == CMI_AUTO)
  493. snd_hda_sequence_write(codec, spec->multi_init);
  494. return 0;
  495. }
  496. #ifdef CONFIG_PM
  497. /*
  498. * resume
  499. */
  500. static int cmi9880_resume(struct hda_codec *codec)
  501. {
  502. struct cmi_spec *spec = codec->spec;
  503. cmi9880_init(codec);
  504. snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
  505. if (spec->surr_switch)
  506. snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
  507. if (spec->multiout.dig_out_nid)
  508. snd_hda_resume_spdif_out(codec);
  509. if (spec->dig_in_nid)
  510. snd_hda_resume_spdif_in(codec);
  511. return 0;
  512. }
  513. #endif
  514. /*
  515. * Analog playback callbacks
  516. */
  517. static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
  518. struct hda_codec *codec,
  519. snd_pcm_substream_t *substream)
  520. {
  521. struct cmi_spec *spec = codec->spec;
  522. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  523. }
  524. static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  525. struct hda_codec *codec,
  526. unsigned int stream_tag,
  527. unsigned int format,
  528. snd_pcm_substream_t *substream)
  529. {
  530. struct cmi_spec *spec = codec->spec;
  531. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
  532. format, substream);
  533. }
  534. static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  535. struct hda_codec *codec,
  536. snd_pcm_substream_t *substream)
  537. {
  538. struct cmi_spec *spec = codec->spec;
  539. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  540. }
  541. /*
  542. * Digital out
  543. */
  544. static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  545. struct hda_codec *codec,
  546. snd_pcm_substream_t *substream)
  547. {
  548. struct cmi_spec *spec = codec->spec;
  549. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  550. }
  551. static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  552. struct hda_codec *codec,
  553. snd_pcm_substream_t *substream)
  554. {
  555. struct cmi_spec *spec = codec->spec;
  556. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  557. }
  558. /*
  559. * Analog capture
  560. */
  561. static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  562. struct hda_codec *codec,
  563. unsigned int stream_tag,
  564. unsigned int format,
  565. snd_pcm_substream_t *substream)
  566. {
  567. struct cmi_spec *spec = codec->spec;
  568. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  569. stream_tag, 0, format);
  570. return 0;
  571. }
  572. static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  573. struct hda_codec *codec,
  574. snd_pcm_substream_t *substream)
  575. {
  576. struct cmi_spec *spec = codec->spec;
  577. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  578. return 0;
  579. }
  580. /*
  581. */
  582. static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
  583. .substreams = 1,
  584. .channels_min = 2,
  585. .channels_max = 8,
  586. .nid = 0x03, /* NID to query formats and rates */
  587. .ops = {
  588. .open = cmi9880_playback_pcm_open,
  589. .prepare = cmi9880_playback_pcm_prepare,
  590. .cleanup = cmi9880_playback_pcm_cleanup
  591. },
  592. };
  593. static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
  594. .substreams = 2,
  595. .channels_min = 2,
  596. .channels_max = 2,
  597. .nid = 0x08, /* NID to query formats and rates */
  598. .ops = {
  599. .prepare = cmi9880_capture_pcm_prepare,
  600. .cleanup = cmi9880_capture_pcm_cleanup
  601. },
  602. };
  603. static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
  604. .substreams = 1,
  605. .channels_min = 2,
  606. .channels_max = 2,
  607. /* NID is set in cmi9880_build_pcms */
  608. .ops = {
  609. .open = cmi9880_dig_playback_pcm_open,
  610. .close = cmi9880_dig_playback_pcm_close
  611. },
  612. };
  613. static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
  614. .substreams = 1,
  615. .channels_min = 2,
  616. .channels_max = 2,
  617. /* NID is set in cmi9880_build_pcms */
  618. };
  619. static int cmi9880_build_pcms(struct hda_codec *codec)
  620. {
  621. struct cmi_spec *spec = codec->spec;
  622. struct hda_pcm *info = spec->pcm_rec;
  623. codec->num_pcms = 1;
  624. codec->pcm_info = info;
  625. info->name = "CMI9880";
  626. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
  627. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
  628. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  629. codec->num_pcms++;
  630. info++;
  631. info->name = "CMI9880 Digital";
  632. if (spec->multiout.dig_out_nid) {
  633. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
  634. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  635. }
  636. if (spec->dig_in_nid) {
  637. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
  638. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  639. }
  640. }
  641. return 0;
  642. }
  643. static void cmi9880_free(struct hda_codec *codec)
  644. {
  645. kfree(codec->spec);
  646. }
  647. /*
  648. */
  649. static struct hda_board_config cmi9880_cfg_tbl[] = {
  650. { .modelname = "minimal", .config = CMI_MINIMAL },
  651. { .modelname = "min_fp", .config = CMI_MIN_FP },
  652. { .modelname = "full", .config = CMI_FULL },
  653. { .modelname = "full_dig", .config = CMI_FULL_DIG },
  654. { .modelname = "allout", .config = CMI_ALLOUT },
  655. { .modelname = "auto", .config = CMI_AUTO },
  656. {} /* terminator */
  657. };
  658. static struct hda_codec_ops cmi9880_patch_ops = {
  659. .build_controls = cmi9880_build_controls,
  660. .build_pcms = cmi9880_build_pcms,
  661. .init = cmi9880_init,
  662. .free = cmi9880_free,
  663. #ifdef CONFIG_PM
  664. .resume = cmi9880_resume,
  665. #endif
  666. };
  667. static int patch_cmi9880(struct hda_codec *codec)
  668. {
  669. struct cmi_spec *spec;
  670. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  671. if (spec == NULL)
  672. return -ENOMEM;
  673. codec->spec = spec;
  674. spec->board_config = snd_hda_check_board_config(codec, cmi9880_cfg_tbl);
  675. if (spec->board_config < 0) {
  676. snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
  677. spec->board_config = CMI_AUTO; /* try everything */
  678. }
  679. /* copy default DAC NIDs */
  680. memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
  681. switch (spec->board_config) {
  682. case CMI_MINIMAL:
  683. case CMI_MIN_FP:
  684. spec->surr_switch = 1;
  685. if (spec->board_config == CMI_MINIMAL)
  686. spec->num_ch_modes = 2;
  687. else {
  688. spec->front_panel = 1;
  689. spec->num_ch_modes = 3;
  690. }
  691. spec->channel_modes = cmi9880_channel_modes;
  692. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  693. spec->input_mux = &cmi9880_basic_mux;
  694. break;
  695. case CMI_FULL:
  696. case CMI_FULL_DIG:
  697. spec->front_panel = 1;
  698. spec->multiout.max_channels = 8;
  699. spec->input_mux = &cmi9880_basic_mux;
  700. if (spec->board_config == CMI_FULL_DIG) {
  701. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  702. spec->dig_in_nid = CMI_DIG_IN_NID;
  703. }
  704. break;
  705. case CMI_ALLOUT:
  706. spec->front_panel = 1;
  707. spec->multiout.max_channels = 8;
  708. spec->no_line_in = 1;
  709. spec->input_mux = &cmi9880_no_line_mux;
  710. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  711. break;
  712. case CMI_AUTO:
  713. {
  714. unsigned int port_e, port_f, port_g, port_h;
  715. unsigned int port_spdifi, port_spdifo;
  716. int max_channels;
  717. /* collect pin default configuration */
  718. cmi9880_get_pin_def_config(codec);
  719. port_e = cmi9880_get_def_config(codec, 0x0f);
  720. port_f = cmi9880_get_def_config(codec, 0x10);
  721. port_g = cmi9880_get_def_config(codec, 0x1f);
  722. port_h = cmi9880_get_def_config(codec, 0x20);
  723. port_spdifi = cmi9880_get_def_config(codec, 0x13);
  724. port_spdifo = cmi9880_get_def_config(codec, 0x12);
  725. spec->front_panel = 1;
  726. if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
  727. get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
  728. spec->surr_switch = 1;
  729. /* no front panel */
  730. if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
  731. get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
  732. /* no optional rear panel */
  733. spec->board_config = CMI_MINIMAL;
  734. spec->front_panel = 0;
  735. spec->num_ch_modes = 2;
  736. } else {
  737. spec->board_config = CMI_MIN_FP;
  738. spec->num_ch_modes = 3;
  739. }
  740. spec->channel_modes = cmi9880_channel_modes;
  741. spec->input_mux = &cmi9880_basic_mux;
  742. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  743. } else {
  744. spec->input_mux = &cmi9880_basic_mux;
  745. if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
  746. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  747. if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
  748. spec->dig_in_nid = CMI_DIG_IN_NID;
  749. spec->multiout.max_channels = 8;
  750. }
  751. max_channels = cmi9880_get_multich_pins(codec);
  752. if (max_channels > 0) {
  753. spec->multiout.max_channels = max_channels;
  754. cmi9880_fill_multi_dac_nids(codec);
  755. cmi9880_fill_multi_init(codec);
  756. } else
  757. snd_printd("patch_cmedia: cannot detect association in defcfg\n");
  758. break;
  759. }
  760. }
  761. spec->multiout.num_dacs = 4;
  762. spec->multiout.dac_nids = spec->dac_nids;
  763. spec->adc_nids = cmi9880_adc_nids;
  764. codec->patch_ops = cmi9880_patch_ops;
  765. return 0;
  766. }
  767. /*
  768. * patch entries
  769. */
  770. struct hda_codec_preset snd_hda_preset_cmedia[] = {
  771. { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
  772. { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
  773. {} /* terminator */
  774. };