hda_codec.c 73 KB

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