hda_codec.c 73 KB

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