hda_codec.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  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 <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/pci.h>
  25. #include <linux/mutex.h>
  26. #include <sound/core.h>
  27. #include "hda_codec.h"
  28. #include <sound/asoundef.h>
  29. #include <sound/tlv.h>
  30. #include <sound/initval.h>
  31. #include "hda_local.h"
  32. #include <sound/hda_hwdep.h>
  33. /*
  34. * vendor / preset table
  35. */
  36. struct hda_vendor_id {
  37. unsigned int id;
  38. const char *name;
  39. };
  40. /* codec vendor labels */
  41. static struct hda_vendor_id hda_vendor_ids[] = {
  42. { 0x1002, "ATI" },
  43. { 0x1057, "Motorola" },
  44. { 0x1095, "Silicon Image" },
  45. { 0x10de, "Nvidia" },
  46. { 0x10ec, "Realtek" },
  47. { 0x1106, "VIA" },
  48. { 0x111d, "IDT" },
  49. { 0x11c1, "LSI" },
  50. { 0x11d4, "Analog Devices" },
  51. { 0x13f6, "C-Media" },
  52. { 0x14f1, "Conexant" },
  53. { 0x17e8, "Chrontel" },
  54. { 0x1854, "LG" },
  55. { 0x1aec, "Wolfson Microelectronics" },
  56. { 0x434d, "C-Media" },
  57. { 0x8086, "Intel" },
  58. { 0x8384, "SigmaTel" },
  59. {} /* terminator */
  60. };
  61. static DEFINE_MUTEX(preset_mutex);
  62. static LIST_HEAD(hda_preset_tables);
  63. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  64. {
  65. mutex_lock(&preset_mutex);
  66. list_add_tail(&preset->list, &hda_preset_tables);
  67. mutex_unlock(&preset_mutex);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
  71. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  72. {
  73. mutex_lock(&preset_mutex);
  74. list_del(&preset->list);
  75. mutex_unlock(&preset_mutex);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
  79. #ifdef CONFIG_SND_HDA_POWER_SAVE
  80. static void hda_power_work(struct work_struct *work);
  81. static void hda_keep_power_on(struct hda_codec *codec);
  82. #else
  83. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  84. #endif
  85. const char *snd_hda_get_jack_location(u32 cfg)
  86. {
  87. static char *bases[7] = {
  88. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  89. };
  90. static unsigned char specials_idx[] = {
  91. 0x07, 0x08,
  92. 0x17, 0x18, 0x19,
  93. 0x37, 0x38
  94. };
  95. static char *specials[] = {
  96. "Rear Panel", "Drive Bar",
  97. "Riser", "HDMI", "ATAPI",
  98. "Mobile-In", "Mobile-Out"
  99. };
  100. int i;
  101. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  102. if ((cfg & 0x0f) < 7)
  103. return bases[cfg & 0x0f];
  104. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  105. if (cfg == specials_idx[i])
  106. return specials[i];
  107. }
  108. return "UNKNOWN";
  109. }
  110. EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
  111. const char *snd_hda_get_jack_connectivity(u32 cfg)
  112. {
  113. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  114. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  115. }
  116. EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
  117. const char *snd_hda_get_jack_type(u32 cfg)
  118. {
  119. static char *jack_types[16] = {
  120. "Line Out", "Speaker", "HP Out", "CD",
  121. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  122. "Line In", "Aux", "Mic", "Telephony",
  123. "SPDIF In", "Digitial In", "Reserved", "Other"
  124. };
  125. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  126. >> AC_DEFCFG_DEVICE_SHIFT];
  127. }
  128. EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
  129. /*
  130. * Compose a 32bit command word to be sent to the HD-audio controller
  131. */
  132. static inline unsigned int
  133. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  134. unsigned int verb, unsigned int parm)
  135. {
  136. u32 val;
  137. val = (u32)(codec->addr & 0x0f) << 28;
  138. val |= (u32)direct << 27;
  139. val |= (u32)nid << 20;
  140. val |= verb << 8;
  141. val |= parm;
  142. return val;
  143. }
  144. /**
  145. * snd_hda_codec_read - send a command and get the response
  146. * @codec: the HDA codec
  147. * @nid: NID to send the command
  148. * @direct: direct flag
  149. * @verb: the verb to send
  150. * @parm: the parameter for the verb
  151. *
  152. * Send a single command and read the corresponding response.
  153. *
  154. * Returns the obtained response value, or -1 for an error.
  155. */
  156. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  157. int direct,
  158. unsigned int verb, unsigned int parm)
  159. {
  160. struct hda_bus *bus = codec->bus;
  161. unsigned int res;
  162. res = make_codec_cmd(codec, nid, direct, verb, parm);
  163. snd_hda_power_up(codec);
  164. mutex_lock(&bus->cmd_mutex);
  165. if (!bus->ops.command(bus, res))
  166. res = bus->ops.get_response(bus);
  167. else
  168. res = (unsigned int)-1;
  169. mutex_unlock(&bus->cmd_mutex);
  170. snd_hda_power_down(codec);
  171. return res;
  172. }
  173. EXPORT_SYMBOL_HDA(snd_hda_codec_read);
  174. /**
  175. * snd_hda_codec_write - send a single command without waiting for response
  176. * @codec: the HDA codec
  177. * @nid: NID to send the command
  178. * @direct: direct flag
  179. * @verb: the verb to send
  180. * @parm: the parameter for the verb
  181. *
  182. * Send a single command without waiting for response.
  183. *
  184. * Returns 0 if successful, or a negative error code.
  185. */
  186. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  187. unsigned int verb, unsigned int parm)
  188. {
  189. struct hda_bus *bus = codec->bus;
  190. unsigned int res;
  191. int err;
  192. res = make_codec_cmd(codec, nid, direct, verb, parm);
  193. snd_hda_power_up(codec);
  194. mutex_lock(&bus->cmd_mutex);
  195. err = bus->ops.command(bus, res);
  196. mutex_unlock(&bus->cmd_mutex);
  197. snd_hda_power_down(codec);
  198. return err;
  199. }
  200. EXPORT_SYMBOL_HDA(snd_hda_codec_write);
  201. /**
  202. * snd_hda_sequence_write - sequence writes
  203. * @codec: the HDA codec
  204. * @seq: VERB array to send
  205. *
  206. * Send the commands sequentially from the given array.
  207. * The array must be terminated with NID=0.
  208. */
  209. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  210. {
  211. for (; seq->nid; seq++)
  212. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  213. }
  214. EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
  215. /**
  216. * snd_hda_get_sub_nodes - get the range of sub nodes
  217. * @codec: the HDA codec
  218. * @nid: NID to parse
  219. * @start_id: the pointer to store the start NID
  220. *
  221. * Parse the NID and store the start NID of its sub-nodes.
  222. * Returns the number of sub-nodes.
  223. */
  224. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  225. hda_nid_t *start_id)
  226. {
  227. unsigned int parm;
  228. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  229. if (parm == -1)
  230. return 0;
  231. *start_id = (parm >> 16) & 0x7fff;
  232. return (int)(parm & 0x7fff);
  233. }
  234. EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
  235. /**
  236. * snd_hda_get_connections - get connection list
  237. * @codec: the HDA codec
  238. * @nid: NID to parse
  239. * @conn_list: connection list array
  240. * @max_conns: max. number of connections to store
  241. *
  242. * Parses the connection list of the given widget and stores the list
  243. * of NIDs.
  244. *
  245. * Returns the number of connections, or a negative error code.
  246. */
  247. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  248. hda_nid_t *conn_list, int max_conns)
  249. {
  250. unsigned int parm;
  251. int i, conn_len, conns;
  252. unsigned int shift, num_elems, mask;
  253. hda_nid_t prev_nid;
  254. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  255. return -EINVAL;
  256. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  257. if (parm & AC_CLIST_LONG) {
  258. /* long form */
  259. shift = 16;
  260. num_elems = 2;
  261. } else {
  262. /* short form */
  263. shift = 8;
  264. num_elems = 4;
  265. }
  266. conn_len = parm & AC_CLIST_LENGTH;
  267. mask = (1 << (shift-1)) - 1;
  268. if (!conn_len)
  269. return 0; /* no connection */
  270. if (conn_len == 1) {
  271. /* single connection */
  272. parm = snd_hda_codec_read(codec, nid, 0,
  273. AC_VERB_GET_CONNECT_LIST, 0);
  274. conn_list[0] = parm & mask;
  275. return 1;
  276. }
  277. /* multi connection */
  278. conns = 0;
  279. prev_nid = 0;
  280. for (i = 0; i < conn_len; i++) {
  281. int range_val;
  282. hda_nid_t val, n;
  283. if (i % num_elems == 0)
  284. parm = snd_hda_codec_read(codec, nid, 0,
  285. AC_VERB_GET_CONNECT_LIST, i);
  286. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  287. val = parm & mask;
  288. parm >>= shift;
  289. if (range_val) {
  290. /* ranges between the previous and this one */
  291. if (!prev_nid || prev_nid >= val) {
  292. snd_printk(KERN_WARNING "hda_codec: "
  293. "invalid dep_range_val %x:%x\n",
  294. prev_nid, val);
  295. continue;
  296. }
  297. for (n = prev_nid + 1; n <= val; n++) {
  298. if (conns >= max_conns) {
  299. snd_printk(KERN_ERR
  300. "Too many connections\n");
  301. return -EINVAL;
  302. }
  303. conn_list[conns++] = n;
  304. }
  305. } else {
  306. if (conns >= max_conns) {
  307. snd_printk(KERN_ERR "Too many connections\n");
  308. return -EINVAL;
  309. }
  310. conn_list[conns++] = val;
  311. }
  312. prev_nid = val;
  313. }
  314. return conns;
  315. }
  316. EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  317. /**
  318. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  319. * @bus: the BUS
  320. * @res: unsolicited event (lower 32bit of RIRB entry)
  321. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  322. *
  323. * Adds the given event to the queue. The events are processed in
  324. * the workqueue asynchronously. Call this function in the interrupt
  325. * hanlder when RIRB receives an unsolicited event.
  326. *
  327. * Returns 0 if successful, or a negative error code.
  328. */
  329. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  330. {
  331. struct hda_bus_unsolicited *unsol;
  332. unsigned int wp;
  333. unsol = bus->unsol;
  334. if (!unsol)
  335. return 0;
  336. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  337. unsol->wp = wp;
  338. wp <<= 1;
  339. unsol->queue[wp] = res;
  340. unsol->queue[wp + 1] = res_ex;
  341. schedule_work(&unsol->work);
  342. return 0;
  343. }
  344. EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
  345. /*
  346. * process queued unsolicited events
  347. */
  348. static void process_unsol_events(struct work_struct *work)
  349. {
  350. struct hda_bus_unsolicited *unsol =
  351. container_of(work, struct hda_bus_unsolicited, work);
  352. struct hda_bus *bus = unsol->bus;
  353. struct hda_codec *codec;
  354. unsigned int rp, caddr, res;
  355. while (unsol->rp != unsol->wp) {
  356. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  357. unsol->rp = rp;
  358. rp <<= 1;
  359. res = unsol->queue[rp];
  360. caddr = unsol->queue[rp + 1];
  361. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  362. continue;
  363. codec = bus->caddr_tbl[caddr & 0x0f];
  364. if (codec && codec->patch_ops.unsol_event)
  365. codec->patch_ops.unsol_event(codec, res);
  366. }
  367. }
  368. /*
  369. * initialize unsolicited queue
  370. */
  371. static int init_unsol_queue(struct hda_bus *bus)
  372. {
  373. struct hda_bus_unsolicited *unsol;
  374. if (bus->unsol) /* already initialized */
  375. return 0;
  376. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  377. if (!unsol) {
  378. snd_printk(KERN_ERR "hda_codec: "
  379. "can't allocate unsolicited queue\n");
  380. return -ENOMEM;
  381. }
  382. INIT_WORK(&unsol->work, process_unsol_events);
  383. unsol->bus = bus;
  384. bus->unsol = unsol;
  385. return 0;
  386. }
  387. /*
  388. * destructor
  389. */
  390. static void snd_hda_codec_free(struct hda_codec *codec);
  391. static int snd_hda_bus_free(struct hda_bus *bus)
  392. {
  393. struct hda_codec *codec, *n;
  394. if (!bus)
  395. return 0;
  396. if (bus->unsol) {
  397. flush_scheduled_work();
  398. kfree(bus->unsol);
  399. }
  400. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  401. snd_hda_codec_free(codec);
  402. }
  403. if (bus->ops.private_free)
  404. bus->ops.private_free(bus);
  405. kfree(bus);
  406. return 0;
  407. }
  408. static int snd_hda_bus_dev_free(struct snd_device *device)
  409. {
  410. struct hda_bus *bus = device->device_data;
  411. bus->shutdown = 1;
  412. return snd_hda_bus_free(bus);
  413. }
  414. #ifdef CONFIG_SND_HDA_HWDEP
  415. static int snd_hda_bus_dev_register(struct snd_device *device)
  416. {
  417. struct hda_bus *bus = device->device_data;
  418. struct hda_codec *codec;
  419. list_for_each_entry(codec, &bus->codec_list, list) {
  420. snd_hda_hwdep_add_sysfs(codec);
  421. }
  422. return 0;
  423. }
  424. #else
  425. #define snd_hda_bus_dev_register NULL
  426. #endif
  427. /**
  428. * snd_hda_bus_new - create a HDA bus
  429. * @card: the card entry
  430. * @temp: the template for hda_bus information
  431. * @busp: the pointer to store the created bus instance
  432. *
  433. * Returns 0 if successful, or a negative error code.
  434. */
  435. int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
  436. const struct hda_bus_template *temp,
  437. struct hda_bus **busp)
  438. {
  439. struct hda_bus *bus;
  440. int err;
  441. static struct snd_device_ops dev_ops = {
  442. .dev_register = snd_hda_bus_dev_register,
  443. .dev_free = snd_hda_bus_dev_free,
  444. };
  445. if (snd_BUG_ON(!temp))
  446. return -EINVAL;
  447. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  448. return -EINVAL;
  449. if (busp)
  450. *busp = NULL;
  451. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  452. if (bus == NULL) {
  453. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  454. return -ENOMEM;
  455. }
  456. bus->card = card;
  457. bus->private_data = temp->private_data;
  458. bus->pci = temp->pci;
  459. bus->modelname = temp->modelname;
  460. bus->power_save = temp->power_save;
  461. bus->ops = temp->ops;
  462. mutex_init(&bus->cmd_mutex);
  463. INIT_LIST_HEAD(&bus->codec_list);
  464. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  465. if (err < 0) {
  466. snd_hda_bus_free(bus);
  467. return err;
  468. }
  469. if (busp)
  470. *busp = bus;
  471. return 0;
  472. }
  473. EXPORT_SYMBOL_HDA(snd_hda_bus_new);
  474. #ifdef CONFIG_SND_HDA_GENERIC
  475. #define is_generic_config(codec) \
  476. (codec->modelname && !strcmp(codec->modelname, "generic"))
  477. #else
  478. #define is_generic_config(codec) 0
  479. #endif
  480. #ifdef MODULE
  481. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  482. #else
  483. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  484. #endif
  485. /*
  486. * find a matching codec preset
  487. */
  488. static const struct hda_codec_preset *
  489. find_codec_preset(struct hda_codec *codec)
  490. {
  491. struct hda_codec_preset_list *tbl;
  492. const struct hda_codec_preset *preset;
  493. int mod_requested = 0;
  494. if (is_generic_config(codec))
  495. return NULL; /* use the generic parser */
  496. again:
  497. mutex_lock(&preset_mutex);
  498. list_for_each_entry(tbl, &hda_preset_tables, list) {
  499. if (!try_module_get(tbl->owner)) {
  500. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  501. continue;
  502. }
  503. for (preset = tbl->preset; preset->id; preset++) {
  504. u32 mask = preset->mask;
  505. if (preset->afg && preset->afg != codec->afg)
  506. continue;
  507. if (preset->mfg && preset->mfg != codec->mfg)
  508. continue;
  509. if (!mask)
  510. mask = ~0;
  511. if (preset->id == (codec->vendor_id & mask) &&
  512. (!preset->rev ||
  513. preset->rev == codec->revision_id)) {
  514. mutex_unlock(&preset_mutex);
  515. codec->owner = tbl->owner;
  516. return preset;
  517. }
  518. }
  519. module_put(tbl->owner);
  520. }
  521. mutex_unlock(&preset_mutex);
  522. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  523. char name[32];
  524. if (!mod_requested)
  525. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  526. codec->vendor_id);
  527. else
  528. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  529. (codec->vendor_id >> 16) & 0xffff);
  530. request_module(name);
  531. mod_requested++;
  532. goto again;
  533. }
  534. return NULL;
  535. }
  536. /*
  537. * get_codec_name - store the codec name
  538. */
  539. static int get_codec_name(struct hda_codec *codec)
  540. {
  541. const struct hda_vendor_id *c;
  542. const char *vendor = NULL;
  543. u16 vendor_id = codec->vendor_id >> 16;
  544. char tmp[16], name[32];
  545. for (c = hda_vendor_ids; c->id; c++) {
  546. if (c->id == vendor_id) {
  547. vendor = c->name;
  548. break;
  549. }
  550. }
  551. if (!vendor) {
  552. sprintf(tmp, "Generic %04x", vendor_id);
  553. vendor = tmp;
  554. }
  555. if (codec->preset && codec->preset->name)
  556. snprintf(name, sizeof(name), "%s %s", vendor,
  557. codec->preset->name);
  558. else
  559. snprintf(name, sizeof(name), "%s ID %x", vendor,
  560. codec->vendor_id & 0xffff);
  561. codec->name = kstrdup(name, GFP_KERNEL);
  562. if (!codec->name)
  563. return -ENOMEM;
  564. return 0;
  565. }
  566. /*
  567. * look for an AFG and MFG nodes
  568. */
  569. static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
  570. {
  571. int i, total_nodes;
  572. hda_nid_t nid;
  573. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  574. for (i = 0; i < total_nodes; i++, nid++) {
  575. unsigned int func;
  576. func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
  577. switch (func & 0xff) {
  578. case AC_GRP_AUDIO_FUNCTION:
  579. codec->afg = nid;
  580. break;
  581. case AC_GRP_MODEM_FUNCTION:
  582. codec->mfg = nid;
  583. break;
  584. default:
  585. break;
  586. }
  587. }
  588. }
  589. /*
  590. * read widget caps for each widget and store in cache
  591. */
  592. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  593. {
  594. int i;
  595. hda_nid_t nid;
  596. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  597. &codec->start_nid);
  598. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  599. if (!codec->wcaps)
  600. return -ENOMEM;
  601. nid = codec->start_nid;
  602. for (i = 0; i < codec->num_nodes; i++, nid++)
  603. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  604. AC_PAR_AUDIO_WIDGET_CAP);
  605. return 0;
  606. }
  607. static void init_hda_cache(struct hda_cache_rec *cache,
  608. unsigned int record_size);
  609. static void free_hda_cache(struct hda_cache_rec *cache);
  610. /*
  611. * codec destructor
  612. */
  613. static void snd_hda_codec_free(struct hda_codec *codec)
  614. {
  615. if (!codec)
  616. return;
  617. #ifdef CONFIG_SND_HDA_POWER_SAVE
  618. cancel_delayed_work(&codec->power_work);
  619. flush_scheduled_work();
  620. #endif
  621. list_del(&codec->list);
  622. snd_array_free(&codec->mixers);
  623. codec->bus->caddr_tbl[codec->addr] = NULL;
  624. if (codec->patch_ops.free)
  625. codec->patch_ops.free(codec);
  626. module_put(codec->owner);
  627. free_hda_cache(&codec->amp_cache);
  628. free_hda_cache(&codec->cmd_cache);
  629. kfree(codec->name);
  630. kfree(codec->modelname);
  631. kfree(codec->wcaps);
  632. kfree(codec);
  633. }
  634. /**
  635. * snd_hda_codec_new - create a HDA codec
  636. * @bus: the bus to assign
  637. * @codec_addr: the codec address
  638. * @codecp: the pointer to store the generated codec
  639. *
  640. * Returns 0 if successful, or a negative error code.
  641. */
  642. int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  643. struct hda_codec **codecp)
  644. {
  645. struct hda_codec *codec;
  646. char component[31];
  647. int err;
  648. if (snd_BUG_ON(!bus))
  649. return -EINVAL;
  650. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  651. return -EINVAL;
  652. if (bus->caddr_tbl[codec_addr]) {
  653. snd_printk(KERN_ERR "hda_codec: "
  654. "address 0x%x is already occupied\n", codec_addr);
  655. return -EBUSY;
  656. }
  657. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  658. if (codec == NULL) {
  659. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  660. return -ENOMEM;
  661. }
  662. codec->bus = bus;
  663. codec->addr = codec_addr;
  664. mutex_init(&codec->spdif_mutex);
  665. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  666. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  667. snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
  668. if (codec->bus->modelname) {
  669. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  670. if (!codec->modelname) {
  671. snd_hda_codec_free(codec);
  672. return -ENODEV;
  673. }
  674. }
  675. #ifdef CONFIG_SND_HDA_POWER_SAVE
  676. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  677. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  678. * the caller has to power down appropriatley after initialization
  679. * phase.
  680. */
  681. hda_keep_power_on(codec);
  682. #endif
  683. list_add_tail(&codec->list, &bus->codec_list);
  684. bus->caddr_tbl[codec_addr] = codec;
  685. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  686. AC_PAR_VENDOR_ID);
  687. if (codec->vendor_id == -1)
  688. /* read again, hopefully the access method was corrected
  689. * in the last read...
  690. */
  691. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  692. AC_PAR_VENDOR_ID);
  693. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  694. AC_PAR_SUBSYSTEM_ID);
  695. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  696. AC_PAR_REV_ID);
  697. setup_fg_nodes(codec);
  698. if (!codec->afg && !codec->mfg) {
  699. snd_printdd("hda_codec: no AFG or MFG node found\n");
  700. snd_hda_codec_free(codec);
  701. return -ENODEV;
  702. }
  703. if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
  704. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  705. snd_hda_codec_free(codec);
  706. return -ENOMEM;
  707. }
  708. if (!codec->subsystem_id) {
  709. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  710. codec->subsystem_id =
  711. snd_hda_codec_read(codec, nid, 0,
  712. AC_VERB_GET_SUBSYSTEM_ID, 0);
  713. }
  714. if (bus->modelname)
  715. codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
  716. err = snd_hda_codec_configure(codec);
  717. if (err < 0) {
  718. snd_hda_codec_free(codec);
  719. return err;
  720. }
  721. snd_hda_codec_proc_new(codec);
  722. snd_hda_create_hwdep(codec);
  723. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  724. codec->subsystem_id, codec->revision_id);
  725. snd_component_add(codec->bus->card, component);
  726. if (codecp)
  727. *codecp = codec;
  728. return 0;
  729. }
  730. EXPORT_SYMBOL_HDA(snd_hda_codec_new);
  731. int snd_hda_codec_configure(struct hda_codec *codec)
  732. {
  733. int err;
  734. codec->preset = find_codec_preset(codec);
  735. if (!codec->name) {
  736. err = get_codec_name(codec);
  737. if (err < 0)
  738. return err;
  739. }
  740. /* audio codec should override the mixer name */
  741. if (codec->afg || !*codec->bus->card->mixername)
  742. strlcpy(codec->bus->card->mixername, codec->name,
  743. sizeof(codec->bus->card->mixername));
  744. if (is_generic_config(codec)) {
  745. err = snd_hda_parse_generic_codec(codec);
  746. goto patched;
  747. }
  748. if (codec->preset && codec->preset->patch) {
  749. err = codec->preset->patch(codec);
  750. goto patched;
  751. }
  752. /* call the default parser */
  753. err = snd_hda_parse_generic_codec(codec);
  754. if (err < 0)
  755. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  756. patched:
  757. if (!err && codec->patch_ops.unsol_event)
  758. err = init_unsol_queue(codec->bus);
  759. return err;
  760. }
  761. /**
  762. * snd_hda_codec_setup_stream - set up the codec for streaming
  763. * @codec: the CODEC to set up
  764. * @nid: the NID to set up
  765. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  766. * @channel_id: channel id to pass, zero based.
  767. * @format: stream format.
  768. */
  769. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  770. u32 stream_tag,
  771. int channel_id, int format)
  772. {
  773. if (!nid)
  774. return;
  775. snd_printdd("hda_codec_setup_stream: "
  776. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  777. nid, stream_tag, channel_id, format);
  778. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  779. (stream_tag << 4) | channel_id);
  780. msleep(1);
  781. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  782. }
  783. EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
  784. void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  785. {
  786. if (!nid)
  787. return;
  788. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  789. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  790. #if 0 /* keep the format */
  791. msleep(1);
  792. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  793. #endif
  794. }
  795. EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
  796. /*
  797. * amp access functions
  798. */
  799. /* FIXME: more better hash key? */
  800. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  801. #define INFO_AMP_CAPS (1<<0)
  802. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  803. /* initialize the hash table */
  804. static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
  805. unsigned int record_size)
  806. {
  807. memset(cache, 0, sizeof(*cache));
  808. memset(cache->hash, 0xff, sizeof(cache->hash));
  809. snd_array_init(&cache->buf, record_size, 64);
  810. }
  811. static void free_hda_cache(struct hda_cache_rec *cache)
  812. {
  813. snd_array_free(&cache->buf);
  814. }
  815. /* query the hash. allocate an entry if not found. */
  816. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  817. u32 key)
  818. {
  819. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  820. u16 cur = cache->hash[idx];
  821. struct hda_cache_head *info;
  822. while (cur != 0xffff) {
  823. info = snd_array_elem(&cache->buf, cur);
  824. if (info->key == key)
  825. return info;
  826. cur = info->next;
  827. }
  828. /* add a new hash entry */
  829. info = snd_array_new(&cache->buf);
  830. if (!info)
  831. return NULL;
  832. cur = snd_array_index(&cache->buf, info);
  833. info->key = key;
  834. info->val = 0;
  835. info->next = cache->hash[idx];
  836. cache->hash[idx] = cur;
  837. return info;
  838. }
  839. /* query and allocate an amp hash entry */
  840. static inline struct hda_amp_info *
  841. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  842. {
  843. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  844. }
  845. /*
  846. * query AMP capabilities for the given widget and direction
  847. */
  848. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  849. {
  850. struct hda_amp_info *info;
  851. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  852. if (!info)
  853. return 0;
  854. if (!(info->head.val & INFO_AMP_CAPS)) {
  855. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  856. nid = codec->afg;
  857. info->amp_caps = snd_hda_param_read(codec, nid,
  858. direction == HDA_OUTPUT ?
  859. AC_PAR_AMP_OUT_CAP :
  860. AC_PAR_AMP_IN_CAP);
  861. if (info->amp_caps)
  862. info->head.val |= INFO_AMP_CAPS;
  863. }
  864. return info->amp_caps;
  865. }
  866. EXPORT_SYMBOL_HDA(query_amp_caps);
  867. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  868. unsigned int caps)
  869. {
  870. struct hda_amp_info *info;
  871. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  872. if (!info)
  873. return -EINVAL;
  874. info->amp_caps = caps;
  875. info->head.val |= INFO_AMP_CAPS;
  876. return 0;
  877. }
  878. EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
  879. /*
  880. * read the current volume to info
  881. * if the cache exists, read the cache value.
  882. */
  883. static unsigned int get_vol_mute(struct hda_codec *codec,
  884. struct hda_amp_info *info, hda_nid_t nid,
  885. int ch, int direction, int index)
  886. {
  887. u32 val, parm;
  888. if (info->head.val & INFO_AMP_VOL(ch))
  889. return info->vol[ch];
  890. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  891. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  892. parm |= index;
  893. val = snd_hda_codec_read(codec, nid, 0,
  894. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  895. info->vol[ch] = val & 0xff;
  896. info->head.val |= INFO_AMP_VOL(ch);
  897. return info->vol[ch];
  898. }
  899. /*
  900. * write the current volume in info to the h/w and update the cache
  901. */
  902. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  903. hda_nid_t nid, int ch, int direction, int index,
  904. int val)
  905. {
  906. u32 parm;
  907. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  908. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  909. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  910. parm |= val;
  911. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  912. info->vol[ch] = val;
  913. }
  914. /*
  915. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  916. */
  917. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  918. int direction, int index)
  919. {
  920. struct hda_amp_info *info;
  921. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  922. if (!info)
  923. return 0;
  924. return get_vol_mute(codec, info, nid, ch, direction, index);
  925. }
  926. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
  927. /*
  928. * update the AMP value, mask = bit mask to set, val = the value
  929. */
  930. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  931. int direction, int idx, int mask, int val)
  932. {
  933. struct hda_amp_info *info;
  934. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  935. if (!info)
  936. return 0;
  937. val &= mask;
  938. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  939. if (info->vol[ch] == val)
  940. return 0;
  941. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  942. return 1;
  943. }
  944. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
  945. /*
  946. * update the AMP stereo with the same mask and value
  947. */
  948. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  949. int direction, int idx, int mask, int val)
  950. {
  951. int ch, ret = 0;
  952. for (ch = 0; ch < 2; ch++)
  953. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  954. idx, mask, val);
  955. return ret;
  956. }
  957. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
  958. #ifdef SND_HDA_NEEDS_RESUME
  959. /* resume the all amp commands from the cache */
  960. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  961. {
  962. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  963. int i;
  964. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  965. u32 key = buffer->head.key;
  966. hda_nid_t nid;
  967. unsigned int idx, dir, ch;
  968. if (!key)
  969. continue;
  970. nid = key & 0xff;
  971. idx = (key >> 16) & 0xff;
  972. dir = (key >> 24) & 0xff;
  973. for (ch = 0; ch < 2; ch++) {
  974. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  975. continue;
  976. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  977. buffer->vol[ch]);
  978. }
  979. }
  980. }
  981. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
  982. #endif /* SND_HDA_NEEDS_RESUME */
  983. /* volume */
  984. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  985. struct snd_ctl_elem_info *uinfo)
  986. {
  987. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  988. u16 nid = get_amp_nid(kcontrol);
  989. u8 chs = get_amp_channels(kcontrol);
  990. int dir = get_amp_direction(kcontrol);
  991. u32 caps;
  992. caps = query_amp_caps(codec, nid, dir);
  993. /* num steps */
  994. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  995. if (!caps) {
  996. printk(KERN_WARNING "hda_codec: "
  997. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  998. kcontrol->id.name);
  999. return -EINVAL;
  1000. }
  1001. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1002. uinfo->count = chs == 3 ? 2 : 1;
  1003. uinfo->value.integer.min = 0;
  1004. uinfo->value.integer.max = caps;
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
  1008. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1009. struct snd_ctl_elem_value *ucontrol)
  1010. {
  1011. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1012. hda_nid_t nid = get_amp_nid(kcontrol);
  1013. int chs = get_amp_channels(kcontrol);
  1014. int dir = get_amp_direction(kcontrol);
  1015. int idx = get_amp_index(kcontrol);
  1016. long *valp = ucontrol->value.integer.value;
  1017. if (chs & 1)
  1018. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
  1019. & HDA_AMP_VOLMASK;
  1020. if (chs & 2)
  1021. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
  1022. & HDA_AMP_VOLMASK;
  1023. return 0;
  1024. }
  1025. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
  1026. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1027. struct snd_ctl_elem_value *ucontrol)
  1028. {
  1029. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1030. hda_nid_t nid = get_amp_nid(kcontrol);
  1031. int chs = get_amp_channels(kcontrol);
  1032. int dir = get_amp_direction(kcontrol);
  1033. int idx = get_amp_index(kcontrol);
  1034. long *valp = ucontrol->value.integer.value;
  1035. int change = 0;
  1036. snd_hda_power_up(codec);
  1037. if (chs & 1) {
  1038. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1039. 0x7f, *valp);
  1040. valp++;
  1041. }
  1042. if (chs & 2)
  1043. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1044. 0x7f, *valp);
  1045. snd_hda_power_down(codec);
  1046. return change;
  1047. }
  1048. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
  1049. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1050. unsigned int size, unsigned int __user *_tlv)
  1051. {
  1052. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1053. hda_nid_t nid = get_amp_nid(kcontrol);
  1054. int dir = get_amp_direction(kcontrol);
  1055. u32 caps, val1, val2;
  1056. if (size < 4 * sizeof(unsigned int))
  1057. return -ENOMEM;
  1058. caps = query_amp_caps(codec, nid, dir);
  1059. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1060. val2 = (val2 + 1) * 25;
  1061. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1062. val1 = ((int)val1) * ((int)val2);
  1063. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1064. return -EFAULT;
  1065. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1066. return -EFAULT;
  1067. if (put_user(val1, _tlv + 2))
  1068. return -EFAULT;
  1069. if (put_user(val2, _tlv + 3))
  1070. return -EFAULT;
  1071. return 0;
  1072. }
  1073. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
  1074. /*
  1075. * set (static) TLV for virtual master volume; recalculated as max 0dB
  1076. */
  1077. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1078. unsigned int *tlv)
  1079. {
  1080. u32 caps;
  1081. int nums, step;
  1082. caps = query_amp_caps(codec, nid, dir);
  1083. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1084. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1085. step = (step + 1) * 25;
  1086. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1087. tlv[1] = 2 * sizeof(unsigned int);
  1088. tlv[2] = -nums * step;
  1089. tlv[3] = step;
  1090. }
  1091. EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
  1092. /* find a mixer control element with the given name */
  1093. static struct snd_kcontrol *
  1094. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1095. const char *name, int idx)
  1096. {
  1097. struct snd_ctl_elem_id id;
  1098. memset(&id, 0, sizeof(id));
  1099. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1100. id.index = idx;
  1101. strcpy(id.name, name);
  1102. return snd_ctl_find_id(codec->bus->card, &id);
  1103. }
  1104. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1105. const char *name)
  1106. {
  1107. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1108. }
  1109. EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
  1110. /* Add a control element and assign to the codec */
  1111. int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
  1112. {
  1113. int err;
  1114. struct snd_kcontrol **knewp;
  1115. err = snd_ctl_add(codec->bus->card, kctl);
  1116. if (err < 0)
  1117. return err;
  1118. knewp = snd_array_new(&codec->mixers);
  1119. if (!knewp)
  1120. return -ENOMEM;
  1121. *knewp = kctl;
  1122. return 0;
  1123. }
  1124. EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
  1125. #ifdef CONFIG_SND_HDA_RECONFIG
  1126. /* Clear all controls assigned to the given codec */
  1127. void snd_hda_ctls_clear(struct hda_codec *codec)
  1128. {
  1129. int i;
  1130. struct snd_kcontrol **kctls = codec->mixers.list;
  1131. for (i = 0; i < codec->mixers.used; i++)
  1132. snd_ctl_remove(codec->bus->card, kctls[i]);
  1133. snd_array_free(&codec->mixers);
  1134. }
  1135. void snd_hda_codec_reset(struct hda_codec *codec)
  1136. {
  1137. int i;
  1138. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1139. cancel_delayed_work(&codec->power_work);
  1140. flush_scheduled_work();
  1141. #endif
  1142. snd_hda_ctls_clear(codec);
  1143. /* relase PCMs */
  1144. for (i = 0; i < codec->num_pcms; i++) {
  1145. if (codec->pcm_info[i].pcm) {
  1146. snd_device_free(codec->bus->card,
  1147. codec->pcm_info[i].pcm);
  1148. clear_bit(codec->pcm_info[i].device,
  1149. codec->bus->pcm_dev_bits);
  1150. }
  1151. }
  1152. if (codec->patch_ops.free)
  1153. codec->patch_ops.free(codec);
  1154. codec->proc_widget_hook = NULL;
  1155. codec->spec = NULL;
  1156. free_hda_cache(&codec->amp_cache);
  1157. free_hda_cache(&codec->cmd_cache);
  1158. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1159. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1160. codec->num_pcms = 0;
  1161. codec->pcm_info = NULL;
  1162. codec->preset = NULL;
  1163. module_put(codec->owner);
  1164. codec->owner = NULL;
  1165. }
  1166. #endif /* CONFIG_SND_HDA_RECONFIG */
  1167. /* create a virtual master control and add slaves */
  1168. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1169. unsigned int *tlv, const char **slaves)
  1170. {
  1171. struct snd_kcontrol *kctl;
  1172. const char **s;
  1173. int err;
  1174. for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
  1175. ;
  1176. if (!*s) {
  1177. snd_printdd("No slave found for %s\n", name);
  1178. return 0;
  1179. }
  1180. kctl = snd_ctl_make_virtual_master(name, tlv);
  1181. if (!kctl)
  1182. return -ENOMEM;
  1183. err = snd_hda_ctl_add(codec, kctl);
  1184. if (err < 0)
  1185. return err;
  1186. for (s = slaves; *s; s++) {
  1187. struct snd_kcontrol *sctl;
  1188. sctl = snd_hda_find_mixer_ctl(codec, *s);
  1189. if (!sctl) {
  1190. snd_printdd("Cannot find slave %s, skipped\n", *s);
  1191. continue;
  1192. }
  1193. err = snd_ctl_add_slave(kctl, sctl);
  1194. if (err < 0)
  1195. return err;
  1196. }
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
  1200. /* switch */
  1201. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1202. struct snd_ctl_elem_info *uinfo)
  1203. {
  1204. int chs = get_amp_channels(kcontrol);
  1205. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1206. uinfo->count = chs == 3 ? 2 : 1;
  1207. uinfo->value.integer.min = 0;
  1208. uinfo->value.integer.max = 1;
  1209. return 0;
  1210. }
  1211. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
  1212. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1213. struct snd_ctl_elem_value *ucontrol)
  1214. {
  1215. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1216. hda_nid_t nid = get_amp_nid(kcontrol);
  1217. int chs = get_amp_channels(kcontrol);
  1218. int dir = get_amp_direction(kcontrol);
  1219. int idx = get_amp_index(kcontrol);
  1220. long *valp = ucontrol->value.integer.value;
  1221. if (chs & 1)
  1222. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1223. HDA_AMP_MUTE) ? 0 : 1;
  1224. if (chs & 2)
  1225. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1226. HDA_AMP_MUTE) ? 0 : 1;
  1227. return 0;
  1228. }
  1229. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
  1230. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1231. struct snd_ctl_elem_value *ucontrol)
  1232. {
  1233. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1234. hda_nid_t nid = get_amp_nid(kcontrol);
  1235. int chs = get_amp_channels(kcontrol);
  1236. int dir = get_amp_direction(kcontrol);
  1237. int idx = get_amp_index(kcontrol);
  1238. long *valp = ucontrol->value.integer.value;
  1239. int change = 0;
  1240. snd_hda_power_up(codec);
  1241. if (chs & 1) {
  1242. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1243. HDA_AMP_MUTE,
  1244. *valp ? 0 : HDA_AMP_MUTE);
  1245. valp++;
  1246. }
  1247. if (chs & 2)
  1248. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1249. HDA_AMP_MUTE,
  1250. *valp ? 0 : HDA_AMP_MUTE);
  1251. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1252. if (codec->patch_ops.check_power_status)
  1253. codec->patch_ops.check_power_status(codec, nid);
  1254. #endif
  1255. snd_hda_power_down(codec);
  1256. return change;
  1257. }
  1258. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
  1259. /*
  1260. * bound volume controls
  1261. *
  1262. * bind multiple volumes (# indices, from 0)
  1263. */
  1264. #define AMP_VAL_IDX_SHIFT 19
  1265. #define AMP_VAL_IDX_MASK (0x0f<<19)
  1266. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  1267. struct snd_ctl_elem_value *ucontrol)
  1268. {
  1269. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1270. unsigned long pval;
  1271. int err;
  1272. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1273. pval = kcontrol->private_value;
  1274. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  1275. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  1276. kcontrol->private_value = pval;
  1277. mutex_unlock(&codec->spdif_mutex);
  1278. return err;
  1279. }
  1280. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
  1281. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  1282. struct snd_ctl_elem_value *ucontrol)
  1283. {
  1284. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1285. unsigned long pval;
  1286. int i, indices, err = 0, change = 0;
  1287. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1288. pval = kcontrol->private_value;
  1289. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  1290. for (i = 0; i < indices; i++) {
  1291. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  1292. (i << AMP_VAL_IDX_SHIFT);
  1293. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  1294. if (err < 0)
  1295. break;
  1296. change |= err;
  1297. }
  1298. kcontrol->private_value = pval;
  1299. mutex_unlock(&codec->spdif_mutex);
  1300. return err < 0 ? err : change;
  1301. }
  1302. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
  1303. /*
  1304. * generic bound volume/swtich controls
  1305. */
  1306. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  1307. struct snd_ctl_elem_info *uinfo)
  1308. {
  1309. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1310. struct hda_bind_ctls *c;
  1311. int err;
  1312. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1313. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1314. kcontrol->private_value = *c->values;
  1315. err = c->ops->info(kcontrol, uinfo);
  1316. kcontrol->private_value = (long)c;
  1317. mutex_unlock(&codec->spdif_mutex);
  1318. return err;
  1319. }
  1320. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
  1321. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  1322. struct snd_ctl_elem_value *ucontrol)
  1323. {
  1324. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1325. struct hda_bind_ctls *c;
  1326. int err;
  1327. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1328. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1329. kcontrol->private_value = *c->values;
  1330. err = c->ops->get(kcontrol, ucontrol);
  1331. kcontrol->private_value = (long)c;
  1332. mutex_unlock(&codec->spdif_mutex);
  1333. return err;
  1334. }
  1335. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
  1336. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  1337. struct snd_ctl_elem_value *ucontrol)
  1338. {
  1339. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1340. struct hda_bind_ctls *c;
  1341. unsigned long *vals;
  1342. int err = 0, change = 0;
  1343. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1344. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1345. for (vals = c->values; *vals; vals++) {
  1346. kcontrol->private_value = *vals;
  1347. err = c->ops->put(kcontrol, ucontrol);
  1348. if (err < 0)
  1349. break;
  1350. change |= err;
  1351. }
  1352. kcontrol->private_value = (long)c;
  1353. mutex_unlock(&codec->spdif_mutex);
  1354. return err < 0 ? err : change;
  1355. }
  1356. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
  1357. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1358. unsigned int size, unsigned int __user *tlv)
  1359. {
  1360. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1361. struct hda_bind_ctls *c;
  1362. int err;
  1363. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1364. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1365. kcontrol->private_value = *c->values;
  1366. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  1367. kcontrol->private_value = (long)c;
  1368. mutex_unlock(&codec->spdif_mutex);
  1369. return err;
  1370. }
  1371. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
  1372. struct hda_ctl_ops snd_hda_bind_vol = {
  1373. .info = snd_hda_mixer_amp_volume_info,
  1374. .get = snd_hda_mixer_amp_volume_get,
  1375. .put = snd_hda_mixer_amp_volume_put,
  1376. .tlv = snd_hda_mixer_amp_tlv
  1377. };
  1378. EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
  1379. struct hda_ctl_ops snd_hda_bind_sw = {
  1380. .info = snd_hda_mixer_amp_switch_info,
  1381. .get = snd_hda_mixer_amp_switch_get,
  1382. .put = snd_hda_mixer_amp_switch_put,
  1383. .tlv = snd_hda_mixer_amp_tlv
  1384. };
  1385. EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
  1386. /*
  1387. * SPDIF out controls
  1388. */
  1389. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1390. struct snd_ctl_elem_info *uinfo)
  1391. {
  1392. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1393. uinfo->count = 1;
  1394. return 0;
  1395. }
  1396. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1397. struct snd_ctl_elem_value *ucontrol)
  1398. {
  1399. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1400. IEC958_AES0_NONAUDIO |
  1401. IEC958_AES0_CON_EMPHASIS_5015 |
  1402. IEC958_AES0_CON_NOT_COPYRIGHT;
  1403. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1404. IEC958_AES1_CON_ORIGINAL;
  1405. return 0;
  1406. }
  1407. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1408. struct snd_ctl_elem_value *ucontrol)
  1409. {
  1410. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1411. IEC958_AES0_NONAUDIO |
  1412. IEC958_AES0_PRO_EMPHASIS_5015;
  1413. return 0;
  1414. }
  1415. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1416. struct snd_ctl_elem_value *ucontrol)
  1417. {
  1418. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1419. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  1420. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  1421. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  1422. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  1423. return 0;
  1424. }
  1425. /* convert from SPDIF status bits to HDA SPDIF bits
  1426. * bit 0 (DigEn) is always set zero (to be filled later)
  1427. */
  1428. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1429. {
  1430. unsigned short val = 0;
  1431. if (sbits & IEC958_AES0_PROFESSIONAL)
  1432. val |= AC_DIG1_PROFESSIONAL;
  1433. if (sbits & IEC958_AES0_NONAUDIO)
  1434. val |= AC_DIG1_NONAUDIO;
  1435. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1436. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1437. IEC958_AES0_PRO_EMPHASIS_5015)
  1438. val |= AC_DIG1_EMPHASIS;
  1439. } else {
  1440. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1441. IEC958_AES0_CON_EMPHASIS_5015)
  1442. val |= AC_DIG1_EMPHASIS;
  1443. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1444. val |= AC_DIG1_COPYRIGHT;
  1445. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1446. val |= AC_DIG1_LEVEL;
  1447. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1448. }
  1449. return val;
  1450. }
  1451. /* convert to SPDIF status bits from HDA SPDIF bits
  1452. */
  1453. static unsigned int convert_to_spdif_status(unsigned short val)
  1454. {
  1455. unsigned int sbits = 0;
  1456. if (val & AC_DIG1_NONAUDIO)
  1457. sbits |= IEC958_AES0_NONAUDIO;
  1458. if (val & AC_DIG1_PROFESSIONAL)
  1459. sbits |= IEC958_AES0_PROFESSIONAL;
  1460. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1461. if (sbits & AC_DIG1_EMPHASIS)
  1462. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1463. } else {
  1464. if (val & AC_DIG1_EMPHASIS)
  1465. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1466. if (!(val & AC_DIG1_COPYRIGHT))
  1467. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1468. if (val & AC_DIG1_LEVEL)
  1469. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1470. sbits |= val & (0x7f << 8);
  1471. }
  1472. return sbits;
  1473. }
  1474. /* set digital convert verbs both for the given NID and its slaves */
  1475. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  1476. int verb, int val)
  1477. {
  1478. hda_nid_t *d;
  1479. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  1480. d = codec->slave_dig_outs;
  1481. if (!d)
  1482. return;
  1483. for (; *d; d++)
  1484. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  1485. }
  1486. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  1487. int dig1, int dig2)
  1488. {
  1489. if (dig1 != -1)
  1490. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  1491. if (dig2 != -1)
  1492. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  1493. }
  1494. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  1495. struct snd_ctl_elem_value *ucontrol)
  1496. {
  1497. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1498. hda_nid_t nid = kcontrol->private_value;
  1499. unsigned short val;
  1500. int change;
  1501. mutex_lock(&codec->spdif_mutex);
  1502. codec->spdif_status = ucontrol->value.iec958.status[0] |
  1503. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  1504. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  1505. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  1506. val = convert_from_spdif_status(codec->spdif_status);
  1507. val |= codec->spdif_ctls & 1;
  1508. change = codec->spdif_ctls != val;
  1509. codec->spdif_ctls = val;
  1510. if (change)
  1511. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  1512. mutex_unlock(&codec->spdif_mutex);
  1513. return change;
  1514. }
  1515. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1516. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1517. struct snd_ctl_elem_value *ucontrol)
  1518. {
  1519. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1520. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1521. return 0;
  1522. }
  1523. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1524. struct snd_ctl_elem_value *ucontrol)
  1525. {
  1526. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1527. hda_nid_t nid = kcontrol->private_value;
  1528. unsigned short val;
  1529. int change;
  1530. mutex_lock(&codec->spdif_mutex);
  1531. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1532. if (ucontrol->value.integer.value[0])
  1533. val |= AC_DIG1_ENABLE;
  1534. change = codec->spdif_ctls != val;
  1535. if (change) {
  1536. codec->spdif_ctls = val;
  1537. set_dig_out_convert(codec, nid, val & 0xff, -1);
  1538. /* unmute amp switch (if any) */
  1539. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1540. (val & AC_DIG1_ENABLE))
  1541. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1542. HDA_AMP_MUTE, 0);
  1543. }
  1544. mutex_unlock(&codec->spdif_mutex);
  1545. return change;
  1546. }
  1547. static struct snd_kcontrol_new dig_mixes[] = {
  1548. {
  1549. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1550. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1551. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1552. .info = snd_hda_spdif_mask_info,
  1553. .get = snd_hda_spdif_cmask_get,
  1554. },
  1555. {
  1556. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1557. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1558. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1559. .info = snd_hda_spdif_mask_info,
  1560. .get = snd_hda_spdif_pmask_get,
  1561. },
  1562. {
  1563. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1564. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1565. .info = snd_hda_spdif_mask_info,
  1566. .get = snd_hda_spdif_default_get,
  1567. .put = snd_hda_spdif_default_put,
  1568. },
  1569. {
  1570. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1571. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1572. .info = snd_hda_spdif_out_switch_info,
  1573. .get = snd_hda_spdif_out_switch_get,
  1574. .put = snd_hda_spdif_out_switch_put,
  1575. },
  1576. { } /* end */
  1577. };
  1578. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  1579. /**
  1580. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1581. * @codec: the HDA codec
  1582. * @nid: audio out widget NID
  1583. *
  1584. * Creates controls related with the SPDIF output.
  1585. * Called from each patch supporting the SPDIF out.
  1586. *
  1587. * Returns 0 if successful, or a negative error code.
  1588. */
  1589. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1590. {
  1591. int err;
  1592. struct snd_kcontrol *kctl;
  1593. struct snd_kcontrol_new *dig_mix;
  1594. int idx;
  1595. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1596. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  1597. idx))
  1598. break;
  1599. }
  1600. if (idx >= SPDIF_MAX_IDX) {
  1601. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  1602. return -EBUSY;
  1603. }
  1604. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1605. kctl = snd_ctl_new1(dig_mix, codec);
  1606. if (!kctl)
  1607. return -ENOMEM;
  1608. kctl->id.index = idx;
  1609. kctl->private_value = nid;
  1610. err = snd_hda_ctl_add(codec, kctl);
  1611. if (err < 0)
  1612. return err;
  1613. }
  1614. codec->spdif_ctls =
  1615. snd_hda_codec_read(codec, nid, 0,
  1616. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1617. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1618. return 0;
  1619. }
  1620. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
  1621. /*
  1622. * SPDIF sharing with analog output
  1623. */
  1624. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  1625. struct snd_ctl_elem_value *ucontrol)
  1626. {
  1627. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1628. ucontrol->value.integer.value[0] = mout->share_spdif;
  1629. return 0;
  1630. }
  1631. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  1632. struct snd_ctl_elem_value *ucontrol)
  1633. {
  1634. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1635. mout->share_spdif = !!ucontrol->value.integer.value[0];
  1636. return 0;
  1637. }
  1638. static struct snd_kcontrol_new spdif_share_sw = {
  1639. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1640. .name = "IEC958 Default PCM Playback Switch",
  1641. .info = snd_ctl_boolean_mono_info,
  1642. .get = spdif_share_sw_get,
  1643. .put = spdif_share_sw_put,
  1644. };
  1645. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  1646. struct hda_multi_out *mout)
  1647. {
  1648. if (!mout->dig_out_nid)
  1649. return 0;
  1650. /* ATTENTION: here mout is passed as private_data, instead of codec */
  1651. return snd_hda_ctl_add(codec,
  1652. snd_ctl_new1(&spdif_share_sw, mout));
  1653. }
  1654. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
  1655. /*
  1656. * SPDIF input
  1657. */
  1658. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1659. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1660. struct snd_ctl_elem_value *ucontrol)
  1661. {
  1662. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1663. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1664. return 0;
  1665. }
  1666. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1667. struct snd_ctl_elem_value *ucontrol)
  1668. {
  1669. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1670. hda_nid_t nid = kcontrol->private_value;
  1671. unsigned int val = !!ucontrol->value.integer.value[0];
  1672. int change;
  1673. mutex_lock(&codec->spdif_mutex);
  1674. change = codec->spdif_in_enable != val;
  1675. if (change) {
  1676. codec->spdif_in_enable = val;
  1677. snd_hda_codec_write_cache(codec, nid, 0,
  1678. AC_VERB_SET_DIGI_CONVERT_1, val);
  1679. }
  1680. mutex_unlock(&codec->spdif_mutex);
  1681. return change;
  1682. }
  1683. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1684. struct snd_ctl_elem_value *ucontrol)
  1685. {
  1686. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1687. hda_nid_t nid = kcontrol->private_value;
  1688. unsigned short val;
  1689. unsigned int sbits;
  1690. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1691. sbits = convert_to_spdif_status(val);
  1692. ucontrol->value.iec958.status[0] = sbits;
  1693. ucontrol->value.iec958.status[1] = sbits >> 8;
  1694. ucontrol->value.iec958.status[2] = sbits >> 16;
  1695. ucontrol->value.iec958.status[3] = sbits >> 24;
  1696. return 0;
  1697. }
  1698. static struct snd_kcontrol_new dig_in_ctls[] = {
  1699. {
  1700. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1701. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1702. .info = snd_hda_spdif_in_switch_info,
  1703. .get = snd_hda_spdif_in_switch_get,
  1704. .put = snd_hda_spdif_in_switch_put,
  1705. },
  1706. {
  1707. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1708. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1709. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1710. .info = snd_hda_spdif_mask_info,
  1711. .get = snd_hda_spdif_in_status_get,
  1712. },
  1713. { } /* end */
  1714. };
  1715. /**
  1716. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1717. * @codec: the HDA codec
  1718. * @nid: audio in widget NID
  1719. *
  1720. * Creates controls related with the SPDIF input.
  1721. * Called from each patch supporting the SPDIF in.
  1722. *
  1723. * Returns 0 if successful, or a negative error code.
  1724. */
  1725. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1726. {
  1727. int err;
  1728. struct snd_kcontrol *kctl;
  1729. struct snd_kcontrol_new *dig_mix;
  1730. int idx;
  1731. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1732. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  1733. idx))
  1734. break;
  1735. }
  1736. if (idx >= SPDIF_MAX_IDX) {
  1737. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  1738. return -EBUSY;
  1739. }
  1740. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1741. kctl = snd_ctl_new1(dig_mix, codec);
  1742. kctl->private_value = nid;
  1743. err = snd_hda_ctl_add(codec, kctl);
  1744. if (err < 0)
  1745. return err;
  1746. }
  1747. codec->spdif_in_enable =
  1748. snd_hda_codec_read(codec, nid, 0,
  1749. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  1750. AC_DIG1_ENABLE;
  1751. return 0;
  1752. }
  1753. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
  1754. #ifdef SND_HDA_NEEDS_RESUME
  1755. /*
  1756. * command cache
  1757. */
  1758. /* build a 32bit cache key with the widget id and the command parameter */
  1759. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1760. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1761. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1762. /**
  1763. * snd_hda_codec_write_cache - send a single command with caching
  1764. * @codec: the HDA codec
  1765. * @nid: NID to send the command
  1766. * @direct: direct flag
  1767. * @verb: the verb to send
  1768. * @parm: the parameter for the verb
  1769. *
  1770. * Send a single command without waiting for response.
  1771. *
  1772. * Returns 0 if successful, or a negative error code.
  1773. */
  1774. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1775. int direct, unsigned int verb, unsigned int parm)
  1776. {
  1777. struct hda_bus *bus = codec->bus;
  1778. unsigned int res;
  1779. int err;
  1780. res = make_codec_cmd(codec, nid, direct, verb, parm);
  1781. snd_hda_power_up(codec);
  1782. mutex_lock(&bus->cmd_mutex);
  1783. err = bus->ops.command(bus, res);
  1784. if (!err) {
  1785. struct hda_cache_head *c;
  1786. u32 key = build_cmd_cache_key(nid, verb);
  1787. c = get_alloc_hash(&codec->cmd_cache, key);
  1788. if (c)
  1789. c->val = parm;
  1790. }
  1791. mutex_unlock(&bus->cmd_mutex);
  1792. snd_hda_power_down(codec);
  1793. return err;
  1794. }
  1795. EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
  1796. /* resume the all commands from the cache */
  1797. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1798. {
  1799. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  1800. int i;
  1801. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  1802. u32 key = buffer->key;
  1803. if (!key)
  1804. continue;
  1805. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1806. get_cmd_cache_cmd(key), buffer->val);
  1807. }
  1808. }
  1809. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
  1810. /**
  1811. * snd_hda_sequence_write_cache - sequence writes with caching
  1812. * @codec: the HDA codec
  1813. * @seq: VERB array to send
  1814. *
  1815. * Send the commands sequentially from the given array.
  1816. * Thte commands are recorded on cache for power-save and resume.
  1817. * The array must be terminated with NID=0.
  1818. */
  1819. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1820. const struct hda_verb *seq)
  1821. {
  1822. for (; seq->nid; seq++)
  1823. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1824. seq->param);
  1825. }
  1826. EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
  1827. #endif /* SND_HDA_NEEDS_RESUME */
  1828. /*
  1829. * set power state of the codec
  1830. */
  1831. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1832. unsigned int power_state)
  1833. {
  1834. hda_nid_t nid;
  1835. int i;
  1836. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1837. power_state);
  1838. msleep(10); /* partial workaround for "azx_get_response timeout" */
  1839. nid = codec->start_nid;
  1840. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1841. unsigned int wcaps = get_wcaps(codec, nid);
  1842. if (wcaps & AC_WCAP_POWER) {
  1843. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1844. AC_WCAP_TYPE_SHIFT;
  1845. if (wid_type == AC_WID_PIN) {
  1846. unsigned int pincap;
  1847. /*
  1848. * don't power down the widget if it controls
  1849. * eapd and EAPD_BTLENABLE is set.
  1850. */
  1851. pincap = snd_hda_param_read(codec, nid,
  1852. AC_PAR_PIN_CAP);
  1853. if (pincap & AC_PINCAP_EAPD) {
  1854. int eapd = snd_hda_codec_read(codec,
  1855. nid, 0,
  1856. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1857. eapd &= 0x02;
  1858. if (power_state == AC_PWRST_D3 && eapd)
  1859. continue;
  1860. }
  1861. }
  1862. snd_hda_codec_write(codec, nid, 0,
  1863. AC_VERB_SET_POWER_STATE,
  1864. power_state);
  1865. }
  1866. }
  1867. if (power_state == AC_PWRST_D0) {
  1868. unsigned long end_time;
  1869. int state;
  1870. msleep(10);
  1871. /* wait until the codec reachs to D0 */
  1872. end_time = jiffies + msecs_to_jiffies(500);
  1873. do {
  1874. state = snd_hda_codec_read(codec, fg, 0,
  1875. AC_VERB_GET_POWER_STATE, 0);
  1876. if (state == power_state)
  1877. break;
  1878. msleep(1);
  1879. } while (time_after_eq(end_time, jiffies));
  1880. }
  1881. }
  1882. #ifdef CONFIG_SND_HDA_HWDEP
  1883. /* execute additional init verbs */
  1884. static void hda_exec_init_verbs(struct hda_codec *codec)
  1885. {
  1886. if (codec->init_verbs.list)
  1887. snd_hda_sequence_write(codec, codec->init_verbs.list);
  1888. }
  1889. #else
  1890. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  1891. #endif
  1892. #ifdef SND_HDA_NEEDS_RESUME
  1893. /*
  1894. * call suspend and power-down; used both from PM and power-save
  1895. */
  1896. static void hda_call_codec_suspend(struct hda_codec *codec)
  1897. {
  1898. if (codec->patch_ops.suspend)
  1899. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1900. hda_set_power_state(codec,
  1901. codec->afg ? codec->afg : codec->mfg,
  1902. AC_PWRST_D3);
  1903. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1904. cancel_delayed_work(&codec->power_work);
  1905. codec->power_on = 0;
  1906. codec->power_transition = 0;
  1907. #endif
  1908. }
  1909. /*
  1910. * kick up codec; used both from PM and power-save
  1911. */
  1912. static void hda_call_codec_resume(struct hda_codec *codec)
  1913. {
  1914. hda_set_power_state(codec,
  1915. codec->afg ? codec->afg : codec->mfg,
  1916. AC_PWRST_D0);
  1917. hda_exec_init_verbs(codec);
  1918. if (codec->patch_ops.resume)
  1919. codec->patch_ops.resume(codec);
  1920. else {
  1921. if (codec->patch_ops.init)
  1922. codec->patch_ops.init(codec);
  1923. snd_hda_codec_resume_amp(codec);
  1924. snd_hda_codec_resume_cache(codec);
  1925. }
  1926. }
  1927. #endif /* SND_HDA_NEEDS_RESUME */
  1928. /**
  1929. * snd_hda_build_controls - build mixer controls
  1930. * @bus: the BUS
  1931. *
  1932. * Creates mixer controls for each codec included in the bus.
  1933. *
  1934. * Returns 0 if successful, otherwise a negative error code.
  1935. */
  1936. int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
  1937. {
  1938. struct hda_codec *codec;
  1939. list_for_each_entry(codec, &bus->codec_list, list) {
  1940. int err = snd_hda_codec_build_controls(codec);
  1941. if (err < 0)
  1942. return err;
  1943. }
  1944. return 0;
  1945. }
  1946. EXPORT_SYMBOL_HDA(snd_hda_build_controls);
  1947. int snd_hda_codec_build_controls(struct hda_codec *codec)
  1948. {
  1949. int err = 0;
  1950. /* fake as if already powered-on */
  1951. hda_keep_power_on(codec);
  1952. /* then fire up */
  1953. hda_set_power_state(codec,
  1954. codec->afg ? codec->afg : codec->mfg,
  1955. AC_PWRST_D0);
  1956. hda_exec_init_verbs(codec);
  1957. /* continue to initialize... */
  1958. if (codec->patch_ops.init)
  1959. err = codec->patch_ops.init(codec);
  1960. if (!err && codec->patch_ops.build_controls)
  1961. err = codec->patch_ops.build_controls(codec);
  1962. snd_hda_power_down(codec);
  1963. if (err < 0)
  1964. return err;
  1965. return 0;
  1966. }
  1967. /*
  1968. * stream formats
  1969. */
  1970. struct hda_rate_tbl {
  1971. unsigned int hz;
  1972. unsigned int alsa_bits;
  1973. unsigned int hda_fmt;
  1974. };
  1975. static struct hda_rate_tbl rate_bits[] = {
  1976. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1977. /* autodetected value used in snd_hda_query_supported_pcm */
  1978. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1979. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1980. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1981. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1982. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1983. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1984. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1985. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1986. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1987. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1988. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1989. #define AC_PAR_PCM_RATE_BITS 11
  1990. /* up to bits 10, 384kHZ isn't supported properly */
  1991. /* not autodetected value */
  1992. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1993. { 0 } /* terminator */
  1994. };
  1995. /**
  1996. * snd_hda_calc_stream_format - calculate format bitset
  1997. * @rate: the sample rate
  1998. * @channels: the number of channels
  1999. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  2000. * @maxbps: the max. bps
  2001. *
  2002. * Calculate the format bitset from the given rate, channels and th PCM format.
  2003. *
  2004. * Return zero if invalid.
  2005. */
  2006. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  2007. unsigned int channels,
  2008. unsigned int format,
  2009. unsigned int maxbps)
  2010. {
  2011. int i;
  2012. unsigned int val = 0;
  2013. for (i = 0; rate_bits[i].hz; i++)
  2014. if (rate_bits[i].hz == rate) {
  2015. val = rate_bits[i].hda_fmt;
  2016. break;
  2017. }
  2018. if (!rate_bits[i].hz) {
  2019. snd_printdd("invalid rate %d\n", rate);
  2020. return 0;
  2021. }
  2022. if (channels == 0 || channels > 8) {
  2023. snd_printdd("invalid channels %d\n", channels);
  2024. return 0;
  2025. }
  2026. val |= channels - 1;
  2027. switch (snd_pcm_format_width(format)) {
  2028. case 8: val |= 0x00; break;
  2029. case 16: val |= 0x10; break;
  2030. case 20:
  2031. case 24:
  2032. case 32:
  2033. if (maxbps >= 32)
  2034. val |= 0x40;
  2035. else if (maxbps >= 24)
  2036. val |= 0x30;
  2037. else
  2038. val |= 0x20;
  2039. break;
  2040. default:
  2041. snd_printdd("invalid format width %d\n",
  2042. snd_pcm_format_width(format));
  2043. return 0;
  2044. }
  2045. return val;
  2046. }
  2047. EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
  2048. /**
  2049. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  2050. * @codec: the HDA codec
  2051. * @nid: NID to query
  2052. * @ratesp: the pointer to store the detected rate bitflags
  2053. * @formatsp: the pointer to store the detected formats
  2054. * @bpsp: the pointer to store the detected format widths
  2055. *
  2056. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  2057. * or @bsps argument is ignored.
  2058. *
  2059. * Returns 0 if successful, otherwise a negative error code.
  2060. */
  2061. static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  2062. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  2063. {
  2064. int i;
  2065. unsigned int val, streams;
  2066. val = 0;
  2067. if (nid != codec->afg &&
  2068. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  2069. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2070. if (val == -1)
  2071. return -EIO;
  2072. }
  2073. if (!val)
  2074. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2075. if (ratesp) {
  2076. u32 rates = 0;
  2077. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  2078. if (val & (1 << i))
  2079. rates |= rate_bits[i].alsa_bits;
  2080. }
  2081. *ratesp = rates;
  2082. }
  2083. if (formatsp || bpsp) {
  2084. u64 formats = 0;
  2085. unsigned int bps;
  2086. unsigned int wcaps;
  2087. wcaps = get_wcaps(codec, nid);
  2088. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2089. if (streams == -1)
  2090. return -EIO;
  2091. if (!streams) {
  2092. streams = snd_hda_param_read(codec, codec->afg,
  2093. AC_PAR_STREAM);
  2094. if (streams == -1)
  2095. return -EIO;
  2096. }
  2097. bps = 0;
  2098. if (streams & AC_SUPFMT_PCM) {
  2099. if (val & AC_SUPPCM_BITS_8) {
  2100. formats |= SNDRV_PCM_FMTBIT_U8;
  2101. bps = 8;
  2102. }
  2103. if (val & AC_SUPPCM_BITS_16) {
  2104. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  2105. bps = 16;
  2106. }
  2107. if (wcaps & AC_WCAP_DIGITAL) {
  2108. if (val & AC_SUPPCM_BITS_32)
  2109. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  2110. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  2111. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2112. if (val & AC_SUPPCM_BITS_24)
  2113. bps = 24;
  2114. else if (val & AC_SUPPCM_BITS_20)
  2115. bps = 20;
  2116. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  2117. AC_SUPPCM_BITS_32)) {
  2118. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2119. if (val & AC_SUPPCM_BITS_32)
  2120. bps = 32;
  2121. else if (val & AC_SUPPCM_BITS_24)
  2122. bps = 24;
  2123. else if (val & AC_SUPPCM_BITS_20)
  2124. bps = 20;
  2125. }
  2126. }
  2127. else if (streams == AC_SUPFMT_FLOAT32) {
  2128. /* should be exclusive */
  2129. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  2130. bps = 32;
  2131. } else if (streams == AC_SUPFMT_AC3) {
  2132. /* should be exclusive */
  2133. /* temporary hack: we have still no proper support
  2134. * for the direct AC3 stream...
  2135. */
  2136. formats |= SNDRV_PCM_FMTBIT_U8;
  2137. bps = 8;
  2138. }
  2139. if (formatsp)
  2140. *formatsp = formats;
  2141. if (bpsp)
  2142. *bpsp = bps;
  2143. }
  2144. return 0;
  2145. }
  2146. /**
  2147. * snd_hda_is_supported_format - check whether the given node supports
  2148. * the format val
  2149. *
  2150. * Returns 1 if supported, 0 if not.
  2151. */
  2152. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  2153. unsigned int format)
  2154. {
  2155. int i;
  2156. unsigned int val = 0, rate, stream;
  2157. if (nid != codec->afg &&
  2158. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  2159. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2160. if (val == -1)
  2161. return 0;
  2162. }
  2163. if (!val) {
  2164. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2165. if (val == -1)
  2166. return 0;
  2167. }
  2168. rate = format & 0xff00;
  2169. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  2170. if (rate_bits[i].hda_fmt == rate) {
  2171. if (val & (1 << i))
  2172. break;
  2173. return 0;
  2174. }
  2175. if (i >= AC_PAR_PCM_RATE_BITS)
  2176. return 0;
  2177. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2178. if (stream == -1)
  2179. return 0;
  2180. if (!stream && nid != codec->afg)
  2181. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  2182. if (!stream || stream == -1)
  2183. return 0;
  2184. if (stream & AC_SUPFMT_PCM) {
  2185. switch (format & 0xf0) {
  2186. case 0x00:
  2187. if (!(val & AC_SUPPCM_BITS_8))
  2188. return 0;
  2189. break;
  2190. case 0x10:
  2191. if (!(val & AC_SUPPCM_BITS_16))
  2192. return 0;
  2193. break;
  2194. case 0x20:
  2195. if (!(val & AC_SUPPCM_BITS_20))
  2196. return 0;
  2197. break;
  2198. case 0x30:
  2199. if (!(val & AC_SUPPCM_BITS_24))
  2200. return 0;
  2201. break;
  2202. case 0x40:
  2203. if (!(val & AC_SUPPCM_BITS_32))
  2204. return 0;
  2205. break;
  2206. default:
  2207. return 0;
  2208. }
  2209. } else {
  2210. /* FIXME: check for float32 and AC3? */
  2211. }
  2212. return 1;
  2213. }
  2214. EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
  2215. /*
  2216. * PCM stuff
  2217. */
  2218. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2219. struct hda_codec *codec,
  2220. struct snd_pcm_substream *substream)
  2221. {
  2222. return 0;
  2223. }
  2224. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2225. struct hda_codec *codec,
  2226. unsigned int stream_tag,
  2227. unsigned int format,
  2228. struct snd_pcm_substream *substream)
  2229. {
  2230. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2231. return 0;
  2232. }
  2233. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2234. struct hda_codec *codec,
  2235. struct snd_pcm_substream *substream)
  2236. {
  2237. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2238. return 0;
  2239. }
  2240. static int set_pcm_default_values(struct hda_codec *codec,
  2241. struct hda_pcm_stream *info)
  2242. {
  2243. /* query support PCM information from the given NID */
  2244. if (info->nid && (!info->rates || !info->formats)) {
  2245. snd_hda_query_supported_pcm(codec, info->nid,
  2246. info->rates ? NULL : &info->rates,
  2247. info->formats ? NULL : &info->formats,
  2248. info->maxbps ? NULL : &info->maxbps);
  2249. }
  2250. if (info->ops.open == NULL)
  2251. info->ops.open = hda_pcm_default_open_close;
  2252. if (info->ops.close == NULL)
  2253. info->ops.close = hda_pcm_default_open_close;
  2254. if (info->ops.prepare == NULL) {
  2255. if (snd_BUG_ON(!info->nid))
  2256. return -EINVAL;
  2257. info->ops.prepare = hda_pcm_default_prepare;
  2258. }
  2259. if (info->ops.cleanup == NULL) {
  2260. if (snd_BUG_ON(!info->nid))
  2261. return -EINVAL;
  2262. info->ops.cleanup = hda_pcm_default_cleanup;
  2263. }
  2264. return 0;
  2265. }
  2266. /*
  2267. * get the empty PCM device number to assign
  2268. */
  2269. static int get_empty_pcm_device(struct hda_bus *bus, int type)
  2270. {
  2271. static const char *dev_name[HDA_PCM_NTYPES] = {
  2272. "Audio", "SPDIF", "HDMI", "Modem"
  2273. };
  2274. /* starting device index for each PCM type */
  2275. static int dev_idx[HDA_PCM_NTYPES] = {
  2276. [HDA_PCM_TYPE_AUDIO] = 0,
  2277. [HDA_PCM_TYPE_SPDIF] = 1,
  2278. [HDA_PCM_TYPE_HDMI] = 3,
  2279. [HDA_PCM_TYPE_MODEM] = 6
  2280. };
  2281. /* normal audio device indices; not linear to keep compatibility */
  2282. static int audio_idx[4] = { 0, 2, 4, 5 };
  2283. int i, dev;
  2284. switch (type) {
  2285. case HDA_PCM_TYPE_AUDIO:
  2286. for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
  2287. dev = audio_idx[i];
  2288. if (!test_bit(dev, bus->pcm_dev_bits))
  2289. break;
  2290. }
  2291. if (i >= ARRAY_SIZE(audio_idx)) {
  2292. snd_printk(KERN_WARNING "Too many audio devices\n");
  2293. return -EAGAIN;
  2294. }
  2295. break;
  2296. case HDA_PCM_TYPE_SPDIF:
  2297. case HDA_PCM_TYPE_HDMI:
  2298. case HDA_PCM_TYPE_MODEM:
  2299. dev = dev_idx[type];
  2300. if (test_bit(dev, bus->pcm_dev_bits)) {
  2301. snd_printk(KERN_WARNING "%s already defined\n",
  2302. dev_name[type]);
  2303. return -EAGAIN;
  2304. }
  2305. break;
  2306. default:
  2307. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  2308. return -EINVAL;
  2309. }
  2310. set_bit(dev, bus->pcm_dev_bits);
  2311. return dev;
  2312. }
  2313. /*
  2314. * attach a new PCM stream
  2315. */
  2316. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  2317. {
  2318. struct hda_bus *bus = codec->bus;
  2319. struct hda_pcm_stream *info;
  2320. int stream, err;
  2321. if (snd_BUG_ON(!pcm->name))
  2322. return -EINVAL;
  2323. for (stream = 0; stream < 2; stream++) {
  2324. info = &pcm->stream[stream];
  2325. if (info->substreams) {
  2326. err = set_pcm_default_values(codec, info);
  2327. if (err < 0)
  2328. return err;
  2329. }
  2330. }
  2331. return bus->ops.attach_pcm(bus, codec, pcm);
  2332. }
  2333. /* assign all PCMs of the given codec */
  2334. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  2335. {
  2336. unsigned int pcm;
  2337. int err;
  2338. if (!codec->num_pcms) {
  2339. if (!codec->patch_ops.build_pcms)
  2340. return 0;
  2341. err = codec->patch_ops.build_pcms(codec);
  2342. if (err < 0)
  2343. return err;
  2344. }
  2345. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2346. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2347. int dev;
  2348. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  2349. return 0; /* no substreams assigned */
  2350. if (!cpcm->pcm) {
  2351. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  2352. if (dev < 0)
  2353. return 0;
  2354. cpcm->device = dev;
  2355. err = snd_hda_attach_pcm(codec, cpcm);
  2356. if (err < 0)
  2357. return err;
  2358. }
  2359. }
  2360. return 0;
  2361. }
  2362. /**
  2363. * snd_hda_build_pcms - build PCM information
  2364. * @bus: the BUS
  2365. *
  2366. * Create PCM information for each codec included in the bus.
  2367. *
  2368. * The build_pcms codec patch is requested to set up codec->num_pcms and
  2369. * codec->pcm_info properly. The array is referred by the top-level driver
  2370. * to create its PCM instances.
  2371. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  2372. * callback.
  2373. *
  2374. * At least, substreams, channels_min and channels_max must be filled for
  2375. * each stream. substreams = 0 indicates that the stream doesn't exist.
  2376. * When rates and/or formats are zero, the supported values are queried
  2377. * from the given nid. The nid is used also by the default ops.prepare
  2378. * and ops.cleanup callbacks.
  2379. *
  2380. * The driver needs to call ops.open in its open callback. Similarly,
  2381. * ops.close is supposed to be called in the close callback.
  2382. * ops.prepare should be called in the prepare or hw_params callback
  2383. * with the proper parameters for set up.
  2384. * ops.cleanup should be called in hw_free for clean up of streams.
  2385. *
  2386. * This function returns 0 if successfull, or a negative error code.
  2387. */
  2388. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  2389. {
  2390. struct hda_codec *codec;
  2391. list_for_each_entry(codec, &bus->codec_list, list) {
  2392. int err = snd_hda_codec_build_pcms(codec);
  2393. if (err < 0)
  2394. return err;
  2395. }
  2396. return 0;
  2397. }
  2398. EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
  2399. /**
  2400. * snd_hda_check_board_config - compare the current codec with the config table
  2401. * @codec: the HDA codec
  2402. * @num_configs: number of config enums
  2403. * @models: array of model name strings
  2404. * @tbl: configuration table, terminated by null entries
  2405. *
  2406. * Compares the modelname or PCI subsystem id of the current codec with the
  2407. * given configuration table. If a matching entry is found, returns its
  2408. * config value (supposed to be 0 or positive).
  2409. *
  2410. * If no entries are matching, the function returns a negative value.
  2411. */
  2412. int snd_hda_check_board_config(struct hda_codec *codec,
  2413. int num_configs, const char **models,
  2414. const struct snd_pci_quirk *tbl)
  2415. {
  2416. if (codec->modelname && models) {
  2417. int i;
  2418. for (i = 0; i < num_configs; i++) {
  2419. if (models[i] &&
  2420. !strcmp(codec->modelname, models[i])) {
  2421. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  2422. "selected\n", models[i]);
  2423. return i;
  2424. }
  2425. }
  2426. }
  2427. if (!codec->bus->pci || !tbl)
  2428. return -1;
  2429. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  2430. if (!tbl)
  2431. return -1;
  2432. if (tbl->value >= 0 && tbl->value < num_configs) {
  2433. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2434. char tmp[10];
  2435. const char *model = NULL;
  2436. if (models)
  2437. model = models[tbl->value];
  2438. if (!model) {
  2439. sprintf(tmp, "#%d", tbl->value);
  2440. model = tmp;
  2441. }
  2442. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2443. "for config %x:%x (%s)\n",
  2444. model, tbl->subvendor, tbl->subdevice,
  2445. (tbl->name ? tbl->name : "Unknown device"));
  2446. #endif
  2447. return tbl->value;
  2448. }
  2449. return -1;
  2450. }
  2451. EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
  2452. /**
  2453. * snd_hda_add_new_ctls - create controls from the array
  2454. * @codec: the HDA codec
  2455. * @knew: the array of struct snd_kcontrol_new
  2456. *
  2457. * This helper function creates and add new controls in the given array.
  2458. * The array must be terminated with an empty entry as terminator.
  2459. *
  2460. * Returns 0 if successful, or a negative error code.
  2461. */
  2462. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2463. {
  2464. int err;
  2465. for (; knew->name; knew++) {
  2466. struct snd_kcontrol *kctl;
  2467. kctl = snd_ctl_new1(knew, codec);
  2468. if (!kctl)
  2469. return -ENOMEM;
  2470. err = snd_hda_ctl_add(codec, kctl);
  2471. if (err < 0) {
  2472. if (!codec->addr)
  2473. return err;
  2474. kctl = snd_ctl_new1(knew, codec);
  2475. if (!kctl)
  2476. return -ENOMEM;
  2477. kctl->id.device = codec->addr;
  2478. err = snd_hda_ctl_add(codec, kctl);
  2479. if (err < 0)
  2480. return err;
  2481. }
  2482. }
  2483. return 0;
  2484. }
  2485. EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
  2486. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2487. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2488. unsigned int power_state);
  2489. static void hda_power_work(struct work_struct *work)
  2490. {
  2491. struct hda_codec *codec =
  2492. container_of(work, struct hda_codec, power_work.work);
  2493. struct hda_bus *bus = codec->bus;
  2494. if (!codec->power_on || codec->power_count) {
  2495. codec->power_transition = 0;
  2496. return;
  2497. }
  2498. hda_call_codec_suspend(codec);
  2499. if (bus->ops.pm_notify)
  2500. bus->ops.pm_notify(bus);
  2501. }
  2502. static void hda_keep_power_on(struct hda_codec *codec)
  2503. {
  2504. codec->power_count++;
  2505. codec->power_on = 1;
  2506. }
  2507. void snd_hda_power_up(struct hda_codec *codec)
  2508. {
  2509. struct hda_bus *bus = codec->bus;
  2510. codec->power_count++;
  2511. if (codec->power_on || codec->power_transition)
  2512. return;
  2513. codec->power_on = 1;
  2514. if (bus->ops.pm_notify)
  2515. bus->ops.pm_notify(bus);
  2516. hda_call_codec_resume(codec);
  2517. cancel_delayed_work(&codec->power_work);
  2518. codec->power_transition = 0;
  2519. }
  2520. EXPORT_SYMBOL_HDA(snd_hda_power_up);
  2521. #define power_save(codec) \
  2522. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2523. #define power_save(codec) \
  2524. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2525. void snd_hda_power_down(struct hda_codec *codec)
  2526. {
  2527. --codec->power_count;
  2528. if (!codec->power_on || codec->power_count || codec->power_transition)
  2529. return;
  2530. if (power_save(codec)) {
  2531. codec->power_transition = 1; /* avoid reentrance */
  2532. schedule_delayed_work(&codec->power_work,
  2533. msecs_to_jiffies(power_save(codec) * 1000));
  2534. }
  2535. }
  2536. EXPORT_SYMBOL_HDA(snd_hda_power_down);
  2537. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2538. struct hda_loopback_check *check,
  2539. hda_nid_t nid)
  2540. {
  2541. struct hda_amp_list *p;
  2542. int ch, v;
  2543. if (!check->amplist)
  2544. return 0;
  2545. for (p = check->amplist; p->nid; p++) {
  2546. if (p->nid == nid)
  2547. break;
  2548. }
  2549. if (!p->nid)
  2550. return 0; /* nothing changed */
  2551. for (p = check->amplist; p->nid; p++) {
  2552. for (ch = 0; ch < 2; ch++) {
  2553. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2554. p->idx);
  2555. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2556. if (!check->power_on) {
  2557. check->power_on = 1;
  2558. snd_hda_power_up(codec);
  2559. }
  2560. return 1;
  2561. }
  2562. }
  2563. }
  2564. if (check->power_on) {
  2565. check->power_on = 0;
  2566. snd_hda_power_down(codec);
  2567. }
  2568. return 0;
  2569. }
  2570. EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
  2571. #endif
  2572. /*
  2573. * Channel mode helper
  2574. */
  2575. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2576. struct snd_ctl_elem_info *uinfo,
  2577. const struct hda_channel_mode *chmode,
  2578. int num_chmodes)
  2579. {
  2580. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2581. uinfo->count = 1;
  2582. uinfo->value.enumerated.items = num_chmodes;
  2583. if (uinfo->value.enumerated.item >= num_chmodes)
  2584. uinfo->value.enumerated.item = num_chmodes - 1;
  2585. sprintf(uinfo->value.enumerated.name, "%dch",
  2586. chmode[uinfo->value.enumerated.item].channels);
  2587. return 0;
  2588. }
  2589. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
  2590. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2591. struct snd_ctl_elem_value *ucontrol,
  2592. const struct hda_channel_mode *chmode,
  2593. int num_chmodes,
  2594. int max_channels)
  2595. {
  2596. int i;
  2597. for (i = 0; i < num_chmodes; i++) {
  2598. if (max_channels == chmode[i].channels) {
  2599. ucontrol->value.enumerated.item[0] = i;
  2600. break;
  2601. }
  2602. }
  2603. return 0;
  2604. }
  2605. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
  2606. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2607. struct snd_ctl_elem_value *ucontrol,
  2608. const struct hda_channel_mode *chmode,
  2609. int num_chmodes,
  2610. int *max_channelsp)
  2611. {
  2612. unsigned int mode;
  2613. mode = ucontrol->value.enumerated.item[0];
  2614. if (mode >= num_chmodes)
  2615. return -EINVAL;
  2616. if (*max_channelsp == chmode[mode].channels)
  2617. return 0;
  2618. /* change the current channel setting */
  2619. *max_channelsp = chmode[mode].channels;
  2620. if (chmode[mode].sequence)
  2621. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2622. return 1;
  2623. }
  2624. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
  2625. /*
  2626. * input MUX helper
  2627. */
  2628. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2629. struct snd_ctl_elem_info *uinfo)
  2630. {
  2631. unsigned int index;
  2632. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2633. uinfo->count = 1;
  2634. uinfo->value.enumerated.items = imux->num_items;
  2635. if (!imux->num_items)
  2636. return 0;
  2637. index = uinfo->value.enumerated.item;
  2638. if (index >= imux->num_items)
  2639. index = imux->num_items - 1;
  2640. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2641. return 0;
  2642. }
  2643. EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
  2644. int snd_hda_input_mux_put(struct hda_codec *codec,
  2645. const struct hda_input_mux *imux,
  2646. struct snd_ctl_elem_value *ucontrol,
  2647. hda_nid_t nid,
  2648. unsigned int *cur_val)
  2649. {
  2650. unsigned int idx;
  2651. if (!imux->num_items)
  2652. return 0;
  2653. idx = ucontrol->value.enumerated.item[0];
  2654. if (idx >= imux->num_items)
  2655. idx = imux->num_items - 1;
  2656. if (*cur_val == idx)
  2657. return 0;
  2658. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2659. imux->items[idx].index);
  2660. *cur_val = idx;
  2661. return 1;
  2662. }
  2663. EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
  2664. /*
  2665. * Multi-channel / digital-out PCM helper functions
  2666. */
  2667. /* setup SPDIF output stream */
  2668. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2669. unsigned int stream_tag, unsigned int format)
  2670. {
  2671. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2672. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2673. set_dig_out_convert(codec, nid,
  2674. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
  2675. -1);
  2676. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2677. if (codec->slave_dig_outs) {
  2678. hda_nid_t *d;
  2679. for (d = codec->slave_dig_outs; *d; d++)
  2680. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  2681. format);
  2682. }
  2683. /* turn on again (if needed) */
  2684. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2685. set_dig_out_convert(codec, nid,
  2686. codec->spdif_ctls & 0xff, -1);
  2687. }
  2688. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  2689. {
  2690. snd_hda_codec_cleanup_stream(codec, nid);
  2691. if (codec->slave_dig_outs) {
  2692. hda_nid_t *d;
  2693. for (d = codec->slave_dig_outs; *d; d++)
  2694. snd_hda_codec_cleanup_stream(codec, *d);
  2695. }
  2696. }
  2697. /*
  2698. * open the digital out in the exclusive mode
  2699. */
  2700. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2701. struct hda_multi_out *mout)
  2702. {
  2703. mutex_lock(&codec->spdif_mutex);
  2704. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2705. /* already opened as analog dup; reset it once */
  2706. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2707. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2708. mutex_unlock(&codec->spdif_mutex);
  2709. return 0;
  2710. }
  2711. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
  2712. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2713. struct hda_multi_out *mout,
  2714. unsigned int stream_tag,
  2715. unsigned int format,
  2716. struct snd_pcm_substream *substream)
  2717. {
  2718. mutex_lock(&codec->spdif_mutex);
  2719. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2720. mutex_unlock(&codec->spdif_mutex);
  2721. return 0;
  2722. }
  2723. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
  2724. /*
  2725. * release the digital out
  2726. */
  2727. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2728. struct hda_multi_out *mout)
  2729. {
  2730. mutex_lock(&codec->spdif_mutex);
  2731. mout->dig_out_used = 0;
  2732. mutex_unlock(&codec->spdif_mutex);
  2733. return 0;
  2734. }
  2735. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
  2736. /*
  2737. * set up more restrictions for analog out
  2738. */
  2739. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2740. struct hda_multi_out *mout,
  2741. struct snd_pcm_substream *substream,
  2742. struct hda_pcm_stream *hinfo)
  2743. {
  2744. struct snd_pcm_runtime *runtime = substream->runtime;
  2745. runtime->hw.channels_max = mout->max_channels;
  2746. if (mout->dig_out_nid) {
  2747. if (!mout->analog_rates) {
  2748. mout->analog_rates = hinfo->rates;
  2749. mout->analog_formats = hinfo->formats;
  2750. mout->analog_maxbps = hinfo->maxbps;
  2751. } else {
  2752. runtime->hw.rates = mout->analog_rates;
  2753. runtime->hw.formats = mout->analog_formats;
  2754. hinfo->maxbps = mout->analog_maxbps;
  2755. }
  2756. if (!mout->spdif_rates) {
  2757. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  2758. &mout->spdif_rates,
  2759. &mout->spdif_formats,
  2760. &mout->spdif_maxbps);
  2761. }
  2762. mutex_lock(&codec->spdif_mutex);
  2763. if (mout->share_spdif) {
  2764. runtime->hw.rates &= mout->spdif_rates;
  2765. runtime->hw.formats &= mout->spdif_formats;
  2766. if (mout->spdif_maxbps < hinfo->maxbps)
  2767. hinfo->maxbps = mout->spdif_maxbps;
  2768. }
  2769. mutex_unlock(&codec->spdif_mutex);
  2770. }
  2771. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2772. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2773. }
  2774. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
  2775. /*
  2776. * set up the i/o for analog out
  2777. * when the digital out is available, copy the front out to digital out, too.
  2778. */
  2779. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2780. struct hda_multi_out *mout,
  2781. unsigned int stream_tag,
  2782. unsigned int format,
  2783. struct snd_pcm_substream *substream)
  2784. {
  2785. hda_nid_t *nids = mout->dac_nids;
  2786. int chs = substream->runtime->channels;
  2787. int i;
  2788. mutex_lock(&codec->spdif_mutex);
  2789. if (mout->dig_out_nid && mout->share_spdif &&
  2790. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2791. if (chs == 2 &&
  2792. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2793. format) &&
  2794. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2795. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2796. setup_dig_out_stream(codec, mout->dig_out_nid,
  2797. stream_tag, format);
  2798. } else {
  2799. mout->dig_out_used = 0;
  2800. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2801. }
  2802. }
  2803. mutex_unlock(&codec->spdif_mutex);
  2804. /* front */
  2805. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2806. 0, format);
  2807. if (!mout->no_share_stream &&
  2808. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2809. /* headphone out will just decode front left/right (stereo) */
  2810. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2811. 0, format);
  2812. /* extra outputs copied from front */
  2813. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2814. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2815. snd_hda_codec_setup_stream(codec,
  2816. mout->extra_out_nid[i],
  2817. stream_tag, 0, format);
  2818. /* surrounds */
  2819. for (i = 1; i < mout->num_dacs; i++) {
  2820. if (chs >= (i + 1) * 2) /* independent out */
  2821. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2822. i * 2, format);
  2823. else if (!mout->no_share_stream) /* copy front */
  2824. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2825. 0, format);
  2826. }
  2827. return 0;
  2828. }
  2829. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
  2830. /*
  2831. * clean up the setting for analog out
  2832. */
  2833. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2834. struct hda_multi_out *mout)
  2835. {
  2836. hda_nid_t *nids = mout->dac_nids;
  2837. int i;
  2838. for (i = 0; i < mout->num_dacs; i++)
  2839. snd_hda_codec_cleanup_stream(codec, nids[i]);
  2840. if (mout->hp_nid)
  2841. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  2842. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2843. if (mout->extra_out_nid[i])
  2844. snd_hda_codec_cleanup_stream(codec,
  2845. mout->extra_out_nid[i]);
  2846. mutex_lock(&codec->spdif_mutex);
  2847. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2848. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2849. mout->dig_out_used = 0;
  2850. }
  2851. mutex_unlock(&codec->spdif_mutex);
  2852. return 0;
  2853. }
  2854. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
  2855. /*
  2856. * Helper for automatic pin configuration
  2857. */
  2858. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2859. {
  2860. for (; *list; list++)
  2861. if (*list == nid)
  2862. return 1;
  2863. return 0;
  2864. }
  2865. /*
  2866. * Sort an associated group of pins according to their sequence numbers.
  2867. */
  2868. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2869. int num_pins)
  2870. {
  2871. int i, j;
  2872. short seq;
  2873. hda_nid_t nid;
  2874. for (i = 0; i < num_pins; i++) {
  2875. for (j = i + 1; j < num_pins; j++) {
  2876. if (sequences[i] > sequences[j]) {
  2877. seq = sequences[i];
  2878. sequences[i] = sequences[j];
  2879. sequences[j] = seq;
  2880. nid = pins[i];
  2881. pins[i] = pins[j];
  2882. pins[j] = nid;
  2883. }
  2884. }
  2885. }
  2886. }
  2887. /*
  2888. * Parse all pin widgets and store the useful pin nids to cfg
  2889. *
  2890. * The number of line-outs or any primary output is stored in line_outs,
  2891. * and the corresponding output pins are assigned to line_out_pins[],
  2892. * in the order of front, rear, CLFE, side, ...
  2893. *
  2894. * If more extra outputs (speaker and headphone) are found, the pins are
  2895. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2896. * is detected, one of speaker of HP pins is assigned as the primary
  2897. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2898. * if any analog output exists.
  2899. *
  2900. * The analog input pins are assigned to input_pins array.
  2901. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2902. * respectively.
  2903. */
  2904. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2905. struct auto_pin_cfg *cfg,
  2906. hda_nid_t *ignore_nids)
  2907. {
  2908. hda_nid_t nid, end_nid;
  2909. short seq, assoc_line_out, assoc_speaker;
  2910. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2911. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2912. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2913. memset(cfg, 0, sizeof(*cfg));
  2914. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2915. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2916. memset(sequences_hp, 0, sizeof(sequences_hp));
  2917. assoc_line_out = assoc_speaker = 0;
  2918. end_nid = codec->start_nid + codec->num_nodes;
  2919. for (nid = codec->start_nid; nid < end_nid; nid++) {
  2920. unsigned int wid_caps = get_wcaps(codec, nid);
  2921. unsigned int wid_type =
  2922. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2923. unsigned int def_conf;
  2924. short assoc, loc;
  2925. /* read all default configuration for pin complex */
  2926. if (wid_type != AC_WID_PIN)
  2927. continue;
  2928. /* ignore the given nids (e.g. pc-beep returns error) */
  2929. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2930. continue;
  2931. def_conf = snd_hda_codec_read(codec, nid, 0,
  2932. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2933. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2934. continue;
  2935. loc = get_defcfg_location(def_conf);
  2936. switch (get_defcfg_device(def_conf)) {
  2937. case AC_JACK_LINE_OUT:
  2938. seq = get_defcfg_sequence(def_conf);
  2939. assoc = get_defcfg_association(def_conf);
  2940. if (!(wid_caps & AC_WCAP_STEREO))
  2941. if (!cfg->mono_out_pin)
  2942. cfg->mono_out_pin = nid;
  2943. if (!assoc)
  2944. continue;
  2945. if (!assoc_line_out)
  2946. assoc_line_out = assoc;
  2947. else if (assoc_line_out != assoc)
  2948. continue;
  2949. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2950. continue;
  2951. cfg->line_out_pins[cfg->line_outs] = nid;
  2952. sequences_line_out[cfg->line_outs] = seq;
  2953. cfg->line_outs++;
  2954. break;
  2955. case AC_JACK_SPEAKER:
  2956. seq = get_defcfg_sequence(def_conf);
  2957. assoc = get_defcfg_association(def_conf);
  2958. if (! assoc)
  2959. continue;
  2960. if (! assoc_speaker)
  2961. assoc_speaker = assoc;
  2962. else if (assoc_speaker != assoc)
  2963. continue;
  2964. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2965. continue;
  2966. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2967. sequences_speaker[cfg->speaker_outs] = seq;
  2968. cfg->speaker_outs++;
  2969. break;
  2970. case AC_JACK_HP_OUT:
  2971. seq = get_defcfg_sequence(def_conf);
  2972. assoc = get_defcfg_association(def_conf);
  2973. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2974. continue;
  2975. cfg->hp_pins[cfg->hp_outs] = nid;
  2976. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2977. cfg->hp_outs++;
  2978. break;
  2979. case AC_JACK_MIC_IN: {
  2980. int preferred, alt;
  2981. if (loc == AC_JACK_LOC_FRONT) {
  2982. preferred = AUTO_PIN_FRONT_MIC;
  2983. alt = AUTO_PIN_MIC;
  2984. } else {
  2985. preferred = AUTO_PIN_MIC;
  2986. alt = AUTO_PIN_FRONT_MIC;
  2987. }
  2988. if (!cfg->input_pins[preferred])
  2989. cfg->input_pins[preferred] = nid;
  2990. else if (!cfg->input_pins[alt])
  2991. cfg->input_pins[alt] = nid;
  2992. break;
  2993. }
  2994. case AC_JACK_LINE_IN:
  2995. if (loc == AC_JACK_LOC_FRONT)
  2996. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2997. else
  2998. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2999. break;
  3000. case AC_JACK_CD:
  3001. cfg->input_pins[AUTO_PIN_CD] = nid;
  3002. break;
  3003. case AC_JACK_AUX:
  3004. cfg->input_pins[AUTO_PIN_AUX] = nid;
  3005. break;
  3006. case AC_JACK_SPDIF_OUT:
  3007. cfg->dig_out_pin = nid;
  3008. break;
  3009. case AC_JACK_SPDIF_IN:
  3010. cfg->dig_in_pin = nid;
  3011. break;
  3012. }
  3013. }
  3014. /* FIX-UP:
  3015. * If no line-out is defined but multiple HPs are found,
  3016. * some of them might be the real line-outs.
  3017. */
  3018. if (!cfg->line_outs && cfg->hp_outs > 1) {
  3019. int i = 0;
  3020. while (i < cfg->hp_outs) {
  3021. /* The real HPs should have the sequence 0x0f */
  3022. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  3023. i++;
  3024. continue;
  3025. }
  3026. /* Move it to the line-out table */
  3027. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  3028. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  3029. cfg->line_outs++;
  3030. cfg->hp_outs--;
  3031. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  3032. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  3033. memmove(sequences_hp + i - 1, sequences_hp + i,
  3034. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  3035. }
  3036. }
  3037. /* sort by sequence */
  3038. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  3039. cfg->line_outs);
  3040. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  3041. cfg->speaker_outs);
  3042. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  3043. cfg->hp_outs);
  3044. /* if we have only one mic, make it AUTO_PIN_MIC */
  3045. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  3046. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  3047. cfg->input_pins[AUTO_PIN_MIC] =
  3048. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  3049. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  3050. }
  3051. /* ditto for line-in */
  3052. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  3053. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  3054. cfg->input_pins[AUTO_PIN_LINE] =
  3055. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  3056. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  3057. }
  3058. /*
  3059. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  3060. * as a primary output
  3061. */
  3062. if (!cfg->line_outs) {
  3063. if (cfg->speaker_outs) {
  3064. cfg->line_outs = cfg->speaker_outs;
  3065. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  3066. sizeof(cfg->speaker_pins));
  3067. cfg->speaker_outs = 0;
  3068. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  3069. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  3070. } else if (cfg->hp_outs) {
  3071. cfg->line_outs = cfg->hp_outs;
  3072. memcpy(cfg->line_out_pins, cfg->hp_pins,
  3073. sizeof(cfg->hp_pins));
  3074. cfg->hp_outs = 0;
  3075. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  3076. cfg->line_out_type = AUTO_PIN_HP_OUT;
  3077. }
  3078. }
  3079. /* Reorder the surround channels
  3080. * ALSA sequence is front/surr/clfe/side
  3081. * HDA sequence is:
  3082. * 4-ch: front/surr => OK as it is
  3083. * 6-ch: front/clfe/surr
  3084. * 8-ch: front/clfe/rear/side|fc
  3085. */
  3086. switch (cfg->line_outs) {
  3087. case 3:
  3088. case 4:
  3089. nid = cfg->line_out_pins[1];
  3090. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  3091. cfg->line_out_pins[2] = nid;
  3092. break;
  3093. }
  3094. /*
  3095. * debug prints of the parsed results
  3096. */
  3097. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3098. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  3099. cfg->line_out_pins[2], cfg->line_out_pins[3],
  3100. cfg->line_out_pins[4]);
  3101. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3102. cfg->speaker_outs, cfg->speaker_pins[0],
  3103. cfg->speaker_pins[1], cfg->speaker_pins[2],
  3104. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  3105. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3106. cfg->hp_outs, cfg->hp_pins[0],
  3107. cfg->hp_pins[1], cfg->hp_pins[2],
  3108. cfg->hp_pins[3], cfg->hp_pins[4]);
  3109. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  3110. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  3111. " cd=0x%x, aux=0x%x\n",
  3112. cfg->input_pins[AUTO_PIN_MIC],
  3113. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  3114. cfg->input_pins[AUTO_PIN_LINE],
  3115. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  3116. cfg->input_pins[AUTO_PIN_CD],
  3117. cfg->input_pins[AUTO_PIN_AUX]);
  3118. return 0;
  3119. }
  3120. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
  3121. /* labels for input pins */
  3122. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  3123. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  3124. };
  3125. EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
  3126. #ifdef CONFIG_PM
  3127. /*
  3128. * power management
  3129. */
  3130. /**
  3131. * snd_hda_suspend - suspend the codecs
  3132. * @bus: the HDA bus
  3133. * @state: suspsend state
  3134. *
  3135. * Returns 0 if successful.
  3136. */
  3137. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  3138. {
  3139. struct hda_codec *codec;
  3140. list_for_each_entry(codec, &bus->codec_list, list) {
  3141. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3142. if (!codec->power_on)
  3143. continue;
  3144. #endif
  3145. hda_call_codec_suspend(codec);
  3146. }
  3147. return 0;
  3148. }
  3149. EXPORT_SYMBOL_HDA(snd_hda_suspend);
  3150. /**
  3151. * snd_hda_resume - resume the codecs
  3152. * @bus: the HDA bus
  3153. *
  3154. * Returns 0 if successful.
  3155. *
  3156. * This fucntion is defined only when POWER_SAVE isn't set.
  3157. * In the power-save mode, the codec is resumed dynamically.
  3158. */
  3159. int snd_hda_resume(struct hda_bus *bus)
  3160. {
  3161. struct hda_codec *codec;
  3162. list_for_each_entry(codec, &bus->codec_list, list) {
  3163. if (snd_hda_codec_needs_resume(codec))
  3164. hda_call_codec_resume(codec);
  3165. }
  3166. return 0;
  3167. }
  3168. EXPORT_SYMBOL_HDA(snd_hda_resume);
  3169. #endif /* CONFIG_PM */
  3170. /*
  3171. * generic arrays
  3172. */
  3173. /* get a new element from the given array
  3174. * if it exceeds the pre-allocated array size, re-allocate the array
  3175. */
  3176. void *snd_array_new(struct snd_array *array)
  3177. {
  3178. if (array->used >= array->alloced) {
  3179. int num = array->alloced + array->alloc_align;
  3180. void *nlist;
  3181. if (snd_BUG_ON(num >= 4096))
  3182. return NULL;
  3183. nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
  3184. if (!nlist)
  3185. return NULL;
  3186. if (array->list) {
  3187. memcpy(nlist, array->list,
  3188. array->elem_size * array->alloced);
  3189. kfree(array->list);
  3190. }
  3191. array->list = nlist;
  3192. array->alloced = num;
  3193. }
  3194. return snd_array_elem(array, array->used++);
  3195. }
  3196. EXPORT_SYMBOL_HDA(snd_array_new);
  3197. /* free the given array elements */
  3198. void snd_array_free(struct snd_array *array)
  3199. {
  3200. kfree(array->list);
  3201. array->used = 0;
  3202. array->alloced = 0;
  3203. array->list = NULL;
  3204. }
  3205. EXPORT_SYMBOL_HDA(snd_array_free);
  3206. /*
  3207. * used by hda_proc.c and hda_eld.c
  3208. */
  3209. void snd_print_pcm_rates(int pcm, char *buf, int buflen)
  3210. {
  3211. static unsigned int rates[] = {
  3212. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  3213. 96000, 176400, 192000, 384000
  3214. };
  3215. int i, j;
  3216. for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
  3217. if (pcm & (1 << i))
  3218. j += snprintf(buf + j, buflen - j, " %d", rates[i]);
  3219. buf[j] = '\0'; /* necessary when j == 0 */
  3220. }
  3221. EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
  3222. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3223. {
  3224. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3225. int i, j;
  3226. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3227. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3228. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  3229. buf[j] = '\0'; /* necessary when j == 0 */
  3230. }
  3231. EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
  3232. MODULE_DESCRIPTION("HDA codec core");
  3233. MODULE_LICENSE("GPL");