hda_codec.c 55 KB

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