hda_codec.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  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,
  1284. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1285. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1286. return 0;
  1287. }
  1288. /*
  1289. * SPDIF input
  1290. */
  1291. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1292. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1293. struct snd_ctl_elem_value *ucontrol)
  1294. {
  1295. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1296. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1297. return 0;
  1298. }
  1299. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1300. struct snd_ctl_elem_value *ucontrol)
  1301. {
  1302. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1303. hda_nid_t nid = kcontrol->private_value;
  1304. unsigned int val = !!ucontrol->value.integer.value[0];
  1305. int change;
  1306. mutex_lock(&codec->spdif_mutex);
  1307. change = codec->spdif_in_enable != val;
  1308. if (change) {
  1309. codec->spdif_in_enable = val;
  1310. snd_hda_codec_write_cache(codec, nid, 0,
  1311. AC_VERB_SET_DIGI_CONVERT_1, val);
  1312. }
  1313. mutex_unlock(&codec->spdif_mutex);
  1314. return change;
  1315. }
  1316. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1317. struct snd_ctl_elem_value *ucontrol)
  1318. {
  1319. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1320. hda_nid_t nid = kcontrol->private_value;
  1321. unsigned short val;
  1322. unsigned int sbits;
  1323. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1324. sbits = convert_to_spdif_status(val);
  1325. ucontrol->value.iec958.status[0] = sbits;
  1326. ucontrol->value.iec958.status[1] = sbits >> 8;
  1327. ucontrol->value.iec958.status[2] = sbits >> 16;
  1328. ucontrol->value.iec958.status[3] = sbits >> 24;
  1329. return 0;
  1330. }
  1331. static struct snd_kcontrol_new dig_in_ctls[] = {
  1332. {
  1333. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1334. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1335. .info = snd_hda_spdif_in_switch_info,
  1336. .get = snd_hda_spdif_in_switch_get,
  1337. .put = snd_hda_spdif_in_switch_put,
  1338. },
  1339. {
  1340. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1341. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1342. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1343. .info = snd_hda_spdif_mask_info,
  1344. .get = snd_hda_spdif_in_status_get,
  1345. },
  1346. { } /* end */
  1347. };
  1348. /**
  1349. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1350. * @codec: the HDA codec
  1351. * @nid: audio in widget NID
  1352. *
  1353. * Creates controls related with the SPDIF input.
  1354. * Called from each patch supporting the SPDIF in.
  1355. *
  1356. * Returns 0 if successful, or a negative error code.
  1357. */
  1358. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1359. {
  1360. int err;
  1361. struct snd_kcontrol *kctl;
  1362. struct snd_kcontrol_new *dig_mix;
  1363. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1364. kctl = snd_ctl_new1(dig_mix, codec);
  1365. kctl->private_value = nid;
  1366. err = snd_ctl_add(codec->bus->card, kctl);
  1367. if (err < 0)
  1368. return err;
  1369. }
  1370. codec->spdif_in_enable =
  1371. snd_hda_codec_read(codec, nid, 0,
  1372. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  1373. AC_DIG1_ENABLE;
  1374. return 0;
  1375. }
  1376. #ifdef SND_HDA_NEEDS_RESUME
  1377. /*
  1378. * command cache
  1379. */
  1380. /* build a 32bit cache key with the widget id and the command parameter */
  1381. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1382. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1383. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1384. /**
  1385. * snd_hda_codec_write_cache - send a single command with caching
  1386. * @codec: the HDA codec
  1387. * @nid: NID to send the command
  1388. * @direct: direct flag
  1389. * @verb: the verb to send
  1390. * @parm: the parameter for the verb
  1391. *
  1392. * Send a single command without waiting for response.
  1393. *
  1394. * Returns 0 if successful, or a negative error code.
  1395. */
  1396. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1397. int direct, unsigned int verb, unsigned int parm)
  1398. {
  1399. int err;
  1400. snd_hda_power_up(codec);
  1401. mutex_lock(&codec->bus->cmd_mutex);
  1402. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  1403. if (!err) {
  1404. struct hda_cache_head *c;
  1405. u32 key = build_cmd_cache_key(nid, verb);
  1406. c = get_alloc_hash(&codec->cmd_cache, key);
  1407. if (c)
  1408. c->val = parm;
  1409. }
  1410. mutex_unlock(&codec->bus->cmd_mutex);
  1411. snd_hda_power_down(codec);
  1412. return err;
  1413. }
  1414. /* resume the all commands from the cache */
  1415. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1416. {
  1417. struct hda_cache_head *buffer = codec->cmd_cache.buffer;
  1418. int i;
  1419. for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
  1420. u32 key = buffer->key;
  1421. if (!key)
  1422. continue;
  1423. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1424. get_cmd_cache_cmd(key), buffer->val);
  1425. }
  1426. }
  1427. /**
  1428. * snd_hda_sequence_write_cache - sequence writes with caching
  1429. * @codec: the HDA codec
  1430. * @seq: VERB array to send
  1431. *
  1432. * Send the commands sequentially from the given array.
  1433. * Thte commands are recorded on cache for power-save and resume.
  1434. * The array must be terminated with NID=0.
  1435. */
  1436. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1437. const struct hda_verb *seq)
  1438. {
  1439. for (; seq->nid; seq++)
  1440. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1441. seq->param);
  1442. }
  1443. #endif /* SND_HDA_NEEDS_RESUME */
  1444. /*
  1445. * set power state of the codec
  1446. */
  1447. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1448. unsigned int power_state)
  1449. {
  1450. hda_nid_t nid;
  1451. int i;
  1452. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1453. power_state);
  1454. nid = codec->start_nid;
  1455. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1456. unsigned int wcaps = get_wcaps(codec, nid);
  1457. if (wcaps & AC_WCAP_POWER) {
  1458. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1459. AC_WCAP_TYPE_SHIFT;
  1460. if (wid_type == AC_WID_PIN) {
  1461. unsigned int pincap;
  1462. /*
  1463. * don't power down the widget if it controls
  1464. * eapd and EAPD_BTLENABLE is set.
  1465. */
  1466. pincap = snd_hda_param_read(codec, nid,
  1467. AC_PAR_PIN_CAP);
  1468. if (pincap & AC_PINCAP_EAPD) {
  1469. int eapd = snd_hda_codec_read(codec,
  1470. nid, 0,
  1471. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1472. eapd &= 0x02;
  1473. if (power_state == AC_PWRST_D3 && eapd)
  1474. continue;
  1475. }
  1476. }
  1477. snd_hda_codec_write(codec, nid, 0,
  1478. AC_VERB_SET_POWER_STATE,
  1479. power_state);
  1480. }
  1481. }
  1482. if (power_state == AC_PWRST_D0) {
  1483. unsigned long end_time;
  1484. int state;
  1485. msleep(10);
  1486. /* wait until the codec reachs to D0 */
  1487. end_time = jiffies + msecs_to_jiffies(500);
  1488. do {
  1489. state = snd_hda_codec_read(codec, fg, 0,
  1490. AC_VERB_GET_POWER_STATE, 0);
  1491. if (state == power_state)
  1492. break;
  1493. msleep(1);
  1494. } while (time_after_eq(end_time, jiffies));
  1495. }
  1496. }
  1497. #ifdef SND_HDA_NEEDS_RESUME
  1498. /*
  1499. * call suspend and power-down; used both from PM and power-save
  1500. */
  1501. static void hda_call_codec_suspend(struct hda_codec *codec)
  1502. {
  1503. if (codec->patch_ops.suspend)
  1504. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1505. hda_set_power_state(codec,
  1506. codec->afg ? codec->afg : codec->mfg,
  1507. AC_PWRST_D3);
  1508. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1509. cancel_delayed_work(&codec->power_work);
  1510. codec->power_on = 0;
  1511. codec->power_transition = 0;
  1512. #endif
  1513. }
  1514. /*
  1515. * kick up codec; used both from PM and power-save
  1516. */
  1517. static void hda_call_codec_resume(struct hda_codec *codec)
  1518. {
  1519. hda_set_power_state(codec,
  1520. codec->afg ? codec->afg : codec->mfg,
  1521. AC_PWRST_D0);
  1522. if (codec->patch_ops.resume)
  1523. codec->patch_ops.resume(codec);
  1524. else {
  1525. if (codec->patch_ops.init)
  1526. codec->patch_ops.init(codec);
  1527. snd_hda_codec_resume_amp(codec);
  1528. snd_hda_codec_resume_cache(codec);
  1529. }
  1530. }
  1531. #endif /* SND_HDA_NEEDS_RESUME */
  1532. /**
  1533. * snd_hda_build_controls - build mixer controls
  1534. * @bus: the BUS
  1535. *
  1536. * Creates mixer controls for each codec included in the bus.
  1537. *
  1538. * Returns 0 if successful, otherwise a negative error code.
  1539. */
  1540. int __devinit snd_hda_build_controls(struct hda_bus *bus)
  1541. {
  1542. struct hda_codec *codec;
  1543. list_for_each_entry(codec, &bus->codec_list, list) {
  1544. int err = 0;
  1545. /* fake as if already powered-on */
  1546. hda_keep_power_on(codec);
  1547. /* then fire up */
  1548. hda_set_power_state(codec,
  1549. codec->afg ? codec->afg : codec->mfg,
  1550. AC_PWRST_D0);
  1551. /* continue to initialize... */
  1552. if (codec->patch_ops.init)
  1553. err = codec->patch_ops.init(codec);
  1554. if (!err && codec->patch_ops.build_controls)
  1555. err = codec->patch_ops.build_controls(codec);
  1556. snd_hda_power_down(codec);
  1557. if (err < 0)
  1558. return err;
  1559. }
  1560. return 0;
  1561. }
  1562. /*
  1563. * stream formats
  1564. */
  1565. struct hda_rate_tbl {
  1566. unsigned int hz;
  1567. unsigned int alsa_bits;
  1568. unsigned int hda_fmt;
  1569. };
  1570. static struct hda_rate_tbl rate_bits[] = {
  1571. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1572. /* autodetected value used in snd_hda_query_supported_pcm */
  1573. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1574. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1575. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1576. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1577. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1578. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1579. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1580. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1581. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1582. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1583. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1584. #define AC_PAR_PCM_RATE_BITS 11
  1585. /* up to bits 10, 384kHZ isn't supported properly */
  1586. /* not autodetected value */
  1587. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1588. { 0 } /* terminator */
  1589. };
  1590. /**
  1591. * snd_hda_calc_stream_format - calculate format bitset
  1592. * @rate: the sample rate
  1593. * @channels: the number of channels
  1594. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1595. * @maxbps: the max. bps
  1596. *
  1597. * Calculate the format bitset from the given rate, channels and th PCM format.
  1598. *
  1599. * Return zero if invalid.
  1600. */
  1601. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1602. unsigned int channels,
  1603. unsigned int format,
  1604. unsigned int maxbps)
  1605. {
  1606. int i;
  1607. unsigned int val = 0;
  1608. for (i = 0; rate_bits[i].hz; i++)
  1609. if (rate_bits[i].hz == rate) {
  1610. val = rate_bits[i].hda_fmt;
  1611. break;
  1612. }
  1613. if (!rate_bits[i].hz) {
  1614. snd_printdd("invalid rate %d\n", rate);
  1615. return 0;
  1616. }
  1617. if (channels == 0 || channels > 8) {
  1618. snd_printdd("invalid channels %d\n", channels);
  1619. return 0;
  1620. }
  1621. val |= channels - 1;
  1622. switch (snd_pcm_format_width(format)) {
  1623. case 8: val |= 0x00; break;
  1624. case 16: val |= 0x10; break;
  1625. case 20:
  1626. case 24:
  1627. case 32:
  1628. if (maxbps >= 32)
  1629. val |= 0x40;
  1630. else if (maxbps >= 24)
  1631. val |= 0x30;
  1632. else
  1633. val |= 0x20;
  1634. break;
  1635. default:
  1636. snd_printdd("invalid format width %d\n",
  1637. snd_pcm_format_width(format));
  1638. return 0;
  1639. }
  1640. return val;
  1641. }
  1642. /**
  1643. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1644. * @codec: the HDA codec
  1645. * @nid: NID to query
  1646. * @ratesp: the pointer to store the detected rate bitflags
  1647. * @formatsp: the pointer to store the detected formats
  1648. * @bpsp: the pointer to store the detected format widths
  1649. *
  1650. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1651. * or @bsps argument is ignored.
  1652. *
  1653. * Returns 0 if successful, otherwise a negative error code.
  1654. */
  1655. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1656. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1657. {
  1658. int i;
  1659. unsigned int val, streams;
  1660. val = 0;
  1661. if (nid != codec->afg &&
  1662. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1663. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1664. if (val == -1)
  1665. return -EIO;
  1666. }
  1667. if (!val)
  1668. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1669. if (ratesp) {
  1670. u32 rates = 0;
  1671. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  1672. if (val & (1 << i))
  1673. rates |= rate_bits[i].alsa_bits;
  1674. }
  1675. *ratesp = rates;
  1676. }
  1677. if (formatsp || bpsp) {
  1678. u64 formats = 0;
  1679. unsigned int bps;
  1680. unsigned int wcaps;
  1681. wcaps = get_wcaps(codec, nid);
  1682. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1683. if (streams == -1)
  1684. return -EIO;
  1685. if (!streams) {
  1686. streams = snd_hda_param_read(codec, codec->afg,
  1687. AC_PAR_STREAM);
  1688. if (streams == -1)
  1689. return -EIO;
  1690. }
  1691. bps = 0;
  1692. if (streams & AC_SUPFMT_PCM) {
  1693. if (val & AC_SUPPCM_BITS_8) {
  1694. formats |= SNDRV_PCM_FMTBIT_U8;
  1695. bps = 8;
  1696. }
  1697. if (val & AC_SUPPCM_BITS_16) {
  1698. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1699. bps = 16;
  1700. }
  1701. if (wcaps & AC_WCAP_DIGITAL) {
  1702. if (val & AC_SUPPCM_BITS_32)
  1703. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1704. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1705. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1706. if (val & AC_SUPPCM_BITS_24)
  1707. bps = 24;
  1708. else if (val & AC_SUPPCM_BITS_20)
  1709. bps = 20;
  1710. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  1711. AC_SUPPCM_BITS_32)) {
  1712. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1713. if (val & AC_SUPPCM_BITS_32)
  1714. bps = 32;
  1715. else if (val & AC_SUPPCM_BITS_24)
  1716. bps = 24;
  1717. else if (val & AC_SUPPCM_BITS_20)
  1718. bps = 20;
  1719. }
  1720. }
  1721. else if (streams == AC_SUPFMT_FLOAT32) {
  1722. /* should be exclusive */
  1723. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1724. bps = 32;
  1725. } else if (streams == AC_SUPFMT_AC3) {
  1726. /* should be exclusive */
  1727. /* temporary hack: we have still no proper support
  1728. * for the direct AC3 stream...
  1729. */
  1730. formats |= SNDRV_PCM_FMTBIT_U8;
  1731. bps = 8;
  1732. }
  1733. if (formatsp)
  1734. *formatsp = formats;
  1735. if (bpsp)
  1736. *bpsp = bps;
  1737. }
  1738. return 0;
  1739. }
  1740. /**
  1741. * snd_hda_is_supported_format - check whether the given node supports
  1742. * the format val
  1743. *
  1744. * Returns 1 if supported, 0 if not.
  1745. */
  1746. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1747. unsigned int format)
  1748. {
  1749. int i;
  1750. unsigned int val = 0, rate, stream;
  1751. if (nid != codec->afg &&
  1752. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1753. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1754. if (val == -1)
  1755. return 0;
  1756. }
  1757. if (!val) {
  1758. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1759. if (val == -1)
  1760. return 0;
  1761. }
  1762. rate = format & 0xff00;
  1763. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  1764. if (rate_bits[i].hda_fmt == rate) {
  1765. if (val & (1 << i))
  1766. break;
  1767. return 0;
  1768. }
  1769. if (i >= AC_PAR_PCM_RATE_BITS)
  1770. return 0;
  1771. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1772. if (stream == -1)
  1773. return 0;
  1774. if (!stream && nid != codec->afg)
  1775. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1776. if (!stream || stream == -1)
  1777. return 0;
  1778. if (stream & AC_SUPFMT_PCM) {
  1779. switch (format & 0xf0) {
  1780. case 0x00:
  1781. if (!(val & AC_SUPPCM_BITS_8))
  1782. return 0;
  1783. break;
  1784. case 0x10:
  1785. if (!(val & AC_SUPPCM_BITS_16))
  1786. return 0;
  1787. break;
  1788. case 0x20:
  1789. if (!(val & AC_SUPPCM_BITS_20))
  1790. return 0;
  1791. break;
  1792. case 0x30:
  1793. if (!(val & AC_SUPPCM_BITS_24))
  1794. return 0;
  1795. break;
  1796. case 0x40:
  1797. if (!(val & AC_SUPPCM_BITS_32))
  1798. return 0;
  1799. break;
  1800. default:
  1801. return 0;
  1802. }
  1803. } else {
  1804. /* FIXME: check for float32 and AC3? */
  1805. }
  1806. return 1;
  1807. }
  1808. /*
  1809. * PCM stuff
  1810. */
  1811. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1812. struct hda_codec *codec,
  1813. struct snd_pcm_substream *substream)
  1814. {
  1815. return 0;
  1816. }
  1817. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1818. struct hda_codec *codec,
  1819. unsigned int stream_tag,
  1820. unsigned int format,
  1821. struct snd_pcm_substream *substream)
  1822. {
  1823. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1824. return 0;
  1825. }
  1826. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1827. struct hda_codec *codec,
  1828. struct snd_pcm_substream *substream)
  1829. {
  1830. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1831. return 0;
  1832. }
  1833. static int __devinit set_pcm_default_values(struct hda_codec *codec,
  1834. struct hda_pcm_stream *info)
  1835. {
  1836. /* query support PCM information from the given NID */
  1837. if (info->nid && (!info->rates || !info->formats)) {
  1838. snd_hda_query_supported_pcm(codec, info->nid,
  1839. info->rates ? NULL : &info->rates,
  1840. info->formats ? NULL : &info->formats,
  1841. info->maxbps ? NULL : &info->maxbps);
  1842. }
  1843. if (info->ops.open == NULL)
  1844. info->ops.open = hda_pcm_default_open_close;
  1845. if (info->ops.close == NULL)
  1846. info->ops.close = hda_pcm_default_open_close;
  1847. if (info->ops.prepare == NULL) {
  1848. snd_assert(info->nid, return -EINVAL);
  1849. info->ops.prepare = hda_pcm_default_prepare;
  1850. }
  1851. if (info->ops.cleanup == NULL) {
  1852. snd_assert(info->nid, return -EINVAL);
  1853. info->ops.cleanup = hda_pcm_default_cleanup;
  1854. }
  1855. return 0;
  1856. }
  1857. /**
  1858. * snd_hda_build_pcms - build PCM information
  1859. * @bus: the BUS
  1860. *
  1861. * Create PCM information for each codec included in the bus.
  1862. *
  1863. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1864. * codec->pcm_info properly. The array is referred by the top-level driver
  1865. * to create its PCM instances.
  1866. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1867. * callback.
  1868. *
  1869. * At least, substreams, channels_min and channels_max must be filled for
  1870. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1871. * When rates and/or formats are zero, the supported values are queried
  1872. * from the given nid. The nid is used also by the default ops.prepare
  1873. * and ops.cleanup callbacks.
  1874. *
  1875. * The driver needs to call ops.open in its open callback. Similarly,
  1876. * ops.close is supposed to be called in the close callback.
  1877. * ops.prepare should be called in the prepare or hw_params callback
  1878. * with the proper parameters for set up.
  1879. * ops.cleanup should be called in hw_free for clean up of streams.
  1880. *
  1881. * This function returns 0 if successfull, or a negative error code.
  1882. */
  1883. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  1884. {
  1885. struct hda_codec *codec;
  1886. list_for_each_entry(codec, &bus->codec_list, list) {
  1887. unsigned int pcm, s;
  1888. int err;
  1889. if (!codec->patch_ops.build_pcms)
  1890. continue;
  1891. err = codec->patch_ops.build_pcms(codec);
  1892. if (err < 0)
  1893. return err;
  1894. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1895. for (s = 0; s < 2; s++) {
  1896. struct hda_pcm_stream *info;
  1897. info = &codec->pcm_info[pcm].stream[s];
  1898. if (!info->substreams)
  1899. continue;
  1900. err = set_pcm_default_values(codec, info);
  1901. if (err < 0)
  1902. return err;
  1903. }
  1904. }
  1905. }
  1906. return 0;
  1907. }
  1908. /**
  1909. * snd_hda_check_board_config - compare the current codec with the config table
  1910. * @codec: the HDA codec
  1911. * @num_configs: number of config enums
  1912. * @models: array of model name strings
  1913. * @tbl: configuration table, terminated by null entries
  1914. *
  1915. * Compares the modelname or PCI subsystem id of the current codec with the
  1916. * given configuration table. If a matching entry is found, returns its
  1917. * config value (supposed to be 0 or positive).
  1918. *
  1919. * If no entries are matching, the function returns a negative value.
  1920. */
  1921. int snd_hda_check_board_config(struct hda_codec *codec,
  1922. int num_configs, const char **models,
  1923. const struct snd_pci_quirk *tbl)
  1924. {
  1925. if (codec->bus->modelname && models) {
  1926. int i;
  1927. for (i = 0; i < num_configs; i++) {
  1928. if (models[i] &&
  1929. !strcmp(codec->bus->modelname, models[i])) {
  1930. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  1931. "selected\n", models[i]);
  1932. return i;
  1933. }
  1934. }
  1935. }
  1936. if (!codec->bus->pci || !tbl)
  1937. return -1;
  1938. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  1939. if (!tbl)
  1940. return -1;
  1941. if (tbl->value >= 0 && tbl->value < num_configs) {
  1942. #ifdef CONFIG_SND_DEBUG_DETECT
  1943. char tmp[10];
  1944. const char *model = NULL;
  1945. if (models)
  1946. model = models[tbl->value];
  1947. if (!model) {
  1948. sprintf(tmp, "#%d", tbl->value);
  1949. model = tmp;
  1950. }
  1951. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  1952. "for config %x:%x (%s)\n",
  1953. model, tbl->subvendor, tbl->subdevice,
  1954. (tbl->name ? tbl->name : "Unknown device"));
  1955. #endif
  1956. return tbl->value;
  1957. }
  1958. return -1;
  1959. }
  1960. /**
  1961. * snd_hda_add_new_ctls - create controls from the array
  1962. * @codec: the HDA codec
  1963. * @knew: the array of struct snd_kcontrol_new
  1964. *
  1965. * This helper function creates and add new controls in the given array.
  1966. * The array must be terminated with an empty entry as terminator.
  1967. *
  1968. * Returns 0 if successful, or a negative error code.
  1969. */
  1970. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  1971. {
  1972. int err;
  1973. for (; knew->name; knew++) {
  1974. struct snd_kcontrol *kctl;
  1975. kctl = snd_ctl_new1(knew, codec);
  1976. if (!kctl)
  1977. return -ENOMEM;
  1978. err = snd_ctl_add(codec->bus->card, kctl);
  1979. if (err < 0) {
  1980. if (!codec->addr)
  1981. return err;
  1982. kctl = snd_ctl_new1(knew, codec);
  1983. if (!kctl)
  1984. return -ENOMEM;
  1985. kctl->id.device = codec->addr;
  1986. err = snd_ctl_add(codec->bus->card, kctl);
  1987. if (err < 0)
  1988. return err;
  1989. }
  1990. }
  1991. return 0;
  1992. }
  1993. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1994. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1995. unsigned int power_state);
  1996. static void hda_power_work(struct work_struct *work)
  1997. {
  1998. struct hda_codec *codec =
  1999. container_of(work, struct hda_codec, power_work.work);
  2000. if (!codec->power_on || codec->power_count) {
  2001. codec->power_transition = 0;
  2002. return;
  2003. }
  2004. hda_call_codec_suspend(codec);
  2005. if (codec->bus->ops.pm_notify)
  2006. codec->bus->ops.pm_notify(codec);
  2007. }
  2008. static void hda_keep_power_on(struct hda_codec *codec)
  2009. {
  2010. codec->power_count++;
  2011. codec->power_on = 1;
  2012. }
  2013. void snd_hda_power_up(struct hda_codec *codec)
  2014. {
  2015. codec->power_count++;
  2016. if (codec->power_on || codec->power_transition)
  2017. return;
  2018. codec->power_on = 1;
  2019. if (codec->bus->ops.pm_notify)
  2020. codec->bus->ops.pm_notify(codec);
  2021. hda_call_codec_resume(codec);
  2022. cancel_delayed_work(&codec->power_work);
  2023. codec->power_transition = 0;
  2024. }
  2025. void snd_hda_power_down(struct hda_codec *codec)
  2026. {
  2027. --codec->power_count;
  2028. if (!codec->power_on || codec->power_count || codec->power_transition)
  2029. return;
  2030. if (power_save) {
  2031. codec->power_transition = 1; /* avoid reentrance */
  2032. schedule_delayed_work(&codec->power_work,
  2033. msecs_to_jiffies(power_save * 1000));
  2034. }
  2035. }
  2036. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2037. struct hda_loopback_check *check,
  2038. hda_nid_t nid)
  2039. {
  2040. struct hda_amp_list *p;
  2041. int ch, v;
  2042. if (!check->amplist)
  2043. return 0;
  2044. for (p = check->amplist; p->nid; p++) {
  2045. if (p->nid == nid)
  2046. break;
  2047. }
  2048. if (!p->nid)
  2049. return 0; /* nothing changed */
  2050. for (p = check->amplist; p->nid; p++) {
  2051. for (ch = 0; ch < 2; ch++) {
  2052. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2053. p->idx);
  2054. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2055. if (!check->power_on) {
  2056. check->power_on = 1;
  2057. snd_hda_power_up(codec);
  2058. }
  2059. return 1;
  2060. }
  2061. }
  2062. }
  2063. if (check->power_on) {
  2064. check->power_on = 0;
  2065. snd_hda_power_down(codec);
  2066. }
  2067. return 0;
  2068. }
  2069. #endif
  2070. /*
  2071. * Channel mode helper
  2072. */
  2073. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2074. struct snd_ctl_elem_info *uinfo,
  2075. const struct hda_channel_mode *chmode,
  2076. int num_chmodes)
  2077. {
  2078. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2079. uinfo->count = 1;
  2080. uinfo->value.enumerated.items = num_chmodes;
  2081. if (uinfo->value.enumerated.item >= num_chmodes)
  2082. uinfo->value.enumerated.item = num_chmodes - 1;
  2083. sprintf(uinfo->value.enumerated.name, "%dch",
  2084. chmode[uinfo->value.enumerated.item].channels);
  2085. return 0;
  2086. }
  2087. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2088. struct snd_ctl_elem_value *ucontrol,
  2089. const struct hda_channel_mode *chmode,
  2090. int num_chmodes,
  2091. int max_channels)
  2092. {
  2093. int i;
  2094. for (i = 0; i < num_chmodes; i++) {
  2095. if (max_channels == chmode[i].channels) {
  2096. ucontrol->value.enumerated.item[0] = i;
  2097. break;
  2098. }
  2099. }
  2100. return 0;
  2101. }
  2102. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2103. struct snd_ctl_elem_value *ucontrol,
  2104. const struct hda_channel_mode *chmode,
  2105. int num_chmodes,
  2106. int *max_channelsp)
  2107. {
  2108. unsigned int mode;
  2109. mode = ucontrol->value.enumerated.item[0];
  2110. if (mode >= num_chmodes)
  2111. return -EINVAL;
  2112. if (*max_channelsp == chmode[mode].channels)
  2113. return 0;
  2114. /* change the current channel setting */
  2115. *max_channelsp = chmode[mode].channels;
  2116. if (chmode[mode].sequence)
  2117. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2118. return 1;
  2119. }
  2120. /*
  2121. * input MUX helper
  2122. */
  2123. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2124. struct snd_ctl_elem_info *uinfo)
  2125. {
  2126. unsigned int index;
  2127. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2128. uinfo->count = 1;
  2129. uinfo->value.enumerated.items = imux->num_items;
  2130. if (!imux->num_items)
  2131. return 0;
  2132. index = uinfo->value.enumerated.item;
  2133. if (index >= imux->num_items)
  2134. index = imux->num_items - 1;
  2135. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2136. return 0;
  2137. }
  2138. int snd_hda_input_mux_put(struct hda_codec *codec,
  2139. const struct hda_input_mux *imux,
  2140. struct snd_ctl_elem_value *ucontrol,
  2141. hda_nid_t nid,
  2142. unsigned int *cur_val)
  2143. {
  2144. unsigned int idx;
  2145. if (!imux->num_items)
  2146. return 0;
  2147. idx = ucontrol->value.enumerated.item[0];
  2148. if (idx >= imux->num_items)
  2149. idx = imux->num_items - 1;
  2150. if (*cur_val == idx)
  2151. return 0;
  2152. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2153. imux->items[idx].index);
  2154. *cur_val = idx;
  2155. return 1;
  2156. }
  2157. /*
  2158. * Multi-channel / digital-out PCM helper functions
  2159. */
  2160. /* setup SPDIF output stream */
  2161. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2162. unsigned int stream_tag, unsigned int format)
  2163. {
  2164. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2165. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  2166. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2167. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
  2168. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2169. /* turn on again (if needed) */
  2170. if (codec->spdif_ctls & AC_DIG1_ENABLE)
  2171. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2172. codec->spdif_ctls & 0xff);
  2173. }
  2174. /*
  2175. * open the digital out in the exclusive mode
  2176. */
  2177. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2178. struct hda_multi_out *mout)
  2179. {
  2180. mutex_lock(&codec->spdif_mutex);
  2181. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2182. /* already opened as analog dup; reset it once */
  2183. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  2184. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2185. mutex_unlock(&codec->spdif_mutex);
  2186. return 0;
  2187. }
  2188. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2189. struct hda_multi_out *mout,
  2190. unsigned int stream_tag,
  2191. unsigned int format,
  2192. struct snd_pcm_substream *substream)
  2193. {
  2194. mutex_lock(&codec->spdif_mutex);
  2195. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2196. mutex_unlock(&codec->spdif_mutex);
  2197. return 0;
  2198. }
  2199. /*
  2200. * release the digital out
  2201. */
  2202. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2203. struct hda_multi_out *mout)
  2204. {
  2205. mutex_lock(&codec->spdif_mutex);
  2206. mout->dig_out_used = 0;
  2207. mutex_unlock(&codec->spdif_mutex);
  2208. return 0;
  2209. }
  2210. /*
  2211. * set up more restrictions for analog out
  2212. */
  2213. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2214. struct hda_multi_out *mout,
  2215. struct snd_pcm_substream *substream)
  2216. {
  2217. substream->runtime->hw.channels_max = mout->max_channels;
  2218. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2219. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2220. }
  2221. /*
  2222. * set up the i/o for analog out
  2223. * when the digital out is available, copy the front out to digital out, too.
  2224. */
  2225. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2226. struct hda_multi_out *mout,
  2227. unsigned int stream_tag,
  2228. unsigned int format,
  2229. struct snd_pcm_substream *substream)
  2230. {
  2231. hda_nid_t *nids = mout->dac_nids;
  2232. int chs = substream->runtime->channels;
  2233. int i;
  2234. mutex_lock(&codec->spdif_mutex);
  2235. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2236. if (chs == 2 &&
  2237. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2238. format) &&
  2239. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2240. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2241. setup_dig_out_stream(codec, mout->dig_out_nid,
  2242. stream_tag, format);
  2243. } else {
  2244. mout->dig_out_used = 0;
  2245. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  2246. 0, 0, 0);
  2247. }
  2248. }
  2249. mutex_unlock(&codec->spdif_mutex);
  2250. /* front */
  2251. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2252. 0, format);
  2253. if (!mout->no_share_stream &&
  2254. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2255. /* headphone out will just decode front left/right (stereo) */
  2256. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2257. 0, format);
  2258. /* extra outputs copied from front */
  2259. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2260. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2261. snd_hda_codec_setup_stream(codec,
  2262. mout->extra_out_nid[i],
  2263. stream_tag, 0, format);
  2264. /* surrounds */
  2265. for (i = 1; i < mout->num_dacs; i++) {
  2266. if (chs >= (i + 1) * 2) /* independent out */
  2267. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2268. i * 2, format);
  2269. else if (!mout->no_share_stream) /* copy front */
  2270. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2271. 0, format);
  2272. }
  2273. return 0;
  2274. }
  2275. /*
  2276. * clean up the setting for analog out
  2277. */
  2278. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2279. struct hda_multi_out *mout)
  2280. {
  2281. hda_nid_t *nids = mout->dac_nids;
  2282. int i;
  2283. for (i = 0; i < mout->num_dacs; i++)
  2284. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  2285. if (mout->hp_nid)
  2286. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  2287. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2288. if (mout->extra_out_nid[i])
  2289. snd_hda_codec_setup_stream(codec,
  2290. mout->extra_out_nid[i],
  2291. 0, 0, 0);
  2292. mutex_lock(&codec->spdif_mutex);
  2293. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2294. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  2295. mout->dig_out_used = 0;
  2296. }
  2297. mutex_unlock(&codec->spdif_mutex);
  2298. return 0;
  2299. }
  2300. /*
  2301. * Helper for automatic ping configuration
  2302. */
  2303. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2304. {
  2305. for (; *list; list++)
  2306. if (*list == nid)
  2307. return 1;
  2308. return 0;
  2309. }
  2310. /*
  2311. * Sort an associated group of pins according to their sequence numbers.
  2312. */
  2313. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2314. int num_pins)
  2315. {
  2316. int i, j;
  2317. short seq;
  2318. hda_nid_t nid;
  2319. for (i = 0; i < num_pins; i++) {
  2320. for (j = i + 1; j < num_pins; j++) {
  2321. if (sequences[i] > sequences[j]) {
  2322. seq = sequences[i];
  2323. sequences[i] = sequences[j];
  2324. sequences[j] = seq;
  2325. nid = pins[i];
  2326. pins[i] = pins[j];
  2327. pins[j] = nid;
  2328. }
  2329. }
  2330. }
  2331. }
  2332. /*
  2333. * Parse all pin widgets and store the useful pin nids to cfg
  2334. *
  2335. * The number of line-outs or any primary output is stored in line_outs,
  2336. * and the corresponding output pins are assigned to line_out_pins[],
  2337. * in the order of front, rear, CLFE, side, ...
  2338. *
  2339. * If more extra outputs (speaker and headphone) are found, the pins are
  2340. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2341. * is detected, one of speaker of HP pins is assigned as the primary
  2342. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2343. * if any analog output exists.
  2344. *
  2345. * The analog input pins are assigned to input_pins array.
  2346. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2347. * respectively.
  2348. */
  2349. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2350. struct auto_pin_cfg *cfg,
  2351. hda_nid_t *ignore_nids)
  2352. {
  2353. hda_nid_t nid, nid_start;
  2354. int nodes;
  2355. short seq, assoc_line_out, assoc_speaker;
  2356. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2357. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2358. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2359. memset(cfg, 0, sizeof(*cfg));
  2360. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2361. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2362. memset(sequences_hp, 0, sizeof(sequences_hp));
  2363. assoc_line_out = assoc_speaker = 0;
  2364. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  2365. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  2366. unsigned int wid_caps = get_wcaps(codec, nid);
  2367. unsigned int wid_type =
  2368. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2369. unsigned int def_conf;
  2370. short assoc, loc;
  2371. /* read all default configuration for pin complex */
  2372. if (wid_type != AC_WID_PIN)
  2373. continue;
  2374. /* ignore the given nids (e.g. pc-beep returns error) */
  2375. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2376. continue;
  2377. def_conf = snd_hda_codec_read(codec, nid, 0,
  2378. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2379. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2380. continue;
  2381. loc = get_defcfg_location(def_conf);
  2382. switch (get_defcfg_device(def_conf)) {
  2383. case AC_JACK_LINE_OUT:
  2384. seq = get_defcfg_sequence(def_conf);
  2385. assoc = get_defcfg_association(def_conf);
  2386. if (!assoc)
  2387. continue;
  2388. if (!assoc_line_out)
  2389. assoc_line_out = assoc;
  2390. else if (assoc_line_out != assoc)
  2391. continue;
  2392. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2393. continue;
  2394. cfg->line_out_pins[cfg->line_outs] = nid;
  2395. sequences_line_out[cfg->line_outs] = seq;
  2396. cfg->line_outs++;
  2397. break;
  2398. case AC_JACK_SPEAKER:
  2399. seq = get_defcfg_sequence(def_conf);
  2400. assoc = get_defcfg_association(def_conf);
  2401. if (! assoc)
  2402. continue;
  2403. if (! assoc_speaker)
  2404. assoc_speaker = assoc;
  2405. else if (assoc_speaker != assoc)
  2406. continue;
  2407. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2408. continue;
  2409. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2410. sequences_speaker[cfg->speaker_outs] = seq;
  2411. cfg->speaker_outs++;
  2412. break;
  2413. case AC_JACK_HP_OUT:
  2414. seq = get_defcfg_sequence(def_conf);
  2415. assoc = get_defcfg_association(def_conf);
  2416. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2417. continue;
  2418. cfg->hp_pins[cfg->hp_outs] = nid;
  2419. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2420. cfg->hp_outs++;
  2421. break;
  2422. case AC_JACK_MIC_IN: {
  2423. int preferred, alt;
  2424. if (loc == AC_JACK_LOC_FRONT) {
  2425. preferred = AUTO_PIN_FRONT_MIC;
  2426. alt = AUTO_PIN_MIC;
  2427. } else {
  2428. preferred = AUTO_PIN_MIC;
  2429. alt = AUTO_PIN_FRONT_MIC;
  2430. }
  2431. if (!cfg->input_pins[preferred])
  2432. cfg->input_pins[preferred] = nid;
  2433. else if (!cfg->input_pins[alt])
  2434. cfg->input_pins[alt] = nid;
  2435. break;
  2436. }
  2437. case AC_JACK_LINE_IN:
  2438. if (loc == AC_JACK_LOC_FRONT)
  2439. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2440. else
  2441. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2442. break;
  2443. case AC_JACK_CD:
  2444. cfg->input_pins[AUTO_PIN_CD] = nid;
  2445. break;
  2446. case AC_JACK_AUX:
  2447. cfg->input_pins[AUTO_PIN_AUX] = nid;
  2448. break;
  2449. case AC_JACK_SPDIF_OUT:
  2450. cfg->dig_out_pin = nid;
  2451. break;
  2452. case AC_JACK_SPDIF_IN:
  2453. cfg->dig_in_pin = nid;
  2454. break;
  2455. }
  2456. }
  2457. /* sort by sequence */
  2458. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  2459. cfg->line_outs);
  2460. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  2461. cfg->speaker_outs);
  2462. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  2463. cfg->hp_outs);
  2464. /* if we have only one mic, make it AUTO_PIN_MIC */
  2465. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  2466. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  2467. cfg->input_pins[AUTO_PIN_MIC] =
  2468. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  2469. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  2470. }
  2471. /* ditto for line-in */
  2472. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  2473. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  2474. cfg->input_pins[AUTO_PIN_LINE] =
  2475. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  2476. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  2477. }
  2478. /*
  2479. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  2480. * as a primary output
  2481. */
  2482. if (!cfg->line_outs) {
  2483. if (cfg->speaker_outs) {
  2484. cfg->line_outs = cfg->speaker_outs;
  2485. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  2486. sizeof(cfg->speaker_pins));
  2487. cfg->speaker_outs = 0;
  2488. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  2489. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  2490. } else if (cfg->hp_outs) {
  2491. cfg->line_outs = cfg->hp_outs;
  2492. memcpy(cfg->line_out_pins, cfg->hp_pins,
  2493. sizeof(cfg->hp_pins));
  2494. cfg->hp_outs = 0;
  2495. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  2496. cfg->line_out_type = AUTO_PIN_HP_OUT;
  2497. }
  2498. }
  2499. /* Reorder the surround channels
  2500. * ALSA sequence is front/surr/clfe/side
  2501. * HDA sequence is:
  2502. * 4-ch: front/surr => OK as it is
  2503. * 6-ch: front/clfe/surr
  2504. * 8-ch: front/clfe/rear/side|fc
  2505. */
  2506. switch (cfg->line_outs) {
  2507. case 3:
  2508. case 4:
  2509. nid = cfg->line_out_pins[1];
  2510. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  2511. cfg->line_out_pins[2] = nid;
  2512. break;
  2513. }
  2514. /*
  2515. * debug prints of the parsed results
  2516. */
  2517. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2518. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  2519. cfg->line_out_pins[2], cfg->line_out_pins[3],
  2520. cfg->line_out_pins[4]);
  2521. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2522. cfg->speaker_outs, cfg->speaker_pins[0],
  2523. cfg->speaker_pins[1], cfg->speaker_pins[2],
  2524. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  2525. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2526. cfg->hp_outs, cfg->hp_pins[0],
  2527. cfg->hp_pins[1], cfg->hp_pins[2],
  2528. cfg->hp_pins[3], cfg->hp_pins[4]);
  2529. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  2530. " cd=0x%x, aux=0x%x\n",
  2531. cfg->input_pins[AUTO_PIN_MIC],
  2532. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  2533. cfg->input_pins[AUTO_PIN_LINE],
  2534. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  2535. cfg->input_pins[AUTO_PIN_CD],
  2536. cfg->input_pins[AUTO_PIN_AUX]);
  2537. return 0;
  2538. }
  2539. /* labels for input pins */
  2540. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  2541. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  2542. };
  2543. #ifdef CONFIG_PM
  2544. /*
  2545. * power management
  2546. */
  2547. /**
  2548. * snd_hda_suspend - suspend the codecs
  2549. * @bus: the HDA bus
  2550. * @state: suspsend state
  2551. *
  2552. * Returns 0 if successful.
  2553. */
  2554. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  2555. {
  2556. struct hda_codec *codec;
  2557. list_for_each_entry(codec, &bus->codec_list, list) {
  2558. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2559. if (!codec->power_on)
  2560. continue;
  2561. #endif
  2562. hda_call_codec_suspend(codec);
  2563. }
  2564. return 0;
  2565. }
  2566. /**
  2567. * snd_hda_resume - resume the codecs
  2568. * @bus: the HDA bus
  2569. * @state: resume state
  2570. *
  2571. * Returns 0 if successful.
  2572. *
  2573. * This fucntion is defined only when POWER_SAVE isn't set.
  2574. * In the power-save mode, the codec is resumed dynamically.
  2575. */
  2576. int snd_hda_resume(struct hda_bus *bus)
  2577. {
  2578. struct hda_codec *codec;
  2579. list_for_each_entry(codec, &bus->codec_list, list) {
  2580. if (snd_hda_codec_needs_resume(codec))
  2581. hda_call_codec_resume(codec);
  2582. }
  2583. return 0;
  2584. }
  2585. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2586. int snd_hda_codecs_inuse(struct hda_bus *bus)
  2587. {
  2588. struct hda_codec *codec;
  2589. list_for_each_entry(codec, &bus->codec_list, list) {
  2590. if (snd_hda_codec_needs_resume(codec))
  2591. return 1;
  2592. }
  2593. return 0;
  2594. }
  2595. #endif
  2596. #endif