hda_codec.c 84 KB

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