hda_codec.c 77 KB

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