hda_codec.c 93 KB

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