hda_codec.c 85 KB

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