hda_codec.c 83 KB

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