hda_codec.c 62 KB

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