hda_codec.c 53 KB

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