hda_codec.c 93 KB

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