hda_codec.c 76 KB

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