hda_codec.c 81 KB

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