hda_codec.c 88 KB

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