hda_codec.c 63 KB

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