hda_codec.c 82 KB

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