hda_codec.c 93 KB

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