hda_codec.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868
  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/mutex.h>
  27. #include <sound/core.h>
  28. #include "hda_codec.h"
  29. #include <sound/asoundef.h>
  30. #include <sound/tlv.h>
  31. #include <sound/initval.h>
  32. #include "hda_local.h"
  33. #include <sound/hda_hwdep.h>
  34. #ifdef CONFIG_SND_HDA_POWER_SAVE
  35. /* define this option here to hide as static */
  36. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  37. module_param(power_save, int, 0644);
  38. MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
  39. "(in second, 0 = disable).");
  40. #endif
  41. /*
  42. * vendor / preset table
  43. */
  44. struct hda_vendor_id {
  45. unsigned int id;
  46. const char *name;
  47. };
  48. /* codec vendor labels */
  49. static struct hda_vendor_id hda_vendor_ids[] = {
  50. { 0x10ec, "Realtek" },
  51. { 0x1057, "Motorola" },
  52. { 0x1106, "VIA" },
  53. { 0x111d, "IDT" },
  54. { 0x11d4, "Analog Devices" },
  55. { 0x13f6, "C-Media" },
  56. { 0x14f1, "Conexant" },
  57. { 0x434d, "C-Media" },
  58. { 0x8384, "SigmaTel" },
  59. {} /* terminator */
  60. };
  61. /* codec presets */
  62. #include "hda_patch.h"
  63. #ifdef CONFIG_SND_HDA_POWER_SAVE
  64. static void hda_power_work(struct work_struct *work);
  65. static void hda_keep_power_on(struct hda_codec *codec);
  66. #else
  67. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  68. #endif
  69. /**
  70. * snd_hda_codec_read - send a command and get the response
  71. * @codec: the HDA codec
  72. * @nid: NID to send the command
  73. * @direct: direct flag
  74. * @verb: the verb to send
  75. * @parm: the parameter for the verb
  76. *
  77. * Send a single command and read the corresponding response.
  78. *
  79. * Returns the obtained response value, or -1 for an error.
  80. */
  81. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  82. int direct,
  83. unsigned int verb, unsigned int parm)
  84. {
  85. unsigned int res;
  86. snd_hda_power_up(codec);
  87. mutex_lock(&codec->bus->cmd_mutex);
  88. if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
  89. res = codec->bus->ops.get_response(codec);
  90. else
  91. res = (unsigned int)-1;
  92. mutex_unlock(&codec->bus->cmd_mutex);
  93. snd_hda_power_down(codec);
  94. return res;
  95. }
  96. /**
  97. * snd_hda_codec_write - send a single command without waiting for response
  98. * @codec: the HDA codec
  99. * @nid: NID to send the command
  100. * @direct: direct flag
  101. * @verb: the verb to send
  102. * @parm: the parameter for the verb
  103. *
  104. * Send a single command without waiting for response.
  105. *
  106. * Returns 0 if successful, or a negative error code.
  107. */
  108. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  109. unsigned int verb, unsigned int parm)
  110. {
  111. int err;
  112. snd_hda_power_up(codec);
  113. mutex_lock(&codec->bus->cmd_mutex);
  114. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  115. mutex_unlock(&codec->bus->cmd_mutex);
  116. snd_hda_power_down(codec);
  117. return err;
  118. }
  119. /**
  120. * snd_hda_sequence_write - sequence writes
  121. * @codec: the HDA codec
  122. * @seq: VERB array to send
  123. *
  124. * Send the commands sequentially from the given array.
  125. * The array must be terminated with NID=0.
  126. */
  127. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  128. {
  129. for (; seq->nid; seq++)
  130. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  131. }
  132. /**
  133. * snd_hda_get_sub_nodes - get the range of sub nodes
  134. * @codec: the HDA codec
  135. * @nid: NID to parse
  136. * @start_id: the pointer to store the start NID
  137. *
  138. * Parse the NID and store the start NID of its sub-nodes.
  139. * Returns the number of sub-nodes.
  140. */
  141. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  142. hda_nid_t *start_id)
  143. {
  144. unsigned int parm;
  145. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  146. if (parm == -1)
  147. return 0;
  148. *start_id = (parm >> 16) & 0x7fff;
  149. return (int)(parm & 0x7fff);
  150. }
  151. /**
  152. * snd_hda_get_connections - get connection list
  153. * @codec: the HDA codec
  154. * @nid: NID to parse
  155. * @conn_list: connection list array
  156. * @max_conns: max. number of connections to store
  157. *
  158. * Parses the connection list of the given widget and stores the list
  159. * of NIDs.
  160. *
  161. * Returns the number of connections, or a negative error code.
  162. */
  163. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  164. hda_nid_t *conn_list, int max_conns)
  165. {
  166. unsigned int parm;
  167. int i, conn_len, conns;
  168. unsigned int shift, num_elems, mask;
  169. hda_nid_t prev_nid;
  170. snd_assert(conn_list && max_conns > 0, return -EINVAL);
  171. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  172. if (parm & AC_CLIST_LONG) {
  173. /* long form */
  174. shift = 16;
  175. num_elems = 2;
  176. } else {
  177. /* short form */
  178. shift = 8;
  179. num_elems = 4;
  180. }
  181. conn_len = parm & AC_CLIST_LENGTH;
  182. mask = (1 << (shift-1)) - 1;
  183. if (!conn_len)
  184. return 0; /* no connection */
  185. if (conn_len == 1) {
  186. /* single connection */
  187. parm = snd_hda_codec_read(codec, nid, 0,
  188. AC_VERB_GET_CONNECT_LIST, 0);
  189. conn_list[0] = parm & mask;
  190. return 1;
  191. }
  192. /* multi connection */
  193. conns = 0;
  194. prev_nid = 0;
  195. for (i = 0; i < conn_len; i++) {
  196. int range_val;
  197. hda_nid_t val, n;
  198. if (i % num_elems == 0)
  199. parm = snd_hda_codec_read(codec, nid, 0,
  200. AC_VERB_GET_CONNECT_LIST, i);
  201. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  202. val = parm & mask;
  203. parm >>= shift;
  204. if (range_val) {
  205. /* ranges between the previous and this one */
  206. if (!prev_nid || prev_nid >= val) {
  207. snd_printk(KERN_WARNING "hda_codec: "
  208. "invalid dep_range_val %x:%x\n",
  209. prev_nid, val);
  210. continue;
  211. }
  212. for (n = prev_nid + 1; n <= val; n++) {
  213. if (conns >= max_conns) {
  214. snd_printk(KERN_ERR
  215. "Too many connections\n");
  216. return -EINVAL;
  217. }
  218. conn_list[conns++] = n;
  219. }
  220. } else {
  221. if (conns >= max_conns) {
  222. snd_printk(KERN_ERR "Too many connections\n");
  223. return -EINVAL;
  224. }
  225. conn_list[conns++] = val;
  226. }
  227. prev_nid = val;
  228. }
  229. return conns;
  230. }
  231. /**
  232. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  233. * @bus: the BUS
  234. * @res: unsolicited event (lower 32bit of RIRB entry)
  235. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  236. *
  237. * Adds the given event to the queue. The events are processed in
  238. * the workqueue asynchronously. Call this function in the interrupt
  239. * hanlder when RIRB receives an unsolicited event.
  240. *
  241. * Returns 0 if successful, or a negative error code.
  242. */
  243. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  244. {
  245. struct hda_bus_unsolicited *unsol;
  246. unsigned int wp;
  247. unsol = bus->unsol;
  248. if (!unsol)
  249. return 0;
  250. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  251. unsol->wp = wp;
  252. wp <<= 1;
  253. unsol->queue[wp] = res;
  254. unsol->queue[wp + 1] = res_ex;
  255. schedule_work(&unsol->work);
  256. return 0;
  257. }
  258. /*
  259. * process queueud unsolicited events
  260. */
  261. static void process_unsol_events(struct work_struct *work)
  262. {
  263. struct hda_bus_unsolicited *unsol =
  264. container_of(work, struct hda_bus_unsolicited, work);
  265. struct hda_bus *bus = unsol->bus;
  266. struct hda_codec *codec;
  267. unsigned int rp, caddr, res;
  268. while (unsol->rp != unsol->wp) {
  269. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  270. unsol->rp = rp;
  271. rp <<= 1;
  272. res = unsol->queue[rp];
  273. caddr = unsol->queue[rp + 1];
  274. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  275. continue;
  276. codec = bus->caddr_tbl[caddr & 0x0f];
  277. if (codec && codec->patch_ops.unsol_event)
  278. codec->patch_ops.unsol_event(codec, res);
  279. }
  280. }
  281. /*
  282. * initialize unsolicited queue
  283. */
  284. static int __devinit init_unsol_queue(struct hda_bus *bus)
  285. {
  286. struct hda_bus_unsolicited *unsol;
  287. if (bus->unsol) /* already initialized */
  288. return 0;
  289. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  290. if (!unsol) {
  291. snd_printk(KERN_ERR "hda_codec: "
  292. "can't allocate unsolicited queue\n");
  293. return -ENOMEM;
  294. }
  295. INIT_WORK(&unsol->work, process_unsol_events);
  296. unsol->bus = bus;
  297. bus->unsol = unsol;
  298. return 0;
  299. }
  300. /*
  301. * destructor
  302. */
  303. static void snd_hda_codec_free(struct hda_codec *codec);
  304. static int snd_hda_bus_free(struct hda_bus *bus)
  305. {
  306. struct hda_codec *codec, *n;
  307. if (!bus)
  308. return 0;
  309. if (bus->unsol) {
  310. flush_scheduled_work();
  311. kfree(bus->unsol);
  312. }
  313. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  314. snd_hda_codec_free(codec);
  315. }
  316. if (bus->ops.private_free)
  317. bus->ops.private_free(bus);
  318. kfree(bus);
  319. return 0;
  320. }
  321. static int snd_hda_bus_dev_free(struct snd_device *device)
  322. {
  323. struct hda_bus *bus = device->device_data;
  324. return snd_hda_bus_free(bus);
  325. }
  326. /**
  327. * snd_hda_bus_new - create a HDA bus
  328. * @card: the card entry
  329. * @temp: the template for hda_bus information
  330. * @busp: the pointer to store the created bus instance
  331. *
  332. * Returns 0 if successful, or a negative error code.
  333. */
  334. int __devinit snd_hda_bus_new(struct snd_card *card,
  335. const struct hda_bus_template *temp,
  336. struct hda_bus **busp)
  337. {
  338. struct hda_bus *bus;
  339. int err;
  340. static struct snd_device_ops dev_ops = {
  341. .dev_free = snd_hda_bus_dev_free,
  342. };
  343. snd_assert(temp, return -EINVAL);
  344. snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
  345. if (busp)
  346. *busp = NULL;
  347. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  348. if (bus == NULL) {
  349. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  350. return -ENOMEM;
  351. }
  352. bus->card = card;
  353. bus->private_data = temp->private_data;
  354. bus->pci = temp->pci;
  355. bus->modelname = temp->modelname;
  356. bus->ops = temp->ops;
  357. mutex_init(&bus->cmd_mutex);
  358. INIT_LIST_HEAD(&bus->codec_list);
  359. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  360. if (err < 0) {
  361. snd_hda_bus_free(bus);
  362. return err;
  363. }
  364. if (busp)
  365. *busp = bus;
  366. return 0;
  367. }
  368. #ifdef CONFIG_SND_HDA_GENERIC
  369. #define is_generic_config(codec) \
  370. (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
  371. #else
  372. #define is_generic_config(codec) 0
  373. #endif
  374. /*
  375. * find a matching codec preset
  376. */
  377. static const struct hda_codec_preset __devinit *
  378. find_codec_preset(struct hda_codec *codec)
  379. {
  380. const struct hda_codec_preset **tbl, *preset;
  381. if (is_generic_config(codec))
  382. return NULL; /* use the generic parser */
  383. for (tbl = hda_preset_tables; *tbl; tbl++) {
  384. for (preset = *tbl; preset->id; preset++) {
  385. u32 mask = preset->mask;
  386. if (!mask)
  387. mask = ~0;
  388. if (preset->id == (codec->vendor_id & mask) &&
  389. (!preset->rev ||
  390. preset->rev == codec->revision_id))
  391. return preset;
  392. }
  393. }
  394. return NULL;
  395. }
  396. /*
  397. * snd_hda_get_codec_name - store the codec name
  398. */
  399. void snd_hda_get_codec_name(struct hda_codec *codec,
  400. char *name, int namelen)
  401. {
  402. const struct hda_vendor_id *c;
  403. const char *vendor = NULL;
  404. u16 vendor_id = codec->vendor_id >> 16;
  405. char tmp[16];
  406. for (c = hda_vendor_ids; c->id; c++) {
  407. if (c->id == vendor_id) {
  408. vendor = c->name;
  409. break;
  410. }
  411. }
  412. if (!vendor) {
  413. sprintf(tmp, "Generic %04x", vendor_id);
  414. vendor = tmp;
  415. }
  416. if (codec->preset && codec->preset->name)
  417. snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
  418. else
  419. snprintf(name, namelen, "%s ID %x", vendor,
  420. codec->vendor_id & 0xffff);
  421. }
  422. /*
  423. * look for an AFG and MFG nodes
  424. */
  425. static void __devinit setup_fg_nodes(struct hda_codec *codec)
  426. {
  427. int i, total_nodes;
  428. hda_nid_t nid;
  429. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  430. for (i = 0; i < total_nodes; i++, nid++) {
  431. unsigned int func;
  432. func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
  433. switch (func & 0xff) {
  434. case AC_GRP_AUDIO_FUNCTION:
  435. codec->afg = nid;
  436. break;
  437. case AC_GRP_MODEM_FUNCTION:
  438. codec->mfg = nid;
  439. break;
  440. default:
  441. break;
  442. }
  443. }
  444. }
  445. /*
  446. * read widget caps for each widget and store in cache
  447. */
  448. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  449. {
  450. int i;
  451. hda_nid_t nid;
  452. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  453. &codec->start_nid);
  454. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  455. if (!codec->wcaps)
  456. return -ENOMEM;
  457. nid = codec->start_nid;
  458. for (i = 0; i < codec->num_nodes; i++, nid++)
  459. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  460. AC_PAR_AUDIO_WIDGET_CAP);
  461. return 0;
  462. }
  463. static void init_hda_cache(struct hda_cache_rec *cache,
  464. unsigned int record_size);
  465. static void free_hda_cache(struct hda_cache_rec *cache);
  466. /*
  467. * codec destructor
  468. */
  469. static void snd_hda_codec_free(struct hda_codec *codec)
  470. {
  471. if (!codec)
  472. return;
  473. #ifdef CONFIG_SND_HDA_POWER_SAVE
  474. cancel_delayed_work(&codec->power_work);
  475. flush_scheduled_work();
  476. #endif
  477. list_del(&codec->list);
  478. codec->bus->caddr_tbl[codec->addr] = NULL;
  479. if (codec->patch_ops.free)
  480. codec->patch_ops.free(codec);
  481. free_hda_cache(&codec->amp_cache);
  482. free_hda_cache(&codec->cmd_cache);
  483. kfree(codec->wcaps);
  484. kfree(codec);
  485. }
  486. /**
  487. * snd_hda_codec_new - create a HDA codec
  488. * @bus: the bus to assign
  489. * @codec_addr: the codec address
  490. * @codecp: the pointer to store the generated codec
  491. *
  492. * Returns 0 if successful, or a negative error code.
  493. */
  494. int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  495. struct hda_codec **codecp)
  496. {
  497. struct hda_codec *codec;
  498. char component[13];
  499. int err;
  500. snd_assert(bus, return -EINVAL);
  501. snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
  502. if (bus->caddr_tbl[codec_addr]) {
  503. snd_printk(KERN_ERR "hda_codec: "
  504. "address 0x%x is already occupied\n", codec_addr);
  505. return -EBUSY;
  506. }
  507. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  508. if (codec == NULL) {
  509. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  510. return -ENOMEM;
  511. }
  512. codec->bus = bus;
  513. codec->addr = codec_addr;
  514. mutex_init(&codec->spdif_mutex);
  515. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  516. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  517. #ifdef CONFIG_SND_HDA_POWER_SAVE
  518. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  519. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  520. * the caller has to power down appropriatley after initialization
  521. * phase.
  522. */
  523. hda_keep_power_on(codec);
  524. #endif
  525. list_add_tail(&codec->list, &bus->codec_list);
  526. bus->caddr_tbl[codec_addr] = codec;
  527. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  528. AC_PAR_VENDOR_ID);
  529. if (codec->vendor_id == -1)
  530. /* read again, hopefully the access method was corrected
  531. * in the last read...
  532. */
  533. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  534. AC_PAR_VENDOR_ID);
  535. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  536. AC_PAR_SUBSYSTEM_ID);
  537. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  538. AC_PAR_REV_ID);
  539. setup_fg_nodes(codec);
  540. if (!codec->afg && !codec->mfg) {
  541. snd_printdd("hda_codec: no AFG or MFG node found\n");
  542. snd_hda_codec_free(codec);
  543. return -ENODEV;
  544. }
  545. if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
  546. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  547. snd_hda_codec_free(codec);
  548. return -ENOMEM;
  549. }
  550. if (!codec->subsystem_id) {
  551. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  552. codec->subsystem_id =
  553. snd_hda_codec_read(codec, nid, 0,
  554. AC_VERB_GET_SUBSYSTEM_ID, 0);
  555. }
  556. codec->preset = find_codec_preset(codec);
  557. /* audio codec should override the mixer name */
  558. if (codec->afg || !*bus->card->mixername)
  559. snd_hda_get_codec_name(codec, bus->card->mixername,
  560. sizeof(bus->card->mixername));
  561. if (is_generic_config(codec)) {
  562. err = snd_hda_parse_generic_codec(codec);
  563. goto patched;
  564. }
  565. if (codec->preset && codec->preset->patch) {
  566. err = codec->preset->patch(codec);
  567. goto patched;
  568. }
  569. /* call the default parser */
  570. err = snd_hda_parse_generic_codec(codec);
  571. if (err < 0)
  572. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  573. patched:
  574. if (err < 0) {
  575. snd_hda_codec_free(codec);
  576. return err;
  577. }
  578. if (codec->patch_ops.unsol_event)
  579. init_unsol_queue(bus);
  580. snd_hda_codec_proc_new(codec);
  581. #ifdef CONFIG_SND_HDA_HWDEP
  582. snd_hda_create_hwdep(codec);
  583. #endif
  584. sprintf(component, "HDA:%08x", codec->vendor_id);
  585. snd_component_add(codec->bus->card, component);
  586. if (codecp)
  587. *codecp = codec;
  588. return 0;
  589. }
  590. /**
  591. * snd_hda_codec_setup_stream - set up the codec for streaming
  592. * @codec: the CODEC to set up
  593. * @nid: the NID to set up
  594. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  595. * @channel_id: channel id to pass, zero based.
  596. * @format: stream format.
  597. */
  598. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  599. u32 stream_tag,
  600. int channel_id, int format)
  601. {
  602. if (!nid)
  603. return;
  604. snd_printdd("hda_codec_setup_stream: "
  605. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  606. nid, stream_tag, channel_id, format);
  607. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  608. (stream_tag << 4) | channel_id);
  609. msleep(1);
  610. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  611. }
  612. /*
  613. * amp access functions
  614. */
  615. /* FIXME: more better hash key? */
  616. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  617. #define INFO_AMP_CAPS (1<<0)
  618. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  619. /* initialize the hash table */
  620. static void __devinit init_hda_cache(struct hda_cache_rec *cache,
  621. unsigned int record_size)
  622. {
  623. memset(cache, 0, sizeof(*cache));
  624. memset(cache->hash, 0xff, sizeof(cache->hash));
  625. cache->record_size = record_size;
  626. }
  627. static void free_hda_cache(struct hda_cache_rec *cache)
  628. {
  629. kfree(cache->buffer);
  630. }
  631. /* query the hash. allocate an entry if not found. */
  632. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  633. u32 key)
  634. {
  635. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  636. u16 cur = cache->hash[idx];
  637. struct hda_cache_head *info;
  638. while (cur != 0xffff) {
  639. info = (struct hda_cache_head *)(cache->buffer +
  640. cur * cache->record_size);
  641. if (info->key == key)
  642. return info;
  643. cur = info->next;
  644. }
  645. /* add a new hash entry */
  646. if (cache->num_entries >= cache->size) {
  647. /* reallocate the array */
  648. unsigned int new_size = cache->size + 64;
  649. void *new_buffer;
  650. new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
  651. if (!new_buffer) {
  652. snd_printk(KERN_ERR "hda_codec: "
  653. "can't malloc amp_info\n");
  654. return NULL;
  655. }
  656. if (cache->buffer) {
  657. memcpy(new_buffer, cache->buffer,
  658. cache->size * cache->record_size);
  659. kfree(cache->buffer);
  660. }
  661. cache->size = new_size;
  662. cache->buffer = new_buffer;
  663. }
  664. cur = cache->num_entries++;
  665. info = (struct hda_cache_head *)(cache->buffer +
  666. cur * cache->record_size);
  667. info->key = key;
  668. info->val = 0;
  669. info->next = cache->hash[idx];
  670. cache->hash[idx] = cur;
  671. return info;
  672. }
  673. /* query and allocate an amp hash entry */
  674. static inline struct hda_amp_info *
  675. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  676. {
  677. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  678. }
  679. /*
  680. * query AMP capabilities for the given widget and direction
  681. */
  682. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  683. {
  684. struct hda_amp_info *info;
  685. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  686. if (!info)
  687. return 0;
  688. if (!(info->head.val & INFO_AMP_CAPS)) {
  689. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  690. nid = codec->afg;
  691. info->amp_caps = snd_hda_param_read(codec, nid,
  692. direction == HDA_OUTPUT ?
  693. AC_PAR_AMP_OUT_CAP :
  694. AC_PAR_AMP_IN_CAP);
  695. if (info->amp_caps)
  696. info->head.val |= INFO_AMP_CAPS;
  697. }
  698. return info->amp_caps;
  699. }
  700. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  701. unsigned int caps)
  702. {
  703. struct hda_amp_info *info;
  704. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  705. if (!info)
  706. return -EINVAL;
  707. info->amp_caps = caps;
  708. info->head.val |= INFO_AMP_CAPS;
  709. return 0;
  710. }
  711. /*
  712. * read the current volume to info
  713. * if the cache exists, read the cache value.
  714. */
  715. static unsigned int get_vol_mute(struct hda_codec *codec,
  716. struct hda_amp_info *info, hda_nid_t nid,
  717. int ch, int direction, int index)
  718. {
  719. u32 val, parm;
  720. if (info->head.val & INFO_AMP_VOL(ch))
  721. return info->vol[ch];
  722. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  723. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  724. parm |= index;
  725. val = snd_hda_codec_read(codec, nid, 0,
  726. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  727. info->vol[ch] = val & 0xff;
  728. info->head.val |= INFO_AMP_VOL(ch);
  729. return info->vol[ch];
  730. }
  731. /*
  732. * write the current volume in info to the h/w and update the cache
  733. */
  734. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  735. hda_nid_t nid, int ch, int direction, int index,
  736. int val)
  737. {
  738. u32 parm;
  739. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  740. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  741. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  742. parm |= val;
  743. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  744. info->vol[ch] = val;
  745. }
  746. /*
  747. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  748. */
  749. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  750. int direction, int index)
  751. {
  752. struct hda_amp_info *info;
  753. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  754. if (!info)
  755. return 0;
  756. return get_vol_mute(codec, info, nid, ch, direction, index);
  757. }
  758. /*
  759. * update the AMP value, mask = bit mask to set, val = the value
  760. */
  761. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  762. int direction, int idx, int mask, int val)
  763. {
  764. struct hda_amp_info *info;
  765. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  766. if (!info)
  767. return 0;
  768. val &= mask;
  769. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  770. if (info->vol[ch] == val)
  771. return 0;
  772. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  773. return 1;
  774. }
  775. /*
  776. * update the AMP stereo with the same mask and value
  777. */
  778. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  779. int direction, int idx, int mask, int val)
  780. {
  781. int ch, ret = 0;
  782. for (ch = 0; ch < 2; ch++)
  783. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  784. idx, mask, val);
  785. return ret;
  786. }
  787. #ifdef SND_HDA_NEEDS_RESUME
  788. /* resume the all amp commands from the cache */
  789. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  790. {
  791. struct hda_amp_info *buffer = codec->amp_cache.buffer;
  792. int i;
  793. for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
  794. u32 key = buffer->head.key;
  795. hda_nid_t nid;
  796. unsigned int idx, dir, ch;
  797. if (!key)
  798. continue;
  799. nid = key & 0xff;
  800. idx = (key >> 16) & 0xff;
  801. dir = (key >> 24) & 0xff;
  802. for (ch = 0; ch < 2; ch++) {
  803. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  804. continue;
  805. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  806. buffer->vol[ch]);
  807. }
  808. }
  809. }
  810. #endif /* SND_HDA_NEEDS_RESUME */
  811. /*
  812. * AMP control callbacks
  813. */
  814. /* retrieve parameters from private_value */
  815. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  816. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  817. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  818. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  819. /* volume */
  820. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  821. struct snd_ctl_elem_info *uinfo)
  822. {
  823. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  824. u16 nid = get_amp_nid(kcontrol);
  825. u8 chs = get_amp_channels(kcontrol);
  826. int dir = get_amp_direction(kcontrol);
  827. u32 caps;
  828. caps = query_amp_caps(codec, nid, dir);
  829. /* num steps */
  830. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  831. if (!caps) {
  832. printk(KERN_WARNING "hda_codec: "
  833. "num_steps = 0 for NID=0x%x\n", nid);
  834. return -EINVAL;
  835. }
  836. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  837. uinfo->count = chs == 3 ? 2 : 1;
  838. uinfo->value.integer.min = 0;
  839. uinfo->value.integer.max = caps;
  840. return 0;
  841. }
  842. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  843. struct snd_ctl_elem_value *ucontrol)
  844. {
  845. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  846. hda_nid_t nid = get_amp_nid(kcontrol);
  847. int chs = get_amp_channels(kcontrol);
  848. int dir = get_amp_direction(kcontrol);
  849. int idx = get_amp_index(kcontrol);
  850. long *valp = ucontrol->value.integer.value;
  851. if (chs & 1)
  852. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
  853. & HDA_AMP_VOLMASK;
  854. if (chs & 2)
  855. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
  856. & HDA_AMP_VOLMASK;
  857. return 0;
  858. }
  859. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  860. struct snd_ctl_elem_value *ucontrol)
  861. {
  862. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  863. hda_nid_t nid = get_amp_nid(kcontrol);
  864. int chs = get_amp_channels(kcontrol);
  865. int dir = get_amp_direction(kcontrol);
  866. int idx = get_amp_index(kcontrol);
  867. long *valp = ucontrol->value.integer.value;
  868. int change = 0;
  869. snd_hda_power_up(codec);
  870. if (chs & 1) {
  871. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  872. 0x7f, *valp);
  873. valp++;
  874. }
  875. if (chs & 2)
  876. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  877. 0x7f, *valp);
  878. snd_hda_power_down(codec);
  879. return change;
  880. }
  881. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  882. unsigned int size, unsigned int __user *_tlv)
  883. {
  884. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  885. hda_nid_t nid = get_amp_nid(kcontrol);
  886. int dir = get_amp_direction(kcontrol);
  887. u32 caps, val1, val2;
  888. if (size < 4 * sizeof(unsigned int))
  889. return -ENOMEM;
  890. caps = query_amp_caps(codec, nid, dir);
  891. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  892. val2 = (val2 + 1) * 25;
  893. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  894. val1 = ((int)val1) * ((int)val2);
  895. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  896. return -EFAULT;
  897. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  898. return -EFAULT;
  899. if (put_user(val1, _tlv + 2))
  900. return -EFAULT;
  901. if (put_user(val2, _tlv + 3))
  902. return -EFAULT;
  903. return 0;
  904. }
  905. /* switch */
  906. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  907. struct snd_ctl_elem_info *uinfo)
  908. {
  909. int chs = get_amp_channels(kcontrol);
  910. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  911. uinfo->count = chs == 3 ? 2 : 1;
  912. uinfo->value.integer.min = 0;
  913. uinfo->value.integer.max = 1;
  914. return 0;
  915. }
  916. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  917. struct snd_ctl_elem_value *ucontrol)
  918. {
  919. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  920. hda_nid_t nid = get_amp_nid(kcontrol);
  921. int chs = get_amp_channels(kcontrol);
  922. int dir = get_amp_direction(kcontrol);
  923. int idx = get_amp_index(kcontrol);
  924. long *valp = ucontrol->value.integer.value;
  925. if (chs & 1)
  926. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  927. HDA_AMP_MUTE) ? 0 : 1;
  928. if (chs & 2)
  929. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  930. HDA_AMP_MUTE) ? 0 : 1;
  931. return 0;
  932. }
  933. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  934. struct snd_ctl_elem_value *ucontrol)
  935. {
  936. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  937. hda_nid_t nid = get_amp_nid(kcontrol);
  938. int chs = get_amp_channels(kcontrol);
  939. int dir = get_amp_direction(kcontrol);
  940. int idx = get_amp_index(kcontrol);
  941. long *valp = ucontrol->value.integer.value;
  942. int change = 0;
  943. snd_hda_power_up(codec);
  944. if (chs & 1) {
  945. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  946. HDA_AMP_MUTE,
  947. *valp ? 0 : HDA_AMP_MUTE);
  948. valp++;
  949. }
  950. if (chs & 2)
  951. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  952. HDA_AMP_MUTE,
  953. *valp ? 0 : HDA_AMP_MUTE);
  954. #ifdef CONFIG_SND_HDA_POWER_SAVE
  955. if (codec->patch_ops.check_power_status)
  956. codec->patch_ops.check_power_status(codec, nid);
  957. #endif
  958. snd_hda_power_down(codec);
  959. return change;
  960. }
  961. /*
  962. * bound volume controls
  963. *
  964. * bind multiple volumes (# indices, from 0)
  965. */
  966. #define AMP_VAL_IDX_SHIFT 19
  967. #define AMP_VAL_IDX_MASK (0x0f<<19)
  968. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  969. struct snd_ctl_elem_value *ucontrol)
  970. {
  971. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  972. unsigned long pval;
  973. int err;
  974. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  975. pval = kcontrol->private_value;
  976. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  977. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  978. kcontrol->private_value = pval;
  979. mutex_unlock(&codec->spdif_mutex);
  980. return err;
  981. }
  982. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  983. struct snd_ctl_elem_value *ucontrol)
  984. {
  985. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  986. unsigned long pval;
  987. int i, indices, err = 0, change = 0;
  988. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  989. pval = kcontrol->private_value;
  990. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  991. for (i = 0; i < indices; i++) {
  992. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  993. (i << AMP_VAL_IDX_SHIFT);
  994. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  995. if (err < 0)
  996. break;
  997. change |= err;
  998. }
  999. kcontrol->private_value = pval;
  1000. mutex_unlock(&codec->spdif_mutex);
  1001. return err < 0 ? err : change;
  1002. }
  1003. /*
  1004. * generic bound volume/swtich controls
  1005. */
  1006. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  1007. struct snd_ctl_elem_info *uinfo)
  1008. {
  1009. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1010. struct hda_bind_ctls *c;
  1011. int err;
  1012. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1013. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1014. kcontrol->private_value = *c->values;
  1015. err = c->ops->info(kcontrol, uinfo);
  1016. kcontrol->private_value = (long)c;
  1017. mutex_unlock(&codec->spdif_mutex);
  1018. return err;
  1019. }
  1020. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  1021. struct snd_ctl_elem_value *ucontrol)
  1022. {
  1023. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1024. struct hda_bind_ctls *c;
  1025. int err;
  1026. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1027. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1028. kcontrol->private_value = *c->values;
  1029. err = c->ops->get(kcontrol, ucontrol);
  1030. kcontrol->private_value = (long)c;
  1031. mutex_unlock(&codec->spdif_mutex);
  1032. return err;
  1033. }
  1034. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  1035. struct snd_ctl_elem_value *ucontrol)
  1036. {
  1037. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1038. struct hda_bind_ctls *c;
  1039. unsigned long *vals;
  1040. int err = 0, change = 0;
  1041. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1042. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1043. for (vals = c->values; *vals; vals++) {
  1044. kcontrol->private_value = *vals;
  1045. err = c->ops->put(kcontrol, ucontrol);
  1046. if (err < 0)
  1047. break;
  1048. change |= err;
  1049. }
  1050. kcontrol->private_value = (long)c;
  1051. mutex_unlock(&codec->spdif_mutex);
  1052. return err < 0 ? err : change;
  1053. }
  1054. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1055. unsigned int size, unsigned int __user *tlv)
  1056. {
  1057. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1058. struct hda_bind_ctls *c;
  1059. int err;
  1060. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1061. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1062. kcontrol->private_value = *c->values;
  1063. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  1064. kcontrol->private_value = (long)c;
  1065. mutex_unlock(&codec->spdif_mutex);
  1066. return err;
  1067. }
  1068. struct hda_ctl_ops snd_hda_bind_vol = {
  1069. .info = snd_hda_mixer_amp_volume_info,
  1070. .get = snd_hda_mixer_amp_volume_get,
  1071. .put = snd_hda_mixer_amp_volume_put,
  1072. .tlv = snd_hda_mixer_amp_tlv
  1073. };
  1074. struct hda_ctl_ops snd_hda_bind_sw = {
  1075. .info = snd_hda_mixer_amp_switch_info,
  1076. .get = snd_hda_mixer_amp_switch_get,
  1077. .put = snd_hda_mixer_amp_switch_put,
  1078. .tlv = snd_hda_mixer_amp_tlv
  1079. };
  1080. /*
  1081. * SPDIF out controls
  1082. */
  1083. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1084. struct snd_ctl_elem_info *uinfo)
  1085. {
  1086. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1087. uinfo->count = 1;
  1088. return 0;
  1089. }
  1090. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1091. struct snd_ctl_elem_value *ucontrol)
  1092. {
  1093. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1094. IEC958_AES0_NONAUDIO |
  1095. IEC958_AES0_CON_EMPHASIS_5015 |
  1096. IEC958_AES0_CON_NOT_COPYRIGHT;
  1097. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1098. IEC958_AES1_CON_ORIGINAL;
  1099. return 0;
  1100. }
  1101. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1102. struct snd_ctl_elem_value *ucontrol)
  1103. {
  1104. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1105. IEC958_AES0_NONAUDIO |
  1106. IEC958_AES0_PRO_EMPHASIS_5015;
  1107. return 0;
  1108. }
  1109. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1110. struct snd_ctl_elem_value *ucontrol)
  1111. {
  1112. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1113. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  1114. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  1115. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  1116. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  1117. return 0;
  1118. }
  1119. /* convert from SPDIF status bits to HDA SPDIF bits
  1120. * bit 0 (DigEn) is always set zero (to be filled later)
  1121. */
  1122. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1123. {
  1124. unsigned short val = 0;
  1125. if (sbits & IEC958_AES0_PROFESSIONAL)
  1126. val |= AC_DIG1_PROFESSIONAL;
  1127. if (sbits & IEC958_AES0_NONAUDIO)
  1128. val |= AC_DIG1_NONAUDIO;
  1129. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1130. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1131. IEC958_AES0_PRO_EMPHASIS_5015)
  1132. val |= AC_DIG1_EMPHASIS;
  1133. } else {
  1134. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1135. IEC958_AES0_CON_EMPHASIS_5015)
  1136. val |= AC_DIG1_EMPHASIS;
  1137. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1138. val |= AC_DIG1_COPYRIGHT;
  1139. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1140. val |= AC_DIG1_LEVEL;
  1141. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1142. }
  1143. return val;
  1144. }
  1145. /* convert to SPDIF status bits from HDA SPDIF bits
  1146. */
  1147. static unsigned int convert_to_spdif_status(unsigned short val)
  1148. {
  1149. unsigned int sbits = 0;
  1150. if (val & AC_DIG1_NONAUDIO)
  1151. sbits |= IEC958_AES0_NONAUDIO;
  1152. if (val & AC_DIG1_PROFESSIONAL)
  1153. sbits |= IEC958_AES0_PROFESSIONAL;
  1154. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1155. if (sbits & AC_DIG1_EMPHASIS)
  1156. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1157. } else {
  1158. if (val & AC_DIG1_EMPHASIS)
  1159. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1160. if (!(val & AC_DIG1_COPYRIGHT))
  1161. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1162. if (val & AC_DIG1_LEVEL)
  1163. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1164. sbits |= val & (0x7f << 8);
  1165. }
  1166. return sbits;
  1167. }
  1168. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  1169. struct snd_ctl_elem_value *ucontrol)
  1170. {
  1171. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1172. hda_nid_t nid = kcontrol->private_value;
  1173. unsigned short val;
  1174. int change;
  1175. mutex_lock(&codec->spdif_mutex);
  1176. codec->spdif_status = ucontrol->value.iec958.status[0] |
  1177. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  1178. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  1179. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  1180. val = convert_from_spdif_status(codec->spdif_status);
  1181. val |= codec->spdif_ctls & 1;
  1182. change = codec->spdif_ctls != val;
  1183. codec->spdif_ctls = val;
  1184. if (change) {
  1185. snd_hda_codec_write_cache(codec, nid, 0,
  1186. AC_VERB_SET_DIGI_CONVERT_1,
  1187. val & 0xff);
  1188. snd_hda_codec_write_cache(codec, nid, 0,
  1189. AC_VERB_SET_DIGI_CONVERT_2,
  1190. val >> 8);
  1191. }
  1192. mutex_unlock(&codec->spdif_mutex);
  1193. return change;
  1194. }
  1195. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1196. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1197. struct snd_ctl_elem_value *ucontrol)
  1198. {
  1199. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1200. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1201. return 0;
  1202. }
  1203. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1204. struct snd_ctl_elem_value *ucontrol)
  1205. {
  1206. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1207. hda_nid_t nid = kcontrol->private_value;
  1208. unsigned short val;
  1209. int change;
  1210. mutex_lock(&codec->spdif_mutex);
  1211. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1212. if (ucontrol->value.integer.value[0])
  1213. val |= AC_DIG1_ENABLE;
  1214. change = codec->spdif_ctls != val;
  1215. if (change) {
  1216. codec->spdif_ctls = val;
  1217. snd_hda_codec_write_cache(codec, nid, 0,
  1218. AC_VERB_SET_DIGI_CONVERT_1,
  1219. val & 0xff);
  1220. /* unmute amp switch (if any) */
  1221. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1222. (val & AC_DIG1_ENABLE))
  1223. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1224. HDA_AMP_MUTE, 0);
  1225. }
  1226. mutex_unlock(&codec->spdif_mutex);
  1227. return change;
  1228. }
  1229. static struct snd_kcontrol_new dig_mixes[] = {
  1230. {
  1231. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1232. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1233. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1234. .info = snd_hda_spdif_mask_info,
  1235. .get = snd_hda_spdif_cmask_get,
  1236. },
  1237. {
  1238. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1239. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1240. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1241. .info = snd_hda_spdif_mask_info,
  1242. .get = snd_hda_spdif_pmask_get,
  1243. },
  1244. {
  1245. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1246. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1247. .info = snd_hda_spdif_mask_info,
  1248. .get = snd_hda_spdif_default_get,
  1249. .put = snd_hda_spdif_default_put,
  1250. },
  1251. {
  1252. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1253. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1254. .info = snd_hda_spdif_out_switch_info,
  1255. .get = snd_hda_spdif_out_switch_get,
  1256. .put = snd_hda_spdif_out_switch_put,
  1257. },
  1258. { } /* end */
  1259. };
  1260. /**
  1261. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1262. * @codec: the HDA codec
  1263. * @nid: audio out widget NID
  1264. *
  1265. * Creates controls related with the SPDIF output.
  1266. * Called from each patch supporting the SPDIF out.
  1267. *
  1268. * Returns 0 if successful, or a negative error code.
  1269. */
  1270. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1271. {
  1272. int err;
  1273. struct snd_kcontrol *kctl;
  1274. struct snd_kcontrol_new *dig_mix;
  1275. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1276. kctl = snd_ctl_new1(dig_mix, codec);
  1277. kctl->private_value = nid;
  1278. err = snd_ctl_add(codec->bus->card, kctl);
  1279. if (err < 0)
  1280. return err;
  1281. }
  1282. codec->spdif_ctls =
  1283. snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1284. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1285. return 0;
  1286. }
  1287. /*
  1288. * SPDIF input
  1289. */
  1290. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1291. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1292. struct snd_ctl_elem_value *ucontrol)
  1293. {
  1294. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1295. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1296. return 0;
  1297. }
  1298. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1299. struct snd_ctl_elem_value *ucontrol)
  1300. {
  1301. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1302. hda_nid_t nid = kcontrol->private_value;
  1303. unsigned int val = !!ucontrol->value.integer.value[0];
  1304. int change;
  1305. mutex_lock(&codec->spdif_mutex);
  1306. change = codec->spdif_in_enable != val;
  1307. if (change) {
  1308. codec->spdif_in_enable = val;
  1309. snd_hda_codec_write_cache(codec, nid, 0,
  1310. AC_VERB_SET_DIGI_CONVERT_1, val);
  1311. }
  1312. mutex_unlock(&codec->spdif_mutex);
  1313. return change;
  1314. }
  1315. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1316. struct snd_ctl_elem_value *ucontrol)
  1317. {
  1318. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1319. hda_nid_t nid = kcontrol->private_value;
  1320. unsigned short val;
  1321. unsigned int sbits;
  1322. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1323. sbits = convert_to_spdif_status(val);
  1324. ucontrol->value.iec958.status[0] = sbits;
  1325. ucontrol->value.iec958.status[1] = sbits >> 8;
  1326. ucontrol->value.iec958.status[2] = sbits >> 16;
  1327. ucontrol->value.iec958.status[3] = sbits >> 24;
  1328. return 0;
  1329. }
  1330. static struct snd_kcontrol_new dig_in_ctls[] = {
  1331. {
  1332. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1333. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1334. .info = snd_hda_spdif_in_switch_info,
  1335. .get = snd_hda_spdif_in_switch_get,
  1336. .put = snd_hda_spdif_in_switch_put,
  1337. },
  1338. {
  1339. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1340. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1341. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1342. .info = snd_hda_spdif_mask_info,
  1343. .get = snd_hda_spdif_in_status_get,
  1344. },
  1345. { } /* end */
  1346. };
  1347. /**
  1348. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1349. * @codec: the HDA codec
  1350. * @nid: audio in widget NID
  1351. *
  1352. * Creates controls related with the SPDIF input.
  1353. * Called from each patch supporting the SPDIF in.
  1354. *
  1355. * Returns 0 if successful, or a negative error code.
  1356. */
  1357. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1358. {
  1359. int err;
  1360. struct snd_kcontrol *kctl;
  1361. struct snd_kcontrol_new *dig_mix;
  1362. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1363. kctl = snd_ctl_new1(dig_mix, codec);
  1364. kctl->private_value = nid;
  1365. err = snd_ctl_add(codec->bus->card, kctl);
  1366. if (err < 0)
  1367. return err;
  1368. }
  1369. codec->spdif_in_enable =
  1370. snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
  1371. AC_DIG1_ENABLE;
  1372. return 0;
  1373. }
  1374. #ifdef SND_HDA_NEEDS_RESUME
  1375. /*
  1376. * command cache
  1377. */
  1378. /* build a 32bit cache key with the widget id and the command parameter */
  1379. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1380. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1381. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1382. /**
  1383. * snd_hda_codec_write_cache - send a single command with caching
  1384. * @codec: the HDA codec
  1385. * @nid: NID to send the command
  1386. * @direct: direct flag
  1387. * @verb: the verb to send
  1388. * @parm: the parameter for the verb
  1389. *
  1390. * Send a single command without waiting for response.
  1391. *
  1392. * Returns 0 if successful, or a negative error code.
  1393. */
  1394. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1395. int direct, unsigned int verb, unsigned int parm)
  1396. {
  1397. int err;
  1398. snd_hda_power_up(codec);
  1399. mutex_lock(&codec->bus->cmd_mutex);
  1400. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  1401. if (!err) {
  1402. struct hda_cache_head *c;
  1403. u32 key = build_cmd_cache_key(nid, verb);
  1404. c = get_alloc_hash(&codec->cmd_cache, key);
  1405. if (c)
  1406. c->val = parm;
  1407. }
  1408. mutex_unlock(&codec->bus->cmd_mutex);
  1409. snd_hda_power_down(codec);
  1410. return err;
  1411. }
  1412. /* resume the all commands from the cache */
  1413. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1414. {
  1415. struct hda_cache_head *buffer = codec->cmd_cache.buffer;
  1416. int i;
  1417. for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
  1418. u32 key = buffer->key;
  1419. if (!key)
  1420. continue;
  1421. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1422. get_cmd_cache_cmd(key), buffer->val);
  1423. }
  1424. }
  1425. /**
  1426. * snd_hda_sequence_write_cache - sequence writes with caching
  1427. * @codec: the HDA codec
  1428. * @seq: VERB array to send
  1429. *
  1430. * Send the commands sequentially from the given array.
  1431. * Thte commands are recorded on cache for power-save and resume.
  1432. * The array must be terminated with NID=0.
  1433. */
  1434. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1435. const struct hda_verb *seq)
  1436. {
  1437. for (; seq->nid; seq++)
  1438. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1439. seq->param);
  1440. }
  1441. #endif /* SND_HDA_NEEDS_RESUME */
  1442. /*
  1443. * set power state of the codec
  1444. */
  1445. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1446. unsigned int power_state)
  1447. {
  1448. hda_nid_t nid;
  1449. int i;
  1450. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1451. power_state);
  1452. nid = codec->start_nid;
  1453. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1454. unsigned int wcaps = get_wcaps(codec, nid);
  1455. if (wcaps & AC_WCAP_POWER) {
  1456. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1457. AC_WCAP_TYPE_SHIFT;
  1458. if (wid_type == AC_WID_PIN) {
  1459. unsigned int pincap;
  1460. /*
  1461. * don't power down the widget if it controls
  1462. * eapd and EAPD_BTLENABLE is set.
  1463. */
  1464. pincap = snd_hda_param_read(codec, nid,
  1465. AC_PAR_PIN_CAP);
  1466. if (pincap & AC_PINCAP_EAPD) {
  1467. int eapd = snd_hda_codec_read(codec,
  1468. nid, 0,
  1469. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1470. eapd &= 0x02;
  1471. if (power_state == AC_PWRST_D3 && eapd)
  1472. continue;
  1473. }
  1474. }
  1475. snd_hda_codec_write(codec, nid, 0,
  1476. AC_VERB_SET_POWER_STATE,
  1477. power_state);
  1478. }
  1479. }
  1480. if (power_state == AC_PWRST_D0) {
  1481. unsigned long end_time;
  1482. int state;
  1483. msleep(10);
  1484. /* wait until the codec reachs to D0 */
  1485. end_time = jiffies + msecs_to_jiffies(500);
  1486. do {
  1487. state = snd_hda_codec_read(codec, fg, 0,
  1488. AC_VERB_GET_POWER_STATE, 0);
  1489. if (state == power_state)
  1490. break;
  1491. msleep(1);
  1492. } while (time_after_eq(end_time, jiffies));
  1493. }
  1494. }
  1495. #ifdef SND_HDA_NEEDS_RESUME
  1496. /*
  1497. * call suspend and power-down; used both from PM and power-save
  1498. */
  1499. static void hda_call_codec_suspend(struct hda_codec *codec)
  1500. {
  1501. if (codec->patch_ops.suspend)
  1502. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1503. hda_set_power_state(codec,
  1504. codec->afg ? codec->afg : codec->mfg,
  1505. AC_PWRST_D3);
  1506. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1507. cancel_delayed_work(&codec->power_work);
  1508. codec->power_on = 0;
  1509. codec->power_transition = 0;
  1510. #endif
  1511. }
  1512. /*
  1513. * kick up codec; used both from PM and power-save
  1514. */
  1515. static void hda_call_codec_resume(struct hda_codec *codec)
  1516. {
  1517. hda_set_power_state(codec,
  1518. codec->afg ? codec->afg : codec->mfg,
  1519. AC_PWRST_D0);
  1520. if (codec->patch_ops.resume)
  1521. codec->patch_ops.resume(codec);
  1522. else {
  1523. if (codec->patch_ops.init)
  1524. codec->patch_ops.init(codec);
  1525. snd_hda_codec_resume_amp(codec);
  1526. snd_hda_codec_resume_cache(codec);
  1527. }
  1528. }
  1529. #endif /* SND_HDA_NEEDS_RESUME */
  1530. /**
  1531. * snd_hda_build_controls - build mixer controls
  1532. * @bus: the BUS
  1533. *
  1534. * Creates mixer controls for each codec included in the bus.
  1535. *
  1536. * Returns 0 if successful, otherwise a negative error code.
  1537. */
  1538. int __devinit snd_hda_build_controls(struct hda_bus *bus)
  1539. {
  1540. struct hda_codec *codec;
  1541. list_for_each_entry(codec, &bus->codec_list, list) {
  1542. int err = 0;
  1543. /* fake as if already powered-on */
  1544. hda_keep_power_on(codec);
  1545. /* then fire up */
  1546. hda_set_power_state(codec,
  1547. codec->afg ? codec->afg : codec->mfg,
  1548. AC_PWRST_D0);
  1549. /* continue to initialize... */
  1550. if (codec->patch_ops.init)
  1551. err = codec->patch_ops.init(codec);
  1552. if (!err && codec->patch_ops.build_controls)
  1553. err = codec->patch_ops.build_controls(codec);
  1554. snd_hda_power_down(codec);
  1555. if (err < 0)
  1556. return err;
  1557. }
  1558. return 0;
  1559. }
  1560. /*
  1561. * stream formats
  1562. */
  1563. struct hda_rate_tbl {
  1564. unsigned int hz;
  1565. unsigned int alsa_bits;
  1566. unsigned int hda_fmt;
  1567. };
  1568. static struct hda_rate_tbl rate_bits[] = {
  1569. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1570. /* autodetected value used in snd_hda_query_supported_pcm */
  1571. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1572. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1573. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1574. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1575. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1576. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1577. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1578. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1579. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1580. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1581. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1582. #define AC_PAR_PCM_RATE_BITS 11
  1583. /* up to bits 10, 384kHZ isn't supported properly */
  1584. /* not autodetected value */
  1585. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1586. { 0 } /* terminator */
  1587. };
  1588. /**
  1589. * snd_hda_calc_stream_format - calculate format bitset
  1590. * @rate: the sample rate
  1591. * @channels: the number of channels
  1592. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1593. * @maxbps: the max. bps
  1594. *
  1595. * Calculate the format bitset from the given rate, channels and th PCM format.
  1596. *
  1597. * Return zero if invalid.
  1598. */
  1599. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1600. unsigned int channels,
  1601. unsigned int format,
  1602. unsigned int maxbps)
  1603. {
  1604. int i;
  1605. unsigned int val = 0;
  1606. for (i = 0; rate_bits[i].hz; i++)
  1607. if (rate_bits[i].hz == rate) {
  1608. val = rate_bits[i].hda_fmt;
  1609. break;
  1610. }
  1611. if (!rate_bits[i].hz) {
  1612. snd_printdd("invalid rate %d\n", rate);
  1613. return 0;
  1614. }
  1615. if (channels == 0 || channels > 8) {
  1616. snd_printdd("invalid channels %d\n", channels);
  1617. return 0;
  1618. }
  1619. val |= channels - 1;
  1620. switch (snd_pcm_format_width(format)) {
  1621. case 8: val |= 0x00; break;
  1622. case 16: val |= 0x10; break;
  1623. case 20:
  1624. case 24:
  1625. case 32:
  1626. if (maxbps >= 32)
  1627. val |= 0x40;
  1628. else if (maxbps >= 24)
  1629. val |= 0x30;
  1630. else
  1631. val |= 0x20;
  1632. break;
  1633. default:
  1634. snd_printdd("invalid format width %d\n",
  1635. snd_pcm_format_width(format));
  1636. return 0;
  1637. }
  1638. return val;
  1639. }
  1640. /**
  1641. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1642. * @codec: the HDA codec
  1643. * @nid: NID to query
  1644. * @ratesp: the pointer to store the detected rate bitflags
  1645. * @formatsp: the pointer to store the detected formats
  1646. * @bpsp: the pointer to store the detected format widths
  1647. *
  1648. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1649. * or @bsps argument is ignored.
  1650. *
  1651. * Returns 0 if successful, otherwise a negative error code.
  1652. */
  1653. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1654. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1655. {
  1656. int i;
  1657. unsigned int val, streams;
  1658. val = 0;
  1659. if (nid != codec->afg &&
  1660. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1661. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1662. if (val == -1)
  1663. return -EIO;
  1664. }
  1665. if (!val)
  1666. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1667. if (ratesp) {
  1668. u32 rates = 0;
  1669. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  1670. if (val & (1 << i))
  1671. rates |= rate_bits[i].alsa_bits;
  1672. }
  1673. *ratesp = rates;
  1674. }
  1675. if (formatsp || bpsp) {
  1676. u64 formats = 0;
  1677. unsigned int bps;
  1678. unsigned int wcaps;
  1679. wcaps = get_wcaps(codec, nid);
  1680. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1681. if (streams == -1)
  1682. return -EIO;
  1683. if (!streams) {
  1684. streams = snd_hda_param_read(codec, codec->afg,
  1685. AC_PAR_STREAM);
  1686. if (streams == -1)
  1687. return -EIO;
  1688. }
  1689. bps = 0;
  1690. if (streams & AC_SUPFMT_PCM) {
  1691. if (val & AC_SUPPCM_BITS_8) {
  1692. formats |= SNDRV_PCM_FMTBIT_U8;
  1693. bps = 8;
  1694. }
  1695. if (val & AC_SUPPCM_BITS_16) {
  1696. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1697. bps = 16;
  1698. }
  1699. if (wcaps & AC_WCAP_DIGITAL) {
  1700. if (val & AC_SUPPCM_BITS_32)
  1701. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1702. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1703. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1704. if (val & AC_SUPPCM_BITS_24)
  1705. bps = 24;
  1706. else if (val & AC_SUPPCM_BITS_20)
  1707. bps = 20;
  1708. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  1709. AC_SUPPCM_BITS_32)) {
  1710. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1711. if (val & AC_SUPPCM_BITS_32)
  1712. bps = 32;
  1713. else if (val & AC_SUPPCM_BITS_24)
  1714. bps = 24;
  1715. else if (val & AC_SUPPCM_BITS_20)
  1716. bps = 20;
  1717. }
  1718. }
  1719. else if (streams == AC_SUPFMT_FLOAT32) {
  1720. /* should be exclusive */
  1721. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1722. bps = 32;
  1723. } else if (streams == AC_SUPFMT_AC3) {
  1724. /* should be exclusive */
  1725. /* temporary hack: we have still no proper support
  1726. * for the direct AC3 stream...
  1727. */
  1728. formats |= SNDRV_PCM_FMTBIT_U8;
  1729. bps = 8;
  1730. }
  1731. if (formatsp)
  1732. *formatsp = formats;
  1733. if (bpsp)
  1734. *bpsp = bps;
  1735. }
  1736. return 0;
  1737. }
  1738. /**
  1739. * snd_hda_is_supported_format - check whether the given node supports
  1740. * the format val
  1741. *
  1742. * Returns 1 if supported, 0 if not.
  1743. */
  1744. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1745. unsigned int format)
  1746. {
  1747. int i;
  1748. unsigned int val = 0, rate, stream;
  1749. if (nid != codec->afg &&
  1750. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1751. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1752. if (val == -1)
  1753. return 0;
  1754. }
  1755. if (!val) {
  1756. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1757. if (val == -1)
  1758. return 0;
  1759. }
  1760. rate = format & 0xff00;
  1761. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  1762. if (rate_bits[i].hda_fmt == rate) {
  1763. if (val & (1 << i))
  1764. break;
  1765. return 0;
  1766. }
  1767. if (i >= AC_PAR_PCM_RATE_BITS)
  1768. return 0;
  1769. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1770. if (stream == -1)
  1771. return 0;
  1772. if (!stream && nid != codec->afg)
  1773. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1774. if (!stream || stream == -1)
  1775. return 0;
  1776. if (stream & AC_SUPFMT_PCM) {
  1777. switch (format & 0xf0) {
  1778. case 0x00:
  1779. if (!(val & AC_SUPPCM_BITS_8))
  1780. return 0;
  1781. break;
  1782. case 0x10:
  1783. if (!(val & AC_SUPPCM_BITS_16))
  1784. return 0;
  1785. break;
  1786. case 0x20:
  1787. if (!(val & AC_SUPPCM_BITS_20))
  1788. return 0;
  1789. break;
  1790. case 0x30:
  1791. if (!(val & AC_SUPPCM_BITS_24))
  1792. return 0;
  1793. break;
  1794. case 0x40:
  1795. if (!(val & AC_SUPPCM_BITS_32))
  1796. return 0;
  1797. break;
  1798. default:
  1799. return 0;
  1800. }
  1801. } else {
  1802. /* FIXME: check for float32 and AC3? */
  1803. }
  1804. return 1;
  1805. }
  1806. /*
  1807. * PCM stuff
  1808. */
  1809. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1810. struct hda_codec *codec,
  1811. struct snd_pcm_substream *substream)
  1812. {
  1813. return 0;
  1814. }
  1815. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1816. struct hda_codec *codec,
  1817. unsigned int stream_tag,
  1818. unsigned int format,
  1819. struct snd_pcm_substream *substream)
  1820. {
  1821. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1822. return 0;
  1823. }
  1824. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1825. struct hda_codec *codec,
  1826. struct snd_pcm_substream *substream)
  1827. {
  1828. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1829. return 0;
  1830. }
  1831. static int __devinit set_pcm_default_values(struct hda_codec *codec,
  1832. struct hda_pcm_stream *info)
  1833. {
  1834. /* query support PCM information from the given NID */
  1835. if (info->nid && (!info->rates || !info->formats)) {
  1836. snd_hda_query_supported_pcm(codec, info->nid,
  1837. info->rates ? NULL : &info->rates,
  1838. info->formats ? NULL : &info->formats,
  1839. info->maxbps ? NULL : &info->maxbps);
  1840. }
  1841. if (info->ops.open == NULL)
  1842. info->ops.open = hda_pcm_default_open_close;
  1843. if (info->ops.close == NULL)
  1844. info->ops.close = hda_pcm_default_open_close;
  1845. if (info->ops.prepare == NULL) {
  1846. snd_assert(info->nid, return -EINVAL);
  1847. info->ops.prepare = hda_pcm_default_prepare;
  1848. }
  1849. if (info->ops.cleanup == NULL) {
  1850. snd_assert(info->nid, return -EINVAL);
  1851. info->ops.cleanup = hda_pcm_default_cleanup;
  1852. }
  1853. return 0;
  1854. }
  1855. /**
  1856. * snd_hda_build_pcms - build PCM information
  1857. * @bus: the BUS
  1858. *
  1859. * Create PCM information for each codec included in the bus.
  1860. *
  1861. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1862. * codec->pcm_info properly. The array is referred by the top-level driver
  1863. * to create its PCM instances.
  1864. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1865. * callback.
  1866. *
  1867. * At least, substreams, channels_min and channels_max must be filled for
  1868. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1869. * When rates and/or formats are zero, the supported values are queried
  1870. * from the given nid. The nid is used also by the default ops.prepare
  1871. * and ops.cleanup callbacks.
  1872. *
  1873. * The driver needs to call ops.open in its open callback. Similarly,
  1874. * ops.close is supposed to be called in the close callback.
  1875. * ops.prepare should be called in the prepare or hw_params callback
  1876. * with the proper parameters for set up.
  1877. * ops.cleanup should be called in hw_free for clean up of streams.
  1878. *
  1879. * This function returns 0 if successfull, or a negative error code.
  1880. */
  1881. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  1882. {
  1883. struct hda_codec *codec;
  1884. list_for_each_entry(codec, &bus->codec_list, list) {
  1885. unsigned int pcm, s;
  1886. int err;
  1887. if (!codec->patch_ops.build_pcms)
  1888. continue;
  1889. err = codec->patch_ops.build_pcms(codec);
  1890. if (err < 0)
  1891. return err;
  1892. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1893. for (s = 0; s < 2; s++) {
  1894. struct hda_pcm_stream *info;
  1895. info = &codec->pcm_info[pcm].stream[s];
  1896. if (!info->substreams)
  1897. continue;
  1898. err = set_pcm_default_values(codec, info);
  1899. if (err < 0)
  1900. return err;
  1901. }
  1902. }
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * snd_hda_check_board_config - compare the current codec with the config table
  1908. * @codec: the HDA codec
  1909. * @num_configs: number of config enums
  1910. * @models: array of model name strings
  1911. * @tbl: configuration table, terminated by null entries
  1912. *
  1913. * Compares the modelname or PCI subsystem id of the current codec with the
  1914. * given configuration table. If a matching entry is found, returns its
  1915. * config value (supposed to be 0 or positive).
  1916. *
  1917. * If no entries are matching, the function returns a negative value.
  1918. */
  1919. int snd_hda_check_board_config(struct hda_codec *codec,
  1920. int num_configs, const char **models,
  1921. const struct snd_pci_quirk *tbl)
  1922. {
  1923. if (codec->bus->modelname && models) {
  1924. int i;
  1925. for (i = 0; i < num_configs; i++) {
  1926. if (models[i] &&
  1927. !strcmp(codec->bus->modelname, models[i])) {
  1928. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  1929. "selected\n", models[i]);
  1930. return i;
  1931. }
  1932. }
  1933. }
  1934. if (!codec->bus->pci || !tbl)
  1935. return -1;
  1936. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  1937. if (!tbl)
  1938. return -1;
  1939. if (tbl->value >= 0 && tbl->value < num_configs) {
  1940. #ifdef CONFIG_SND_DEBUG_DETECT
  1941. char tmp[10];
  1942. const char *model = NULL;
  1943. if (models)
  1944. model = models[tbl->value];
  1945. if (!model) {
  1946. sprintf(tmp, "#%d", tbl->value);
  1947. model = tmp;
  1948. }
  1949. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  1950. "for config %x:%x (%s)\n",
  1951. model, tbl->subvendor, tbl->subdevice,
  1952. (tbl->name ? tbl->name : "Unknown device"));
  1953. #endif
  1954. return tbl->value;
  1955. }
  1956. return -1;
  1957. }
  1958. /**
  1959. * snd_hda_add_new_ctls - create controls from the array
  1960. * @codec: the HDA codec
  1961. * @knew: the array of struct snd_kcontrol_new
  1962. *
  1963. * This helper function creates and add new controls in the given array.
  1964. * The array must be terminated with an empty entry as terminator.
  1965. *
  1966. * Returns 0 if successful, or a negative error code.
  1967. */
  1968. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  1969. {
  1970. int err;
  1971. for (; knew->name; knew++) {
  1972. struct snd_kcontrol *kctl;
  1973. kctl = snd_ctl_new1(knew, codec);
  1974. if (!kctl)
  1975. return -ENOMEM;
  1976. err = snd_ctl_add(codec->bus->card, kctl);
  1977. if (err < 0) {
  1978. if (!codec->addr)
  1979. return err;
  1980. kctl = snd_ctl_new1(knew, codec);
  1981. if (!kctl)
  1982. return -ENOMEM;
  1983. kctl->id.device = codec->addr;
  1984. err = snd_ctl_add(codec->bus->card, kctl);
  1985. if (err < 0)
  1986. return err;
  1987. }
  1988. }
  1989. return 0;
  1990. }
  1991. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1992. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1993. unsigned int power_state);
  1994. static void hda_power_work(struct work_struct *work)
  1995. {
  1996. struct hda_codec *codec =
  1997. container_of(work, struct hda_codec, power_work.work);
  1998. if (!codec->power_on || codec->power_count) {
  1999. codec->power_transition = 0;
  2000. return;
  2001. }
  2002. hda_call_codec_suspend(codec);
  2003. if (codec->bus->ops.pm_notify)
  2004. codec->bus->ops.pm_notify(codec);
  2005. }
  2006. static void hda_keep_power_on(struct hda_codec *codec)
  2007. {
  2008. codec->power_count++;
  2009. codec->power_on = 1;
  2010. }
  2011. void snd_hda_power_up(struct hda_codec *codec)
  2012. {
  2013. codec->power_count++;
  2014. if (codec->power_on || codec->power_transition)
  2015. return;
  2016. codec->power_on = 1;
  2017. if (codec->bus->ops.pm_notify)
  2018. codec->bus->ops.pm_notify(codec);
  2019. hda_call_codec_resume(codec);
  2020. cancel_delayed_work(&codec->power_work);
  2021. codec->power_transition = 0;
  2022. }
  2023. void snd_hda_power_down(struct hda_codec *codec)
  2024. {
  2025. --codec->power_count;
  2026. if (!codec->power_on || codec->power_count || codec->power_transition)
  2027. return;
  2028. if (power_save) {
  2029. codec->power_transition = 1; /* avoid reentrance */
  2030. schedule_delayed_work(&codec->power_work,
  2031. msecs_to_jiffies(power_save * 1000));
  2032. }
  2033. }
  2034. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2035. struct hda_loopback_check *check,
  2036. hda_nid_t nid)
  2037. {
  2038. struct hda_amp_list *p;
  2039. int ch, v;
  2040. if (!check->amplist)
  2041. return 0;
  2042. for (p = check->amplist; p->nid; p++) {
  2043. if (p->nid == nid)
  2044. break;
  2045. }
  2046. if (!p->nid)
  2047. return 0; /* nothing changed */
  2048. for (p = check->amplist; p->nid; p++) {
  2049. for (ch = 0; ch < 2; ch++) {
  2050. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2051. p->idx);
  2052. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2053. if (!check->power_on) {
  2054. check->power_on = 1;
  2055. snd_hda_power_up(codec);
  2056. }
  2057. return 1;
  2058. }
  2059. }
  2060. }
  2061. if (check->power_on) {
  2062. check->power_on = 0;
  2063. snd_hda_power_down(codec);
  2064. }
  2065. return 0;
  2066. }
  2067. #endif
  2068. /*
  2069. * Channel mode helper
  2070. */
  2071. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2072. struct snd_ctl_elem_info *uinfo,
  2073. const struct hda_channel_mode *chmode,
  2074. int num_chmodes)
  2075. {
  2076. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2077. uinfo->count = 1;
  2078. uinfo->value.enumerated.items = num_chmodes;
  2079. if (uinfo->value.enumerated.item >= num_chmodes)
  2080. uinfo->value.enumerated.item = num_chmodes - 1;
  2081. sprintf(uinfo->value.enumerated.name, "%dch",
  2082. chmode[uinfo->value.enumerated.item].channels);
  2083. return 0;
  2084. }
  2085. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2086. struct snd_ctl_elem_value *ucontrol,
  2087. const struct hda_channel_mode *chmode,
  2088. int num_chmodes,
  2089. int max_channels)
  2090. {
  2091. int i;
  2092. for (i = 0; i < num_chmodes; i++) {
  2093. if (max_channels == chmode[i].channels) {
  2094. ucontrol->value.enumerated.item[0] = i;
  2095. break;
  2096. }
  2097. }
  2098. return 0;
  2099. }
  2100. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2101. struct snd_ctl_elem_value *ucontrol,
  2102. const struct hda_channel_mode *chmode,
  2103. int num_chmodes,
  2104. int *max_channelsp)
  2105. {
  2106. unsigned int mode;
  2107. mode = ucontrol->value.enumerated.item[0];
  2108. snd_assert(mode < num_chmodes, return -EINVAL);
  2109. if (*max_channelsp == chmode[mode].channels)
  2110. return 0;
  2111. /* change the current channel setting */
  2112. *max_channelsp = chmode[mode].channels;
  2113. if (chmode[mode].sequence)
  2114. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2115. return 1;
  2116. }
  2117. /*
  2118. * input MUX helper
  2119. */
  2120. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2121. struct snd_ctl_elem_info *uinfo)
  2122. {
  2123. unsigned int index;
  2124. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2125. uinfo->count = 1;
  2126. uinfo->value.enumerated.items = imux->num_items;
  2127. if (!imux->num_items)
  2128. return 0;
  2129. index = uinfo->value.enumerated.item;
  2130. if (index >= imux->num_items)
  2131. index = imux->num_items - 1;
  2132. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2133. return 0;
  2134. }
  2135. int snd_hda_input_mux_put(struct hda_codec *codec,
  2136. const struct hda_input_mux *imux,
  2137. struct snd_ctl_elem_value *ucontrol,
  2138. hda_nid_t nid,
  2139. unsigned int *cur_val)
  2140. {
  2141. unsigned int idx;
  2142. if (!imux->num_items)
  2143. return 0;
  2144. idx = ucontrol->value.enumerated.item[0];
  2145. if (idx >= imux->num_items)
  2146. idx = imux->num_items - 1;
  2147. if (*cur_val == idx)
  2148. return 0;
  2149. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2150. imux->items[idx].index);
  2151. *cur_val = idx;
  2152. return 1;
  2153. }
  2154. /*
  2155. * Multi-channel / digital-out PCM helper functions
  2156. */
  2157. /* setup SPDIF output stream */
  2158. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2159. unsigned int stream_tag, unsigned int format)
  2160. {
  2161. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2162. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  2163. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2164. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
  2165. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2166. /* turn on again (if needed) */
  2167. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  2168. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2169. codec->spdif_ctls & 0xff);
  2170. }
  2171. /*
  2172. * open the digital out in the exclusive mode
  2173. */
  2174. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2175. struct hda_multi_out *mout)
  2176. {
  2177. mutex_lock(&codec->spdif_mutex);
  2178. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2179. /* already opened as analog dup; reset it once */
  2180. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  2181. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2182. mutex_unlock(&codec->spdif_mutex);
  2183. return 0;
  2184. }
  2185. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2186. struct hda_multi_out *mout,
  2187. unsigned int stream_tag,
  2188. unsigned int format,
  2189. struct snd_pcm_substream *substream)
  2190. {
  2191. mutex_lock(&codec->spdif_mutex);
  2192. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2193. mutex_unlock(&codec->spdif_mutex);
  2194. return 0;
  2195. }
  2196. /*
  2197. * release the digital out
  2198. */
  2199. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2200. struct hda_multi_out *mout)
  2201. {
  2202. mutex_lock(&codec->spdif_mutex);
  2203. mout->dig_out_used = 0;
  2204. mutex_unlock(&codec->spdif_mutex);
  2205. return 0;
  2206. }
  2207. /*
  2208. * set up more restrictions for analog out
  2209. */
  2210. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2211. struct hda_multi_out *mout,
  2212. struct snd_pcm_substream *substream)
  2213. {
  2214. substream->runtime->hw.channels_max = mout->max_channels;
  2215. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2216. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2217. }
  2218. /*
  2219. * set up the i/o for analog out
  2220. * when the digital out is available, copy the front out to digital out, too.
  2221. */
  2222. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2223. struct hda_multi_out *mout,
  2224. unsigned int stream_tag,
  2225. unsigned int format,
  2226. struct snd_pcm_substream *substream)
  2227. {
  2228. hda_nid_t *nids = mout->dac_nids;
  2229. int chs = substream->runtime->channels;
  2230. int i;
  2231. mutex_lock(&codec->spdif_mutex);
  2232. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2233. if (chs == 2 &&
  2234. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2235. format) &&
  2236. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2237. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2238. setup_dig_out_stream(codec, mout->dig_out_nid,
  2239. stream_tag, format);
  2240. } else {
  2241. mout->dig_out_used = 0;
  2242. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  2243. 0, 0, 0);
  2244. }
  2245. }
  2246. mutex_unlock(&codec->spdif_mutex);
  2247. /* front */
  2248. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2249. 0, format);
  2250. if (!mout->no_share_stream &&
  2251. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2252. /* headphone out will just decode front left/right (stereo) */
  2253. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2254. 0, format);
  2255. /* extra outputs copied from front */
  2256. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2257. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2258. snd_hda_codec_setup_stream(codec,
  2259. mout->extra_out_nid[i],
  2260. stream_tag, 0, format);
  2261. /* surrounds */
  2262. for (i = 1; i < mout->num_dacs; i++) {
  2263. if (chs >= (i + 1) * 2) /* independent out */
  2264. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2265. i * 2, format);
  2266. else if (!mout->no_share_stream) /* copy front */
  2267. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2268. 0, format);
  2269. }
  2270. return 0;
  2271. }
  2272. /*
  2273. * clean up the setting for analog out
  2274. */
  2275. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2276. struct hda_multi_out *mout)
  2277. {
  2278. hda_nid_t *nids = mout->dac_nids;
  2279. int i;
  2280. for (i = 0; i < mout->num_dacs; i++)
  2281. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  2282. if (mout->hp_nid)
  2283. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  2284. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2285. if (mout->extra_out_nid[i])
  2286. snd_hda_codec_setup_stream(codec,
  2287. mout->extra_out_nid[i],
  2288. 0, 0, 0);
  2289. mutex_lock(&codec->spdif_mutex);
  2290. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2291. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  2292. mout->dig_out_used = 0;
  2293. }
  2294. mutex_unlock(&codec->spdif_mutex);
  2295. return 0;
  2296. }
  2297. /*
  2298. * Helper for automatic ping configuration
  2299. */
  2300. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2301. {
  2302. for (; *list; list++)
  2303. if (*list == nid)
  2304. return 1;
  2305. return 0;
  2306. }
  2307. /*
  2308. * Sort an associated group of pins according to their sequence numbers.
  2309. */
  2310. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2311. int num_pins)
  2312. {
  2313. int i, j;
  2314. short seq;
  2315. hda_nid_t nid;
  2316. for (i = 0; i < num_pins; i++) {
  2317. for (j = i + 1; j < num_pins; j++) {
  2318. if (sequences[i] > sequences[j]) {
  2319. seq = sequences[i];
  2320. sequences[i] = sequences[j];
  2321. sequences[j] = seq;
  2322. nid = pins[i];
  2323. pins[i] = pins[j];
  2324. pins[j] = nid;
  2325. }
  2326. }
  2327. }
  2328. }
  2329. /*
  2330. * Parse all pin widgets and store the useful pin nids to cfg
  2331. *
  2332. * The number of line-outs or any primary output is stored in line_outs,
  2333. * and the corresponding output pins are assigned to line_out_pins[],
  2334. * in the order of front, rear, CLFE, side, ...
  2335. *
  2336. * If more extra outputs (speaker and headphone) are found, the pins are
  2337. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2338. * is detected, one of speaker of HP pins is assigned as the primary
  2339. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2340. * if any analog output exists.
  2341. *
  2342. * The analog input pins are assigned to input_pins array.
  2343. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2344. * respectively.
  2345. */
  2346. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2347. struct auto_pin_cfg *cfg,
  2348. hda_nid_t *ignore_nids)
  2349. {
  2350. hda_nid_t nid, nid_start;
  2351. int nodes;
  2352. short seq, assoc_line_out, assoc_speaker;
  2353. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2354. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2355. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2356. memset(cfg, 0, sizeof(*cfg));
  2357. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2358. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2359. memset(sequences_hp, 0, sizeof(sequences_hp));
  2360. assoc_line_out = assoc_speaker = 0;
  2361. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  2362. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  2363. unsigned int wid_caps = get_wcaps(codec, nid);
  2364. unsigned int wid_type =
  2365. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2366. unsigned int def_conf;
  2367. short assoc, loc;
  2368. /* read all default configuration for pin complex */
  2369. if (wid_type != AC_WID_PIN)
  2370. continue;
  2371. /* ignore the given nids (e.g. pc-beep returns error) */
  2372. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2373. continue;
  2374. def_conf = snd_hda_codec_read(codec, nid, 0,
  2375. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2376. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2377. continue;
  2378. loc = get_defcfg_location(def_conf);
  2379. switch (get_defcfg_device(def_conf)) {
  2380. case AC_JACK_LINE_OUT:
  2381. seq = get_defcfg_sequence(def_conf);
  2382. assoc = get_defcfg_association(def_conf);
  2383. if (!assoc)
  2384. continue;
  2385. if (!assoc_line_out)
  2386. assoc_line_out = assoc;
  2387. else if (assoc_line_out != assoc)
  2388. continue;
  2389. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2390. continue;
  2391. cfg->line_out_pins[cfg->line_outs] = nid;
  2392. sequences_line_out[cfg->line_outs] = seq;
  2393. cfg->line_outs++;
  2394. break;
  2395. case AC_JACK_SPEAKER:
  2396. seq = get_defcfg_sequence(def_conf);
  2397. assoc = get_defcfg_association(def_conf);
  2398. if (! assoc)
  2399. continue;
  2400. if (! assoc_speaker)
  2401. assoc_speaker = assoc;
  2402. else if (assoc_speaker != assoc)
  2403. continue;
  2404. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2405. continue;
  2406. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2407. sequences_speaker[cfg->speaker_outs] = seq;
  2408. cfg->speaker_outs++;
  2409. break;
  2410. case AC_JACK_HP_OUT:
  2411. seq = get_defcfg_sequence(def_conf);
  2412. assoc = get_defcfg_association(def_conf);
  2413. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2414. continue;
  2415. cfg->hp_pins[cfg->hp_outs] = nid;
  2416. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2417. cfg->hp_outs++;
  2418. break;
  2419. case AC_JACK_MIC_IN: {
  2420. int preferred, alt;
  2421. if (loc == AC_JACK_LOC_FRONT) {
  2422. preferred = AUTO_PIN_FRONT_MIC;
  2423. alt = AUTO_PIN_MIC;
  2424. } else {
  2425. preferred = AUTO_PIN_MIC;
  2426. alt = AUTO_PIN_FRONT_MIC;
  2427. }
  2428. if (!cfg->input_pins[preferred])
  2429. cfg->input_pins[preferred] = nid;
  2430. else if (!cfg->input_pins[alt])
  2431. cfg->input_pins[alt] = nid;
  2432. break;
  2433. }
  2434. case AC_JACK_LINE_IN:
  2435. if (loc == AC_JACK_LOC_FRONT)
  2436. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2437. else
  2438. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2439. break;
  2440. case AC_JACK_CD:
  2441. cfg->input_pins[AUTO_PIN_CD] = nid;
  2442. break;
  2443. case AC_JACK_AUX:
  2444. cfg->input_pins[AUTO_PIN_AUX] = nid;
  2445. break;
  2446. case AC_JACK_SPDIF_OUT:
  2447. cfg->dig_out_pin = nid;
  2448. break;
  2449. case AC_JACK_SPDIF_IN:
  2450. cfg->dig_in_pin = nid;
  2451. break;
  2452. }
  2453. }
  2454. /* sort by sequence */
  2455. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  2456. cfg->line_outs);
  2457. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  2458. cfg->speaker_outs);
  2459. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  2460. cfg->hp_outs);
  2461. /* if we have only one mic, make it AUTO_PIN_MIC */
  2462. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  2463. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  2464. cfg->input_pins[AUTO_PIN_MIC] =
  2465. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  2466. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  2467. }
  2468. /* ditto for line-in */
  2469. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  2470. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  2471. cfg->input_pins[AUTO_PIN_LINE] =
  2472. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  2473. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  2474. }
  2475. /*
  2476. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  2477. * as a primary output
  2478. */
  2479. if (!cfg->line_outs) {
  2480. if (cfg->speaker_outs) {
  2481. cfg->line_outs = cfg->speaker_outs;
  2482. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  2483. sizeof(cfg->speaker_pins));
  2484. cfg->speaker_outs = 0;
  2485. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  2486. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  2487. } else if (cfg->hp_outs) {
  2488. cfg->line_outs = cfg->hp_outs;
  2489. memcpy(cfg->line_out_pins, cfg->hp_pins,
  2490. sizeof(cfg->hp_pins));
  2491. cfg->hp_outs = 0;
  2492. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  2493. cfg->line_out_type = AUTO_PIN_HP_OUT;
  2494. }
  2495. }
  2496. /* Reorder the surround channels
  2497. * ALSA sequence is front/surr/clfe/side
  2498. * HDA sequence is:
  2499. * 4-ch: front/surr => OK as it is
  2500. * 6-ch: front/clfe/surr
  2501. * 8-ch: front/clfe/rear/side|fc
  2502. */
  2503. switch (cfg->line_outs) {
  2504. case 3:
  2505. case 4:
  2506. nid = cfg->line_out_pins[1];
  2507. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  2508. cfg->line_out_pins[2] = nid;
  2509. break;
  2510. }
  2511. /*
  2512. * debug prints of the parsed results
  2513. */
  2514. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2515. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  2516. cfg->line_out_pins[2], cfg->line_out_pins[3],
  2517. cfg->line_out_pins[4]);
  2518. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2519. cfg->speaker_outs, cfg->speaker_pins[0],
  2520. cfg->speaker_pins[1], cfg->speaker_pins[2],
  2521. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  2522. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2523. cfg->hp_outs, cfg->hp_pins[0],
  2524. cfg->hp_pins[1], cfg->hp_pins[2],
  2525. cfg->hp_pins[3], cfg->hp_pins[4]);
  2526. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  2527. " cd=0x%x, aux=0x%x\n",
  2528. cfg->input_pins[AUTO_PIN_MIC],
  2529. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  2530. cfg->input_pins[AUTO_PIN_LINE],
  2531. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  2532. cfg->input_pins[AUTO_PIN_CD],
  2533. cfg->input_pins[AUTO_PIN_AUX]);
  2534. return 0;
  2535. }
  2536. /* labels for input pins */
  2537. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  2538. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  2539. };
  2540. #ifdef CONFIG_PM
  2541. /*
  2542. * power management
  2543. */
  2544. /**
  2545. * snd_hda_suspend - suspend the codecs
  2546. * @bus: the HDA bus
  2547. * @state: suspsend state
  2548. *
  2549. * Returns 0 if successful.
  2550. */
  2551. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  2552. {
  2553. struct hda_codec *codec;
  2554. list_for_each_entry(codec, &bus->codec_list, list) {
  2555. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2556. if (!codec->power_on)
  2557. continue;
  2558. #endif
  2559. hda_call_codec_suspend(codec);
  2560. }
  2561. return 0;
  2562. }
  2563. /**
  2564. * snd_hda_resume - resume the codecs
  2565. * @bus: the HDA bus
  2566. * @state: resume state
  2567. *
  2568. * Returns 0 if successful.
  2569. *
  2570. * This fucntion is defined only when POWER_SAVE isn't set.
  2571. * In the power-save mode, the codec is resumed dynamically.
  2572. */
  2573. int snd_hda_resume(struct hda_bus *bus)
  2574. {
  2575. struct hda_codec *codec;
  2576. list_for_each_entry(codec, &bus->codec_list, list) {
  2577. if (snd_hda_codec_needs_resume(codec))
  2578. hda_call_codec_resume(codec);
  2579. }
  2580. return 0;
  2581. }
  2582. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2583. int snd_hda_codecs_inuse(struct hda_bus *bus)
  2584. {
  2585. struct hda_codec *codec;
  2586. list_for_each_entry(codec, &bus->codec_list, list) {
  2587. if (snd_hda_codec_needs_resume(codec))
  2588. return 1;
  2589. }
  2590. return 0;
  2591. }
  2592. #endif
  2593. #endif