hda_codec.c 88 KB

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