hda_codec.c 94 KB

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