hda_codec.c 62 KB

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