hda_codec.c 76 KB

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