hda_codec.c 93 KB

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