hda_codec.c 64 KB

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