hda_codec.c 64 KB

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