hda_codec.c 87 KB

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