hda_codec.c 58 KB

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