hda_codec.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114
  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[13];
  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", codec->vendor_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. snd_hda_codec_write_cache(codec, nid, 0,
  1303. AC_VERB_SET_DIGI_CONVERT_1,
  1304. val & 0xff);
  1305. snd_hda_codec_write_cache(codec, nid, 0,
  1306. AC_VERB_SET_DIGI_CONVERT_2,
  1307. val >> 8);
  1308. }
  1309. mutex_unlock(&codec->spdif_mutex);
  1310. return change;
  1311. }
  1312. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1313. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1314. struct snd_ctl_elem_value *ucontrol)
  1315. {
  1316. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1317. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1318. return 0;
  1319. }
  1320. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1321. struct snd_ctl_elem_value *ucontrol)
  1322. {
  1323. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1324. hda_nid_t nid = kcontrol->private_value;
  1325. unsigned short val;
  1326. int change;
  1327. mutex_lock(&codec->spdif_mutex);
  1328. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1329. if (ucontrol->value.integer.value[0])
  1330. val |= AC_DIG1_ENABLE;
  1331. change = codec->spdif_ctls != val;
  1332. if (change) {
  1333. codec->spdif_ctls = val;
  1334. snd_hda_codec_write_cache(codec, nid, 0,
  1335. AC_VERB_SET_DIGI_CONVERT_1,
  1336. val & 0xff);
  1337. /* unmute amp switch (if any) */
  1338. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1339. (val & AC_DIG1_ENABLE))
  1340. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1341. HDA_AMP_MUTE, 0);
  1342. }
  1343. mutex_unlock(&codec->spdif_mutex);
  1344. return change;
  1345. }
  1346. static struct snd_kcontrol_new dig_mixes[] = {
  1347. {
  1348. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1349. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1350. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1351. .info = snd_hda_spdif_mask_info,
  1352. .get = snd_hda_spdif_cmask_get,
  1353. },
  1354. {
  1355. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1356. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1357. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1358. .info = snd_hda_spdif_mask_info,
  1359. .get = snd_hda_spdif_pmask_get,
  1360. },
  1361. {
  1362. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1363. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1364. .info = snd_hda_spdif_mask_info,
  1365. .get = snd_hda_spdif_default_get,
  1366. .put = snd_hda_spdif_default_put,
  1367. },
  1368. {
  1369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1370. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1371. .info = snd_hda_spdif_out_switch_info,
  1372. .get = snd_hda_spdif_out_switch_get,
  1373. .put = snd_hda_spdif_out_switch_put,
  1374. },
  1375. { } /* end */
  1376. };
  1377. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  1378. /**
  1379. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1380. * @codec: the HDA codec
  1381. * @nid: audio out widget NID
  1382. *
  1383. * Creates controls related with the SPDIF output.
  1384. * Called from each patch supporting the SPDIF out.
  1385. *
  1386. * Returns 0 if successful, or a negative error code.
  1387. */
  1388. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1389. {
  1390. int err;
  1391. struct snd_kcontrol *kctl;
  1392. struct snd_kcontrol_new *dig_mix;
  1393. int idx;
  1394. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1395. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  1396. idx))
  1397. break;
  1398. }
  1399. if (idx >= SPDIF_MAX_IDX) {
  1400. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  1401. return -EBUSY;
  1402. }
  1403. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1404. kctl = snd_ctl_new1(dig_mix, codec);
  1405. kctl->id.index = idx;
  1406. kctl->private_value = nid;
  1407. err = snd_ctl_add(codec->bus->card, kctl);
  1408. if (err < 0)
  1409. return err;
  1410. }
  1411. codec->spdif_ctls =
  1412. snd_hda_codec_read(codec, nid, 0,
  1413. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1414. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1415. return 0;
  1416. }
  1417. /*
  1418. * SPDIF sharing with analog output
  1419. */
  1420. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  1421. struct snd_ctl_elem_value *ucontrol)
  1422. {
  1423. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1424. ucontrol->value.integer.value[0] = mout->share_spdif;
  1425. return 0;
  1426. }
  1427. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  1428. struct snd_ctl_elem_value *ucontrol)
  1429. {
  1430. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1431. mout->share_spdif = !!ucontrol->value.integer.value[0];
  1432. return 0;
  1433. }
  1434. static struct snd_kcontrol_new spdif_share_sw = {
  1435. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1436. .name = "IEC958 Default PCM Playback Switch",
  1437. .info = snd_ctl_boolean_mono_info,
  1438. .get = spdif_share_sw_get,
  1439. .put = spdif_share_sw_put,
  1440. };
  1441. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  1442. struct hda_multi_out *mout)
  1443. {
  1444. if (!mout->dig_out_nid)
  1445. return 0;
  1446. /* ATTENTION: here mout is passed as private_data, instead of codec */
  1447. return snd_ctl_add(codec->bus->card,
  1448. snd_ctl_new1(&spdif_share_sw, mout));
  1449. }
  1450. /*
  1451. * SPDIF input
  1452. */
  1453. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1454. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1455. struct snd_ctl_elem_value *ucontrol)
  1456. {
  1457. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1458. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1459. return 0;
  1460. }
  1461. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1462. struct snd_ctl_elem_value *ucontrol)
  1463. {
  1464. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1465. hda_nid_t nid = kcontrol->private_value;
  1466. unsigned int val = !!ucontrol->value.integer.value[0];
  1467. int change;
  1468. mutex_lock(&codec->spdif_mutex);
  1469. change = codec->spdif_in_enable != val;
  1470. if (change) {
  1471. codec->spdif_in_enable = val;
  1472. snd_hda_codec_write_cache(codec, nid, 0,
  1473. AC_VERB_SET_DIGI_CONVERT_1, val);
  1474. }
  1475. mutex_unlock(&codec->spdif_mutex);
  1476. return change;
  1477. }
  1478. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1479. struct snd_ctl_elem_value *ucontrol)
  1480. {
  1481. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1482. hda_nid_t nid = kcontrol->private_value;
  1483. unsigned short val;
  1484. unsigned int sbits;
  1485. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1486. sbits = convert_to_spdif_status(val);
  1487. ucontrol->value.iec958.status[0] = sbits;
  1488. ucontrol->value.iec958.status[1] = sbits >> 8;
  1489. ucontrol->value.iec958.status[2] = sbits >> 16;
  1490. ucontrol->value.iec958.status[3] = sbits >> 24;
  1491. return 0;
  1492. }
  1493. static struct snd_kcontrol_new dig_in_ctls[] = {
  1494. {
  1495. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1496. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1497. .info = snd_hda_spdif_in_switch_info,
  1498. .get = snd_hda_spdif_in_switch_get,
  1499. .put = snd_hda_spdif_in_switch_put,
  1500. },
  1501. {
  1502. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1503. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1504. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1505. .info = snd_hda_spdif_mask_info,
  1506. .get = snd_hda_spdif_in_status_get,
  1507. },
  1508. { } /* end */
  1509. };
  1510. /**
  1511. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1512. * @codec: the HDA codec
  1513. * @nid: audio in widget NID
  1514. *
  1515. * Creates controls related with the SPDIF input.
  1516. * Called from each patch supporting the SPDIF in.
  1517. *
  1518. * Returns 0 if successful, or a negative error code.
  1519. */
  1520. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1521. {
  1522. int err;
  1523. struct snd_kcontrol *kctl;
  1524. struct snd_kcontrol_new *dig_mix;
  1525. int idx;
  1526. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1527. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  1528. idx))
  1529. break;
  1530. }
  1531. if (idx >= SPDIF_MAX_IDX) {
  1532. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  1533. return -EBUSY;
  1534. }
  1535. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1536. kctl = snd_ctl_new1(dig_mix, codec);
  1537. kctl->private_value = nid;
  1538. err = snd_ctl_add(codec->bus->card, kctl);
  1539. if (err < 0)
  1540. return err;
  1541. }
  1542. codec->spdif_in_enable =
  1543. snd_hda_codec_read(codec, nid, 0,
  1544. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  1545. AC_DIG1_ENABLE;
  1546. return 0;
  1547. }
  1548. #ifdef SND_HDA_NEEDS_RESUME
  1549. /*
  1550. * command cache
  1551. */
  1552. /* build a 32bit cache key with the widget id and the command parameter */
  1553. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1554. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1555. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1556. /**
  1557. * snd_hda_codec_write_cache - send a single command with caching
  1558. * @codec: the HDA codec
  1559. * @nid: NID to send the command
  1560. * @direct: direct flag
  1561. * @verb: the verb to send
  1562. * @parm: the parameter for the verb
  1563. *
  1564. * Send a single command without waiting for response.
  1565. *
  1566. * Returns 0 if successful, or a negative error code.
  1567. */
  1568. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1569. int direct, unsigned int verb, unsigned int parm)
  1570. {
  1571. int err;
  1572. snd_hda_power_up(codec);
  1573. mutex_lock(&codec->bus->cmd_mutex);
  1574. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  1575. if (!err) {
  1576. struct hda_cache_head *c;
  1577. u32 key = build_cmd_cache_key(nid, verb);
  1578. c = get_alloc_hash(&codec->cmd_cache, key);
  1579. if (c)
  1580. c->val = parm;
  1581. }
  1582. mutex_unlock(&codec->bus->cmd_mutex);
  1583. snd_hda_power_down(codec);
  1584. return err;
  1585. }
  1586. /* resume the all commands from the cache */
  1587. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1588. {
  1589. struct hda_cache_head *buffer = codec->cmd_cache.buffer;
  1590. int i;
  1591. for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
  1592. u32 key = buffer->key;
  1593. if (!key)
  1594. continue;
  1595. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1596. get_cmd_cache_cmd(key), buffer->val);
  1597. }
  1598. }
  1599. /**
  1600. * snd_hda_sequence_write_cache - sequence writes with caching
  1601. * @codec: the HDA codec
  1602. * @seq: VERB array to send
  1603. *
  1604. * Send the commands sequentially from the given array.
  1605. * Thte commands are recorded on cache for power-save and resume.
  1606. * The array must be terminated with NID=0.
  1607. */
  1608. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1609. const struct hda_verb *seq)
  1610. {
  1611. for (; seq->nid; seq++)
  1612. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1613. seq->param);
  1614. }
  1615. #endif /* SND_HDA_NEEDS_RESUME */
  1616. /*
  1617. * set power state of the codec
  1618. */
  1619. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1620. unsigned int power_state)
  1621. {
  1622. hda_nid_t nid;
  1623. int i;
  1624. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1625. power_state);
  1626. msleep(10); /* partial workaround for "azx_get_response timeout" */
  1627. nid = codec->start_nid;
  1628. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1629. unsigned int wcaps = get_wcaps(codec, nid);
  1630. if (wcaps & AC_WCAP_POWER) {
  1631. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1632. AC_WCAP_TYPE_SHIFT;
  1633. if (wid_type == AC_WID_PIN) {
  1634. unsigned int pincap;
  1635. /*
  1636. * don't power down the widget if it controls
  1637. * eapd and EAPD_BTLENABLE is set.
  1638. */
  1639. pincap = snd_hda_param_read(codec, nid,
  1640. AC_PAR_PIN_CAP);
  1641. if (pincap & AC_PINCAP_EAPD) {
  1642. int eapd = snd_hda_codec_read(codec,
  1643. nid, 0,
  1644. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1645. eapd &= 0x02;
  1646. if (power_state == AC_PWRST_D3 && eapd)
  1647. continue;
  1648. }
  1649. }
  1650. snd_hda_codec_write(codec, nid, 0,
  1651. AC_VERB_SET_POWER_STATE,
  1652. power_state);
  1653. }
  1654. }
  1655. if (power_state == AC_PWRST_D0) {
  1656. unsigned long end_time;
  1657. int state;
  1658. msleep(10);
  1659. /* wait until the codec reachs to D0 */
  1660. end_time = jiffies + msecs_to_jiffies(500);
  1661. do {
  1662. state = snd_hda_codec_read(codec, fg, 0,
  1663. AC_VERB_GET_POWER_STATE, 0);
  1664. if (state == power_state)
  1665. break;
  1666. msleep(1);
  1667. } while (time_after_eq(end_time, jiffies));
  1668. }
  1669. }
  1670. #ifdef SND_HDA_NEEDS_RESUME
  1671. /*
  1672. * call suspend and power-down; used both from PM and power-save
  1673. */
  1674. static void hda_call_codec_suspend(struct hda_codec *codec)
  1675. {
  1676. if (codec->patch_ops.suspend)
  1677. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1678. hda_set_power_state(codec,
  1679. codec->afg ? codec->afg : codec->mfg,
  1680. AC_PWRST_D3);
  1681. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1682. cancel_delayed_work(&codec->power_work);
  1683. codec->power_on = 0;
  1684. codec->power_transition = 0;
  1685. #endif
  1686. }
  1687. /*
  1688. * kick up codec; used both from PM and power-save
  1689. */
  1690. static void hda_call_codec_resume(struct hda_codec *codec)
  1691. {
  1692. hda_set_power_state(codec,
  1693. codec->afg ? codec->afg : codec->mfg,
  1694. AC_PWRST_D0);
  1695. if (codec->patch_ops.resume)
  1696. codec->patch_ops.resume(codec);
  1697. else {
  1698. if (codec->patch_ops.init)
  1699. codec->patch_ops.init(codec);
  1700. snd_hda_codec_resume_amp(codec);
  1701. snd_hda_codec_resume_cache(codec);
  1702. }
  1703. }
  1704. #endif /* SND_HDA_NEEDS_RESUME */
  1705. /**
  1706. * snd_hda_build_controls - build mixer controls
  1707. * @bus: the BUS
  1708. *
  1709. * Creates mixer controls for each codec included in the bus.
  1710. *
  1711. * Returns 0 if successful, otherwise a negative error code.
  1712. */
  1713. int __devinit snd_hda_build_controls(struct hda_bus *bus)
  1714. {
  1715. struct hda_codec *codec;
  1716. list_for_each_entry(codec, &bus->codec_list, list) {
  1717. int err = 0;
  1718. /* fake as if already powered-on */
  1719. hda_keep_power_on(codec);
  1720. /* then fire up */
  1721. hda_set_power_state(codec,
  1722. codec->afg ? codec->afg : codec->mfg,
  1723. AC_PWRST_D0);
  1724. /* continue to initialize... */
  1725. if (codec->patch_ops.init)
  1726. err = codec->patch_ops.init(codec);
  1727. if (!err && codec->patch_ops.build_controls)
  1728. err = codec->patch_ops.build_controls(codec);
  1729. snd_hda_power_down(codec);
  1730. if (err < 0)
  1731. return err;
  1732. }
  1733. return 0;
  1734. }
  1735. /*
  1736. * stream formats
  1737. */
  1738. struct hda_rate_tbl {
  1739. unsigned int hz;
  1740. unsigned int alsa_bits;
  1741. unsigned int hda_fmt;
  1742. };
  1743. static struct hda_rate_tbl rate_bits[] = {
  1744. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1745. /* autodetected value used in snd_hda_query_supported_pcm */
  1746. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1747. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1748. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1749. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1750. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1751. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1752. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1753. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1754. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1755. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1756. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1757. #define AC_PAR_PCM_RATE_BITS 11
  1758. /* up to bits 10, 384kHZ isn't supported properly */
  1759. /* not autodetected value */
  1760. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1761. { 0 } /* terminator */
  1762. };
  1763. /**
  1764. * snd_hda_calc_stream_format - calculate format bitset
  1765. * @rate: the sample rate
  1766. * @channels: the number of channels
  1767. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1768. * @maxbps: the max. bps
  1769. *
  1770. * Calculate the format bitset from the given rate, channels and th PCM format.
  1771. *
  1772. * Return zero if invalid.
  1773. */
  1774. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1775. unsigned int channels,
  1776. unsigned int format,
  1777. unsigned int maxbps)
  1778. {
  1779. int i;
  1780. unsigned int val = 0;
  1781. for (i = 0; rate_bits[i].hz; i++)
  1782. if (rate_bits[i].hz == rate) {
  1783. val = rate_bits[i].hda_fmt;
  1784. break;
  1785. }
  1786. if (!rate_bits[i].hz) {
  1787. snd_printdd("invalid rate %d\n", rate);
  1788. return 0;
  1789. }
  1790. if (channels == 0 || channels > 8) {
  1791. snd_printdd("invalid channels %d\n", channels);
  1792. return 0;
  1793. }
  1794. val |= channels - 1;
  1795. switch (snd_pcm_format_width(format)) {
  1796. case 8: val |= 0x00; break;
  1797. case 16: val |= 0x10; break;
  1798. case 20:
  1799. case 24:
  1800. case 32:
  1801. if (maxbps >= 32)
  1802. val |= 0x40;
  1803. else if (maxbps >= 24)
  1804. val |= 0x30;
  1805. else
  1806. val |= 0x20;
  1807. break;
  1808. default:
  1809. snd_printdd("invalid format width %d\n",
  1810. snd_pcm_format_width(format));
  1811. return 0;
  1812. }
  1813. return val;
  1814. }
  1815. /**
  1816. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1817. * @codec: the HDA codec
  1818. * @nid: NID to query
  1819. * @ratesp: the pointer to store the detected rate bitflags
  1820. * @formatsp: the pointer to store the detected formats
  1821. * @bpsp: the pointer to store the detected format widths
  1822. *
  1823. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1824. * or @bsps argument is ignored.
  1825. *
  1826. * Returns 0 if successful, otherwise a negative error code.
  1827. */
  1828. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1829. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1830. {
  1831. int i;
  1832. unsigned int val, streams;
  1833. val = 0;
  1834. if (nid != codec->afg &&
  1835. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1836. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1837. if (val == -1)
  1838. return -EIO;
  1839. }
  1840. if (!val)
  1841. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1842. if (ratesp) {
  1843. u32 rates = 0;
  1844. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  1845. if (val & (1 << i))
  1846. rates |= rate_bits[i].alsa_bits;
  1847. }
  1848. *ratesp = rates;
  1849. }
  1850. if (formatsp || bpsp) {
  1851. u64 formats = 0;
  1852. unsigned int bps;
  1853. unsigned int wcaps;
  1854. wcaps = get_wcaps(codec, nid);
  1855. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1856. if (streams == -1)
  1857. return -EIO;
  1858. if (!streams) {
  1859. streams = snd_hda_param_read(codec, codec->afg,
  1860. AC_PAR_STREAM);
  1861. if (streams == -1)
  1862. return -EIO;
  1863. }
  1864. bps = 0;
  1865. if (streams & AC_SUPFMT_PCM) {
  1866. if (val & AC_SUPPCM_BITS_8) {
  1867. formats |= SNDRV_PCM_FMTBIT_U8;
  1868. bps = 8;
  1869. }
  1870. if (val & AC_SUPPCM_BITS_16) {
  1871. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1872. bps = 16;
  1873. }
  1874. if (wcaps & AC_WCAP_DIGITAL) {
  1875. if (val & AC_SUPPCM_BITS_32)
  1876. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1877. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1878. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1879. if (val & AC_SUPPCM_BITS_24)
  1880. bps = 24;
  1881. else if (val & AC_SUPPCM_BITS_20)
  1882. bps = 20;
  1883. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  1884. AC_SUPPCM_BITS_32)) {
  1885. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1886. if (val & AC_SUPPCM_BITS_32)
  1887. bps = 32;
  1888. else if (val & AC_SUPPCM_BITS_24)
  1889. bps = 24;
  1890. else if (val & AC_SUPPCM_BITS_20)
  1891. bps = 20;
  1892. }
  1893. }
  1894. else if (streams == AC_SUPFMT_FLOAT32) {
  1895. /* should be exclusive */
  1896. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1897. bps = 32;
  1898. } else if (streams == AC_SUPFMT_AC3) {
  1899. /* should be exclusive */
  1900. /* temporary hack: we have still no proper support
  1901. * for the direct AC3 stream...
  1902. */
  1903. formats |= SNDRV_PCM_FMTBIT_U8;
  1904. bps = 8;
  1905. }
  1906. if (formatsp)
  1907. *formatsp = formats;
  1908. if (bpsp)
  1909. *bpsp = bps;
  1910. }
  1911. return 0;
  1912. }
  1913. /**
  1914. * snd_hda_is_supported_format - check whether the given node supports
  1915. * the format val
  1916. *
  1917. * Returns 1 if supported, 0 if not.
  1918. */
  1919. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1920. unsigned int format)
  1921. {
  1922. int i;
  1923. unsigned int val = 0, rate, stream;
  1924. if (nid != codec->afg &&
  1925. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1926. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1927. if (val == -1)
  1928. return 0;
  1929. }
  1930. if (!val) {
  1931. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1932. if (val == -1)
  1933. return 0;
  1934. }
  1935. rate = format & 0xff00;
  1936. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  1937. if (rate_bits[i].hda_fmt == rate) {
  1938. if (val & (1 << i))
  1939. break;
  1940. return 0;
  1941. }
  1942. if (i >= AC_PAR_PCM_RATE_BITS)
  1943. return 0;
  1944. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1945. if (stream == -1)
  1946. return 0;
  1947. if (!stream && nid != codec->afg)
  1948. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1949. if (!stream || stream == -1)
  1950. return 0;
  1951. if (stream & AC_SUPFMT_PCM) {
  1952. switch (format & 0xf0) {
  1953. case 0x00:
  1954. if (!(val & AC_SUPPCM_BITS_8))
  1955. return 0;
  1956. break;
  1957. case 0x10:
  1958. if (!(val & AC_SUPPCM_BITS_16))
  1959. return 0;
  1960. break;
  1961. case 0x20:
  1962. if (!(val & AC_SUPPCM_BITS_20))
  1963. return 0;
  1964. break;
  1965. case 0x30:
  1966. if (!(val & AC_SUPPCM_BITS_24))
  1967. return 0;
  1968. break;
  1969. case 0x40:
  1970. if (!(val & AC_SUPPCM_BITS_32))
  1971. return 0;
  1972. break;
  1973. default:
  1974. return 0;
  1975. }
  1976. } else {
  1977. /* FIXME: check for float32 and AC3? */
  1978. }
  1979. return 1;
  1980. }
  1981. /*
  1982. * PCM stuff
  1983. */
  1984. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1985. struct hda_codec *codec,
  1986. struct snd_pcm_substream *substream)
  1987. {
  1988. return 0;
  1989. }
  1990. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1991. struct hda_codec *codec,
  1992. unsigned int stream_tag,
  1993. unsigned int format,
  1994. struct snd_pcm_substream *substream)
  1995. {
  1996. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1997. return 0;
  1998. }
  1999. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2000. struct hda_codec *codec,
  2001. struct snd_pcm_substream *substream)
  2002. {
  2003. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2004. return 0;
  2005. }
  2006. static int __devinit set_pcm_default_values(struct hda_codec *codec,
  2007. struct hda_pcm_stream *info)
  2008. {
  2009. /* query support PCM information from the given NID */
  2010. if (info->nid && (!info->rates || !info->formats)) {
  2011. snd_hda_query_supported_pcm(codec, info->nid,
  2012. info->rates ? NULL : &info->rates,
  2013. info->formats ? NULL : &info->formats,
  2014. info->maxbps ? NULL : &info->maxbps);
  2015. }
  2016. if (info->ops.open == NULL)
  2017. info->ops.open = hda_pcm_default_open_close;
  2018. if (info->ops.close == NULL)
  2019. info->ops.close = hda_pcm_default_open_close;
  2020. if (info->ops.prepare == NULL) {
  2021. if (snd_BUG_ON(!info->nid))
  2022. return -EINVAL;
  2023. info->ops.prepare = hda_pcm_default_prepare;
  2024. }
  2025. if (info->ops.cleanup == NULL) {
  2026. if (snd_BUG_ON(!info->nid))
  2027. return -EINVAL;
  2028. info->ops.cleanup = hda_pcm_default_cleanup;
  2029. }
  2030. return 0;
  2031. }
  2032. /**
  2033. * snd_hda_build_pcms - build PCM information
  2034. * @bus: the BUS
  2035. *
  2036. * Create PCM information for each codec included in the bus.
  2037. *
  2038. * The build_pcms codec patch is requested to set up codec->num_pcms and
  2039. * codec->pcm_info properly. The array is referred by the top-level driver
  2040. * to create its PCM instances.
  2041. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  2042. * callback.
  2043. *
  2044. * At least, substreams, channels_min and channels_max must be filled for
  2045. * each stream. substreams = 0 indicates that the stream doesn't exist.
  2046. * When rates and/or formats are zero, the supported values are queried
  2047. * from the given nid. The nid is used also by the default ops.prepare
  2048. * and ops.cleanup callbacks.
  2049. *
  2050. * The driver needs to call ops.open in its open callback. Similarly,
  2051. * ops.close is supposed to be called in the close callback.
  2052. * ops.prepare should be called in the prepare or hw_params callback
  2053. * with the proper parameters for set up.
  2054. * ops.cleanup should be called in hw_free for clean up of streams.
  2055. *
  2056. * This function returns 0 if successfull, or a negative error code.
  2057. */
  2058. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  2059. {
  2060. struct hda_codec *codec;
  2061. list_for_each_entry(codec, &bus->codec_list, list) {
  2062. unsigned int pcm, s;
  2063. int err;
  2064. if (!codec->patch_ops.build_pcms)
  2065. continue;
  2066. err = codec->patch_ops.build_pcms(codec);
  2067. if (err < 0)
  2068. return err;
  2069. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2070. for (s = 0; s < 2; s++) {
  2071. struct hda_pcm_stream *info;
  2072. info = &codec->pcm_info[pcm].stream[s];
  2073. if (!info->substreams)
  2074. continue;
  2075. err = set_pcm_default_values(codec, info);
  2076. if (err < 0)
  2077. return err;
  2078. }
  2079. }
  2080. }
  2081. return 0;
  2082. }
  2083. /**
  2084. * snd_hda_check_board_config - compare the current codec with the config table
  2085. * @codec: the HDA codec
  2086. * @num_configs: number of config enums
  2087. * @models: array of model name strings
  2088. * @tbl: configuration table, terminated by null entries
  2089. *
  2090. * Compares the modelname or PCI subsystem id of the current codec with the
  2091. * given configuration table. If a matching entry is found, returns its
  2092. * config value (supposed to be 0 or positive).
  2093. *
  2094. * If no entries are matching, the function returns a negative value.
  2095. */
  2096. int snd_hda_check_board_config(struct hda_codec *codec,
  2097. int num_configs, const char **models,
  2098. const struct snd_pci_quirk *tbl)
  2099. {
  2100. if (codec->bus->modelname && models) {
  2101. int i;
  2102. for (i = 0; i < num_configs; i++) {
  2103. if (models[i] &&
  2104. !strcmp(codec->bus->modelname, models[i])) {
  2105. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  2106. "selected\n", models[i]);
  2107. return i;
  2108. }
  2109. }
  2110. }
  2111. if (!codec->bus->pci || !tbl)
  2112. return -1;
  2113. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  2114. if (!tbl)
  2115. return -1;
  2116. if (tbl->value >= 0 && tbl->value < num_configs) {
  2117. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2118. char tmp[10];
  2119. const char *model = NULL;
  2120. if (models)
  2121. model = models[tbl->value];
  2122. if (!model) {
  2123. sprintf(tmp, "#%d", tbl->value);
  2124. model = tmp;
  2125. }
  2126. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2127. "for config %x:%x (%s)\n",
  2128. model, tbl->subvendor, tbl->subdevice,
  2129. (tbl->name ? tbl->name : "Unknown device"));
  2130. #endif
  2131. return tbl->value;
  2132. }
  2133. return -1;
  2134. }
  2135. /**
  2136. * snd_hda_add_new_ctls - create controls from the array
  2137. * @codec: the HDA codec
  2138. * @knew: the array of struct snd_kcontrol_new
  2139. *
  2140. * This helper function creates and add new controls in the given array.
  2141. * The array must be terminated with an empty entry as terminator.
  2142. *
  2143. * Returns 0 if successful, or a negative error code.
  2144. */
  2145. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2146. {
  2147. int err;
  2148. for (; knew->name; knew++) {
  2149. struct snd_kcontrol *kctl;
  2150. kctl = snd_ctl_new1(knew, codec);
  2151. if (!kctl)
  2152. return -ENOMEM;
  2153. err = snd_ctl_add(codec->bus->card, kctl);
  2154. if (err < 0) {
  2155. if (!codec->addr)
  2156. return err;
  2157. kctl = snd_ctl_new1(knew, codec);
  2158. if (!kctl)
  2159. return -ENOMEM;
  2160. kctl->id.device = codec->addr;
  2161. err = snd_ctl_add(codec->bus->card, kctl);
  2162. if (err < 0)
  2163. return err;
  2164. }
  2165. }
  2166. return 0;
  2167. }
  2168. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2169. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2170. unsigned int power_state);
  2171. static void hda_power_work(struct work_struct *work)
  2172. {
  2173. struct hda_codec *codec =
  2174. container_of(work, struct hda_codec, power_work.work);
  2175. if (!codec->power_on || codec->power_count) {
  2176. codec->power_transition = 0;
  2177. return;
  2178. }
  2179. hda_call_codec_suspend(codec);
  2180. if (codec->bus->ops.pm_notify)
  2181. codec->bus->ops.pm_notify(codec);
  2182. }
  2183. static void hda_keep_power_on(struct hda_codec *codec)
  2184. {
  2185. codec->power_count++;
  2186. codec->power_on = 1;
  2187. }
  2188. void snd_hda_power_up(struct hda_codec *codec)
  2189. {
  2190. codec->power_count++;
  2191. if (codec->power_on || codec->power_transition)
  2192. return;
  2193. codec->power_on = 1;
  2194. if (codec->bus->ops.pm_notify)
  2195. codec->bus->ops.pm_notify(codec);
  2196. hda_call_codec_resume(codec);
  2197. cancel_delayed_work(&codec->power_work);
  2198. codec->power_transition = 0;
  2199. }
  2200. void snd_hda_power_down(struct hda_codec *codec)
  2201. {
  2202. --codec->power_count;
  2203. if (!codec->power_on || codec->power_count || codec->power_transition)
  2204. return;
  2205. if (power_save) {
  2206. codec->power_transition = 1; /* avoid reentrance */
  2207. schedule_delayed_work(&codec->power_work,
  2208. msecs_to_jiffies(power_save * 1000));
  2209. }
  2210. }
  2211. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2212. struct hda_loopback_check *check,
  2213. hda_nid_t nid)
  2214. {
  2215. struct hda_amp_list *p;
  2216. int ch, v;
  2217. if (!check->amplist)
  2218. return 0;
  2219. for (p = check->amplist; p->nid; p++) {
  2220. if (p->nid == nid)
  2221. break;
  2222. }
  2223. if (!p->nid)
  2224. return 0; /* nothing changed */
  2225. for (p = check->amplist; p->nid; p++) {
  2226. for (ch = 0; ch < 2; ch++) {
  2227. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2228. p->idx);
  2229. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2230. if (!check->power_on) {
  2231. check->power_on = 1;
  2232. snd_hda_power_up(codec);
  2233. }
  2234. return 1;
  2235. }
  2236. }
  2237. }
  2238. if (check->power_on) {
  2239. check->power_on = 0;
  2240. snd_hda_power_down(codec);
  2241. }
  2242. return 0;
  2243. }
  2244. #endif
  2245. /*
  2246. * Channel mode helper
  2247. */
  2248. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2249. struct snd_ctl_elem_info *uinfo,
  2250. const struct hda_channel_mode *chmode,
  2251. int num_chmodes)
  2252. {
  2253. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2254. uinfo->count = 1;
  2255. uinfo->value.enumerated.items = num_chmodes;
  2256. if (uinfo->value.enumerated.item >= num_chmodes)
  2257. uinfo->value.enumerated.item = num_chmodes - 1;
  2258. sprintf(uinfo->value.enumerated.name, "%dch",
  2259. chmode[uinfo->value.enumerated.item].channels);
  2260. return 0;
  2261. }
  2262. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2263. struct snd_ctl_elem_value *ucontrol,
  2264. const struct hda_channel_mode *chmode,
  2265. int num_chmodes,
  2266. int max_channels)
  2267. {
  2268. int i;
  2269. for (i = 0; i < num_chmodes; i++) {
  2270. if (max_channels == chmode[i].channels) {
  2271. ucontrol->value.enumerated.item[0] = i;
  2272. break;
  2273. }
  2274. }
  2275. return 0;
  2276. }
  2277. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2278. struct snd_ctl_elem_value *ucontrol,
  2279. const struct hda_channel_mode *chmode,
  2280. int num_chmodes,
  2281. int *max_channelsp)
  2282. {
  2283. unsigned int mode;
  2284. mode = ucontrol->value.enumerated.item[0];
  2285. if (mode >= num_chmodes)
  2286. return -EINVAL;
  2287. if (*max_channelsp == chmode[mode].channels)
  2288. return 0;
  2289. /* change the current channel setting */
  2290. *max_channelsp = chmode[mode].channels;
  2291. if (chmode[mode].sequence)
  2292. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2293. return 1;
  2294. }
  2295. /*
  2296. * input MUX helper
  2297. */
  2298. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2299. struct snd_ctl_elem_info *uinfo)
  2300. {
  2301. unsigned int index;
  2302. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2303. uinfo->count = 1;
  2304. uinfo->value.enumerated.items = imux->num_items;
  2305. if (!imux->num_items)
  2306. return 0;
  2307. index = uinfo->value.enumerated.item;
  2308. if (index >= imux->num_items)
  2309. index = imux->num_items - 1;
  2310. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2311. return 0;
  2312. }
  2313. int snd_hda_input_mux_put(struct hda_codec *codec,
  2314. const struct hda_input_mux *imux,
  2315. struct snd_ctl_elem_value *ucontrol,
  2316. hda_nid_t nid,
  2317. unsigned int *cur_val)
  2318. {
  2319. unsigned int idx;
  2320. if (!imux->num_items)
  2321. return 0;
  2322. idx = ucontrol->value.enumerated.item[0];
  2323. if (idx >= imux->num_items)
  2324. idx = imux->num_items - 1;
  2325. if (*cur_val == idx)
  2326. return 0;
  2327. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2328. imux->items[idx].index);
  2329. *cur_val = idx;
  2330. return 1;
  2331. }
  2332. /*
  2333. * Multi-channel / digital-out PCM helper functions
  2334. */
  2335. /* setup SPDIF output stream */
  2336. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2337. unsigned int stream_tag, unsigned int format)
  2338. {
  2339. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2340. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2341. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2342. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
  2343. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2344. /* turn on again (if needed) */
  2345. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2346. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  2347. codec->spdif_ctls & 0xff);
  2348. }
  2349. /*
  2350. * open the digital out in the exclusive mode
  2351. */
  2352. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2353. struct hda_multi_out *mout)
  2354. {
  2355. mutex_lock(&codec->spdif_mutex);
  2356. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2357. /* already opened as analog dup; reset it once */
  2358. snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
  2359. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2360. mutex_unlock(&codec->spdif_mutex);
  2361. return 0;
  2362. }
  2363. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2364. struct hda_multi_out *mout,
  2365. unsigned int stream_tag,
  2366. unsigned int format,
  2367. struct snd_pcm_substream *substream)
  2368. {
  2369. mutex_lock(&codec->spdif_mutex);
  2370. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2371. mutex_unlock(&codec->spdif_mutex);
  2372. return 0;
  2373. }
  2374. /*
  2375. * release the digital out
  2376. */
  2377. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2378. struct hda_multi_out *mout)
  2379. {
  2380. mutex_lock(&codec->spdif_mutex);
  2381. mout->dig_out_used = 0;
  2382. mutex_unlock(&codec->spdif_mutex);
  2383. return 0;
  2384. }
  2385. /*
  2386. * set up more restrictions for analog out
  2387. */
  2388. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2389. struct hda_multi_out *mout,
  2390. struct snd_pcm_substream *substream,
  2391. struct hda_pcm_stream *hinfo)
  2392. {
  2393. struct snd_pcm_runtime *runtime = substream->runtime;
  2394. runtime->hw.channels_max = mout->max_channels;
  2395. if (mout->dig_out_nid) {
  2396. if (!mout->analog_rates) {
  2397. mout->analog_rates = hinfo->rates;
  2398. mout->analog_formats = hinfo->formats;
  2399. mout->analog_maxbps = hinfo->maxbps;
  2400. } else {
  2401. runtime->hw.rates = mout->analog_rates;
  2402. runtime->hw.formats = mout->analog_formats;
  2403. hinfo->maxbps = mout->analog_maxbps;
  2404. }
  2405. if (!mout->spdif_rates) {
  2406. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  2407. &mout->spdif_rates,
  2408. &mout->spdif_formats,
  2409. &mout->spdif_maxbps);
  2410. }
  2411. mutex_lock(&codec->spdif_mutex);
  2412. if (mout->share_spdif) {
  2413. runtime->hw.rates &= mout->spdif_rates;
  2414. runtime->hw.formats &= mout->spdif_formats;
  2415. if (mout->spdif_maxbps < hinfo->maxbps)
  2416. hinfo->maxbps = mout->spdif_maxbps;
  2417. }
  2418. mutex_unlock(&codec->spdif_mutex);
  2419. }
  2420. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2421. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2422. }
  2423. /*
  2424. * set up the i/o for analog out
  2425. * when the digital out is available, copy the front out to digital out, too.
  2426. */
  2427. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2428. struct hda_multi_out *mout,
  2429. unsigned int stream_tag,
  2430. unsigned int format,
  2431. struct snd_pcm_substream *substream)
  2432. {
  2433. hda_nid_t *nids = mout->dac_nids;
  2434. int chs = substream->runtime->channels;
  2435. int i;
  2436. mutex_lock(&codec->spdif_mutex);
  2437. if (mout->dig_out_nid && mout->share_spdif &&
  2438. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2439. if (chs == 2 &&
  2440. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2441. format) &&
  2442. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2443. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2444. setup_dig_out_stream(codec, mout->dig_out_nid,
  2445. stream_tag, format);
  2446. } else {
  2447. mout->dig_out_used = 0;
  2448. snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
  2449. }
  2450. }
  2451. mutex_unlock(&codec->spdif_mutex);
  2452. /* front */
  2453. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2454. 0, format);
  2455. if (!mout->no_share_stream &&
  2456. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2457. /* headphone out will just decode front left/right (stereo) */
  2458. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2459. 0, format);
  2460. /* extra outputs copied from front */
  2461. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2462. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2463. snd_hda_codec_setup_stream(codec,
  2464. mout->extra_out_nid[i],
  2465. stream_tag, 0, format);
  2466. /* surrounds */
  2467. for (i = 1; i < mout->num_dacs; i++) {
  2468. if (chs >= (i + 1) * 2) /* independent out */
  2469. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2470. i * 2, format);
  2471. else if (!mout->no_share_stream) /* copy front */
  2472. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2473. 0, format);
  2474. }
  2475. return 0;
  2476. }
  2477. /*
  2478. * clean up the setting for analog out
  2479. */
  2480. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2481. struct hda_multi_out *mout)
  2482. {
  2483. hda_nid_t *nids = mout->dac_nids;
  2484. int i;
  2485. for (i = 0; i < mout->num_dacs; i++)
  2486. snd_hda_codec_cleanup_stream(codec, nids[i]);
  2487. if (mout->hp_nid)
  2488. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  2489. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2490. if (mout->extra_out_nid[i])
  2491. snd_hda_codec_cleanup_stream(codec,
  2492. mout->extra_out_nid[i]);
  2493. mutex_lock(&codec->spdif_mutex);
  2494. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2495. snd_hda_codec_cleanup_stream(codec, mout->dig_out_nid);
  2496. mout->dig_out_used = 0;
  2497. }
  2498. mutex_unlock(&codec->spdif_mutex);
  2499. return 0;
  2500. }
  2501. /*
  2502. * Helper for automatic ping configuration
  2503. */
  2504. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2505. {
  2506. for (; *list; list++)
  2507. if (*list == nid)
  2508. return 1;
  2509. return 0;
  2510. }
  2511. /*
  2512. * Sort an associated group of pins according to their sequence numbers.
  2513. */
  2514. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2515. int num_pins)
  2516. {
  2517. int i, j;
  2518. short seq;
  2519. hda_nid_t nid;
  2520. for (i = 0; i < num_pins; i++) {
  2521. for (j = i + 1; j < num_pins; j++) {
  2522. if (sequences[i] > sequences[j]) {
  2523. seq = sequences[i];
  2524. sequences[i] = sequences[j];
  2525. sequences[j] = seq;
  2526. nid = pins[i];
  2527. pins[i] = pins[j];
  2528. pins[j] = nid;
  2529. }
  2530. }
  2531. }
  2532. }
  2533. /*
  2534. * Parse all pin widgets and store the useful pin nids to cfg
  2535. *
  2536. * The number of line-outs or any primary output is stored in line_outs,
  2537. * and the corresponding output pins are assigned to line_out_pins[],
  2538. * in the order of front, rear, CLFE, side, ...
  2539. *
  2540. * If more extra outputs (speaker and headphone) are found, the pins are
  2541. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2542. * is detected, one of speaker of HP pins is assigned as the primary
  2543. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2544. * if any analog output exists.
  2545. *
  2546. * The analog input pins are assigned to input_pins array.
  2547. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2548. * respectively.
  2549. */
  2550. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2551. struct auto_pin_cfg *cfg,
  2552. hda_nid_t *ignore_nids)
  2553. {
  2554. hda_nid_t nid, end_nid;
  2555. short seq, assoc_line_out, assoc_speaker;
  2556. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2557. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2558. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2559. memset(cfg, 0, sizeof(*cfg));
  2560. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2561. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2562. memset(sequences_hp, 0, sizeof(sequences_hp));
  2563. assoc_line_out = assoc_speaker = 0;
  2564. end_nid = codec->start_nid + codec->num_nodes;
  2565. for (nid = codec->start_nid; nid < end_nid; nid++) {
  2566. unsigned int wid_caps = get_wcaps(codec, nid);
  2567. unsigned int wid_type =
  2568. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2569. unsigned int def_conf;
  2570. short assoc, loc;
  2571. /* read all default configuration for pin complex */
  2572. if (wid_type != AC_WID_PIN)
  2573. continue;
  2574. /* ignore the given nids (e.g. pc-beep returns error) */
  2575. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2576. continue;
  2577. def_conf = snd_hda_codec_read(codec, nid, 0,
  2578. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2579. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2580. continue;
  2581. loc = get_defcfg_location(def_conf);
  2582. switch (get_defcfg_device(def_conf)) {
  2583. case AC_JACK_LINE_OUT:
  2584. seq = get_defcfg_sequence(def_conf);
  2585. assoc = get_defcfg_association(def_conf);
  2586. if (!(wid_caps & AC_WCAP_STEREO))
  2587. if (!cfg->mono_out_pin)
  2588. cfg->mono_out_pin = nid;
  2589. if (!assoc)
  2590. continue;
  2591. if (!assoc_line_out)
  2592. assoc_line_out = assoc;
  2593. else if (assoc_line_out != assoc)
  2594. continue;
  2595. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2596. continue;
  2597. cfg->line_out_pins[cfg->line_outs] = nid;
  2598. sequences_line_out[cfg->line_outs] = seq;
  2599. cfg->line_outs++;
  2600. break;
  2601. case AC_JACK_SPEAKER:
  2602. seq = get_defcfg_sequence(def_conf);
  2603. assoc = get_defcfg_association(def_conf);
  2604. if (! assoc)
  2605. continue;
  2606. if (! assoc_speaker)
  2607. assoc_speaker = assoc;
  2608. else if (assoc_speaker != assoc)
  2609. continue;
  2610. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2611. continue;
  2612. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2613. sequences_speaker[cfg->speaker_outs] = seq;
  2614. cfg->speaker_outs++;
  2615. break;
  2616. case AC_JACK_HP_OUT:
  2617. seq = get_defcfg_sequence(def_conf);
  2618. assoc = get_defcfg_association(def_conf);
  2619. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2620. continue;
  2621. cfg->hp_pins[cfg->hp_outs] = nid;
  2622. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2623. cfg->hp_outs++;
  2624. break;
  2625. case AC_JACK_MIC_IN: {
  2626. int preferred, alt;
  2627. if (loc == AC_JACK_LOC_FRONT) {
  2628. preferred = AUTO_PIN_FRONT_MIC;
  2629. alt = AUTO_PIN_MIC;
  2630. } else {
  2631. preferred = AUTO_PIN_MIC;
  2632. alt = AUTO_PIN_FRONT_MIC;
  2633. }
  2634. if (!cfg->input_pins[preferred])
  2635. cfg->input_pins[preferred] = nid;
  2636. else if (!cfg->input_pins[alt])
  2637. cfg->input_pins[alt] = nid;
  2638. break;
  2639. }
  2640. case AC_JACK_LINE_IN:
  2641. if (loc == AC_JACK_LOC_FRONT)
  2642. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2643. else
  2644. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2645. break;
  2646. case AC_JACK_CD:
  2647. cfg->input_pins[AUTO_PIN_CD] = nid;
  2648. break;
  2649. case AC_JACK_AUX:
  2650. cfg->input_pins[AUTO_PIN_AUX] = nid;
  2651. break;
  2652. case AC_JACK_SPDIF_OUT:
  2653. cfg->dig_out_pin = nid;
  2654. break;
  2655. case AC_JACK_SPDIF_IN:
  2656. cfg->dig_in_pin = nid;
  2657. break;
  2658. }
  2659. }
  2660. /* FIX-UP:
  2661. * If no line-out is defined but multiple HPs are found,
  2662. * some of them might be the real line-outs.
  2663. */
  2664. if (!cfg->line_outs && cfg->hp_outs > 1) {
  2665. int i = 0;
  2666. while (i < cfg->hp_outs) {
  2667. /* The real HPs should have the sequence 0x0f */
  2668. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  2669. i++;
  2670. continue;
  2671. }
  2672. /* Move it to the line-out table */
  2673. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  2674. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  2675. cfg->line_outs++;
  2676. cfg->hp_outs--;
  2677. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  2678. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  2679. memmove(sequences_hp + i - 1, sequences_hp + i,
  2680. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  2681. }
  2682. }
  2683. /* sort by sequence */
  2684. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  2685. cfg->line_outs);
  2686. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  2687. cfg->speaker_outs);
  2688. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  2689. cfg->hp_outs);
  2690. /* if we have only one mic, make it AUTO_PIN_MIC */
  2691. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  2692. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  2693. cfg->input_pins[AUTO_PIN_MIC] =
  2694. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  2695. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  2696. }
  2697. /* ditto for line-in */
  2698. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  2699. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  2700. cfg->input_pins[AUTO_PIN_LINE] =
  2701. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  2702. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  2703. }
  2704. /*
  2705. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  2706. * as a primary output
  2707. */
  2708. if (!cfg->line_outs) {
  2709. if (cfg->speaker_outs) {
  2710. cfg->line_outs = cfg->speaker_outs;
  2711. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  2712. sizeof(cfg->speaker_pins));
  2713. cfg->speaker_outs = 0;
  2714. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  2715. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  2716. } else if (cfg->hp_outs) {
  2717. cfg->line_outs = cfg->hp_outs;
  2718. memcpy(cfg->line_out_pins, cfg->hp_pins,
  2719. sizeof(cfg->hp_pins));
  2720. cfg->hp_outs = 0;
  2721. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  2722. cfg->line_out_type = AUTO_PIN_HP_OUT;
  2723. }
  2724. }
  2725. /* Reorder the surround channels
  2726. * ALSA sequence is front/surr/clfe/side
  2727. * HDA sequence is:
  2728. * 4-ch: front/surr => OK as it is
  2729. * 6-ch: front/clfe/surr
  2730. * 8-ch: front/clfe/rear/side|fc
  2731. */
  2732. switch (cfg->line_outs) {
  2733. case 3:
  2734. case 4:
  2735. nid = cfg->line_out_pins[1];
  2736. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  2737. cfg->line_out_pins[2] = nid;
  2738. break;
  2739. }
  2740. /*
  2741. * debug prints of the parsed results
  2742. */
  2743. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2744. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  2745. cfg->line_out_pins[2], cfg->line_out_pins[3],
  2746. cfg->line_out_pins[4]);
  2747. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2748. cfg->speaker_outs, cfg->speaker_pins[0],
  2749. cfg->speaker_pins[1], cfg->speaker_pins[2],
  2750. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  2751. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2752. cfg->hp_outs, cfg->hp_pins[0],
  2753. cfg->hp_pins[1], cfg->hp_pins[2],
  2754. cfg->hp_pins[3], cfg->hp_pins[4]);
  2755. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  2756. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  2757. " cd=0x%x, aux=0x%x\n",
  2758. cfg->input_pins[AUTO_PIN_MIC],
  2759. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  2760. cfg->input_pins[AUTO_PIN_LINE],
  2761. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  2762. cfg->input_pins[AUTO_PIN_CD],
  2763. cfg->input_pins[AUTO_PIN_AUX]);
  2764. return 0;
  2765. }
  2766. /* labels for input pins */
  2767. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  2768. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  2769. };
  2770. #ifdef CONFIG_PM
  2771. /*
  2772. * power management
  2773. */
  2774. /**
  2775. * snd_hda_suspend - suspend the codecs
  2776. * @bus: the HDA bus
  2777. * @state: suspsend state
  2778. *
  2779. * Returns 0 if successful.
  2780. */
  2781. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  2782. {
  2783. struct hda_codec *codec;
  2784. list_for_each_entry(codec, &bus->codec_list, list) {
  2785. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2786. if (!codec->power_on)
  2787. continue;
  2788. #endif
  2789. hda_call_codec_suspend(codec);
  2790. }
  2791. return 0;
  2792. }
  2793. /**
  2794. * snd_hda_resume - resume the codecs
  2795. * @bus: the HDA bus
  2796. * @state: resume state
  2797. *
  2798. * Returns 0 if successful.
  2799. *
  2800. * This fucntion is defined only when POWER_SAVE isn't set.
  2801. * In the power-save mode, the codec is resumed dynamically.
  2802. */
  2803. int snd_hda_resume(struct hda_bus *bus)
  2804. {
  2805. struct hda_codec *codec;
  2806. list_for_each_entry(codec, &bus->codec_list, list) {
  2807. if (snd_hda_codec_needs_resume(codec))
  2808. hda_call_codec_resume(codec);
  2809. }
  2810. return 0;
  2811. }
  2812. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2813. int snd_hda_codecs_inuse(struct hda_bus *bus)
  2814. {
  2815. struct hda_codec *codec;
  2816. list_for_each_entry(codec, &bus->codec_list, list) {
  2817. if (snd_hda_codec_needs_resume(codec))
  2818. return 1;
  2819. }
  2820. return 0;
  2821. }
  2822. #endif
  2823. #endif