hda_codec.c 87 KB

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