hda_codec.c 58 KB

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