hda_codec.c 82 KB

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