hda_codec.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include "hda_codec.h"
  29. #include <sound/asoundef.h>
  30. #include <sound/initval.h>
  31. #include "hda_local.h"
  32. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  33. MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
  34. MODULE_LICENSE("GPL");
  35. /*
  36. * vendor / preset table
  37. */
  38. struct hda_vendor_id {
  39. unsigned int id;
  40. const char *name;
  41. };
  42. /* codec vendor labels */
  43. static struct hda_vendor_id hda_vendor_ids[] = {
  44. { 0x10ec, "Realtek" },
  45. { 0x11d4, "Analog Devices" },
  46. { 0x13f6, "C-Media" },
  47. { 0x434d, "C-Media" },
  48. { 0x8384, "SigmaTel" },
  49. {} /* terminator */
  50. };
  51. /* codec presets */
  52. #include "hda_patch.h"
  53. /**
  54. * snd_hda_codec_read - send a command and get the response
  55. * @codec: the HDA codec
  56. * @nid: NID to send the command
  57. * @direct: direct flag
  58. * @verb: the verb to send
  59. * @parm: the parameter for the verb
  60. *
  61. * Send a single command and read the corresponding response.
  62. *
  63. * Returns the obtained response value, or -1 for an error.
  64. */
  65. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
  66. unsigned int verb, unsigned int parm)
  67. {
  68. unsigned int res;
  69. down(&codec->bus->cmd_mutex);
  70. if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
  71. res = codec->bus->ops.get_response(codec);
  72. else
  73. res = (unsigned int)-1;
  74. up(&codec->bus->cmd_mutex);
  75. return res;
  76. }
  77. /**
  78. * snd_hda_codec_write - send a single command without waiting for response
  79. * @codec: the HDA codec
  80. * @nid: NID to send the command
  81. * @direct: direct flag
  82. * @verb: the verb to send
  83. * @parm: the parameter for the verb
  84. *
  85. * Send a single command without waiting for response.
  86. *
  87. * Returns 0 if successful, or a negative error code.
  88. */
  89. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  90. unsigned int verb, unsigned int parm)
  91. {
  92. int err;
  93. down(&codec->bus->cmd_mutex);
  94. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  95. up(&codec->bus->cmd_mutex);
  96. return err;
  97. }
  98. /**
  99. * snd_hda_sequence_write - sequence writes
  100. * @codec: the HDA codec
  101. * @seq: VERB array to send
  102. *
  103. * Send the commands sequentially from the given array.
  104. * The array must be terminated with NID=0.
  105. */
  106. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  107. {
  108. for (; seq->nid; seq++)
  109. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  110. }
  111. /**
  112. * snd_hda_get_sub_nodes - get the range of sub nodes
  113. * @codec: the HDA codec
  114. * @nid: NID to parse
  115. * @start_id: the pointer to store the start NID
  116. *
  117. * Parse the NID and store the start NID of its sub-nodes.
  118. * Returns the number of sub-nodes.
  119. */
  120. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
  121. {
  122. unsigned int parm;
  123. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  124. *start_id = (parm >> 16) & 0x7fff;
  125. return (int)(parm & 0x7fff);
  126. }
  127. /**
  128. * snd_hda_get_connections - get connection list
  129. * @codec: the HDA codec
  130. * @nid: NID to parse
  131. * @conn_list: connection list array
  132. * @max_conns: max. number of connections to store
  133. *
  134. * Parses the connection list of the given widget and stores the list
  135. * of NIDs.
  136. *
  137. * Returns the number of connections, or a negative error code.
  138. */
  139. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  140. hda_nid_t *conn_list, int max_conns)
  141. {
  142. unsigned int parm;
  143. int i, j, conn_len, num_tupples, conns;
  144. unsigned int shift, num_elems, mask;
  145. snd_assert(conn_list && max_conns > 0, return -EINVAL);
  146. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  147. if (parm & AC_CLIST_LONG) {
  148. /* long form */
  149. shift = 16;
  150. num_elems = 2;
  151. } else {
  152. /* short form */
  153. shift = 8;
  154. num_elems = 4;
  155. }
  156. conn_len = parm & AC_CLIST_LENGTH;
  157. num_tupples = num_elems / 2;
  158. mask = (1 << (shift-1)) - 1;
  159. if (! conn_len)
  160. return 0; /* no connection */
  161. if (conn_len == 1) {
  162. /* single connection */
  163. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
  164. conn_list[0] = parm & mask;
  165. return 1;
  166. }
  167. /* multi connection */
  168. conns = 0;
  169. for (i = 0; i < conn_len; i += num_elems) {
  170. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, i);
  171. for (j = 0; j < num_tupples; j++) {
  172. int range_val;
  173. hda_nid_t val1, val2, n;
  174. range_val = parm & (1 << (shift-1)); /* ranges */
  175. val1 = parm & mask;
  176. parm >>= shift;
  177. val2 = parm & mask;
  178. parm >>= shift;
  179. if (range_val) {
  180. /* ranges between val1 and val2 */
  181. if (val1 > val2) {
  182. snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
  183. continue;
  184. }
  185. for (n = val1; n <= val2; n++) {
  186. if (conns >= max_conns)
  187. return -EINVAL;
  188. conn_list[conns++] = n;
  189. }
  190. } else {
  191. if (! val1)
  192. break;
  193. if (conns >= max_conns)
  194. return -EINVAL;
  195. conn_list[conns++] = val1;
  196. if (! val2)
  197. break;
  198. if (conns >= max_conns)
  199. return -EINVAL;
  200. conn_list[conns++] = val2;
  201. }
  202. }
  203. }
  204. return conns;
  205. }
  206. /**
  207. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  208. * @bus: the BUS
  209. * @res: unsolicited event (lower 32bit of RIRB entry)
  210. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  211. *
  212. * Adds the given event to the queue. The events are processed in
  213. * the workqueue asynchronously. Call this function in the interrupt
  214. * hanlder when RIRB receives an unsolicited event.
  215. *
  216. * Returns 0 if successful, or a negative error code.
  217. */
  218. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  219. {
  220. struct hda_bus_unsolicited *unsol;
  221. unsigned int wp;
  222. if ((unsol = bus->unsol) == NULL)
  223. return 0;
  224. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  225. unsol->wp = wp;
  226. wp <<= 1;
  227. unsol->queue[wp] = res;
  228. unsol->queue[wp + 1] = res_ex;
  229. queue_work(unsol->workq, &unsol->work);
  230. return 0;
  231. }
  232. /*
  233. * process queueud unsolicited events
  234. */
  235. static void process_unsol_events(void *data)
  236. {
  237. struct hda_bus *bus = data;
  238. struct hda_bus_unsolicited *unsol = bus->unsol;
  239. struct hda_codec *codec;
  240. unsigned int rp, caddr, res;
  241. while (unsol->rp != unsol->wp) {
  242. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  243. unsol->rp = rp;
  244. rp <<= 1;
  245. res = unsol->queue[rp];
  246. caddr = unsol->queue[rp + 1];
  247. if (! (caddr & (1 << 4))) /* no unsolicited event? */
  248. continue;
  249. codec = bus->caddr_tbl[caddr & 0x0f];
  250. if (codec && codec->patch_ops.unsol_event)
  251. codec->patch_ops.unsol_event(codec, res);
  252. }
  253. }
  254. /*
  255. * initialize unsolicited queue
  256. */
  257. static int init_unsol_queue(struct hda_bus *bus)
  258. {
  259. struct hda_bus_unsolicited *unsol;
  260. unsol = kcalloc(1, sizeof(*unsol), GFP_KERNEL);
  261. if (! unsol) {
  262. snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
  263. return -ENOMEM;
  264. }
  265. unsol->workq = create_workqueue("hda_codec");
  266. if (! unsol->workq) {
  267. snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
  268. kfree(unsol);
  269. return -ENOMEM;
  270. }
  271. INIT_WORK(&unsol->work, process_unsol_events, bus);
  272. bus->unsol = unsol;
  273. return 0;
  274. }
  275. /*
  276. * destructor
  277. */
  278. static void snd_hda_codec_free(struct hda_codec *codec);
  279. static int snd_hda_bus_free(struct hda_bus *bus)
  280. {
  281. struct list_head *p, *n;
  282. if (! bus)
  283. return 0;
  284. if (bus->unsol) {
  285. destroy_workqueue(bus->unsol->workq);
  286. kfree(bus->unsol);
  287. }
  288. list_for_each_safe(p, n, &bus->codec_list) {
  289. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  290. snd_hda_codec_free(codec);
  291. }
  292. if (bus->ops.private_free)
  293. bus->ops.private_free(bus);
  294. kfree(bus);
  295. return 0;
  296. }
  297. static int snd_hda_bus_dev_free(snd_device_t *device)
  298. {
  299. struct hda_bus *bus = device->device_data;
  300. return snd_hda_bus_free(bus);
  301. }
  302. /**
  303. * snd_hda_bus_new - create a HDA bus
  304. * @card: the card entry
  305. * @temp: the template for hda_bus information
  306. * @busp: the pointer to store the created bus instance
  307. *
  308. * Returns 0 if successful, or a negative error code.
  309. */
  310. int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
  311. struct hda_bus **busp)
  312. {
  313. struct hda_bus *bus;
  314. int err;
  315. static snd_device_ops_t dev_ops = {
  316. .dev_free = snd_hda_bus_dev_free,
  317. };
  318. snd_assert(temp, return -EINVAL);
  319. snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
  320. if (busp)
  321. *busp = NULL;
  322. bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
  323. if (bus == NULL) {
  324. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  325. return -ENOMEM;
  326. }
  327. bus->card = card;
  328. bus->private_data = temp->private_data;
  329. bus->pci = temp->pci;
  330. bus->modelname = temp->modelname;
  331. bus->ops = temp->ops;
  332. init_MUTEX(&bus->cmd_mutex);
  333. INIT_LIST_HEAD(&bus->codec_list);
  334. init_unsol_queue(bus);
  335. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  336. snd_hda_bus_free(bus);
  337. return err;
  338. }
  339. if (busp)
  340. *busp = bus;
  341. return 0;
  342. }
  343. /*
  344. * find a matching codec preset
  345. */
  346. static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
  347. {
  348. const struct hda_codec_preset **tbl, *preset;
  349. for (tbl = hda_preset_tables; *tbl; tbl++) {
  350. for (preset = *tbl; preset->id; preset++) {
  351. u32 mask = preset->mask;
  352. if (! mask)
  353. mask = ~0;
  354. if (preset->id == (codec->vendor_id & mask))
  355. return preset;
  356. }
  357. }
  358. return NULL;
  359. }
  360. /*
  361. * snd_hda_get_codec_name - store the codec name
  362. */
  363. void snd_hda_get_codec_name(struct hda_codec *codec,
  364. char *name, int namelen)
  365. {
  366. const struct hda_vendor_id *c;
  367. const char *vendor = NULL;
  368. u16 vendor_id = codec->vendor_id >> 16;
  369. char tmp[16];
  370. for (c = hda_vendor_ids; c->id; c++) {
  371. if (c->id == vendor_id) {
  372. vendor = c->name;
  373. break;
  374. }
  375. }
  376. if (! vendor) {
  377. sprintf(tmp, "Generic %04x", vendor_id);
  378. vendor = tmp;
  379. }
  380. if (codec->preset && codec->preset->name)
  381. snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
  382. else
  383. snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
  384. }
  385. /*
  386. * look for an AFG and MFG nodes
  387. */
  388. static void setup_fg_nodes(struct hda_codec *codec)
  389. {
  390. int i, total_nodes;
  391. hda_nid_t nid;
  392. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  393. for (i = 0; i < total_nodes; i++, nid++) {
  394. switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
  395. case AC_GRP_AUDIO_FUNCTION:
  396. codec->afg = nid;
  397. break;
  398. case AC_GRP_MODEM_FUNCTION:
  399. codec->mfg = nid;
  400. break;
  401. default:
  402. break;
  403. }
  404. }
  405. }
  406. /*
  407. * codec destructor
  408. */
  409. static void snd_hda_codec_free(struct hda_codec *codec)
  410. {
  411. if (! codec)
  412. return;
  413. list_del(&codec->list);
  414. codec->bus->caddr_tbl[codec->addr] = NULL;
  415. if (codec->patch_ops.free)
  416. codec->patch_ops.free(codec);
  417. kfree(codec);
  418. }
  419. static void init_amp_hash(struct hda_codec *codec);
  420. /**
  421. * snd_hda_codec_new - create a HDA codec
  422. * @bus: the bus to assign
  423. * @codec_addr: the codec address
  424. * @codecp: the pointer to store the generated codec
  425. *
  426. * Returns 0 if successful, or a negative error code.
  427. */
  428. int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  429. struct hda_codec **codecp)
  430. {
  431. struct hda_codec *codec;
  432. char component[13];
  433. int err;
  434. snd_assert(bus, return -EINVAL);
  435. snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
  436. if (bus->caddr_tbl[codec_addr]) {
  437. snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
  438. return -EBUSY;
  439. }
  440. codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
  441. if (codec == NULL) {
  442. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  443. return -ENOMEM;
  444. }
  445. codec->bus = bus;
  446. codec->addr = codec_addr;
  447. init_MUTEX(&codec->spdif_mutex);
  448. init_amp_hash(codec);
  449. list_add_tail(&codec->list, &bus->codec_list);
  450. bus->caddr_tbl[codec_addr] = codec;
  451. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
  452. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
  453. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
  454. setup_fg_nodes(codec);
  455. if (! codec->afg && ! codec->mfg) {
  456. snd_printdd("hda_codec: no AFG or MFG node found\n");
  457. snd_hda_codec_free(codec);
  458. return -ENODEV;
  459. }
  460. codec->preset = find_codec_preset(codec);
  461. if (! *bus->card->mixername)
  462. snd_hda_get_codec_name(codec, bus->card->mixername,
  463. sizeof(bus->card->mixername));
  464. if (codec->preset && codec->preset->patch)
  465. err = codec->preset->patch(codec);
  466. else
  467. err = snd_hda_parse_generic_codec(codec);
  468. if (err < 0) {
  469. snd_hda_codec_free(codec);
  470. return err;
  471. }
  472. snd_hda_codec_proc_new(codec);
  473. sprintf(component, "HDA:%08x", codec->vendor_id);
  474. snd_component_add(codec->bus->card, component);
  475. if (codecp)
  476. *codecp = codec;
  477. return 0;
  478. }
  479. /**
  480. * snd_hda_codec_setup_stream - set up the codec for streaming
  481. * @codec: the CODEC to set up
  482. * @nid: the NID to set up
  483. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  484. * @channel_id: channel id to pass, zero based.
  485. * @format: stream format.
  486. */
  487. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
  488. int channel_id, int format)
  489. {
  490. if (! nid)
  491. return;
  492. snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  493. nid, stream_tag, channel_id, format);
  494. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  495. (stream_tag << 4) | channel_id);
  496. msleep(1);
  497. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  498. }
  499. /*
  500. * amp access functions
  501. */
  502. /* FIXME: more better hash key? */
  503. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  504. #define INFO_AMP_CAPS (1<<0)
  505. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  506. /* initialize the hash table */
  507. static void init_amp_hash(struct hda_codec *codec)
  508. {
  509. memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
  510. codec->num_amp_entries = 0;
  511. }
  512. /* query the hash. allocate an entry if not found. */
  513. static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  514. {
  515. u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
  516. u16 cur = codec->amp_hash[idx];
  517. struct hda_amp_info *info;
  518. while (cur != 0xffff) {
  519. info = &codec->amp_info[cur];
  520. if (info->key == key)
  521. return info;
  522. cur = info->next;
  523. }
  524. /* add a new hash entry */
  525. if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
  526. snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
  527. return NULL;
  528. }
  529. cur = codec->num_amp_entries++;
  530. info = &codec->amp_info[cur];
  531. info->key = key;
  532. info->status = 0; /* not initialized yet */
  533. info->next = codec->amp_hash[idx];
  534. codec->amp_hash[idx] = cur;
  535. return info;
  536. }
  537. /*
  538. * query AMP capabilities for the given widget and direction
  539. */
  540. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  541. {
  542. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  543. if (! info)
  544. return 0;
  545. if (! (info->status & INFO_AMP_CAPS)) {
  546. if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
  547. nid = codec->afg;
  548. info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
  549. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  550. info->status |= INFO_AMP_CAPS;
  551. }
  552. return info->amp_caps;
  553. }
  554. /*
  555. * read the current volume to info
  556. * if the cache exists, read the cache value.
  557. */
  558. static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  559. hda_nid_t nid, int ch, int direction, int index)
  560. {
  561. u32 val, parm;
  562. if (info->status & INFO_AMP_VOL(ch))
  563. return info->vol[ch];
  564. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  565. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  566. parm |= index;
  567. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
  568. info->vol[ch] = val & 0xff;
  569. info->status |= INFO_AMP_VOL(ch);
  570. return info->vol[ch];
  571. }
  572. /*
  573. * write the current volume in info to the h/w and update the cache
  574. */
  575. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  576. hda_nid_t nid, int ch, int direction, int index, int val)
  577. {
  578. u32 parm;
  579. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  580. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  581. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  582. parm |= val;
  583. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  584. info->vol[ch] = val;
  585. }
  586. /*
  587. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  588. */
  589. static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
  590. {
  591. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  592. if (! info)
  593. return 0;
  594. return get_vol_mute(codec, info, nid, ch, direction, index);
  595. }
  596. /*
  597. * update the AMP value, mask = bit mask to set, val = the value
  598. */
  599. static int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int mask, int val)
  600. {
  601. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  602. if (! info)
  603. return 0;
  604. val &= mask;
  605. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  606. if (info->vol[ch] == val && ! codec->in_resume)
  607. return 0;
  608. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  609. return 1;
  610. }
  611. /*
  612. * AMP control callbacks
  613. */
  614. /* retrieve parameters from private_value */
  615. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  616. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  617. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  618. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  619. /* volume */
  620. int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  621. {
  622. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  623. u16 nid = get_amp_nid(kcontrol);
  624. u8 chs = get_amp_channels(kcontrol);
  625. int dir = get_amp_direction(kcontrol);
  626. u32 caps;
  627. caps = query_amp_caps(codec, nid, dir);
  628. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
  629. if (! caps) {
  630. printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
  631. return -EINVAL;
  632. }
  633. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  634. uinfo->count = chs == 3 ? 2 : 1;
  635. uinfo->value.integer.min = 0;
  636. uinfo->value.integer.max = caps;
  637. return 0;
  638. }
  639. int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  640. {
  641. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  642. hda_nid_t nid = get_amp_nid(kcontrol);
  643. int chs = get_amp_channels(kcontrol);
  644. int dir = get_amp_direction(kcontrol);
  645. int idx = get_amp_index(kcontrol);
  646. long *valp = ucontrol->value.integer.value;
  647. if (chs & 1)
  648. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  649. if (chs & 2)
  650. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  651. return 0;
  652. }
  653. int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  654. {
  655. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  656. hda_nid_t nid = get_amp_nid(kcontrol);
  657. int chs = get_amp_channels(kcontrol);
  658. int dir = get_amp_direction(kcontrol);
  659. int idx = get_amp_index(kcontrol);
  660. long *valp = ucontrol->value.integer.value;
  661. int change = 0;
  662. if (chs & 1) {
  663. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  664. 0x7f, *valp);
  665. valp++;
  666. }
  667. if (chs & 2)
  668. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  669. 0x7f, *valp);
  670. return change;
  671. }
  672. /* switch */
  673. int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  674. {
  675. int chs = get_amp_channels(kcontrol);
  676. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  677. uinfo->count = chs == 3 ? 2 : 1;
  678. uinfo->value.integer.min = 0;
  679. uinfo->value.integer.max = 1;
  680. return 0;
  681. }
  682. int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  683. {
  684. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  685. hda_nid_t nid = get_amp_nid(kcontrol);
  686. int chs = get_amp_channels(kcontrol);
  687. int dir = get_amp_direction(kcontrol);
  688. int idx = get_amp_index(kcontrol);
  689. long *valp = ucontrol->value.integer.value;
  690. if (chs & 1)
  691. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  692. if (chs & 2)
  693. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  694. return 0;
  695. }
  696. int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  697. {
  698. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  699. hda_nid_t nid = get_amp_nid(kcontrol);
  700. int chs = get_amp_channels(kcontrol);
  701. int dir = get_amp_direction(kcontrol);
  702. int idx = get_amp_index(kcontrol);
  703. long *valp = ucontrol->value.integer.value;
  704. int change = 0;
  705. if (chs & 1) {
  706. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  707. 0x80, *valp ? 0 : 0x80);
  708. valp++;
  709. }
  710. if (chs & 2)
  711. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  712. 0x80, *valp ? 0 : 0x80);
  713. return change;
  714. }
  715. /*
  716. * SPDIF out controls
  717. */
  718. static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  719. {
  720. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  721. uinfo->count = 1;
  722. return 0;
  723. }
  724. static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  725. {
  726. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  727. IEC958_AES0_NONAUDIO |
  728. IEC958_AES0_CON_EMPHASIS_5015 |
  729. IEC958_AES0_CON_NOT_COPYRIGHT;
  730. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  731. IEC958_AES1_CON_ORIGINAL;
  732. return 0;
  733. }
  734. static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  735. {
  736. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  737. IEC958_AES0_NONAUDIO |
  738. IEC958_AES0_PRO_EMPHASIS_5015;
  739. return 0;
  740. }
  741. static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  742. {
  743. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  744. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  745. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  746. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  747. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  748. return 0;
  749. }
  750. /* convert from SPDIF status bits to HDA SPDIF bits
  751. * bit 0 (DigEn) is always set zero (to be filled later)
  752. */
  753. static unsigned short convert_from_spdif_status(unsigned int sbits)
  754. {
  755. unsigned short val = 0;
  756. if (sbits & IEC958_AES0_PROFESSIONAL)
  757. val |= 1 << 6;
  758. if (sbits & IEC958_AES0_NONAUDIO)
  759. val |= 1 << 5;
  760. if (sbits & IEC958_AES0_PROFESSIONAL) {
  761. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  762. val |= 1 << 3;
  763. } else {
  764. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  765. val |= 1 << 3;
  766. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  767. val |= 1 << 4;
  768. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  769. val |= 1 << 7;
  770. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  771. }
  772. return val;
  773. }
  774. /* convert to SPDIF status bits from HDA SPDIF bits
  775. */
  776. static unsigned int convert_to_spdif_status(unsigned short val)
  777. {
  778. unsigned int sbits = 0;
  779. if (val & (1 << 5))
  780. sbits |= IEC958_AES0_NONAUDIO;
  781. if (val & (1 << 6))
  782. sbits |= IEC958_AES0_PROFESSIONAL;
  783. if (sbits & IEC958_AES0_PROFESSIONAL) {
  784. if (sbits & (1 << 3))
  785. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  786. } else {
  787. if (val & (1 << 3))
  788. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  789. if (! (val & (1 << 4)))
  790. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  791. if (val & (1 << 7))
  792. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  793. sbits |= val & (0x7f << 8);
  794. }
  795. return sbits;
  796. }
  797. static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  798. {
  799. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  800. hda_nid_t nid = kcontrol->private_value;
  801. unsigned short val;
  802. int change;
  803. down(&codec->spdif_mutex);
  804. codec->spdif_status = ucontrol->value.iec958.status[0] |
  805. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  806. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  807. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  808. val = convert_from_spdif_status(codec->spdif_status);
  809. val |= codec->spdif_ctls & 1;
  810. change = codec->spdif_ctls != val;
  811. codec->spdif_ctls = val;
  812. if (change || codec->in_resume) {
  813. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  814. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  815. }
  816. up(&codec->spdif_mutex);
  817. return change;
  818. }
  819. static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  820. {
  821. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  822. uinfo->count = 1;
  823. uinfo->value.integer.min = 0;
  824. uinfo->value.integer.max = 1;
  825. return 0;
  826. }
  827. static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  828. {
  829. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  830. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  831. return 0;
  832. }
  833. static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  834. {
  835. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  836. hda_nid_t nid = kcontrol->private_value;
  837. unsigned short val;
  838. int change;
  839. down(&codec->spdif_mutex);
  840. val = codec->spdif_ctls & ~1;
  841. if (ucontrol->value.integer.value[0])
  842. val |= 1;
  843. change = codec->spdif_ctls != val;
  844. if (change || codec->in_resume) {
  845. codec->spdif_ctls = val;
  846. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  847. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  848. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  849. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  850. }
  851. up(&codec->spdif_mutex);
  852. return change;
  853. }
  854. static snd_kcontrol_new_t dig_mixes[] = {
  855. {
  856. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  857. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  858. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  859. .info = snd_hda_spdif_mask_info,
  860. .get = snd_hda_spdif_cmask_get,
  861. },
  862. {
  863. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  864. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  865. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  866. .info = snd_hda_spdif_mask_info,
  867. .get = snd_hda_spdif_pmask_get,
  868. },
  869. {
  870. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  871. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  872. .info = snd_hda_spdif_mask_info,
  873. .get = snd_hda_spdif_default_get,
  874. .put = snd_hda_spdif_default_put,
  875. },
  876. {
  877. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  878. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  879. .info = snd_hda_spdif_out_switch_info,
  880. .get = snd_hda_spdif_out_switch_get,
  881. .put = snd_hda_spdif_out_switch_put,
  882. },
  883. { } /* end */
  884. };
  885. /**
  886. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  887. * @codec: the HDA codec
  888. * @nid: audio out widget NID
  889. *
  890. * Creates controls related with the SPDIF output.
  891. * Called from each patch supporting the SPDIF out.
  892. *
  893. * Returns 0 if successful, or a negative error code.
  894. */
  895. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  896. {
  897. int err;
  898. snd_kcontrol_t *kctl;
  899. snd_kcontrol_new_t *dig_mix;
  900. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  901. kctl = snd_ctl_new1(dig_mix, codec);
  902. kctl->private_value = nid;
  903. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  904. return err;
  905. }
  906. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  907. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  908. return 0;
  909. }
  910. /*
  911. * SPDIF input
  912. */
  913. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  914. static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  915. {
  916. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  917. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  918. return 0;
  919. }
  920. static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  921. {
  922. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  923. hda_nid_t nid = kcontrol->private_value;
  924. unsigned int val = !!ucontrol->value.integer.value[0];
  925. int change;
  926. down(&codec->spdif_mutex);
  927. change = codec->spdif_in_enable != val;
  928. if (change || codec->in_resume) {
  929. codec->spdif_in_enable = val;
  930. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  931. }
  932. up(&codec->spdif_mutex);
  933. return change;
  934. }
  935. static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  936. {
  937. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  938. hda_nid_t nid = kcontrol->private_value;
  939. unsigned short val;
  940. unsigned int sbits;
  941. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  942. sbits = convert_to_spdif_status(val);
  943. ucontrol->value.iec958.status[0] = sbits;
  944. ucontrol->value.iec958.status[1] = sbits >> 8;
  945. ucontrol->value.iec958.status[2] = sbits >> 16;
  946. ucontrol->value.iec958.status[3] = sbits >> 24;
  947. return 0;
  948. }
  949. static snd_kcontrol_new_t dig_in_ctls[] = {
  950. {
  951. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  952. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  953. .info = snd_hda_spdif_in_switch_info,
  954. .get = snd_hda_spdif_in_switch_get,
  955. .put = snd_hda_spdif_in_switch_put,
  956. },
  957. {
  958. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  959. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  960. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  961. .info = snd_hda_spdif_mask_info,
  962. .get = snd_hda_spdif_in_status_get,
  963. },
  964. { } /* end */
  965. };
  966. /**
  967. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  968. * @codec: the HDA codec
  969. * @nid: audio in widget NID
  970. *
  971. * Creates controls related with the SPDIF input.
  972. * Called from each patch supporting the SPDIF in.
  973. *
  974. * Returns 0 if successful, or a negative error code.
  975. */
  976. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  977. {
  978. int err;
  979. snd_kcontrol_t *kctl;
  980. snd_kcontrol_new_t *dig_mix;
  981. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  982. kctl = snd_ctl_new1(dig_mix, codec);
  983. kctl->private_value = nid;
  984. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  985. return err;
  986. }
  987. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  988. return 0;
  989. }
  990. /**
  991. * snd_hda_build_controls - build mixer controls
  992. * @bus: the BUS
  993. *
  994. * Creates mixer controls for each codec included in the bus.
  995. *
  996. * Returns 0 if successful, otherwise a negative error code.
  997. */
  998. int snd_hda_build_controls(struct hda_bus *bus)
  999. {
  1000. struct list_head *p;
  1001. /* build controls */
  1002. list_for_each(p, &bus->codec_list) {
  1003. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1004. int err;
  1005. if (! codec->patch_ops.build_controls)
  1006. continue;
  1007. err = codec->patch_ops.build_controls(codec);
  1008. if (err < 0)
  1009. return err;
  1010. }
  1011. /* initialize */
  1012. list_for_each(p, &bus->codec_list) {
  1013. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1014. int err;
  1015. if (! codec->patch_ops.init)
  1016. continue;
  1017. err = codec->patch_ops.init(codec);
  1018. if (err < 0)
  1019. return err;
  1020. }
  1021. return 0;
  1022. }
  1023. /*
  1024. * stream formats
  1025. */
  1026. struct hda_rate_tbl {
  1027. unsigned int hz;
  1028. unsigned int alsa_bits;
  1029. unsigned int hda_fmt;
  1030. };
  1031. static struct hda_rate_tbl rate_bits[] = {
  1032. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1033. /* autodetected value used in snd_hda_query_supported_pcm */
  1034. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1035. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1036. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1037. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1038. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1039. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1040. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1041. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1042. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1043. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1044. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1045. /* not autodetected value */
  1046. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1047. { 0 } /* terminator */
  1048. };
  1049. /**
  1050. * snd_hda_calc_stream_format - calculate format bitset
  1051. * @rate: the sample rate
  1052. * @channels: the number of channels
  1053. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1054. * @maxbps: the max. bps
  1055. *
  1056. * Calculate the format bitset from the given rate, channels and th PCM format.
  1057. *
  1058. * Return zero if invalid.
  1059. */
  1060. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1061. unsigned int channels,
  1062. unsigned int format,
  1063. unsigned int maxbps)
  1064. {
  1065. int i;
  1066. unsigned int val = 0;
  1067. for (i = 0; rate_bits[i].hz; i++)
  1068. if (rate_bits[i].hz == rate) {
  1069. val = rate_bits[i].hda_fmt;
  1070. break;
  1071. }
  1072. if (! rate_bits[i].hz) {
  1073. snd_printdd("invalid rate %d\n", rate);
  1074. return 0;
  1075. }
  1076. if (channels == 0 || channels > 8) {
  1077. snd_printdd("invalid channels %d\n", channels);
  1078. return 0;
  1079. }
  1080. val |= channels - 1;
  1081. switch (snd_pcm_format_width(format)) {
  1082. case 8: val |= 0x00; break;
  1083. case 16: val |= 0x10; break;
  1084. case 20:
  1085. case 24:
  1086. case 32:
  1087. if (maxbps >= 32)
  1088. val |= 0x40;
  1089. else if (maxbps >= 24)
  1090. val |= 0x30;
  1091. else
  1092. val |= 0x20;
  1093. break;
  1094. default:
  1095. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1096. return 0;
  1097. }
  1098. return val;
  1099. }
  1100. /**
  1101. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1102. * @codec: the HDA codec
  1103. * @nid: NID to query
  1104. * @ratesp: the pointer to store the detected rate bitflags
  1105. * @formatsp: the pointer to store the detected formats
  1106. * @bpsp: the pointer to store the detected format widths
  1107. *
  1108. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1109. * or @bsps argument is ignored.
  1110. *
  1111. * Returns 0 if successful, otherwise a negative error code.
  1112. */
  1113. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1114. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1115. {
  1116. int i;
  1117. unsigned int val, streams;
  1118. val = 0;
  1119. if (nid != codec->afg &&
  1120. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1121. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1122. if (val == -1)
  1123. return -EIO;
  1124. }
  1125. if (! val)
  1126. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1127. if (ratesp) {
  1128. u32 rates = 0;
  1129. for (i = 0; rate_bits[i].hz; i++) {
  1130. if (val & (1 << i))
  1131. rates |= rate_bits[i].alsa_bits;
  1132. }
  1133. *ratesp = rates;
  1134. }
  1135. if (formatsp || bpsp) {
  1136. u64 formats = 0;
  1137. unsigned int bps;
  1138. unsigned int wcaps;
  1139. wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  1140. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1141. if (streams == -1)
  1142. return -EIO;
  1143. if (! streams) {
  1144. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1145. if (streams == -1)
  1146. return -EIO;
  1147. }
  1148. bps = 0;
  1149. if (streams & AC_SUPFMT_PCM) {
  1150. if (val & AC_SUPPCM_BITS_8) {
  1151. formats |= SNDRV_PCM_FMTBIT_U8;
  1152. bps = 8;
  1153. }
  1154. if (val & AC_SUPPCM_BITS_16) {
  1155. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1156. bps = 16;
  1157. }
  1158. if (wcaps & AC_WCAP_DIGITAL) {
  1159. if (val & AC_SUPPCM_BITS_32)
  1160. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1161. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1162. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1163. if (val & AC_SUPPCM_BITS_24)
  1164. bps = 24;
  1165. else if (val & AC_SUPPCM_BITS_20)
  1166. bps = 20;
  1167. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1168. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1169. if (val & AC_SUPPCM_BITS_32)
  1170. bps = 32;
  1171. else if (val & AC_SUPPCM_BITS_20)
  1172. bps = 20;
  1173. else if (val & AC_SUPPCM_BITS_24)
  1174. bps = 24;
  1175. }
  1176. }
  1177. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1178. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1179. bps = 32;
  1180. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1181. /* temporary hack: we have still no proper support
  1182. * for the direct AC3 stream...
  1183. */
  1184. formats |= SNDRV_PCM_FMTBIT_U8;
  1185. bps = 8;
  1186. }
  1187. if (formatsp)
  1188. *formatsp = formats;
  1189. if (bpsp)
  1190. *bpsp = bps;
  1191. }
  1192. return 0;
  1193. }
  1194. /**
  1195. * snd_hda_is_supported_format - check whether the given node supports the format val
  1196. *
  1197. * Returns 1 if supported, 0 if not.
  1198. */
  1199. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1200. unsigned int format)
  1201. {
  1202. int i;
  1203. unsigned int val = 0, rate, stream;
  1204. if (nid != codec->afg &&
  1205. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1206. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1207. if (val == -1)
  1208. return 0;
  1209. }
  1210. if (! val) {
  1211. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1212. if (val == -1)
  1213. return 0;
  1214. }
  1215. rate = format & 0xff00;
  1216. for (i = 0; rate_bits[i].hz; i++)
  1217. if (rate_bits[i].hda_fmt == rate) {
  1218. if (val & (1 << i))
  1219. break;
  1220. return 0;
  1221. }
  1222. if (! rate_bits[i].hz)
  1223. return 0;
  1224. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1225. if (stream == -1)
  1226. return 0;
  1227. if (! stream && nid != codec->afg)
  1228. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1229. if (! stream || stream == -1)
  1230. return 0;
  1231. if (stream & AC_SUPFMT_PCM) {
  1232. switch (format & 0xf0) {
  1233. case 0x00:
  1234. if (! (val & AC_SUPPCM_BITS_8))
  1235. return 0;
  1236. break;
  1237. case 0x10:
  1238. if (! (val & AC_SUPPCM_BITS_16))
  1239. return 0;
  1240. break;
  1241. case 0x20:
  1242. if (! (val & AC_SUPPCM_BITS_20))
  1243. return 0;
  1244. break;
  1245. case 0x30:
  1246. if (! (val & AC_SUPPCM_BITS_24))
  1247. return 0;
  1248. break;
  1249. case 0x40:
  1250. if (! (val & AC_SUPPCM_BITS_32))
  1251. return 0;
  1252. break;
  1253. default:
  1254. return 0;
  1255. }
  1256. } else {
  1257. /* FIXME: check for float32 and AC3? */
  1258. }
  1259. return 1;
  1260. }
  1261. /*
  1262. * PCM stuff
  1263. */
  1264. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1265. struct hda_codec *codec,
  1266. snd_pcm_substream_t *substream)
  1267. {
  1268. return 0;
  1269. }
  1270. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1271. struct hda_codec *codec,
  1272. unsigned int stream_tag,
  1273. unsigned int format,
  1274. snd_pcm_substream_t *substream)
  1275. {
  1276. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1277. return 0;
  1278. }
  1279. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1280. struct hda_codec *codec,
  1281. snd_pcm_substream_t *substream)
  1282. {
  1283. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1284. return 0;
  1285. }
  1286. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1287. {
  1288. if (info->nid) {
  1289. /* query support PCM information from the given NID */
  1290. if (! info->rates || ! info->formats)
  1291. snd_hda_query_supported_pcm(codec, info->nid,
  1292. info->rates ? NULL : &info->rates,
  1293. info->formats ? NULL : &info->formats,
  1294. info->maxbps ? NULL : &info->maxbps);
  1295. }
  1296. if (info->ops.open == NULL)
  1297. info->ops.open = hda_pcm_default_open_close;
  1298. if (info->ops.close == NULL)
  1299. info->ops.close = hda_pcm_default_open_close;
  1300. if (info->ops.prepare == NULL) {
  1301. snd_assert(info->nid, return -EINVAL);
  1302. info->ops.prepare = hda_pcm_default_prepare;
  1303. }
  1304. if (info->ops.cleanup == NULL) {
  1305. snd_assert(info->nid, return -EINVAL);
  1306. info->ops.cleanup = hda_pcm_default_cleanup;
  1307. }
  1308. return 0;
  1309. }
  1310. /**
  1311. * snd_hda_build_pcms - build PCM information
  1312. * @bus: the BUS
  1313. *
  1314. * Create PCM information for each codec included in the bus.
  1315. *
  1316. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1317. * codec->pcm_info properly. The array is referred by the top-level driver
  1318. * to create its PCM instances.
  1319. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1320. * callback.
  1321. *
  1322. * At least, substreams, channels_min and channels_max must be filled for
  1323. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1324. * When rates and/or formats are zero, the supported values are queried
  1325. * from the given nid. The nid is used also by the default ops.prepare
  1326. * and ops.cleanup callbacks.
  1327. *
  1328. * The driver needs to call ops.open in its open callback. Similarly,
  1329. * ops.close is supposed to be called in the close callback.
  1330. * ops.prepare should be called in the prepare or hw_params callback
  1331. * with the proper parameters for set up.
  1332. * ops.cleanup should be called in hw_free for clean up of streams.
  1333. *
  1334. * This function returns 0 if successfull, or a negative error code.
  1335. */
  1336. int snd_hda_build_pcms(struct hda_bus *bus)
  1337. {
  1338. struct list_head *p;
  1339. list_for_each(p, &bus->codec_list) {
  1340. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1341. unsigned int pcm, s;
  1342. int err;
  1343. if (! codec->patch_ops.build_pcms)
  1344. continue;
  1345. err = codec->patch_ops.build_pcms(codec);
  1346. if (err < 0)
  1347. return err;
  1348. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1349. for (s = 0; s < 2; s++) {
  1350. struct hda_pcm_stream *info;
  1351. info = &codec->pcm_info[pcm].stream[s];
  1352. if (! info->substreams)
  1353. continue;
  1354. err = set_pcm_default_values(codec, info);
  1355. if (err < 0)
  1356. return err;
  1357. }
  1358. }
  1359. }
  1360. return 0;
  1361. }
  1362. /**
  1363. * snd_hda_check_board_config - compare the current codec with the config table
  1364. * @codec: the HDA codec
  1365. * @tbl: configuration table, terminated by null entries
  1366. *
  1367. * Compares the modelname or PCI subsystem id of the current codec with the
  1368. * given configuration table. If a matching entry is found, returns its
  1369. * config value (supposed to be 0 or positive).
  1370. *
  1371. * If no entries are matching, the function returns a negative value.
  1372. */
  1373. int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
  1374. {
  1375. const struct hda_board_config *c;
  1376. if (codec->bus->modelname) {
  1377. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1378. if (c->modelname &&
  1379. ! strcmp(codec->bus->modelname, c->modelname)) {
  1380. snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
  1381. return c->config;
  1382. }
  1383. }
  1384. }
  1385. if (codec->bus->pci) {
  1386. u16 subsystem_vendor, subsystem_device;
  1387. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1388. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1389. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1390. if (c->pci_subvendor == subsystem_vendor &&
  1391. (! c->pci_subdevice /* all match */||
  1392. (c->pci_subdevice == subsystem_device))) {
  1393. snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
  1394. subsystem_vendor, subsystem_device, c->config);
  1395. return c->config;
  1396. }
  1397. }
  1398. }
  1399. return -1;
  1400. }
  1401. /**
  1402. * snd_hda_add_new_ctls - create controls from the array
  1403. * @codec: the HDA codec
  1404. * @knew: the array of snd_kcontrol_new_t
  1405. *
  1406. * This helper function creates and add new controls in the given array.
  1407. * The array must be terminated with an empty entry as terminator.
  1408. *
  1409. * Returns 0 if successful, or a negative error code.
  1410. */
  1411. int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1412. {
  1413. int err;
  1414. for (; knew->name; knew++) {
  1415. err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
  1416. if (err < 0)
  1417. return err;
  1418. }
  1419. return 0;
  1420. }
  1421. /*
  1422. * input MUX helper
  1423. */
  1424. int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
  1425. {
  1426. unsigned int index;
  1427. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1428. uinfo->count = 1;
  1429. uinfo->value.enumerated.items = imux->num_items;
  1430. index = uinfo->value.enumerated.item;
  1431. if (index >= imux->num_items)
  1432. index = imux->num_items - 1;
  1433. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1434. return 0;
  1435. }
  1436. int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
  1437. snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
  1438. unsigned int *cur_val)
  1439. {
  1440. unsigned int idx;
  1441. idx = ucontrol->value.enumerated.item[0];
  1442. if (idx >= imux->num_items)
  1443. idx = imux->num_items - 1;
  1444. if (*cur_val == idx && ! codec->in_resume)
  1445. return 0;
  1446. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1447. imux->items[idx].index);
  1448. *cur_val = idx;
  1449. return 1;
  1450. }
  1451. /*
  1452. * Multi-channel / digital-out PCM helper functions
  1453. */
  1454. /*
  1455. * open the digital out in the exclusive mode
  1456. */
  1457. int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
  1458. {
  1459. down(&codec->spdif_mutex);
  1460. if (mout->dig_out_used) {
  1461. up(&codec->spdif_mutex);
  1462. return -EBUSY; /* already being used */
  1463. }
  1464. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1465. up(&codec->spdif_mutex);
  1466. return 0;
  1467. }
  1468. /*
  1469. * release the digital out
  1470. */
  1471. int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
  1472. {
  1473. down(&codec->spdif_mutex);
  1474. mout->dig_out_used = 0;
  1475. up(&codec->spdif_mutex);
  1476. return 0;
  1477. }
  1478. /*
  1479. * set up more restrictions for analog out
  1480. */
  1481. int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
  1482. snd_pcm_substream_t *substream)
  1483. {
  1484. substream->runtime->hw.channels_max = mout->max_channels;
  1485. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1486. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1487. }
  1488. /*
  1489. * set up the i/o for analog out
  1490. * when the digital out is available, copy the front out to digital out, too.
  1491. */
  1492. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  1493. unsigned int stream_tag,
  1494. unsigned int format,
  1495. snd_pcm_substream_t *substream)
  1496. {
  1497. hda_nid_t *nids = mout->dac_nids;
  1498. int chs = substream->runtime->channels;
  1499. int i;
  1500. down(&codec->spdif_mutex);
  1501. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1502. if (chs == 2 &&
  1503. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  1504. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1505. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1506. /* setup digital receiver */
  1507. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1508. stream_tag, 0, format);
  1509. } else {
  1510. mout->dig_out_used = 0;
  1511. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1512. }
  1513. }
  1514. up(&codec->spdif_mutex);
  1515. /* front */
  1516. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  1517. if (mout->hp_nid)
  1518. /* headphone out will just decode front left/right (stereo) */
  1519. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  1520. /* surrounds */
  1521. for (i = 1; i < mout->num_dacs; i++) {
  1522. if (chs >= (i + 1) * 2) /* independent out */
  1523. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  1524. format);
  1525. else /* copy front */
  1526. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
  1527. format);
  1528. }
  1529. return 0;
  1530. }
  1531. /*
  1532. * clean up the setting for analog out
  1533. */
  1534. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
  1535. {
  1536. hda_nid_t *nids = mout->dac_nids;
  1537. int i;
  1538. for (i = 0; i < mout->num_dacs; i++)
  1539. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1540. if (mout->hp_nid)
  1541. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1542. down(&codec->spdif_mutex);
  1543. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1544. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1545. mout->dig_out_used = 0;
  1546. }
  1547. up(&codec->spdif_mutex);
  1548. return 0;
  1549. }
  1550. /*
  1551. * Helper for automatic ping configuration
  1552. */
  1553. /* parse all pin widgets and store the useful pin nids to cfg */
  1554. int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  1555. {
  1556. hda_nid_t nid, nid_start;
  1557. int i, j, nodes;
  1558. short seq, sequences[4], assoc_line_out;
  1559. memset(cfg, 0, sizeof(*cfg));
  1560. memset(sequences, 0, sizeof(sequences));
  1561. assoc_line_out = 0;
  1562. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  1563. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1564. unsigned int wid_caps = snd_hda_param_read(codec, nid,
  1565. AC_PAR_AUDIO_WIDGET_CAP);
  1566. unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  1567. unsigned int def_conf;
  1568. short assoc, loc;
  1569. /* read all default configuration for pin complex */
  1570. if (wid_type != AC_WID_PIN)
  1571. continue;
  1572. def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  1573. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  1574. continue;
  1575. loc = get_defcfg_location(def_conf);
  1576. switch (get_defcfg_device(def_conf)) {
  1577. case AC_JACK_LINE_OUT:
  1578. case AC_JACK_SPEAKER:
  1579. seq = get_defcfg_sequence(def_conf);
  1580. assoc = get_defcfg_association(def_conf);
  1581. if (! assoc)
  1582. continue;
  1583. if (! assoc_line_out)
  1584. assoc_line_out = assoc;
  1585. else if (assoc_line_out != assoc)
  1586. continue;
  1587. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  1588. continue;
  1589. cfg->line_out_pins[cfg->line_outs] = nid;
  1590. sequences[cfg->line_outs] = seq;
  1591. cfg->line_outs++;
  1592. break;
  1593. case AC_JACK_HP_OUT:
  1594. cfg->hp_pin = nid;
  1595. break;
  1596. case AC_JACK_MIC_IN:
  1597. if (loc == AC_JACK_LOC_FRONT)
  1598. cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
  1599. else
  1600. cfg->input_pins[AUTO_PIN_MIC] = nid;
  1601. break;
  1602. case AC_JACK_LINE_IN:
  1603. if (loc == AC_JACK_LOC_FRONT)
  1604. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  1605. else
  1606. cfg->input_pins[AUTO_PIN_LINE] = nid;
  1607. break;
  1608. case AC_JACK_CD:
  1609. cfg->input_pins[AUTO_PIN_CD] = nid;
  1610. break;
  1611. case AC_JACK_AUX:
  1612. cfg->input_pins[AUTO_PIN_AUX] = nid;
  1613. break;
  1614. case AC_JACK_SPDIF_OUT:
  1615. cfg->dig_out_pin = nid;
  1616. break;
  1617. case AC_JACK_SPDIF_IN:
  1618. cfg->dig_in_pin = nid;
  1619. break;
  1620. }
  1621. }
  1622. /* sort by sequence */
  1623. for (i = 0; i < cfg->line_outs; i++)
  1624. for (j = i + 1; j < cfg->line_outs; j++)
  1625. if (sequences[i] > sequences[j]) {
  1626. seq = sequences[i];
  1627. sequences[i] = sequences[j];
  1628. sequences[j] = seq;
  1629. nid = cfg->line_out_pins[i];
  1630. cfg->line_out_pins[i] = cfg->line_out_pins[j];
  1631. cfg->line_out_pins[j] = nid;
  1632. }
  1633. /* Reorder the surround channels
  1634. * ALSA sequence is front/surr/clfe/side
  1635. * HDA sequence is:
  1636. * 4-ch: front/surr => OK as it is
  1637. * 6-ch: front/clfe/surr
  1638. * 8-ch: front/clfe/side/surr
  1639. */
  1640. switch (cfg->line_outs) {
  1641. case 3:
  1642. nid = cfg->line_out_pins[1];
  1643. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  1644. cfg->line_out_pins[2] = nid;
  1645. break;
  1646. case 4:
  1647. nid = cfg->line_out_pins[1];
  1648. cfg->line_out_pins[1] = cfg->line_out_pins[3];
  1649. cfg->line_out_pins[3] = cfg->line_out_pins[2];
  1650. cfg->line_out_pins[2] = nid;
  1651. break;
  1652. }
  1653. return 0;
  1654. }
  1655. #ifdef CONFIG_PM
  1656. /*
  1657. * power management
  1658. */
  1659. /**
  1660. * snd_hda_suspend - suspend the codecs
  1661. * @bus: the HDA bus
  1662. * @state: suspsend state
  1663. *
  1664. * Returns 0 if successful.
  1665. */
  1666. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  1667. {
  1668. struct list_head *p;
  1669. /* FIXME: should handle power widget capabilities */
  1670. list_for_each(p, &bus->codec_list) {
  1671. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1672. if (codec->patch_ops.suspend)
  1673. codec->patch_ops.suspend(codec, state);
  1674. }
  1675. return 0;
  1676. }
  1677. /**
  1678. * snd_hda_resume - resume the codecs
  1679. * @bus: the HDA bus
  1680. * @state: resume state
  1681. *
  1682. * Returns 0 if successful.
  1683. */
  1684. int snd_hda_resume(struct hda_bus *bus)
  1685. {
  1686. struct list_head *p;
  1687. list_for_each(p, &bus->codec_list) {
  1688. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1689. if (codec->patch_ops.resume)
  1690. codec->patch_ops.resume(codec);
  1691. }
  1692. return 0;
  1693. }
  1694. /**
  1695. * snd_hda_resume_ctls - resume controls in the new control list
  1696. * @codec: the HDA codec
  1697. * @knew: the array of snd_kcontrol_new_t
  1698. *
  1699. * This function resumes the mixer controls in the snd_kcontrol_new_t array,
  1700. * originally for snd_hda_add_new_ctls().
  1701. * The array must be terminated with an empty entry as terminator.
  1702. */
  1703. int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1704. {
  1705. snd_ctl_elem_value_t *val;
  1706. val = kmalloc(sizeof(*val), GFP_KERNEL);
  1707. if (! val)
  1708. return -ENOMEM;
  1709. codec->in_resume = 1;
  1710. for (; knew->name; knew++) {
  1711. int i, count;
  1712. count = knew->count ? knew->count : 1;
  1713. for (i = 0; i < count; i++) {
  1714. memset(val, 0, sizeof(*val));
  1715. val->id.iface = knew->iface;
  1716. val->id.device = knew->device;
  1717. val->id.subdevice = knew->subdevice;
  1718. strcpy(val->id.name, knew->name);
  1719. val->id.index = knew->index ? knew->index : i;
  1720. /* Assume that get callback reads only from cache,
  1721. * not accessing to the real hardware
  1722. */
  1723. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  1724. continue;
  1725. snd_ctl_elem_write(codec->bus->card, NULL, val);
  1726. }
  1727. }
  1728. codec->in_resume = 0;
  1729. kfree(val);
  1730. return 0;
  1731. }
  1732. /**
  1733. * snd_hda_resume_spdif_out - resume the digital out
  1734. * @codec: the HDA codec
  1735. */
  1736. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  1737. {
  1738. return snd_hda_resume_ctls(codec, dig_mixes);
  1739. }
  1740. /**
  1741. * snd_hda_resume_spdif_in - resume the digital in
  1742. * @codec: the HDA codec
  1743. */
  1744. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  1745. {
  1746. return snd_hda_resume_ctls(codec, dig_in_ctls);
  1747. }
  1748. #endif
  1749. /*
  1750. * symbols exported for controller modules
  1751. */
  1752. EXPORT_SYMBOL(snd_hda_codec_read);
  1753. EXPORT_SYMBOL(snd_hda_codec_write);
  1754. EXPORT_SYMBOL(snd_hda_sequence_write);
  1755. EXPORT_SYMBOL(snd_hda_get_sub_nodes);
  1756. EXPORT_SYMBOL(snd_hda_queue_unsol_event);
  1757. EXPORT_SYMBOL(snd_hda_bus_new);
  1758. EXPORT_SYMBOL(snd_hda_codec_new);
  1759. EXPORT_SYMBOL(snd_hda_codec_setup_stream);
  1760. EXPORT_SYMBOL(snd_hda_calc_stream_format);
  1761. EXPORT_SYMBOL(snd_hda_build_pcms);
  1762. EXPORT_SYMBOL(snd_hda_build_controls);
  1763. #ifdef CONFIG_PM
  1764. EXPORT_SYMBOL(snd_hda_suspend);
  1765. EXPORT_SYMBOL(snd_hda_resume);
  1766. #endif
  1767. /*
  1768. * INIT part
  1769. */
  1770. static int __init alsa_hda_init(void)
  1771. {
  1772. return 0;
  1773. }
  1774. static void __exit alsa_hda_exit(void)
  1775. {
  1776. }
  1777. module_init(alsa_hda_init)
  1778. module_exit(alsa_hda_exit)