soc-core.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456
  1. /*
  2. * soc-core.c -- ALSA SoC Audio Layer
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. * Copyright (C) 2010 Slimlogic Ltd.
  7. * Copyright (C) 2010 Texas Instruments Inc.
  8. *
  9. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  10. * with code, comments and ideas from :-
  11. * Richard Purdie <richard@openedhand.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * TODO:
  19. * o Add hw rules to enforce rates, etc.
  20. * o More testing with other codecs/machines.
  21. * o Add more codecs and platforms to ensure good API coverage.
  22. * o Support TDM on PCM and I2S
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/pm.h>
  29. #include <linux/bitops.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/ctype.h>
  33. #include <linux/slab.h>
  34. #include <linux/of.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dpcm.h>
  42. #include <sound/initval.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/asoc.h>
  45. #define NAME_SIZE 32
  46. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  47. #ifdef CONFIG_DEBUG_FS
  48. struct dentry *snd_soc_debugfs_root;
  49. EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  50. #endif
  51. static DEFINE_MUTEX(client_mutex);
  52. static LIST_HEAD(dai_list);
  53. static LIST_HEAD(platform_list);
  54. static LIST_HEAD(codec_list);
  55. static LIST_HEAD(component_list);
  56. /*
  57. * This is a timeout to do a DAPM powerdown after a stream is closed().
  58. * It can be used to eliminate pops between different playback streams, e.g.
  59. * between two audio tracks.
  60. */
  61. static int pmdown_time = 5000;
  62. module_param(pmdown_time, int, 0);
  63. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  64. /* returns the minimum number of bytes needed to represent
  65. * a particular given value */
  66. static int min_bytes_needed(unsigned long val)
  67. {
  68. int c = 0;
  69. int i;
  70. for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  71. if (val & (1UL << i))
  72. break;
  73. c = (sizeof val * 8) - c;
  74. if (!c || (c % 8))
  75. c = (c + 8) / 8;
  76. else
  77. c /= 8;
  78. return c;
  79. }
  80. /* fill buf which is 'len' bytes with a formatted
  81. * string of the form 'reg: value\n' */
  82. static int format_register_str(struct snd_soc_codec *codec,
  83. unsigned int reg, char *buf, size_t len)
  84. {
  85. int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  86. int regsize = codec->driver->reg_word_size * 2;
  87. int ret;
  88. char tmpbuf[len + 1];
  89. char regbuf[regsize + 1];
  90. /* since tmpbuf is allocated on the stack, warn the callers if they
  91. * try to abuse this function */
  92. WARN_ON(len > 63);
  93. /* +2 for ': ' and + 1 for '\n' */
  94. if (wordsize + regsize + 2 + 1 != len)
  95. return -EINVAL;
  96. ret = snd_soc_read(codec, reg);
  97. if (ret < 0) {
  98. memset(regbuf, 'X', regsize);
  99. regbuf[regsize] = '\0';
  100. } else {
  101. snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
  102. }
  103. /* prepare the buffer */
  104. snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
  105. /* copy it back to the caller without the '\0' */
  106. memcpy(buf, tmpbuf, len);
  107. return 0;
  108. }
  109. /* codec register dump */
  110. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  111. size_t count, loff_t pos)
  112. {
  113. int i, step = 1;
  114. int wordsize, regsize;
  115. int len;
  116. size_t total = 0;
  117. loff_t p = 0;
  118. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  119. regsize = codec->driver->reg_word_size * 2;
  120. len = wordsize + regsize + 2 + 1;
  121. if (!codec->driver->reg_cache_size)
  122. return 0;
  123. if (codec->driver->reg_cache_step)
  124. step = codec->driver->reg_cache_step;
  125. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  126. if (!snd_soc_codec_readable_register(codec, i))
  127. continue;
  128. if (codec->driver->display_register) {
  129. count += codec->driver->display_register(codec, buf + count,
  130. PAGE_SIZE - count, i);
  131. } else {
  132. /* only support larger than PAGE_SIZE bytes debugfs
  133. * entries for the default case */
  134. if (p >= pos) {
  135. if (total + len >= count - 1)
  136. break;
  137. format_register_str(codec, i, buf + total, len);
  138. total += len;
  139. }
  140. p += len;
  141. }
  142. }
  143. total = min(total, count - 1);
  144. return total;
  145. }
  146. static ssize_t codec_reg_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  150. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  151. }
  152. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  153. static ssize_t pmdown_time_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  157. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  158. }
  159. static ssize_t pmdown_time_set(struct device *dev,
  160. struct device_attribute *attr,
  161. const char *buf, size_t count)
  162. {
  163. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  164. int ret;
  165. ret = strict_strtol(buf, 10, &rtd->pmdown_time);
  166. if (ret)
  167. return ret;
  168. return count;
  169. }
  170. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  171. #ifdef CONFIG_DEBUG_FS
  172. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. ssize_t ret;
  176. struct snd_soc_codec *codec = file->private_data;
  177. char *buf;
  178. if (*ppos < 0 || !count)
  179. return -EINVAL;
  180. buf = kmalloc(count, GFP_KERNEL);
  181. if (!buf)
  182. return -ENOMEM;
  183. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  184. if (ret >= 0) {
  185. if (copy_to_user(user_buf, buf, ret)) {
  186. kfree(buf);
  187. return -EFAULT;
  188. }
  189. *ppos += ret;
  190. }
  191. kfree(buf);
  192. return ret;
  193. }
  194. static ssize_t codec_reg_write_file(struct file *file,
  195. const char __user *user_buf, size_t count, loff_t *ppos)
  196. {
  197. char buf[32];
  198. size_t buf_size;
  199. char *start = buf;
  200. unsigned long reg, value;
  201. struct snd_soc_codec *codec = file->private_data;
  202. buf_size = min(count, (sizeof(buf)-1));
  203. if (copy_from_user(buf, user_buf, buf_size))
  204. return -EFAULT;
  205. buf[buf_size] = 0;
  206. while (*start == ' ')
  207. start++;
  208. reg = simple_strtoul(start, &start, 16);
  209. while (*start == ' ')
  210. start++;
  211. if (strict_strtoul(start, 16, &value))
  212. return -EINVAL;
  213. /* Userspace has been fiddling around behind the kernel's back */
  214. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  215. snd_soc_write(codec, reg, value);
  216. return buf_size;
  217. }
  218. static const struct file_operations codec_reg_fops = {
  219. .open = simple_open,
  220. .read = codec_reg_read_file,
  221. .write = codec_reg_write_file,
  222. .llseek = default_llseek,
  223. };
  224. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  225. {
  226. struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
  227. codec->debugfs_codec_root = debugfs_create_dir(codec->name,
  228. debugfs_card_root);
  229. if (!codec->debugfs_codec_root) {
  230. dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
  231. " directory\n");
  232. return;
  233. }
  234. debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
  235. &codec->cache_sync);
  236. debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
  237. &codec->cache_only);
  238. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  239. codec->debugfs_codec_root,
  240. codec, &codec_reg_fops);
  241. if (!codec->debugfs_reg)
  242. dev_warn(codec->dev, "ASoC: Failed to create codec register"
  243. " debugfs file\n");
  244. snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
  245. }
  246. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  247. {
  248. debugfs_remove_recursive(codec->debugfs_codec_root);
  249. }
  250. static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  251. {
  252. struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
  253. platform->debugfs_platform_root = debugfs_create_dir(platform->name,
  254. debugfs_card_root);
  255. if (!platform->debugfs_platform_root) {
  256. dev_warn(platform->dev,
  257. "ASoC: Failed to create platform debugfs directory\n");
  258. return;
  259. }
  260. snd_soc_dapm_debugfs_init(&platform->dapm,
  261. platform->debugfs_platform_root);
  262. }
  263. static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  264. {
  265. debugfs_remove_recursive(platform->debugfs_platform_root);
  266. }
  267. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  268. size_t count, loff_t *ppos)
  269. {
  270. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  271. ssize_t len, ret = 0;
  272. struct snd_soc_codec *codec;
  273. if (!buf)
  274. return -ENOMEM;
  275. list_for_each_entry(codec, &codec_list, list) {
  276. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  277. codec->name);
  278. if (len >= 0)
  279. ret += len;
  280. if (ret > PAGE_SIZE) {
  281. ret = PAGE_SIZE;
  282. break;
  283. }
  284. }
  285. if (ret >= 0)
  286. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  287. kfree(buf);
  288. return ret;
  289. }
  290. static const struct file_operations codec_list_fops = {
  291. .read = codec_list_read_file,
  292. .llseek = default_llseek,/* read accesses f_pos */
  293. };
  294. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  295. size_t count, loff_t *ppos)
  296. {
  297. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  298. ssize_t len, ret = 0;
  299. struct snd_soc_dai *dai;
  300. if (!buf)
  301. return -ENOMEM;
  302. list_for_each_entry(dai, &dai_list, list) {
  303. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
  304. if (len >= 0)
  305. ret += len;
  306. if (ret > PAGE_SIZE) {
  307. ret = PAGE_SIZE;
  308. break;
  309. }
  310. }
  311. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  312. kfree(buf);
  313. return ret;
  314. }
  315. static const struct file_operations dai_list_fops = {
  316. .read = dai_list_read_file,
  317. .llseek = default_llseek,/* read accesses f_pos */
  318. };
  319. static ssize_t platform_list_read_file(struct file *file,
  320. char __user *user_buf,
  321. size_t count, loff_t *ppos)
  322. {
  323. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  324. ssize_t len, ret = 0;
  325. struct snd_soc_platform *platform;
  326. if (!buf)
  327. return -ENOMEM;
  328. list_for_each_entry(platform, &platform_list, list) {
  329. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  330. platform->name);
  331. if (len >= 0)
  332. ret += len;
  333. if (ret > PAGE_SIZE) {
  334. ret = PAGE_SIZE;
  335. break;
  336. }
  337. }
  338. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  339. kfree(buf);
  340. return ret;
  341. }
  342. static const struct file_operations platform_list_fops = {
  343. .read = platform_list_read_file,
  344. .llseek = default_llseek,/* read accesses f_pos */
  345. };
  346. static void soc_init_card_debugfs(struct snd_soc_card *card)
  347. {
  348. card->debugfs_card_root = debugfs_create_dir(card->name,
  349. snd_soc_debugfs_root);
  350. if (!card->debugfs_card_root) {
  351. dev_warn(card->dev,
  352. "ASoC: Failed to create card debugfs directory\n");
  353. return;
  354. }
  355. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  356. card->debugfs_card_root,
  357. &card->pop_time);
  358. if (!card->debugfs_pop_time)
  359. dev_warn(card->dev,
  360. "ASoC: Failed to create pop time debugfs file\n");
  361. }
  362. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  363. {
  364. debugfs_remove_recursive(card->debugfs_card_root);
  365. }
  366. #else
  367. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  368. {
  369. }
  370. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  371. {
  372. }
  373. static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  374. {
  375. }
  376. static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  377. {
  378. }
  379. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  380. {
  381. }
  382. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  383. {
  384. }
  385. #endif
  386. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  387. const char *dai_link, int stream)
  388. {
  389. int i;
  390. for (i = 0; i < card->num_links; i++) {
  391. if (card->rtd[i].dai_link->no_pcm &&
  392. !strcmp(card->rtd[i].dai_link->name, dai_link))
  393. return card->rtd[i].pcm->streams[stream].substream;
  394. }
  395. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  396. return NULL;
  397. }
  398. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  399. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  400. const char *dai_link)
  401. {
  402. int i;
  403. for (i = 0; i < card->num_links; i++) {
  404. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  405. return &card->rtd[i];
  406. }
  407. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  408. return NULL;
  409. }
  410. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  411. #ifdef CONFIG_SND_SOC_AC97_BUS
  412. /* unregister ac97 codec */
  413. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  414. {
  415. if (codec->ac97->dev.bus)
  416. device_unregister(&codec->ac97->dev);
  417. return 0;
  418. }
  419. /* stop no dev release warning */
  420. static void soc_ac97_device_release(struct device *dev){}
  421. /* register ac97 codec to bus */
  422. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  423. {
  424. int err;
  425. codec->ac97->dev.bus = &ac97_bus_type;
  426. codec->ac97->dev.parent = codec->card->dev;
  427. codec->ac97->dev.release = soc_ac97_device_release;
  428. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  429. codec->card->snd_card->number, 0, codec->name);
  430. err = device_register(&codec->ac97->dev);
  431. if (err < 0) {
  432. dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
  433. codec->ac97->dev.bus = NULL;
  434. return err;
  435. }
  436. return 0;
  437. }
  438. #endif
  439. #ifdef CONFIG_PM_SLEEP
  440. /* powers down audio subsystem for suspend */
  441. int snd_soc_suspend(struct device *dev)
  442. {
  443. struct snd_soc_card *card = dev_get_drvdata(dev);
  444. struct snd_soc_codec *codec;
  445. int i;
  446. /* If the initialization of this soc device failed, there is no codec
  447. * associated with it. Just bail out in this case.
  448. */
  449. if (list_empty(&card->codec_dev_list))
  450. return 0;
  451. /* Due to the resume being scheduled into a workqueue we could
  452. * suspend before that's finished - wait for it to complete.
  453. */
  454. snd_power_lock(card->snd_card);
  455. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  456. snd_power_unlock(card->snd_card);
  457. /* we're going to block userspace touching us until resume completes */
  458. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  459. /* mute any active DACs */
  460. for (i = 0; i < card->num_rtd; i++) {
  461. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  462. struct snd_soc_dai_driver *drv = dai->driver;
  463. if (card->rtd[i].dai_link->ignore_suspend)
  464. continue;
  465. if (drv->ops->digital_mute && dai->playback_active)
  466. drv->ops->digital_mute(dai, 1);
  467. }
  468. /* suspend all pcms */
  469. for (i = 0; i < card->num_rtd; i++) {
  470. if (card->rtd[i].dai_link->ignore_suspend)
  471. continue;
  472. snd_pcm_suspend_all(card->rtd[i].pcm);
  473. }
  474. if (card->suspend_pre)
  475. card->suspend_pre(card);
  476. for (i = 0; i < card->num_rtd; i++) {
  477. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  478. struct snd_soc_platform *platform = card->rtd[i].platform;
  479. if (card->rtd[i].dai_link->ignore_suspend)
  480. continue;
  481. if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
  482. cpu_dai->driver->suspend(cpu_dai);
  483. if (platform->driver->suspend && !platform->suspended) {
  484. platform->driver->suspend(cpu_dai);
  485. platform->suspended = 1;
  486. }
  487. }
  488. /* close any waiting streams and save state */
  489. for (i = 0; i < card->num_rtd; i++) {
  490. flush_delayed_work(&card->rtd[i].delayed_work);
  491. card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
  492. }
  493. for (i = 0; i < card->num_rtd; i++) {
  494. if (card->rtd[i].dai_link->ignore_suspend)
  495. continue;
  496. snd_soc_dapm_stream_event(&card->rtd[i],
  497. SNDRV_PCM_STREAM_PLAYBACK,
  498. SND_SOC_DAPM_STREAM_SUSPEND);
  499. snd_soc_dapm_stream_event(&card->rtd[i],
  500. SNDRV_PCM_STREAM_CAPTURE,
  501. SND_SOC_DAPM_STREAM_SUSPEND);
  502. }
  503. /* Recheck all analogue paths too */
  504. dapm_mark_io_dirty(&card->dapm);
  505. snd_soc_dapm_sync(&card->dapm);
  506. /* suspend all CODECs */
  507. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  508. /* If there are paths active then the CODEC will be held with
  509. * bias _ON and should not be suspended. */
  510. if (!codec->suspended && codec->driver->suspend) {
  511. switch (codec->dapm.bias_level) {
  512. case SND_SOC_BIAS_STANDBY:
  513. /*
  514. * If the CODEC is capable of idle
  515. * bias off then being in STANDBY
  516. * means it's doing something,
  517. * otherwise fall through.
  518. */
  519. if (codec->dapm.idle_bias_off) {
  520. dev_dbg(codec->dev,
  521. "ASoC: idle_bias_off CODEC on"
  522. " over suspend\n");
  523. break;
  524. }
  525. case SND_SOC_BIAS_OFF:
  526. codec->driver->suspend(codec);
  527. codec->suspended = 1;
  528. codec->cache_sync = 1;
  529. if (codec->using_regmap)
  530. regcache_mark_dirty(codec->control_data);
  531. break;
  532. default:
  533. dev_dbg(codec->dev, "ASoC: CODEC is on"
  534. " over suspend\n");
  535. break;
  536. }
  537. }
  538. }
  539. for (i = 0; i < card->num_rtd; i++) {
  540. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  541. if (card->rtd[i].dai_link->ignore_suspend)
  542. continue;
  543. if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
  544. cpu_dai->driver->suspend(cpu_dai);
  545. }
  546. if (card->suspend_post)
  547. card->suspend_post(card);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  551. /* deferred resume work, so resume can complete before we finished
  552. * setting our codec back up, which can be very slow on I2C
  553. */
  554. static void soc_resume_deferred(struct work_struct *work)
  555. {
  556. struct snd_soc_card *card =
  557. container_of(work, struct snd_soc_card, deferred_resume_work);
  558. struct snd_soc_codec *codec;
  559. int i;
  560. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  561. * so userspace apps are blocked from touching us
  562. */
  563. dev_dbg(card->dev, "ASoC: starting resume work\n");
  564. /* Bring us up into D2 so that DAPM starts enabling things */
  565. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  566. if (card->resume_pre)
  567. card->resume_pre(card);
  568. /* resume AC97 DAIs */
  569. for (i = 0; i < card->num_rtd; i++) {
  570. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  571. if (card->rtd[i].dai_link->ignore_suspend)
  572. continue;
  573. if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
  574. cpu_dai->driver->resume(cpu_dai);
  575. }
  576. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  577. /* If the CODEC was idle over suspend then it will have been
  578. * left with bias OFF or STANDBY and suspended so we must now
  579. * resume. Otherwise the suspend was suppressed.
  580. */
  581. if (codec->driver->resume && codec->suspended) {
  582. switch (codec->dapm.bias_level) {
  583. case SND_SOC_BIAS_STANDBY:
  584. case SND_SOC_BIAS_OFF:
  585. codec->driver->resume(codec);
  586. codec->suspended = 0;
  587. break;
  588. default:
  589. dev_dbg(codec->dev, "ASoC: CODEC was on over"
  590. " suspend\n");
  591. break;
  592. }
  593. }
  594. }
  595. for (i = 0; i < card->num_rtd; i++) {
  596. if (card->rtd[i].dai_link->ignore_suspend)
  597. continue;
  598. snd_soc_dapm_stream_event(&card->rtd[i],
  599. SNDRV_PCM_STREAM_PLAYBACK,
  600. SND_SOC_DAPM_STREAM_RESUME);
  601. snd_soc_dapm_stream_event(&card->rtd[i],
  602. SNDRV_PCM_STREAM_CAPTURE,
  603. SND_SOC_DAPM_STREAM_RESUME);
  604. }
  605. /* unmute any active DACs */
  606. for (i = 0; i < card->num_rtd; i++) {
  607. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  608. struct snd_soc_dai_driver *drv = dai->driver;
  609. if (card->rtd[i].dai_link->ignore_suspend)
  610. continue;
  611. if (drv->ops->digital_mute && dai->playback_active)
  612. drv->ops->digital_mute(dai, 0);
  613. }
  614. for (i = 0; i < card->num_rtd; i++) {
  615. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  616. struct snd_soc_platform *platform = card->rtd[i].platform;
  617. if (card->rtd[i].dai_link->ignore_suspend)
  618. continue;
  619. if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
  620. cpu_dai->driver->resume(cpu_dai);
  621. if (platform->driver->resume && platform->suspended) {
  622. platform->driver->resume(cpu_dai);
  623. platform->suspended = 0;
  624. }
  625. }
  626. if (card->resume_post)
  627. card->resume_post(card);
  628. dev_dbg(card->dev, "ASoC: resume work completed\n");
  629. /* userspace can access us now we are back as we were before */
  630. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  631. /* Recheck all analogue paths too */
  632. dapm_mark_io_dirty(&card->dapm);
  633. snd_soc_dapm_sync(&card->dapm);
  634. }
  635. /* powers up audio subsystem after a suspend */
  636. int snd_soc_resume(struct device *dev)
  637. {
  638. struct snd_soc_card *card = dev_get_drvdata(dev);
  639. int i, ac97_control = 0;
  640. /* If the initialization of this soc device failed, there is no codec
  641. * associated with it. Just bail out in this case.
  642. */
  643. if (list_empty(&card->codec_dev_list))
  644. return 0;
  645. /* AC97 devices might have other drivers hanging off them so
  646. * need to resume immediately. Other drivers don't have that
  647. * problem and may take a substantial amount of time to resume
  648. * due to I/O costs and anti-pop so handle them out of line.
  649. */
  650. for (i = 0; i < card->num_rtd; i++) {
  651. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  652. ac97_control |= cpu_dai->driver->ac97_control;
  653. }
  654. if (ac97_control) {
  655. dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
  656. soc_resume_deferred(&card->deferred_resume_work);
  657. } else {
  658. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  659. if (!schedule_work(&card->deferred_resume_work))
  660. dev_err(dev, "ASoC: resume work item may be lost\n");
  661. }
  662. return 0;
  663. }
  664. EXPORT_SYMBOL_GPL(snd_soc_resume);
  665. #else
  666. #define snd_soc_suspend NULL
  667. #define snd_soc_resume NULL
  668. #endif
  669. static const struct snd_soc_dai_ops null_dai_ops = {
  670. };
  671. static int soc_bind_dai_link(struct snd_soc_card *card, int num)
  672. {
  673. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  674. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  675. struct snd_soc_codec *codec;
  676. struct snd_soc_platform *platform;
  677. struct snd_soc_dai *codec_dai, *cpu_dai;
  678. const char *platform_name;
  679. dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
  680. /* Find CPU DAI from registered DAIs*/
  681. list_for_each_entry(cpu_dai, &dai_list, list) {
  682. if (dai_link->cpu_of_node &&
  683. (cpu_dai->dev->of_node != dai_link->cpu_of_node))
  684. continue;
  685. if (dai_link->cpu_name &&
  686. strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
  687. continue;
  688. if (dai_link->cpu_dai_name &&
  689. strcmp(cpu_dai->name, dai_link->cpu_dai_name))
  690. continue;
  691. rtd->cpu_dai = cpu_dai;
  692. }
  693. if (!rtd->cpu_dai) {
  694. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  695. dai_link->cpu_dai_name);
  696. return -EPROBE_DEFER;
  697. }
  698. /* Find CODEC from registered CODECs */
  699. list_for_each_entry(codec, &codec_list, list) {
  700. if (dai_link->codec_of_node) {
  701. if (codec->dev->of_node != dai_link->codec_of_node)
  702. continue;
  703. } else {
  704. if (strcmp(codec->name, dai_link->codec_name))
  705. continue;
  706. }
  707. rtd->codec = codec;
  708. /*
  709. * CODEC found, so find CODEC DAI from registered DAIs from
  710. * this CODEC
  711. */
  712. list_for_each_entry(codec_dai, &dai_list, list) {
  713. if (codec->dev == codec_dai->dev &&
  714. !strcmp(codec_dai->name,
  715. dai_link->codec_dai_name)) {
  716. rtd->codec_dai = codec_dai;
  717. }
  718. }
  719. if (!rtd->codec_dai) {
  720. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  721. dai_link->codec_dai_name);
  722. return -EPROBE_DEFER;
  723. }
  724. }
  725. if (!rtd->codec) {
  726. dev_err(card->dev, "ASoC: CODEC %s not registered\n",
  727. dai_link->codec_name);
  728. return -EPROBE_DEFER;
  729. }
  730. /* if there's no platform we match on the empty platform */
  731. platform_name = dai_link->platform_name;
  732. if (!platform_name && !dai_link->platform_of_node)
  733. platform_name = "snd-soc-dummy";
  734. /* find one from the set of registered platforms */
  735. list_for_each_entry(platform, &platform_list, list) {
  736. if (dai_link->platform_of_node) {
  737. if (platform->dev->of_node !=
  738. dai_link->platform_of_node)
  739. continue;
  740. } else {
  741. if (strcmp(platform->name, platform_name))
  742. continue;
  743. }
  744. rtd->platform = platform;
  745. }
  746. if (!rtd->platform) {
  747. dev_err(card->dev, "ASoC: platform %s not registered\n",
  748. dai_link->platform_name);
  749. return -EPROBE_DEFER;
  750. }
  751. card->num_rtd++;
  752. return 0;
  753. }
  754. static int soc_remove_platform(struct snd_soc_platform *platform)
  755. {
  756. int ret;
  757. if (platform->driver->remove) {
  758. ret = platform->driver->remove(platform);
  759. if (ret < 0)
  760. dev_err(platform->dev, "ASoC: failed to remove %d\n",
  761. ret);
  762. }
  763. /* Make sure all DAPM widgets are freed */
  764. snd_soc_dapm_free(&platform->dapm);
  765. soc_cleanup_platform_debugfs(platform);
  766. platform->probed = 0;
  767. list_del(&platform->card_list);
  768. module_put(platform->dev->driver->owner);
  769. return 0;
  770. }
  771. static void soc_remove_codec(struct snd_soc_codec *codec)
  772. {
  773. int err;
  774. if (codec->driver->remove) {
  775. err = codec->driver->remove(codec);
  776. if (err < 0)
  777. dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
  778. }
  779. /* Make sure all DAPM widgets are freed */
  780. snd_soc_dapm_free(&codec->dapm);
  781. soc_cleanup_codec_debugfs(codec);
  782. codec->probed = 0;
  783. list_del(&codec->card_list);
  784. module_put(codec->dev->driver->owner);
  785. }
  786. static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
  787. {
  788. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  789. struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
  790. int err;
  791. /* unregister the rtd device */
  792. if (rtd->dev_registered) {
  793. device_remove_file(rtd->dev, &dev_attr_pmdown_time);
  794. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  795. device_unregister(rtd->dev);
  796. rtd->dev_registered = 0;
  797. }
  798. /* remove the CODEC DAI */
  799. if (codec_dai && codec_dai->probed &&
  800. codec_dai->driver->remove_order == order) {
  801. if (codec_dai->driver->remove) {
  802. err = codec_dai->driver->remove(codec_dai);
  803. if (err < 0)
  804. dev_err(codec_dai->dev,
  805. "ASoC: failed to remove %s: %d\n",
  806. codec_dai->name, err);
  807. }
  808. codec_dai->probed = 0;
  809. list_del(&codec_dai->card_list);
  810. }
  811. /* remove the cpu_dai */
  812. if (cpu_dai && cpu_dai->probed &&
  813. cpu_dai->driver->remove_order == order) {
  814. if (cpu_dai->driver->remove) {
  815. err = cpu_dai->driver->remove(cpu_dai);
  816. if (err < 0)
  817. dev_err(cpu_dai->dev,
  818. "ASoC: failed to remove %s: %d\n",
  819. cpu_dai->name, err);
  820. }
  821. cpu_dai->probed = 0;
  822. list_del(&cpu_dai->card_list);
  823. if (!cpu_dai->codec) {
  824. snd_soc_dapm_free(&cpu_dai->dapm);
  825. module_put(cpu_dai->dev->driver->owner);
  826. }
  827. }
  828. }
  829. static void soc_remove_link_components(struct snd_soc_card *card, int num,
  830. int order)
  831. {
  832. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  833. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  834. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  835. struct snd_soc_platform *platform = rtd->platform;
  836. struct snd_soc_codec *codec;
  837. /* remove the platform */
  838. if (platform && platform->probed &&
  839. platform->driver->remove_order == order) {
  840. soc_remove_platform(platform);
  841. }
  842. /* remove the CODEC-side CODEC */
  843. if (codec_dai) {
  844. codec = codec_dai->codec;
  845. if (codec && codec->probed &&
  846. codec->driver->remove_order == order)
  847. soc_remove_codec(codec);
  848. }
  849. /* remove any CPU-side CODEC */
  850. if (cpu_dai) {
  851. codec = cpu_dai->codec;
  852. if (codec && codec->probed &&
  853. codec->driver->remove_order == order)
  854. soc_remove_codec(codec);
  855. }
  856. }
  857. static void soc_remove_dai_links(struct snd_soc_card *card)
  858. {
  859. int dai, order;
  860. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  861. order++) {
  862. for (dai = 0; dai < card->num_rtd; dai++)
  863. soc_remove_link_dais(card, dai, order);
  864. }
  865. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  866. order++) {
  867. for (dai = 0; dai < card->num_rtd; dai++)
  868. soc_remove_link_components(card, dai, order);
  869. }
  870. card->num_rtd = 0;
  871. }
  872. static void soc_set_name_prefix(struct snd_soc_card *card,
  873. struct snd_soc_codec *codec)
  874. {
  875. int i;
  876. if (card->codec_conf == NULL)
  877. return;
  878. for (i = 0; i < card->num_configs; i++) {
  879. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  880. if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
  881. codec->name_prefix = map->name_prefix;
  882. break;
  883. }
  884. }
  885. }
  886. static int soc_probe_codec(struct snd_soc_card *card,
  887. struct snd_soc_codec *codec)
  888. {
  889. int ret = 0;
  890. const struct snd_soc_codec_driver *driver = codec->driver;
  891. struct snd_soc_dai *dai;
  892. codec->card = card;
  893. codec->dapm.card = card;
  894. soc_set_name_prefix(card, codec);
  895. if (!try_module_get(codec->dev->driver->owner))
  896. return -ENODEV;
  897. soc_init_codec_debugfs(codec);
  898. if (driver->dapm_widgets)
  899. snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
  900. driver->num_dapm_widgets);
  901. /* Create DAPM widgets for each DAI stream */
  902. list_for_each_entry(dai, &dai_list, list) {
  903. if (dai->dev != codec->dev)
  904. continue;
  905. snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
  906. }
  907. codec->dapm.idle_bias_off = driver->idle_bias_off;
  908. if (driver->probe) {
  909. ret = driver->probe(codec);
  910. if (ret < 0) {
  911. dev_err(codec->dev,
  912. "ASoC: failed to probe CODEC %d\n", ret);
  913. goto err_probe;
  914. }
  915. WARN(codec->dapm.idle_bias_off &&
  916. codec->dapm.bias_level != SND_SOC_BIAS_OFF,
  917. "codec %s can not start from non-off bias"
  918. " with idle_bias_off==1\n", codec->name);
  919. }
  920. /* If the driver didn't set I/O up try regmap */
  921. if (!codec->write && dev_get_regmap(codec->dev, NULL))
  922. snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  923. if (driver->controls)
  924. snd_soc_add_codec_controls(codec, driver->controls,
  925. driver->num_controls);
  926. if (driver->dapm_routes)
  927. snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
  928. driver->num_dapm_routes);
  929. /* mark codec as probed and add to card codec list */
  930. codec->probed = 1;
  931. list_add(&codec->card_list, &card->codec_dev_list);
  932. list_add(&codec->dapm.list, &card->dapm_list);
  933. return 0;
  934. err_probe:
  935. soc_cleanup_codec_debugfs(codec);
  936. module_put(codec->dev->driver->owner);
  937. return ret;
  938. }
  939. static int soc_probe_platform(struct snd_soc_card *card,
  940. struct snd_soc_platform *platform)
  941. {
  942. int ret = 0;
  943. const struct snd_soc_platform_driver *driver = platform->driver;
  944. struct snd_soc_dai *dai;
  945. platform->card = card;
  946. platform->dapm.card = card;
  947. if (!try_module_get(platform->dev->driver->owner))
  948. return -ENODEV;
  949. soc_init_platform_debugfs(platform);
  950. if (driver->dapm_widgets)
  951. snd_soc_dapm_new_controls(&platform->dapm,
  952. driver->dapm_widgets, driver->num_dapm_widgets);
  953. /* Create DAPM widgets for each DAI stream */
  954. list_for_each_entry(dai, &dai_list, list) {
  955. if (dai->dev != platform->dev)
  956. continue;
  957. snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
  958. }
  959. platform->dapm.idle_bias_off = 1;
  960. if (driver->probe) {
  961. ret = driver->probe(platform);
  962. if (ret < 0) {
  963. dev_err(platform->dev,
  964. "ASoC: failed to probe platform %d\n", ret);
  965. goto err_probe;
  966. }
  967. }
  968. if (driver->controls)
  969. snd_soc_add_platform_controls(platform, driver->controls,
  970. driver->num_controls);
  971. if (driver->dapm_routes)
  972. snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
  973. driver->num_dapm_routes);
  974. /* mark platform as probed and add to card platform list */
  975. platform->probed = 1;
  976. list_add(&platform->card_list, &card->platform_dev_list);
  977. list_add(&platform->dapm.list, &card->dapm_list);
  978. return 0;
  979. err_probe:
  980. soc_cleanup_platform_debugfs(platform);
  981. module_put(platform->dev->driver->owner);
  982. return ret;
  983. }
  984. static void rtd_release(struct device *dev)
  985. {
  986. kfree(dev);
  987. }
  988. static int soc_post_component_init(struct snd_soc_card *card,
  989. struct snd_soc_codec *codec,
  990. int num, int dailess)
  991. {
  992. struct snd_soc_dai_link *dai_link = NULL;
  993. struct snd_soc_aux_dev *aux_dev = NULL;
  994. struct snd_soc_pcm_runtime *rtd;
  995. const char *temp, *name;
  996. int ret = 0;
  997. if (!dailess) {
  998. dai_link = &card->dai_link[num];
  999. rtd = &card->rtd[num];
  1000. name = dai_link->name;
  1001. } else {
  1002. aux_dev = &card->aux_dev[num];
  1003. rtd = &card->rtd_aux[num];
  1004. name = aux_dev->name;
  1005. }
  1006. rtd->card = card;
  1007. /* Make sure all DAPM widgets are instantiated */
  1008. snd_soc_dapm_new_widgets(&codec->dapm);
  1009. /* machine controls, routes and widgets are not prefixed */
  1010. temp = codec->name_prefix;
  1011. codec->name_prefix = NULL;
  1012. /* do machine specific initialization */
  1013. if (!dailess && dai_link->init)
  1014. ret = dai_link->init(rtd);
  1015. else if (dailess && aux_dev->init)
  1016. ret = aux_dev->init(&codec->dapm);
  1017. if (ret < 0) {
  1018. dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
  1019. return ret;
  1020. }
  1021. codec->name_prefix = temp;
  1022. /* register the rtd device */
  1023. rtd->codec = codec;
  1024. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1025. if (!rtd->dev)
  1026. return -ENOMEM;
  1027. device_initialize(rtd->dev);
  1028. rtd->dev->parent = card->dev;
  1029. rtd->dev->release = rtd_release;
  1030. rtd->dev->init_name = name;
  1031. dev_set_drvdata(rtd->dev, rtd);
  1032. mutex_init(&rtd->pcm_mutex);
  1033. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1034. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1035. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1036. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1037. ret = device_add(rtd->dev);
  1038. if (ret < 0) {
  1039. /* calling put_device() here to free the rtd->dev */
  1040. put_device(rtd->dev);
  1041. dev_err(card->dev,
  1042. "ASoC: failed to register runtime device: %d\n", ret);
  1043. return ret;
  1044. }
  1045. rtd->dev_registered = 1;
  1046. /* add DAPM sysfs entries for this codec */
  1047. ret = snd_soc_dapm_sys_add(rtd->dev);
  1048. if (ret < 0)
  1049. dev_err(codec->dev,
  1050. "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
  1051. /* add codec sysfs entries */
  1052. ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
  1053. if (ret < 0)
  1054. dev_err(codec->dev,
  1055. "ASoC: failed to add codec sysfs files: %d\n", ret);
  1056. #ifdef CONFIG_DEBUG_FS
  1057. /* add DPCM sysfs entries */
  1058. if (!dailess && !dai_link->dynamic)
  1059. goto out;
  1060. ret = soc_dpcm_debugfs_add(rtd);
  1061. if (ret < 0)
  1062. dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
  1063. out:
  1064. #endif
  1065. return 0;
  1066. }
  1067. static int soc_probe_link_components(struct snd_soc_card *card, int num,
  1068. int order)
  1069. {
  1070. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1071. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1072. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1073. struct snd_soc_platform *platform = rtd->platform;
  1074. int ret;
  1075. /* probe the CPU-side component, if it is a CODEC */
  1076. if (cpu_dai->codec &&
  1077. !cpu_dai->codec->probed &&
  1078. cpu_dai->codec->driver->probe_order == order) {
  1079. ret = soc_probe_codec(card, cpu_dai->codec);
  1080. if (ret < 0)
  1081. return ret;
  1082. }
  1083. /* probe the CODEC-side component */
  1084. if (!codec_dai->codec->probed &&
  1085. codec_dai->codec->driver->probe_order == order) {
  1086. ret = soc_probe_codec(card, codec_dai->codec);
  1087. if (ret < 0)
  1088. return ret;
  1089. }
  1090. /* probe the platform */
  1091. if (!platform->probed &&
  1092. platform->driver->probe_order == order) {
  1093. ret = soc_probe_platform(card, platform);
  1094. if (ret < 0)
  1095. return ret;
  1096. }
  1097. return 0;
  1098. }
  1099. static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
  1100. {
  1101. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  1102. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1103. struct snd_soc_codec *codec = rtd->codec;
  1104. struct snd_soc_platform *platform = rtd->platform;
  1105. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1106. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1107. struct snd_soc_dapm_widget *play_w, *capture_w;
  1108. int ret;
  1109. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1110. card->name, num, order);
  1111. /* config components */
  1112. cpu_dai->platform = platform;
  1113. codec_dai->card = card;
  1114. cpu_dai->card = card;
  1115. /* set default power off timeout */
  1116. rtd->pmdown_time = pmdown_time;
  1117. /* probe the cpu_dai */
  1118. if (!cpu_dai->probed &&
  1119. cpu_dai->driver->probe_order == order) {
  1120. if (!cpu_dai->codec) {
  1121. cpu_dai->dapm.card = card;
  1122. if (!try_module_get(cpu_dai->dev->driver->owner))
  1123. return -ENODEV;
  1124. list_add(&cpu_dai->dapm.list, &card->dapm_list);
  1125. snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
  1126. }
  1127. if (cpu_dai->driver->probe) {
  1128. ret = cpu_dai->driver->probe(cpu_dai);
  1129. if (ret < 0) {
  1130. dev_err(cpu_dai->dev,
  1131. "ASoC: failed to probe CPU DAI %s: %d\n",
  1132. cpu_dai->name, ret);
  1133. module_put(cpu_dai->dev->driver->owner);
  1134. return ret;
  1135. }
  1136. }
  1137. cpu_dai->probed = 1;
  1138. /* mark cpu_dai as probed and add to card dai list */
  1139. list_add(&cpu_dai->card_list, &card->dai_dev_list);
  1140. }
  1141. /* probe the CODEC DAI */
  1142. if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
  1143. if (codec_dai->driver->probe) {
  1144. ret = codec_dai->driver->probe(codec_dai);
  1145. if (ret < 0) {
  1146. dev_err(codec_dai->dev,
  1147. "ASoC: failed to probe CODEC DAI %s: %d\n",
  1148. codec_dai->name, ret);
  1149. return ret;
  1150. }
  1151. }
  1152. /* mark codec_dai as probed and add to card dai list */
  1153. codec_dai->probed = 1;
  1154. list_add(&codec_dai->card_list, &card->dai_dev_list);
  1155. }
  1156. /* complete DAI probe during last probe */
  1157. if (order != SND_SOC_COMP_ORDER_LAST)
  1158. return 0;
  1159. ret = soc_post_component_init(card, codec, num, 0);
  1160. if (ret)
  1161. return ret;
  1162. ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
  1163. if (ret < 0)
  1164. dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
  1165. ret);
  1166. if (cpu_dai->driver->compress_dai) {
  1167. /*create compress_device"*/
  1168. ret = soc_new_compress(rtd, num);
  1169. if (ret < 0) {
  1170. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1171. dai_link->stream_name);
  1172. return ret;
  1173. }
  1174. } else {
  1175. if (!dai_link->params) {
  1176. /* create the pcm */
  1177. ret = soc_new_pcm(rtd, num);
  1178. if (ret < 0) {
  1179. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1180. dai_link->stream_name, ret);
  1181. return ret;
  1182. }
  1183. } else {
  1184. /* link the DAI widgets */
  1185. play_w = codec_dai->playback_widget;
  1186. capture_w = cpu_dai->capture_widget;
  1187. if (play_w && capture_w) {
  1188. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1189. capture_w, play_w);
  1190. if (ret != 0) {
  1191. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1192. play_w->name, capture_w->name, ret);
  1193. return ret;
  1194. }
  1195. }
  1196. play_w = cpu_dai->playback_widget;
  1197. capture_w = codec_dai->capture_widget;
  1198. if (play_w && capture_w) {
  1199. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1200. capture_w, play_w);
  1201. if (ret != 0) {
  1202. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1203. play_w->name, capture_w->name, ret);
  1204. return ret;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. /* add platform data for AC97 devices */
  1210. if (rtd->codec_dai->driver->ac97_control)
  1211. snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
  1212. return 0;
  1213. }
  1214. #ifdef CONFIG_SND_SOC_AC97_BUS
  1215. static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
  1216. {
  1217. int ret;
  1218. /* Only instantiate AC97 if not already done by the adaptor
  1219. * for the generic AC97 subsystem.
  1220. */
  1221. if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
  1222. /*
  1223. * It is possible that the AC97 device is already registered to
  1224. * the device subsystem. This happens when the device is created
  1225. * via snd_ac97_mixer(). Currently only SoC codec that does so
  1226. * is the generic AC97 glue but others migh emerge.
  1227. *
  1228. * In those cases we don't try to register the device again.
  1229. */
  1230. if (!rtd->codec->ac97_created)
  1231. return 0;
  1232. ret = soc_ac97_dev_register(rtd->codec);
  1233. if (ret < 0) {
  1234. dev_err(rtd->codec->dev,
  1235. "ASoC: AC97 device register failed: %d\n", ret);
  1236. return ret;
  1237. }
  1238. rtd->codec->ac97_registered = 1;
  1239. }
  1240. return 0;
  1241. }
  1242. static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
  1243. {
  1244. if (codec->ac97_registered) {
  1245. soc_ac97_dev_unregister(codec);
  1246. codec->ac97_registered = 0;
  1247. }
  1248. }
  1249. #endif
  1250. static int soc_check_aux_dev(struct snd_soc_card *card, int num)
  1251. {
  1252. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1253. struct snd_soc_codec *codec;
  1254. /* find CODEC from registered CODECs*/
  1255. list_for_each_entry(codec, &codec_list, list) {
  1256. if (!strcmp(codec->name, aux_dev->codec_name))
  1257. return 0;
  1258. }
  1259. dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
  1260. return -EPROBE_DEFER;
  1261. }
  1262. static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
  1263. {
  1264. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1265. struct snd_soc_codec *codec;
  1266. int ret = -ENODEV;
  1267. /* find CODEC from registered CODECs*/
  1268. list_for_each_entry(codec, &codec_list, list) {
  1269. if (!strcmp(codec->name, aux_dev->codec_name)) {
  1270. if (codec->probed) {
  1271. dev_err(codec->dev,
  1272. "ASoC: codec already probed");
  1273. ret = -EBUSY;
  1274. goto out;
  1275. }
  1276. goto found;
  1277. }
  1278. }
  1279. /* codec not found */
  1280. dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
  1281. return -EPROBE_DEFER;
  1282. found:
  1283. ret = soc_probe_codec(card, codec);
  1284. if (ret < 0)
  1285. return ret;
  1286. ret = soc_post_component_init(card, codec, num, 1);
  1287. out:
  1288. return ret;
  1289. }
  1290. static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
  1291. {
  1292. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1293. struct snd_soc_codec *codec = rtd->codec;
  1294. /* unregister the rtd device */
  1295. if (rtd->dev_registered) {
  1296. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  1297. device_unregister(rtd->dev);
  1298. rtd->dev_registered = 0;
  1299. }
  1300. if (codec && codec->probed)
  1301. soc_remove_codec(codec);
  1302. }
  1303. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
  1304. enum snd_soc_compress_type compress_type)
  1305. {
  1306. int ret;
  1307. if (codec->cache_init)
  1308. return 0;
  1309. /* override the compress_type if necessary */
  1310. if (compress_type && codec->compress_type != compress_type)
  1311. codec->compress_type = compress_type;
  1312. ret = snd_soc_cache_init(codec);
  1313. if (ret < 0) {
  1314. dev_err(codec->dev, "ASoC: Failed to set cache compression"
  1315. " type: %d\n", ret);
  1316. return ret;
  1317. }
  1318. codec->cache_init = 1;
  1319. return 0;
  1320. }
  1321. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1322. {
  1323. struct snd_soc_codec *codec;
  1324. struct snd_soc_codec_conf *codec_conf;
  1325. enum snd_soc_compress_type compress_type;
  1326. struct snd_soc_dai_link *dai_link;
  1327. int ret, i, order, dai_fmt;
  1328. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1329. /* bind DAIs */
  1330. for (i = 0; i < card->num_links; i++) {
  1331. ret = soc_bind_dai_link(card, i);
  1332. if (ret != 0)
  1333. goto base_error;
  1334. }
  1335. /* check aux_devs too */
  1336. for (i = 0; i < card->num_aux_devs; i++) {
  1337. ret = soc_check_aux_dev(card, i);
  1338. if (ret != 0)
  1339. goto base_error;
  1340. }
  1341. /* initialize the register cache for each available codec */
  1342. list_for_each_entry(codec, &codec_list, list) {
  1343. if (codec->cache_init)
  1344. continue;
  1345. /* by default we don't override the compress_type */
  1346. compress_type = 0;
  1347. /* check to see if we need to override the compress_type */
  1348. for (i = 0; i < card->num_configs; ++i) {
  1349. codec_conf = &card->codec_conf[i];
  1350. if (!strcmp(codec->name, codec_conf->dev_name)) {
  1351. compress_type = codec_conf->compress_type;
  1352. if (compress_type && compress_type
  1353. != codec->compress_type)
  1354. break;
  1355. }
  1356. }
  1357. ret = snd_soc_init_codec_cache(codec, compress_type);
  1358. if (ret < 0)
  1359. goto base_error;
  1360. }
  1361. /* card bind complete so register a sound card */
  1362. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1363. card->owner, 0, &card->snd_card);
  1364. if (ret < 0) {
  1365. dev_err(card->dev, "ASoC: can't create sound card for"
  1366. " card %s: %d\n", card->name, ret);
  1367. goto base_error;
  1368. }
  1369. card->snd_card->dev = card->dev;
  1370. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1371. card->dapm.dev = card->dev;
  1372. card->dapm.card = card;
  1373. list_add(&card->dapm.list, &card->dapm_list);
  1374. #ifdef CONFIG_DEBUG_FS
  1375. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1376. #endif
  1377. #ifdef CONFIG_PM_SLEEP
  1378. /* deferred resume work */
  1379. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1380. #endif
  1381. if (card->dapm_widgets)
  1382. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1383. card->num_dapm_widgets);
  1384. /* initialise the sound card only once */
  1385. if (card->probe) {
  1386. ret = card->probe(card);
  1387. if (ret < 0)
  1388. goto card_probe_error;
  1389. }
  1390. /* probe all components used by DAI links on this card */
  1391. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1392. order++) {
  1393. for (i = 0; i < card->num_links; i++) {
  1394. ret = soc_probe_link_components(card, i, order);
  1395. if (ret < 0) {
  1396. dev_err(card->dev,
  1397. "ASoC: failed to instantiate card %d\n",
  1398. ret);
  1399. goto probe_dai_err;
  1400. }
  1401. }
  1402. }
  1403. /* probe all DAI links on this card */
  1404. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1405. order++) {
  1406. for (i = 0; i < card->num_links; i++) {
  1407. ret = soc_probe_link_dais(card, i, order);
  1408. if (ret < 0) {
  1409. dev_err(card->dev,
  1410. "ASoC: failed to instantiate card %d\n",
  1411. ret);
  1412. goto probe_dai_err;
  1413. }
  1414. }
  1415. }
  1416. for (i = 0; i < card->num_aux_devs; i++) {
  1417. ret = soc_probe_aux_dev(card, i);
  1418. if (ret < 0) {
  1419. dev_err(card->dev,
  1420. "ASoC: failed to add auxiliary devices %d\n",
  1421. ret);
  1422. goto probe_aux_dev_err;
  1423. }
  1424. }
  1425. snd_soc_dapm_link_dai_widgets(card);
  1426. if (card->controls)
  1427. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1428. if (card->dapm_routes)
  1429. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1430. card->num_dapm_routes);
  1431. snd_soc_dapm_new_widgets(&card->dapm);
  1432. for (i = 0; i < card->num_links; i++) {
  1433. dai_link = &card->dai_link[i];
  1434. dai_fmt = dai_link->dai_fmt;
  1435. if (dai_fmt) {
  1436. ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
  1437. dai_fmt);
  1438. if (ret != 0 && ret != -ENOTSUPP)
  1439. dev_warn(card->rtd[i].codec_dai->dev,
  1440. "ASoC: Failed to set DAI format: %d\n",
  1441. ret);
  1442. }
  1443. /* If this is a regular CPU link there will be a platform */
  1444. if (dai_fmt &&
  1445. (dai_link->platform_name || dai_link->platform_of_node)) {
  1446. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1447. dai_fmt);
  1448. if (ret != 0 && ret != -ENOTSUPP)
  1449. dev_warn(card->rtd[i].cpu_dai->dev,
  1450. "ASoC: Failed to set DAI format: %d\n",
  1451. ret);
  1452. } else if (dai_fmt) {
  1453. /* Flip the polarity for the "CPU" end */
  1454. dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  1455. switch (dai_link->dai_fmt &
  1456. SND_SOC_DAIFMT_MASTER_MASK) {
  1457. case SND_SOC_DAIFMT_CBM_CFM:
  1458. dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1459. break;
  1460. case SND_SOC_DAIFMT_CBM_CFS:
  1461. dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1462. break;
  1463. case SND_SOC_DAIFMT_CBS_CFM:
  1464. dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1465. break;
  1466. case SND_SOC_DAIFMT_CBS_CFS:
  1467. dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1468. break;
  1469. }
  1470. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1471. dai_fmt);
  1472. if (ret != 0 && ret != -ENOTSUPP)
  1473. dev_warn(card->rtd[i].cpu_dai->dev,
  1474. "ASoC: Failed to set DAI format: %d\n",
  1475. ret);
  1476. }
  1477. }
  1478. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1479. "%s", card->name);
  1480. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1481. "%s", card->long_name ? card->long_name : card->name);
  1482. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1483. "%s", card->driver_name ? card->driver_name : card->name);
  1484. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1485. switch (card->snd_card->driver[i]) {
  1486. case '_':
  1487. case '-':
  1488. case '\0':
  1489. break;
  1490. default:
  1491. if (!isalnum(card->snd_card->driver[i]))
  1492. card->snd_card->driver[i] = '_';
  1493. break;
  1494. }
  1495. }
  1496. if (card->late_probe) {
  1497. ret = card->late_probe(card);
  1498. if (ret < 0) {
  1499. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1500. card->name, ret);
  1501. goto probe_aux_dev_err;
  1502. }
  1503. }
  1504. snd_soc_dapm_new_widgets(&card->dapm);
  1505. if (card->fully_routed)
  1506. list_for_each_entry(codec, &card->codec_dev_list, card_list)
  1507. snd_soc_dapm_auto_nc_codec_pins(codec);
  1508. ret = snd_card_register(card->snd_card);
  1509. if (ret < 0) {
  1510. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1511. ret);
  1512. goto probe_aux_dev_err;
  1513. }
  1514. #ifdef CONFIG_SND_SOC_AC97_BUS
  1515. /* register any AC97 codecs */
  1516. for (i = 0; i < card->num_rtd; i++) {
  1517. ret = soc_register_ac97_dai_link(&card->rtd[i]);
  1518. if (ret < 0) {
  1519. dev_err(card->dev, "ASoC: failed to register AC97:"
  1520. " %d\n", ret);
  1521. while (--i >= 0)
  1522. soc_unregister_ac97_dai_link(card->rtd[i].codec);
  1523. goto probe_aux_dev_err;
  1524. }
  1525. }
  1526. #endif
  1527. card->instantiated = 1;
  1528. snd_soc_dapm_sync(&card->dapm);
  1529. mutex_unlock(&card->mutex);
  1530. return 0;
  1531. probe_aux_dev_err:
  1532. for (i = 0; i < card->num_aux_devs; i++)
  1533. soc_remove_aux_dev(card, i);
  1534. probe_dai_err:
  1535. soc_remove_dai_links(card);
  1536. card_probe_error:
  1537. if (card->remove)
  1538. card->remove(card);
  1539. snd_card_free(card->snd_card);
  1540. base_error:
  1541. mutex_unlock(&card->mutex);
  1542. return ret;
  1543. }
  1544. /* probes a new socdev */
  1545. static int soc_probe(struct platform_device *pdev)
  1546. {
  1547. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1548. /*
  1549. * no card, so machine driver should be registering card
  1550. * we should not be here in that case so ret error
  1551. */
  1552. if (!card)
  1553. return -EINVAL;
  1554. dev_warn(&pdev->dev,
  1555. "ASoC: machine %s should use snd_soc_register_card()\n",
  1556. card->name);
  1557. /* Bodge while we unpick instantiation */
  1558. card->dev = &pdev->dev;
  1559. return snd_soc_register_card(card);
  1560. }
  1561. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1562. {
  1563. int i;
  1564. /* make sure any delayed work runs */
  1565. for (i = 0; i < card->num_rtd; i++) {
  1566. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1567. flush_delayed_work(&rtd->delayed_work);
  1568. }
  1569. /* remove auxiliary devices */
  1570. for (i = 0; i < card->num_aux_devs; i++)
  1571. soc_remove_aux_dev(card, i);
  1572. /* remove and free each DAI */
  1573. soc_remove_dai_links(card);
  1574. soc_cleanup_card_debugfs(card);
  1575. /* remove the card */
  1576. if (card->remove)
  1577. card->remove(card);
  1578. snd_soc_dapm_free(&card->dapm);
  1579. snd_card_free(card->snd_card);
  1580. return 0;
  1581. }
  1582. /* removes a socdev */
  1583. static int soc_remove(struct platform_device *pdev)
  1584. {
  1585. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1586. snd_soc_unregister_card(card);
  1587. return 0;
  1588. }
  1589. int snd_soc_poweroff(struct device *dev)
  1590. {
  1591. struct snd_soc_card *card = dev_get_drvdata(dev);
  1592. int i;
  1593. if (!card->instantiated)
  1594. return 0;
  1595. /* Flush out pmdown_time work - we actually do want to run it
  1596. * now, we're shutting down so no imminent restart. */
  1597. for (i = 0; i < card->num_rtd; i++) {
  1598. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1599. flush_delayed_work(&rtd->delayed_work);
  1600. }
  1601. snd_soc_dapm_shutdown(card);
  1602. return 0;
  1603. }
  1604. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1605. const struct dev_pm_ops snd_soc_pm_ops = {
  1606. .suspend = snd_soc_suspend,
  1607. .resume = snd_soc_resume,
  1608. .freeze = snd_soc_suspend,
  1609. .thaw = snd_soc_resume,
  1610. .poweroff = snd_soc_poweroff,
  1611. .restore = snd_soc_resume,
  1612. };
  1613. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1614. /* ASoC platform driver */
  1615. static struct platform_driver soc_driver = {
  1616. .driver = {
  1617. .name = "soc-audio",
  1618. .owner = THIS_MODULE,
  1619. .pm = &snd_soc_pm_ops,
  1620. },
  1621. .probe = soc_probe,
  1622. .remove = soc_remove,
  1623. };
  1624. /**
  1625. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1626. *
  1627. * @codec: CODEC to query.
  1628. * @reg: Register to query.
  1629. *
  1630. * Boolean function indiciating if a CODEC register is volatile.
  1631. */
  1632. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1633. unsigned int reg)
  1634. {
  1635. if (codec->volatile_register)
  1636. return codec->volatile_register(codec, reg);
  1637. else
  1638. return 0;
  1639. }
  1640. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1641. /**
  1642. * snd_soc_codec_readable_register: Report if a register is readable.
  1643. *
  1644. * @codec: CODEC to query.
  1645. * @reg: Register to query.
  1646. *
  1647. * Boolean function indicating if a CODEC register is readable.
  1648. */
  1649. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1650. unsigned int reg)
  1651. {
  1652. if (codec->readable_register)
  1653. return codec->readable_register(codec, reg);
  1654. else
  1655. return 1;
  1656. }
  1657. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1658. /**
  1659. * snd_soc_codec_writable_register: Report if a register is writable.
  1660. *
  1661. * @codec: CODEC to query.
  1662. * @reg: Register to query.
  1663. *
  1664. * Boolean function indicating if a CODEC register is writable.
  1665. */
  1666. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1667. unsigned int reg)
  1668. {
  1669. if (codec->writable_register)
  1670. return codec->writable_register(codec, reg);
  1671. else
  1672. return 1;
  1673. }
  1674. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1675. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1676. unsigned int reg)
  1677. {
  1678. unsigned int ret;
  1679. if (!platform->driver->read) {
  1680. dev_err(platform->dev, "ASoC: platform has no read back\n");
  1681. return -1;
  1682. }
  1683. ret = platform->driver->read(platform, reg);
  1684. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1685. trace_snd_soc_preg_read(platform, reg, ret);
  1686. return ret;
  1687. }
  1688. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1689. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1690. unsigned int reg, unsigned int val)
  1691. {
  1692. if (!platform->driver->write) {
  1693. dev_err(platform->dev, "ASoC: platform has no write back\n");
  1694. return -1;
  1695. }
  1696. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1697. trace_snd_soc_preg_write(platform, reg, val);
  1698. return platform->driver->write(platform, reg, val);
  1699. }
  1700. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1701. /**
  1702. * snd_soc_new_ac97_codec - initailise AC97 device
  1703. * @codec: audio codec
  1704. * @ops: AC97 bus operations
  1705. * @num: AC97 codec number
  1706. *
  1707. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1708. */
  1709. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1710. struct snd_ac97_bus_ops *ops, int num)
  1711. {
  1712. mutex_lock(&codec->mutex);
  1713. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1714. if (codec->ac97 == NULL) {
  1715. mutex_unlock(&codec->mutex);
  1716. return -ENOMEM;
  1717. }
  1718. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1719. if (codec->ac97->bus == NULL) {
  1720. kfree(codec->ac97);
  1721. codec->ac97 = NULL;
  1722. mutex_unlock(&codec->mutex);
  1723. return -ENOMEM;
  1724. }
  1725. codec->ac97->bus->ops = ops;
  1726. codec->ac97->num = num;
  1727. /*
  1728. * Mark the AC97 device to be created by us. This way we ensure that the
  1729. * device will be registered with the device subsystem later on.
  1730. */
  1731. codec->ac97_created = 1;
  1732. mutex_unlock(&codec->mutex);
  1733. return 0;
  1734. }
  1735. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1736. /**
  1737. * snd_soc_free_ac97_codec - free AC97 codec device
  1738. * @codec: audio codec
  1739. *
  1740. * Frees AC97 codec device resources.
  1741. */
  1742. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1743. {
  1744. mutex_lock(&codec->mutex);
  1745. #ifdef CONFIG_SND_SOC_AC97_BUS
  1746. soc_unregister_ac97_dai_link(codec);
  1747. #endif
  1748. kfree(codec->ac97->bus);
  1749. kfree(codec->ac97);
  1750. codec->ac97 = NULL;
  1751. codec->ac97_created = 0;
  1752. mutex_unlock(&codec->mutex);
  1753. }
  1754. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1755. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1756. {
  1757. unsigned int ret;
  1758. ret = codec->read(codec, reg);
  1759. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1760. trace_snd_soc_reg_read(codec, reg, ret);
  1761. return ret;
  1762. }
  1763. EXPORT_SYMBOL_GPL(snd_soc_read);
  1764. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1765. unsigned int reg, unsigned int val)
  1766. {
  1767. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1768. trace_snd_soc_reg_write(codec, reg, val);
  1769. return codec->write(codec, reg, val);
  1770. }
  1771. EXPORT_SYMBOL_GPL(snd_soc_write);
  1772. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1773. unsigned int reg, const void *data, size_t len)
  1774. {
  1775. return codec->bulk_write_raw(codec, reg, data, len);
  1776. }
  1777. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1778. /**
  1779. * snd_soc_update_bits - update codec register bits
  1780. * @codec: audio codec
  1781. * @reg: codec register
  1782. * @mask: register mask
  1783. * @value: new value
  1784. *
  1785. * Writes new register value.
  1786. *
  1787. * Returns 1 for change, 0 for no change, or negative error code.
  1788. */
  1789. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1790. unsigned int mask, unsigned int value)
  1791. {
  1792. bool change;
  1793. unsigned int old, new;
  1794. int ret;
  1795. if (codec->using_regmap) {
  1796. ret = regmap_update_bits_check(codec->control_data, reg,
  1797. mask, value, &change);
  1798. } else {
  1799. ret = snd_soc_read(codec, reg);
  1800. if (ret < 0)
  1801. return ret;
  1802. old = ret;
  1803. new = (old & ~mask) | (value & mask);
  1804. change = old != new;
  1805. if (change)
  1806. ret = snd_soc_write(codec, reg, new);
  1807. }
  1808. if (ret < 0)
  1809. return ret;
  1810. return change;
  1811. }
  1812. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1813. /**
  1814. * snd_soc_update_bits_locked - update codec register bits
  1815. * @codec: audio codec
  1816. * @reg: codec register
  1817. * @mask: register mask
  1818. * @value: new value
  1819. *
  1820. * Writes new register value, and takes the codec mutex.
  1821. *
  1822. * Returns 1 for change else 0.
  1823. */
  1824. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1825. unsigned short reg, unsigned int mask,
  1826. unsigned int value)
  1827. {
  1828. int change;
  1829. mutex_lock(&codec->mutex);
  1830. change = snd_soc_update_bits(codec, reg, mask, value);
  1831. mutex_unlock(&codec->mutex);
  1832. return change;
  1833. }
  1834. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1835. /**
  1836. * snd_soc_test_bits - test register for change
  1837. * @codec: audio codec
  1838. * @reg: codec register
  1839. * @mask: register mask
  1840. * @value: new value
  1841. *
  1842. * Tests a register with a new value and checks if the new value is
  1843. * different from the old value.
  1844. *
  1845. * Returns 1 for change else 0.
  1846. */
  1847. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1848. unsigned int mask, unsigned int value)
  1849. {
  1850. int change;
  1851. unsigned int old, new;
  1852. old = snd_soc_read(codec, reg);
  1853. new = (old & ~mask) | value;
  1854. change = old != new;
  1855. return change;
  1856. }
  1857. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1858. /**
  1859. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1860. * @substream: the pcm substream
  1861. * @hw: the hardware parameters
  1862. *
  1863. * Sets the substream runtime hardware parameters.
  1864. */
  1865. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1866. const struct snd_pcm_hardware *hw)
  1867. {
  1868. struct snd_pcm_runtime *runtime = substream->runtime;
  1869. runtime->hw.info = hw->info;
  1870. runtime->hw.formats = hw->formats;
  1871. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1872. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1873. runtime->hw.periods_min = hw->periods_min;
  1874. runtime->hw.periods_max = hw->periods_max;
  1875. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1876. runtime->hw.fifo_size = hw->fifo_size;
  1877. return 0;
  1878. }
  1879. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1880. /**
  1881. * snd_soc_cnew - create new control
  1882. * @_template: control template
  1883. * @data: control private data
  1884. * @long_name: control long name
  1885. * @prefix: control name prefix
  1886. *
  1887. * Create a new mixer control from a template control.
  1888. *
  1889. * Returns 0 for success, else error.
  1890. */
  1891. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1892. void *data, const char *long_name,
  1893. const char *prefix)
  1894. {
  1895. struct snd_kcontrol_new template;
  1896. struct snd_kcontrol *kcontrol;
  1897. char *name = NULL;
  1898. int name_len;
  1899. memcpy(&template, _template, sizeof(template));
  1900. template.index = 0;
  1901. if (!long_name)
  1902. long_name = template.name;
  1903. if (prefix) {
  1904. name_len = strlen(long_name) + strlen(prefix) + 2;
  1905. name = kmalloc(name_len, GFP_KERNEL);
  1906. if (!name)
  1907. return NULL;
  1908. snprintf(name, name_len, "%s %s", prefix, long_name);
  1909. template.name = name;
  1910. } else {
  1911. template.name = long_name;
  1912. }
  1913. kcontrol = snd_ctl_new1(&template, data);
  1914. kfree(name);
  1915. return kcontrol;
  1916. }
  1917. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1918. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1919. const struct snd_kcontrol_new *controls, int num_controls,
  1920. const char *prefix, void *data)
  1921. {
  1922. int err, i;
  1923. for (i = 0; i < num_controls; i++) {
  1924. const struct snd_kcontrol_new *control = &controls[i];
  1925. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1926. control->name, prefix));
  1927. if (err < 0) {
  1928. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1929. control->name, err);
  1930. return err;
  1931. }
  1932. }
  1933. return 0;
  1934. }
  1935. /**
  1936. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1937. * Convenience function to add a list of controls. Many codecs were
  1938. * duplicating this code.
  1939. *
  1940. * @codec: codec to add controls to
  1941. * @controls: array of controls to add
  1942. * @num_controls: number of elements in the array
  1943. *
  1944. * Return 0 for success, else error.
  1945. */
  1946. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1947. const struct snd_kcontrol_new *controls, int num_controls)
  1948. {
  1949. struct snd_card *card = codec->card->snd_card;
  1950. return snd_soc_add_controls(card, codec->dev, controls, num_controls,
  1951. codec->name_prefix, codec);
  1952. }
  1953. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1954. /**
  1955. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1956. * Convenience function to add a list of controls.
  1957. *
  1958. * @platform: platform to add controls to
  1959. * @controls: array of controls to add
  1960. * @num_controls: number of elements in the array
  1961. *
  1962. * Return 0 for success, else error.
  1963. */
  1964. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1965. const struct snd_kcontrol_new *controls, int num_controls)
  1966. {
  1967. struct snd_card *card = platform->card->snd_card;
  1968. return snd_soc_add_controls(card, platform->dev, controls, num_controls,
  1969. NULL, platform);
  1970. }
  1971. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1972. /**
  1973. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1974. * Convenience function to add a list of controls.
  1975. *
  1976. * @soc_card: SoC card to add controls to
  1977. * @controls: array of controls to add
  1978. * @num_controls: number of elements in the array
  1979. *
  1980. * Return 0 for success, else error.
  1981. */
  1982. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1983. const struct snd_kcontrol_new *controls, int num_controls)
  1984. {
  1985. struct snd_card *card = soc_card->snd_card;
  1986. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1987. NULL, soc_card);
  1988. }
  1989. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1990. /**
  1991. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1992. * Convienience function to add a list of controls.
  1993. *
  1994. * @dai: DAI to add controls to
  1995. * @controls: array of controls to add
  1996. * @num_controls: number of elements in the array
  1997. *
  1998. * Return 0 for success, else error.
  1999. */
  2000. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  2001. const struct snd_kcontrol_new *controls, int num_controls)
  2002. {
  2003. struct snd_card *card = dai->card->snd_card;
  2004. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  2005. NULL, dai);
  2006. }
  2007. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  2008. /**
  2009. * snd_soc_info_enum_double - enumerated double mixer info callback
  2010. * @kcontrol: mixer control
  2011. * @uinfo: control element information
  2012. *
  2013. * Callback to provide information about a double enumerated
  2014. * mixer control.
  2015. *
  2016. * Returns 0 for success.
  2017. */
  2018. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  2019. struct snd_ctl_elem_info *uinfo)
  2020. {
  2021. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2022. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2023. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  2024. uinfo->value.enumerated.items = e->max;
  2025. if (uinfo->value.enumerated.item > e->max - 1)
  2026. uinfo->value.enumerated.item = e->max - 1;
  2027. strcpy(uinfo->value.enumerated.name,
  2028. e->texts[uinfo->value.enumerated.item]);
  2029. return 0;
  2030. }
  2031. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  2032. /**
  2033. * snd_soc_get_enum_double - enumerated double mixer get callback
  2034. * @kcontrol: mixer control
  2035. * @ucontrol: control element information
  2036. *
  2037. * Callback to get the value of a double enumerated mixer.
  2038. *
  2039. * Returns 0 for success.
  2040. */
  2041. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  2042. struct snd_ctl_elem_value *ucontrol)
  2043. {
  2044. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2045. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2046. unsigned int val;
  2047. val = snd_soc_read(codec, e->reg);
  2048. ucontrol->value.enumerated.item[0]
  2049. = (val >> e->shift_l) & e->mask;
  2050. if (e->shift_l != e->shift_r)
  2051. ucontrol->value.enumerated.item[1] =
  2052. (val >> e->shift_r) & e->mask;
  2053. return 0;
  2054. }
  2055. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  2056. /**
  2057. * snd_soc_put_enum_double - enumerated double mixer put callback
  2058. * @kcontrol: mixer control
  2059. * @ucontrol: control element information
  2060. *
  2061. * Callback to set the value of a double enumerated mixer.
  2062. *
  2063. * Returns 0 for success.
  2064. */
  2065. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  2066. struct snd_ctl_elem_value *ucontrol)
  2067. {
  2068. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2069. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2070. unsigned int val;
  2071. unsigned int mask;
  2072. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2073. return -EINVAL;
  2074. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  2075. mask = e->mask << e->shift_l;
  2076. if (e->shift_l != e->shift_r) {
  2077. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2078. return -EINVAL;
  2079. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2080. mask |= e->mask << e->shift_r;
  2081. }
  2082. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2083. }
  2084. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  2085. /**
  2086. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  2087. * @kcontrol: mixer control
  2088. * @ucontrol: control element information
  2089. *
  2090. * Callback to get the value of a double semi enumerated mixer.
  2091. *
  2092. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2093. * used for handling bitfield coded enumeration for example.
  2094. *
  2095. * Returns 0 for success.
  2096. */
  2097. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2098. struct snd_ctl_elem_value *ucontrol)
  2099. {
  2100. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2101. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2102. unsigned int reg_val, val, mux;
  2103. reg_val = snd_soc_read(codec, e->reg);
  2104. val = (reg_val >> e->shift_l) & e->mask;
  2105. for (mux = 0; mux < e->max; mux++) {
  2106. if (val == e->values[mux])
  2107. break;
  2108. }
  2109. ucontrol->value.enumerated.item[0] = mux;
  2110. if (e->shift_l != e->shift_r) {
  2111. val = (reg_val >> e->shift_r) & e->mask;
  2112. for (mux = 0; mux < e->max; mux++) {
  2113. if (val == e->values[mux])
  2114. break;
  2115. }
  2116. ucontrol->value.enumerated.item[1] = mux;
  2117. }
  2118. return 0;
  2119. }
  2120. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  2121. /**
  2122. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  2123. * @kcontrol: mixer control
  2124. * @ucontrol: control element information
  2125. *
  2126. * Callback to set the value of a double semi enumerated mixer.
  2127. *
  2128. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2129. * used for handling bitfield coded enumeration for example.
  2130. *
  2131. * Returns 0 for success.
  2132. */
  2133. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2134. struct snd_ctl_elem_value *ucontrol)
  2135. {
  2136. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2137. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2138. unsigned int val;
  2139. unsigned int mask;
  2140. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2141. return -EINVAL;
  2142. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2143. mask = e->mask << e->shift_l;
  2144. if (e->shift_l != e->shift_r) {
  2145. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2146. return -EINVAL;
  2147. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2148. mask |= e->mask << e->shift_r;
  2149. }
  2150. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2151. }
  2152. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  2153. /**
  2154. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  2155. * @kcontrol: mixer control
  2156. * @uinfo: control element information
  2157. *
  2158. * Callback to provide information about an external enumerated
  2159. * single mixer.
  2160. *
  2161. * Returns 0 for success.
  2162. */
  2163. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  2164. struct snd_ctl_elem_info *uinfo)
  2165. {
  2166. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2167. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2168. uinfo->count = 1;
  2169. uinfo->value.enumerated.items = e->max;
  2170. if (uinfo->value.enumerated.item > e->max - 1)
  2171. uinfo->value.enumerated.item = e->max - 1;
  2172. strcpy(uinfo->value.enumerated.name,
  2173. e->texts[uinfo->value.enumerated.item]);
  2174. return 0;
  2175. }
  2176. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  2177. /**
  2178. * snd_soc_info_volsw_ext - external single mixer info callback
  2179. * @kcontrol: mixer control
  2180. * @uinfo: control element information
  2181. *
  2182. * Callback to provide information about a single external mixer control.
  2183. *
  2184. * Returns 0 for success.
  2185. */
  2186. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  2187. struct snd_ctl_elem_info *uinfo)
  2188. {
  2189. int max = kcontrol->private_value;
  2190. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2191. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2192. else
  2193. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2194. uinfo->count = 1;
  2195. uinfo->value.integer.min = 0;
  2196. uinfo->value.integer.max = max;
  2197. return 0;
  2198. }
  2199. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  2200. /**
  2201. * snd_soc_info_volsw - single mixer info callback
  2202. * @kcontrol: mixer control
  2203. * @uinfo: control element information
  2204. *
  2205. * Callback to provide information about a single mixer control, or a double
  2206. * mixer control that spans 2 registers.
  2207. *
  2208. * Returns 0 for success.
  2209. */
  2210. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  2211. struct snd_ctl_elem_info *uinfo)
  2212. {
  2213. struct soc_mixer_control *mc =
  2214. (struct soc_mixer_control *)kcontrol->private_value;
  2215. int platform_max;
  2216. if (!mc->platform_max)
  2217. mc->platform_max = mc->max;
  2218. platform_max = mc->platform_max;
  2219. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2220. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2221. else
  2222. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2223. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2224. uinfo->value.integer.min = 0;
  2225. uinfo->value.integer.max = platform_max;
  2226. return 0;
  2227. }
  2228. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  2229. /**
  2230. * snd_soc_get_volsw - single mixer get callback
  2231. * @kcontrol: mixer control
  2232. * @ucontrol: control element information
  2233. *
  2234. * Callback to get the value of a single mixer control, or a double mixer
  2235. * control that spans 2 registers.
  2236. *
  2237. * Returns 0 for success.
  2238. */
  2239. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  2240. struct snd_ctl_elem_value *ucontrol)
  2241. {
  2242. struct soc_mixer_control *mc =
  2243. (struct soc_mixer_control *)kcontrol->private_value;
  2244. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2245. unsigned int reg = mc->reg;
  2246. unsigned int reg2 = mc->rreg;
  2247. unsigned int shift = mc->shift;
  2248. unsigned int rshift = mc->rshift;
  2249. int max = mc->max;
  2250. unsigned int mask = (1 << fls(max)) - 1;
  2251. unsigned int invert = mc->invert;
  2252. ucontrol->value.integer.value[0] =
  2253. (snd_soc_read(codec, reg) >> shift) & mask;
  2254. if (invert)
  2255. ucontrol->value.integer.value[0] =
  2256. max - ucontrol->value.integer.value[0];
  2257. if (snd_soc_volsw_is_stereo(mc)) {
  2258. if (reg == reg2)
  2259. ucontrol->value.integer.value[1] =
  2260. (snd_soc_read(codec, reg) >> rshift) & mask;
  2261. else
  2262. ucontrol->value.integer.value[1] =
  2263. (snd_soc_read(codec, reg2) >> shift) & mask;
  2264. if (invert)
  2265. ucontrol->value.integer.value[1] =
  2266. max - ucontrol->value.integer.value[1];
  2267. }
  2268. return 0;
  2269. }
  2270. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2271. /**
  2272. * snd_soc_put_volsw - single mixer put callback
  2273. * @kcontrol: mixer control
  2274. * @ucontrol: control element information
  2275. *
  2276. * Callback to set the value of a single mixer control, or a double mixer
  2277. * control that spans 2 registers.
  2278. *
  2279. * Returns 0 for success.
  2280. */
  2281. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2282. struct snd_ctl_elem_value *ucontrol)
  2283. {
  2284. struct soc_mixer_control *mc =
  2285. (struct soc_mixer_control *)kcontrol->private_value;
  2286. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2287. unsigned int reg = mc->reg;
  2288. unsigned int reg2 = mc->rreg;
  2289. unsigned int shift = mc->shift;
  2290. unsigned int rshift = mc->rshift;
  2291. int max = mc->max;
  2292. unsigned int mask = (1 << fls(max)) - 1;
  2293. unsigned int invert = mc->invert;
  2294. int err;
  2295. bool type_2r = 0;
  2296. unsigned int val2 = 0;
  2297. unsigned int val, val_mask;
  2298. val = (ucontrol->value.integer.value[0] & mask);
  2299. if (invert)
  2300. val = max - val;
  2301. val_mask = mask << shift;
  2302. val = val << shift;
  2303. if (snd_soc_volsw_is_stereo(mc)) {
  2304. val2 = (ucontrol->value.integer.value[1] & mask);
  2305. if (invert)
  2306. val2 = max - val2;
  2307. if (reg == reg2) {
  2308. val_mask |= mask << rshift;
  2309. val |= val2 << rshift;
  2310. } else {
  2311. val2 = val2 << shift;
  2312. type_2r = 1;
  2313. }
  2314. }
  2315. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2316. if (err < 0)
  2317. return err;
  2318. if (type_2r)
  2319. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2320. return err;
  2321. }
  2322. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2323. /**
  2324. * snd_soc_get_volsw_sx - single mixer get callback
  2325. * @kcontrol: mixer control
  2326. * @ucontrol: control element information
  2327. *
  2328. * Callback to get the value of a single mixer control, or a double mixer
  2329. * control that spans 2 registers.
  2330. *
  2331. * Returns 0 for success.
  2332. */
  2333. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  2334. struct snd_ctl_elem_value *ucontrol)
  2335. {
  2336. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2337. struct soc_mixer_control *mc =
  2338. (struct soc_mixer_control *)kcontrol->private_value;
  2339. unsigned int reg = mc->reg;
  2340. unsigned int reg2 = mc->rreg;
  2341. unsigned int shift = mc->shift;
  2342. unsigned int rshift = mc->rshift;
  2343. int max = mc->max;
  2344. int min = mc->min;
  2345. int mask = (1 << (fls(min + max) - 1)) - 1;
  2346. ucontrol->value.integer.value[0] =
  2347. ((snd_soc_read(codec, reg) >> shift) - min) & mask;
  2348. if (snd_soc_volsw_is_stereo(mc))
  2349. ucontrol->value.integer.value[1] =
  2350. ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
  2351. return 0;
  2352. }
  2353. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  2354. /**
  2355. * snd_soc_put_volsw_sx - double mixer set callback
  2356. * @kcontrol: mixer control
  2357. * @uinfo: control element information
  2358. *
  2359. * Callback to set the value of a double mixer control that spans 2 registers.
  2360. *
  2361. * Returns 0 for success.
  2362. */
  2363. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  2364. struct snd_ctl_elem_value *ucontrol)
  2365. {
  2366. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2367. struct soc_mixer_control *mc =
  2368. (struct soc_mixer_control *)kcontrol->private_value;
  2369. unsigned int reg = mc->reg;
  2370. unsigned int reg2 = mc->rreg;
  2371. unsigned int shift = mc->shift;
  2372. unsigned int rshift = mc->rshift;
  2373. int max = mc->max;
  2374. int min = mc->min;
  2375. int mask = (1 << (fls(min + max) - 1)) - 1;
  2376. int err = 0;
  2377. unsigned short val, val_mask, val2 = 0;
  2378. val_mask = mask << shift;
  2379. val = (ucontrol->value.integer.value[0] + min) & mask;
  2380. val = val << shift;
  2381. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2382. if (err < 0)
  2383. return err;
  2384. if (snd_soc_volsw_is_stereo(mc)) {
  2385. val_mask = mask << rshift;
  2386. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  2387. val2 = val2 << rshift;
  2388. if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
  2389. return err;
  2390. }
  2391. return 0;
  2392. }
  2393. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  2394. /**
  2395. * snd_soc_info_volsw_s8 - signed mixer info callback
  2396. * @kcontrol: mixer control
  2397. * @uinfo: control element information
  2398. *
  2399. * Callback to provide information about a signed mixer control.
  2400. *
  2401. * Returns 0 for success.
  2402. */
  2403. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2404. struct snd_ctl_elem_info *uinfo)
  2405. {
  2406. struct soc_mixer_control *mc =
  2407. (struct soc_mixer_control *)kcontrol->private_value;
  2408. int platform_max;
  2409. int min = mc->min;
  2410. if (!mc->platform_max)
  2411. mc->platform_max = mc->max;
  2412. platform_max = mc->platform_max;
  2413. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2414. uinfo->count = 2;
  2415. uinfo->value.integer.min = 0;
  2416. uinfo->value.integer.max = platform_max - min;
  2417. return 0;
  2418. }
  2419. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2420. /**
  2421. * snd_soc_get_volsw_s8 - signed mixer get callback
  2422. * @kcontrol: mixer control
  2423. * @ucontrol: control element information
  2424. *
  2425. * Callback to get the value of a signed mixer control.
  2426. *
  2427. * Returns 0 for success.
  2428. */
  2429. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2430. struct snd_ctl_elem_value *ucontrol)
  2431. {
  2432. struct soc_mixer_control *mc =
  2433. (struct soc_mixer_control *)kcontrol->private_value;
  2434. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2435. unsigned int reg = mc->reg;
  2436. int min = mc->min;
  2437. int val = snd_soc_read(codec, reg);
  2438. ucontrol->value.integer.value[0] =
  2439. ((signed char)(val & 0xff))-min;
  2440. ucontrol->value.integer.value[1] =
  2441. ((signed char)((val >> 8) & 0xff))-min;
  2442. return 0;
  2443. }
  2444. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2445. /**
  2446. * snd_soc_put_volsw_sgn - signed mixer put callback
  2447. * @kcontrol: mixer control
  2448. * @ucontrol: control element information
  2449. *
  2450. * Callback to set the value of a signed mixer control.
  2451. *
  2452. * Returns 0 for success.
  2453. */
  2454. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2455. struct snd_ctl_elem_value *ucontrol)
  2456. {
  2457. struct soc_mixer_control *mc =
  2458. (struct soc_mixer_control *)kcontrol->private_value;
  2459. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2460. unsigned int reg = mc->reg;
  2461. int min = mc->min;
  2462. unsigned int val;
  2463. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2464. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2465. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2466. }
  2467. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2468. /**
  2469. * snd_soc_info_volsw_range - single mixer info callback with range.
  2470. * @kcontrol: mixer control
  2471. * @uinfo: control element information
  2472. *
  2473. * Callback to provide information, within a range, about a single
  2474. * mixer control.
  2475. *
  2476. * returns 0 for success.
  2477. */
  2478. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  2479. struct snd_ctl_elem_info *uinfo)
  2480. {
  2481. struct soc_mixer_control *mc =
  2482. (struct soc_mixer_control *)kcontrol->private_value;
  2483. int platform_max;
  2484. int min = mc->min;
  2485. if (!mc->platform_max)
  2486. mc->platform_max = mc->max;
  2487. platform_max = mc->platform_max;
  2488. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2489. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2490. uinfo->value.integer.min = 0;
  2491. uinfo->value.integer.max = platform_max - min;
  2492. return 0;
  2493. }
  2494. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  2495. /**
  2496. * snd_soc_put_volsw_range - single mixer put value callback with range.
  2497. * @kcontrol: mixer control
  2498. * @ucontrol: control element information
  2499. *
  2500. * Callback to set the value, within a range, for a single mixer control.
  2501. *
  2502. * Returns 0 for success.
  2503. */
  2504. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  2505. struct snd_ctl_elem_value *ucontrol)
  2506. {
  2507. struct soc_mixer_control *mc =
  2508. (struct soc_mixer_control *)kcontrol->private_value;
  2509. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2510. unsigned int reg = mc->reg;
  2511. unsigned int rreg = mc->rreg;
  2512. unsigned int shift = mc->shift;
  2513. int min = mc->min;
  2514. int max = mc->max;
  2515. unsigned int mask = (1 << fls(max)) - 1;
  2516. unsigned int invert = mc->invert;
  2517. unsigned int val, val_mask;
  2518. int ret;
  2519. val = ((ucontrol->value.integer.value[0] + min) & mask);
  2520. if (invert)
  2521. val = max - val;
  2522. val_mask = mask << shift;
  2523. val = val << shift;
  2524. ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2525. if (ret != 0)
  2526. return ret;
  2527. if (snd_soc_volsw_is_stereo(mc)) {
  2528. val = ((ucontrol->value.integer.value[1] + min) & mask);
  2529. if (invert)
  2530. val = max - val;
  2531. val_mask = mask << shift;
  2532. val = val << shift;
  2533. ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
  2534. }
  2535. return ret;
  2536. }
  2537. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  2538. /**
  2539. * snd_soc_get_volsw_range - single mixer get callback with range
  2540. * @kcontrol: mixer control
  2541. * @ucontrol: control element information
  2542. *
  2543. * Callback to get the value, within a range, of a single mixer control.
  2544. *
  2545. * Returns 0 for success.
  2546. */
  2547. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  2548. struct snd_ctl_elem_value *ucontrol)
  2549. {
  2550. struct soc_mixer_control *mc =
  2551. (struct soc_mixer_control *)kcontrol->private_value;
  2552. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2553. unsigned int reg = mc->reg;
  2554. unsigned int rreg = mc->rreg;
  2555. unsigned int shift = mc->shift;
  2556. int min = mc->min;
  2557. int max = mc->max;
  2558. unsigned int mask = (1 << fls(max)) - 1;
  2559. unsigned int invert = mc->invert;
  2560. ucontrol->value.integer.value[0] =
  2561. (snd_soc_read(codec, reg) >> shift) & mask;
  2562. if (invert)
  2563. ucontrol->value.integer.value[0] =
  2564. max - ucontrol->value.integer.value[0];
  2565. ucontrol->value.integer.value[0] =
  2566. ucontrol->value.integer.value[0] - min;
  2567. if (snd_soc_volsw_is_stereo(mc)) {
  2568. ucontrol->value.integer.value[1] =
  2569. (snd_soc_read(codec, rreg) >> shift) & mask;
  2570. if (invert)
  2571. ucontrol->value.integer.value[1] =
  2572. max - ucontrol->value.integer.value[1];
  2573. ucontrol->value.integer.value[1] =
  2574. ucontrol->value.integer.value[1] - min;
  2575. }
  2576. return 0;
  2577. }
  2578. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  2579. /**
  2580. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2581. *
  2582. * @codec: where to look for the control
  2583. * @name: Name of the control
  2584. * @max: new maximum limit
  2585. *
  2586. * Return 0 for success, else error.
  2587. */
  2588. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2589. const char *name, int max)
  2590. {
  2591. struct snd_card *card = codec->card->snd_card;
  2592. struct snd_kcontrol *kctl;
  2593. struct soc_mixer_control *mc;
  2594. int found = 0;
  2595. int ret = -EINVAL;
  2596. /* Sanity check for name and max */
  2597. if (unlikely(!name || max <= 0))
  2598. return -EINVAL;
  2599. list_for_each_entry(kctl, &card->controls, list) {
  2600. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2601. found = 1;
  2602. break;
  2603. }
  2604. }
  2605. if (found) {
  2606. mc = (struct soc_mixer_control *)kctl->private_value;
  2607. if (max <= mc->max) {
  2608. mc->platform_max = max;
  2609. ret = 0;
  2610. }
  2611. }
  2612. return ret;
  2613. }
  2614. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2615. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  2616. struct snd_ctl_elem_info *uinfo)
  2617. {
  2618. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2619. struct soc_bytes *params = (void *)kcontrol->private_value;
  2620. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  2621. uinfo->count = params->num_regs * codec->val_bytes;
  2622. return 0;
  2623. }
  2624. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  2625. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  2626. struct snd_ctl_elem_value *ucontrol)
  2627. {
  2628. struct soc_bytes *params = (void *)kcontrol->private_value;
  2629. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2630. int ret;
  2631. if (codec->using_regmap)
  2632. ret = regmap_raw_read(codec->control_data, params->base,
  2633. ucontrol->value.bytes.data,
  2634. params->num_regs * codec->val_bytes);
  2635. else
  2636. ret = -EINVAL;
  2637. /* Hide any masked bytes to ensure consistent data reporting */
  2638. if (ret == 0 && params->mask) {
  2639. switch (codec->val_bytes) {
  2640. case 1:
  2641. ucontrol->value.bytes.data[0] &= ~params->mask;
  2642. break;
  2643. case 2:
  2644. ((u16 *)(&ucontrol->value.bytes.data))[0]
  2645. &= ~params->mask;
  2646. break;
  2647. case 4:
  2648. ((u32 *)(&ucontrol->value.bytes.data))[0]
  2649. &= ~params->mask;
  2650. break;
  2651. default:
  2652. return -EINVAL;
  2653. }
  2654. }
  2655. return ret;
  2656. }
  2657. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  2658. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  2659. struct snd_ctl_elem_value *ucontrol)
  2660. {
  2661. struct soc_bytes *params = (void *)kcontrol->private_value;
  2662. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2663. int ret, len;
  2664. unsigned int val;
  2665. void *data;
  2666. if (!codec->using_regmap)
  2667. return -EINVAL;
  2668. len = params->num_regs * codec->val_bytes;
  2669. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  2670. if (!data)
  2671. return -ENOMEM;
  2672. /*
  2673. * If we've got a mask then we need to preserve the register
  2674. * bits. We shouldn't modify the incoming data so take a
  2675. * copy.
  2676. */
  2677. if (params->mask) {
  2678. ret = regmap_read(codec->control_data, params->base, &val);
  2679. if (ret != 0)
  2680. return ret;
  2681. val &= params->mask;
  2682. switch (codec->val_bytes) {
  2683. case 1:
  2684. ((u8 *)data)[0] &= ~params->mask;
  2685. ((u8 *)data)[0] |= val;
  2686. break;
  2687. case 2:
  2688. ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
  2689. ((u16 *)data)[0] |= cpu_to_be16(val);
  2690. break;
  2691. case 4:
  2692. ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
  2693. ((u32 *)data)[0] |= cpu_to_be32(val);
  2694. break;
  2695. default:
  2696. return -EINVAL;
  2697. }
  2698. }
  2699. ret = regmap_raw_write(codec->control_data, params->base,
  2700. data, len);
  2701. kfree(data);
  2702. return ret;
  2703. }
  2704. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  2705. /**
  2706. * snd_soc_info_xr_sx - signed multi register info callback
  2707. * @kcontrol: mreg control
  2708. * @uinfo: control element information
  2709. *
  2710. * Callback to provide information of a control that can
  2711. * span multiple codec registers which together
  2712. * forms a single signed value in a MSB/LSB manner.
  2713. *
  2714. * Returns 0 for success.
  2715. */
  2716. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  2717. struct snd_ctl_elem_info *uinfo)
  2718. {
  2719. struct soc_mreg_control *mc =
  2720. (struct soc_mreg_control *)kcontrol->private_value;
  2721. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2722. uinfo->count = 1;
  2723. uinfo->value.integer.min = mc->min;
  2724. uinfo->value.integer.max = mc->max;
  2725. return 0;
  2726. }
  2727. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  2728. /**
  2729. * snd_soc_get_xr_sx - signed multi register get callback
  2730. * @kcontrol: mreg control
  2731. * @ucontrol: control element information
  2732. *
  2733. * Callback to get the value of a control that can span
  2734. * multiple codec registers which together forms a single
  2735. * signed value in a MSB/LSB manner. The control supports
  2736. * specifying total no of bits used to allow for bitfields
  2737. * across the multiple codec registers.
  2738. *
  2739. * Returns 0 for success.
  2740. */
  2741. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  2742. struct snd_ctl_elem_value *ucontrol)
  2743. {
  2744. struct soc_mreg_control *mc =
  2745. (struct soc_mreg_control *)kcontrol->private_value;
  2746. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2747. unsigned int regbase = mc->regbase;
  2748. unsigned int regcount = mc->regcount;
  2749. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2750. unsigned int regwmask = (1<<regwshift)-1;
  2751. unsigned int invert = mc->invert;
  2752. unsigned long mask = (1UL<<mc->nbits)-1;
  2753. long min = mc->min;
  2754. long max = mc->max;
  2755. long val = 0;
  2756. unsigned long regval;
  2757. unsigned int i;
  2758. for (i = 0; i < regcount; i++) {
  2759. regval = snd_soc_read(codec, regbase+i) & regwmask;
  2760. val |= regval << (regwshift*(regcount-i-1));
  2761. }
  2762. val &= mask;
  2763. if (min < 0 && val > max)
  2764. val |= ~mask;
  2765. if (invert)
  2766. val = max - val;
  2767. ucontrol->value.integer.value[0] = val;
  2768. return 0;
  2769. }
  2770. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  2771. /**
  2772. * snd_soc_put_xr_sx - signed multi register get callback
  2773. * @kcontrol: mreg control
  2774. * @ucontrol: control element information
  2775. *
  2776. * Callback to set the value of a control that can span
  2777. * multiple codec registers which together forms a single
  2778. * signed value in a MSB/LSB manner. The control supports
  2779. * specifying total no of bits used to allow for bitfields
  2780. * across the multiple codec registers.
  2781. *
  2782. * Returns 0 for success.
  2783. */
  2784. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  2785. struct snd_ctl_elem_value *ucontrol)
  2786. {
  2787. struct soc_mreg_control *mc =
  2788. (struct soc_mreg_control *)kcontrol->private_value;
  2789. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2790. unsigned int regbase = mc->regbase;
  2791. unsigned int regcount = mc->regcount;
  2792. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2793. unsigned int regwmask = (1<<regwshift)-1;
  2794. unsigned int invert = mc->invert;
  2795. unsigned long mask = (1UL<<mc->nbits)-1;
  2796. long max = mc->max;
  2797. long val = ucontrol->value.integer.value[0];
  2798. unsigned int i, regval, regmask;
  2799. int err;
  2800. if (invert)
  2801. val = max - val;
  2802. val &= mask;
  2803. for (i = 0; i < regcount; i++) {
  2804. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  2805. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  2806. err = snd_soc_update_bits_locked(codec, regbase+i,
  2807. regmask, regval);
  2808. if (err < 0)
  2809. return err;
  2810. }
  2811. return 0;
  2812. }
  2813. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  2814. /**
  2815. * snd_soc_get_strobe - strobe get callback
  2816. * @kcontrol: mixer control
  2817. * @ucontrol: control element information
  2818. *
  2819. * Callback get the value of a strobe mixer control.
  2820. *
  2821. * Returns 0 for success.
  2822. */
  2823. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  2824. struct snd_ctl_elem_value *ucontrol)
  2825. {
  2826. struct soc_mixer_control *mc =
  2827. (struct soc_mixer_control *)kcontrol->private_value;
  2828. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2829. unsigned int reg = mc->reg;
  2830. unsigned int shift = mc->shift;
  2831. unsigned int mask = 1 << shift;
  2832. unsigned int invert = mc->invert != 0;
  2833. unsigned int val = snd_soc_read(codec, reg) & mask;
  2834. if (shift != 0 && val != 0)
  2835. val = val >> shift;
  2836. ucontrol->value.enumerated.item[0] = val ^ invert;
  2837. return 0;
  2838. }
  2839. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  2840. /**
  2841. * snd_soc_put_strobe - strobe put callback
  2842. * @kcontrol: mixer control
  2843. * @ucontrol: control element information
  2844. *
  2845. * Callback strobe a register bit to high then low (or the inverse)
  2846. * in one pass of a single mixer enum control.
  2847. *
  2848. * Returns 1 for success.
  2849. */
  2850. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  2851. struct snd_ctl_elem_value *ucontrol)
  2852. {
  2853. struct soc_mixer_control *mc =
  2854. (struct soc_mixer_control *)kcontrol->private_value;
  2855. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2856. unsigned int reg = mc->reg;
  2857. unsigned int shift = mc->shift;
  2858. unsigned int mask = 1 << shift;
  2859. unsigned int invert = mc->invert != 0;
  2860. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  2861. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  2862. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  2863. int err;
  2864. err = snd_soc_update_bits_locked(codec, reg, mask, val1);
  2865. if (err < 0)
  2866. return err;
  2867. err = snd_soc_update_bits_locked(codec, reg, mask, val2);
  2868. return err;
  2869. }
  2870. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
  2871. /**
  2872. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2873. * @dai: DAI
  2874. * @clk_id: DAI specific clock ID
  2875. * @freq: new clock frequency in Hz
  2876. * @dir: new clock direction - input/output.
  2877. *
  2878. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2879. */
  2880. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2881. unsigned int freq, int dir)
  2882. {
  2883. if (dai->driver && dai->driver->ops->set_sysclk)
  2884. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2885. else if (dai->codec && dai->codec->driver->set_sysclk)
  2886. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2887. freq, dir);
  2888. else
  2889. return -EINVAL;
  2890. }
  2891. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2892. /**
  2893. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2894. * @codec: CODEC
  2895. * @clk_id: DAI specific clock ID
  2896. * @source: Source for the clock
  2897. * @freq: new clock frequency in Hz
  2898. * @dir: new clock direction - input/output.
  2899. *
  2900. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2901. */
  2902. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2903. int source, unsigned int freq, int dir)
  2904. {
  2905. if (codec->driver->set_sysclk)
  2906. return codec->driver->set_sysclk(codec, clk_id, source,
  2907. freq, dir);
  2908. else
  2909. return -EINVAL;
  2910. }
  2911. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2912. /**
  2913. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2914. * @dai: DAI
  2915. * @div_id: DAI specific clock divider ID
  2916. * @div: new clock divisor.
  2917. *
  2918. * Configures the clock dividers. This is used to derive the best DAI bit and
  2919. * frame clocks from the system or master clock. It's best to set the DAI bit
  2920. * and frame clocks as low as possible to save system power.
  2921. */
  2922. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2923. int div_id, int div)
  2924. {
  2925. if (dai->driver && dai->driver->ops->set_clkdiv)
  2926. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2927. else
  2928. return -EINVAL;
  2929. }
  2930. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2931. /**
  2932. * snd_soc_dai_set_pll - configure DAI PLL.
  2933. * @dai: DAI
  2934. * @pll_id: DAI specific PLL ID
  2935. * @source: DAI specific source for the PLL
  2936. * @freq_in: PLL input clock frequency in Hz
  2937. * @freq_out: requested PLL output clock frequency in Hz
  2938. *
  2939. * Configures and enables PLL to generate output clock based on input clock.
  2940. */
  2941. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2942. unsigned int freq_in, unsigned int freq_out)
  2943. {
  2944. if (dai->driver && dai->driver->ops->set_pll)
  2945. return dai->driver->ops->set_pll(dai, pll_id, source,
  2946. freq_in, freq_out);
  2947. else if (dai->codec && dai->codec->driver->set_pll)
  2948. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2949. freq_in, freq_out);
  2950. else
  2951. return -EINVAL;
  2952. }
  2953. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2954. /*
  2955. * snd_soc_codec_set_pll - configure codec PLL.
  2956. * @codec: CODEC
  2957. * @pll_id: DAI specific PLL ID
  2958. * @source: DAI specific source for the PLL
  2959. * @freq_in: PLL input clock frequency in Hz
  2960. * @freq_out: requested PLL output clock frequency in Hz
  2961. *
  2962. * Configures and enables PLL to generate output clock based on input clock.
  2963. */
  2964. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2965. unsigned int freq_in, unsigned int freq_out)
  2966. {
  2967. if (codec->driver->set_pll)
  2968. return codec->driver->set_pll(codec, pll_id, source,
  2969. freq_in, freq_out);
  2970. else
  2971. return -EINVAL;
  2972. }
  2973. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2974. /**
  2975. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2976. * @dai: DAI
  2977. * @fmt: SND_SOC_DAIFMT_ format value.
  2978. *
  2979. * Configures the DAI hardware format and clocking.
  2980. */
  2981. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2982. {
  2983. if (dai->driver == NULL)
  2984. return -EINVAL;
  2985. if (dai->driver->ops->set_fmt == NULL)
  2986. return -ENOTSUPP;
  2987. return dai->driver->ops->set_fmt(dai, fmt);
  2988. }
  2989. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2990. /**
  2991. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2992. * @dai: DAI
  2993. * @tx_mask: bitmask representing active TX slots.
  2994. * @rx_mask: bitmask representing active RX slots.
  2995. * @slots: Number of slots in use.
  2996. * @slot_width: Width in bits for each slot.
  2997. *
  2998. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2999. * specific.
  3000. */
  3001. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  3002. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  3003. {
  3004. if (dai->driver && dai->driver->ops->set_tdm_slot)
  3005. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  3006. slots, slot_width);
  3007. else
  3008. return -EINVAL;
  3009. }
  3010. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  3011. /**
  3012. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  3013. * @dai: DAI
  3014. * @tx_num: how many TX channels
  3015. * @tx_slot: pointer to an array which imply the TX slot number channel
  3016. * 0~num-1 uses
  3017. * @rx_num: how many RX channels
  3018. * @rx_slot: pointer to an array which imply the RX slot number channel
  3019. * 0~num-1 uses
  3020. *
  3021. * configure the relationship between channel number and TDM slot number.
  3022. */
  3023. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  3024. unsigned int tx_num, unsigned int *tx_slot,
  3025. unsigned int rx_num, unsigned int *rx_slot)
  3026. {
  3027. if (dai->driver && dai->driver->ops->set_channel_map)
  3028. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  3029. rx_num, rx_slot);
  3030. else
  3031. return -EINVAL;
  3032. }
  3033. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  3034. /**
  3035. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  3036. * @dai: DAI
  3037. * @tristate: tristate enable
  3038. *
  3039. * Tristates the DAI so that others can use it.
  3040. */
  3041. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  3042. {
  3043. if (dai->driver && dai->driver->ops->set_tristate)
  3044. return dai->driver->ops->set_tristate(dai, tristate);
  3045. else
  3046. return -EINVAL;
  3047. }
  3048. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  3049. /**
  3050. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  3051. * @dai: DAI
  3052. * @mute: mute enable
  3053. * @direction: stream to mute
  3054. *
  3055. * Mutes the DAI DAC.
  3056. */
  3057. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  3058. int direction)
  3059. {
  3060. if (!dai->driver)
  3061. return -ENOTSUPP;
  3062. if (dai->driver->ops->mute_stream)
  3063. return dai->driver->ops->mute_stream(dai, mute, direction);
  3064. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  3065. dai->driver->ops->digital_mute)
  3066. return dai->driver->ops->digital_mute(dai, mute);
  3067. else
  3068. return -ENOTSUPP;
  3069. }
  3070. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  3071. /**
  3072. * snd_soc_register_card - Register a card with the ASoC core
  3073. *
  3074. * @card: Card to register
  3075. *
  3076. */
  3077. int snd_soc_register_card(struct snd_soc_card *card)
  3078. {
  3079. int i, ret;
  3080. if (!card->name || !card->dev)
  3081. return -EINVAL;
  3082. for (i = 0; i < card->num_links; i++) {
  3083. struct snd_soc_dai_link *link = &card->dai_link[i];
  3084. /*
  3085. * Codec must be specified by 1 of name or OF node,
  3086. * not both or neither.
  3087. */
  3088. if (!!link->codec_name == !!link->codec_of_node) {
  3089. dev_err(card->dev, "ASoC: Neither/both codec"
  3090. " name/of_node are set for %s\n", link->name);
  3091. return -EINVAL;
  3092. }
  3093. /* Codec DAI name must be specified */
  3094. if (!link->codec_dai_name) {
  3095. dev_err(card->dev, "ASoC: codec_dai_name not"
  3096. " set for %s\n", link->name);
  3097. return -EINVAL;
  3098. }
  3099. /*
  3100. * Platform may be specified by either name or OF node, but
  3101. * can be left unspecified, and a dummy platform will be used.
  3102. */
  3103. if (link->platform_name && link->platform_of_node) {
  3104. dev_err(card->dev, "ASoC: Both platform name/of_node"
  3105. " are set for %s\n", link->name);
  3106. return -EINVAL;
  3107. }
  3108. /*
  3109. * CPU device may be specified by either name or OF node, but
  3110. * can be left unspecified, and will be matched based on DAI
  3111. * name alone..
  3112. */
  3113. if (link->cpu_name && link->cpu_of_node) {
  3114. dev_err(card->dev, "ASoC: Neither/both "
  3115. "cpu name/of_node are set for %s\n",link->name);
  3116. return -EINVAL;
  3117. }
  3118. /*
  3119. * At least one of CPU DAI name or CPU device name/node must be
  3120. * specified
  3121. */
  3122. if (!link->cpu_dai_name &&
  3123. !(link->cpu_name || link->cpu_of_node)) {
  3124. dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
  3125. "cpu_name/of_node are set for %s\n", link->name);
  3126. return -EINVAL;
  3127. }
  3128. }
  3129. dev_set_drvdata(card->dev, card);
  3130. snd_soc_initialize_card_lists(card);
  3131. soc_init_card_debugfs(card);
  3132. card->rtd = devm_kzalloc(card->dev,
  3133. sizeof(struct snd_soc_pcm_runtime) *
  3134. (card->num_links + card->num_aux_devs),
  3135. GFP_KERNEL);
  3136. if (card->rtd == NULL)
  3137. return -ENOMEM;
  3138. card->num_rtd = 0;
  3139. card->rtd_aux = &card->rtd[card->num_links];
  3140. for (i = 0; i < card->num_links; i++)
  3141. card->rtd[i].dai_link = &card->dai_link[i];
  3142. INIT_LIST_HEAD(&card->list);
  3143. INIT_LIST_HEAD(&card->dapm_dirty);
  3144. card->instantiated = 0;
  3145. mutex_init(&card->mutex);
  3146. mutex_init(&card->dapm_mutex);
  3147. ret = snd_soc_instantiate_card(card);
  3148. if (ret != 0)
  3149. soc_cleanup_card_debugfs(card);
  3150. return ret;
  3151. }
  3152. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  3153. /**
  3154. * snd_soc_unregister_card - Unregister a card with the ASoC core
  3155. *
  3156. * @card: Card to unregister
  3157. *
  3158. */
  3159. int snd_soc_unregister_card(struct snd_soc_card *card)
  3160. {
  3161. if (card->instantiated)
  3162. soc_cleanup_card_resources(card);
  3163. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  3164. return 0;
  3165. }
  3166. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  3167. /*
  3168. * Simplify DAI link configuration by removing ".-1" from device names
  3169. * and sanitizing names.
  3170. */
  3171. static char *fmt_single_name(struct device *dev, int *id)
  3172. {
  3173. char *found, name[NAME_SIZE];
  3174. int id1, id2;
  3175. if (dev_name(dev) == NULL)
  3176. return NULL;
  3177. strlcpy(name, dev_name(dev), NAME_SIZE);
  3178. /* are we a "%s.%d" name (platform and SPI components) */
  3179. found = strstr(name, dev->driver->name);
  3180. if (found) {
  3181. /* get ID */
  3182. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  3183. /* discard ID from name if ID == -1 */
  3184. if (*id == -1)
  3185. found[strlen(dev->driver->name)] = '\0';
  3186. }
  3187. } else {
  3188. /* I2C component devices are named "bus-addr" */
  3189. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  3190. char tmp[NAME_SIZE];
  3191. /* create unique ID number from I2C addr and bus */
  3192. *id = ((id1 & 0xffff) << 16) + id2;
  3193. /* sanitize component name for DAI link creation */
  3194. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  3195. strlcpy(name, tmp, NAME_SIZE);
  3196. } else
  3197. *id = 0;
  3198. }
  3199. return kstrdup(name, GFP_KERNEL);
  3200. }
  3201. /*
  3202. * Simplify DAI link naming for single devices with multiple DAIs by removing
  3203. * any ".-1" and using the DAI name (instead of device name).
  3204. */
  3205. static inline char *fmt_multiple_name(struct device *dev,
  3206. struct snd_soc_dai_driver *dai_drv)
  3207. {
  3208. if (dai_drv->name == NULL) {
  3209. dev_err(dev, "ASoC: error - multiple DAI %s registered with"
  3210. " no name\n", dev_name(dev));
  3211. return NULL;
  3212. }
  3213. return kstrdup(dai_drv->name, GFP_KERNEL);
  3214. }
  3215. /**
  3216. * snd_soc_register_dai - Register a DAI with the ASoC core
  3217. *
  3218. * @dai: DAI to register
  3219. */
  3220. int snd_soc_register_dai(struct device *dev,
  3221. struct snd_soc_dai_driver *dai_drv)
  3222. {
  3223. struct snd_soc_codec *codec;
  3224. struct snd_soc_dai *dai;
  3225. dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
  3226. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3227. if (dai == NULL)
  3228. return -ENOMEM;
  3229. /* create DAI component name */
  3230. dai->name = fmt_single_name(dev, &dai->id);
  3231. if (dai->name == NULL) {
  3232. kfree(dai);
  3233. return -ENOMEM;
  3234. }
  3235. dai->dev = dev;
  3236. dai->driver = dai_drv;
  3237. dai->dapm.dev = dev;
  3238. if (!dai->driver->ops)
  3239. dai->driver->ops = &null_dai_ops;
  3240. mutex_lock(&client_mutex);
  3241. list_for_each_entry(codec, &codec_list, list) {
  3242. if (codec->dev == dev) {
  3243. dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
  3244. dai->name, codec->name);
  3245. dai->codec = codec;
  3246. break;
  3247. }
  3248. }
  3249. if (!dai->codec)
  3250. dai->dapm.idle_bias_off = 1;
  3251. list_add(&dai->list, &dai_list);
  3252. mutex_unlock(&client_mutex);
  3253. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3254. return 0;
  3255. }
  3256. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  3257. /**
  3258. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  3259. *
  3260. * @dai: DAI to unregister
  3261. */
  3262. void snd_soc_unregister_dai(struct device *dev)
  3263. {
  3264. struct snd_soc_dai *dai;
  3265. list_for_each_entry(dai, &dai_list, list) {
  3266. if (dev == dai->dev)
  3267. goto found;
  3268. }
  3269. return;
  3270. found:
  3271. mutex_lock(&client_mutex);
  3272. list_del(&dai->list);
  3273. mutex_unlock(&client_mutex);
  3274. dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
  3275. kfree(dai->name);
  3276. kfree(dai);
  3277. }
  3278. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  3279. /**
  3280. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  3281. *
  3282. * @dai: Array of DAIs to register
  3283. * @count: Number of DAIs
  3284. */
  3285. int snd_soc_register_dais(struct device *dev,
  3286. struct snd_soc_dai_driver *dai_drv, size_t count)
  3287. {
  3288. struct snd_soc_codec *codec;
  3289. struct snd_soc_dai *dai;
  3290. int i, ret = 0;
  3291. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  3292. for (i = 0; i < count; i++) {
  3293. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3294. if (dai == NULL) {
  3295. ret = -ENOMEM;
  3296. goto err;
  3297. }
  3298. /* create DAI component name */
  3299. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  3300. if (dai->name == NULL) {
  3301. kfree(dai);
  3302. ret = -EINVAL;
  3303. goto err;
  3304. }
  3305. dai->dev = dev;
  3306. dai->driver = &dai_drv[i];
  3307. if (dai->driver->id)
  3308. dai->id = dai->driver->id;
  3309. else
  3310. dai->id = i;
  3311. dai->dapm.dev = dev;
  3312. if (!dai->driver->ops)
  3313. dai->driver->ops = &null_dai_ops;
  3314. mutex_lock(&client_mutex);
  3315. list_for_each_entry(codec, &codec_list, list) {
  3316. if (codec->dev == dev) {
  3317. dev_dbg(dev, "ASoC: Mapped DAI %s to "
  3318. "CODEC %s\n", dai->name, codec->name);
  3319. dai->codec = codec;
  3320. break;
  3321. }
  3322. }
  3323. if (!dai->codec)
  3324. dai->dapm.idle_bias_off = 1;
  3325. list_add(&dai->list, &dai_list);
  3326. mutex_unlock(&client_mutex);
  3327. dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3328. }
  3329. return 0;
  3330. err:
  3331. for (i--; i >= 0; i--)
  3332. snd_soc_unregister_dai(dev);
  3333. return ret;
  3334. }
  3335. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  3336. /**
  3337. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  3338. *
  3339. * @dai: Array of DAIs to unregister
  3340. * @count: Number of DAIs
  3341. */
  3342. void snd_soc_unregister_dais(struct device *dev, size_t count)
  3343. {
  3344. int i;
  3345. for (i = 0; i < count; i++)
  3346. snd_soc_unregister_dai(dev);
  3347. }
  3348. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  3349. /**
  3350. * snd_soc_register_platform - Register a platform with the ASoC core
  3351. *
  3352. * @platform: platform to register
  3353. */
  3354. int snd_soc_register_platform(struct device *dev,
  3355. struct snd_soc_platform_driver *platform_drv)
  3356. {
  3357. struct snd_soc_platform *platform;
  3358. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  3359. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  3360. if (platform == NULL)
  3361. return -ENOMEM;
  3362. /* create platform component name */
  3363. platform->name = fmt_single_name(dev, &platform->id);
  3364. if (platform->name == NULL) {
  3365. kfree(platform);
  3366. return -ENOMEM;
  3367. }
  3368. platform->dev = dev;
  3369. platform->driver = platform_drv;
  3370. platform->dapm.dev = dev;
  3371. platform->dapm.platform = platform;
  3372. platform->dapm.stream_event = platform_drv->stream_event;
  3373. mutex_init(&platform->mutex);
  3374. mutex_lock(&client_mutex);
  3375. list_add(&platform->list, &platform_list);
  3376. mutex_unlock(&client_mutex);
  3377. dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
  3378. return 0;
  3379. }
  3380. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  3381. /**
  3382. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  3383. *
  3384. * @platform: platform to unregister
  3385. */
  3386. void snd_soc_unregister_platform(struct device *dev)
  3387. {
  3388. struct snd_soc_platform *platform;
  3389. list_for_each_entry(platform, &platform_list, list) {
  3390. if (dev == platform->dev)
  3391. goto found;
  3392. }
  3393. return;
  3394. found:
  3395. mutex_lock(&client_mutex);
  3396. list_del(&platform->list);
  3397. mutex_unlock(&client_mutex);
  3398. dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
  3399. kfree(platform->name);
  3400. kfree(platform);
  3401. }
  3402. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  3403. static u64 codec_format_map[] = {
  3404. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  3405. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  3406. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  3407. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  3408. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  3409. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  3410. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3411. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3412. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  3413. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  3414. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  3415. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  3416. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  3417. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  3418. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  3419. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  3420. };
  3421. /* Fix up the DAI formats for endianness: codecs don't actually see
  3422. * the endianness of the data but we're using the CPU format
  3423. * definitions which do need to include endianness so we ensure that
  3424. * codec DAIs always have both big and little endian variants set.
  3425. */
  3426. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  3427. {
  3428. int i;
  3429. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  3430. if (stream->formats & codec_format_map[i])
  3431. stream->formats |= codec_format_map[i];
  3432. }
  3433. /**
  3434. * snd_soc_register_codec - Register a codec with the ASoC core
  3435. *
  3436. * @codec: codec to register
  3437. */
  3438. int snd_soc_register_codec(struct device *dev,
  3439. const struct snd_soc_codec_driver *codec_drv,
  3440. struct snd_soc_dai_driver *dai_drv,
  3441. int num_dai)
  3442. {
  3443. size_t reg_size;
  3444. struct snd_soc_codec *codec;
  3445. int ret, i;
  3446. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  3447. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  3448. if (codec == NULL)
  3449. return -ENOMEM;
  3450. /* create CODEC component name */
  3451. codec->name = fmt_single_name(dev, &codec->id);
  3452. if (codec->name == NULL) {
  3453. kfree(codec);
  3454. return -ENOMEM;
  3455. }
  3456. if (codec_drv->compress_type)
  3457. codec->compress_type = codec_drv->compress_type;
  3458. else
  3459. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  3460. codec->write = codec_drv->write;
  3461. codec->read = codec_drv->read;
  3462. codec->volatile_register = codec_drv->volatile_register;
  3463. codec->readable_register = codec_drv->readable_register;
  3464. codec->writable_register = codec_drv->writable_register;
  3465. codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  3466. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  3467. codec->dapm.dev = dev;
  3468. codec->dapm.codec = codec;
  3469. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  3470. codec->dapm.stream_event = codec_drv->stream_event;
  3471. codec->dev = dev;
  3472. codec->driver = codec_drv;
  3473. codec->num_dai = num_dai;
  3474. mutex_init(&codec->mutex);
  3475. /* allocate CODEC register cache */
  3476. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  3477. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  3478. codec->reg_size = reg_size;
  3479. /* it is necessary to make a copy of the default register cache
  3480. * because in the case of using a compression type that requires
  3481. * the default register cache to be marked as the
  3482. * kernel might have freed the array by the time we initialize
  3483. * the cache.
  3484. */
  3485. if (codec_drv->reg_cache_default) {
  3486. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  3487. reg_size, GFP_KERNEL);
  3488. if (!codec->reg_def_copy) {
  3489. ret = -ENOMEM;
  3490. goto fail;
  3491. }
  3492. }
  3493. }
  3494. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  3495. if (!codec->volatile_register)
  3496. codec->volatile_register = snd_soc_default_volatile_register;
  3497. if (!codec->readable_register)
  3498. codec->readable_register = snd_soc_default_readable_register;
  3499. if (!codec->writable_register)
  3500. codec->writable_register = snd_soc_default_writable_register;
  3501. }
  3502. for (i = 0; i < num_dai; i++) {
  3503. fixup_codec_formats(&dai_drv[i].playback);
  3504. fixup_codec_formats(&dai_drv[i].capture);
  3505. }
  3506. mutex_lock(&client_mutex);
  3507. list_add(&codec->list, &codec_list);
  3508. mutex_unlock(&client_mutex);
  3509. /* register any DAIs */
  3510. if (num_dai) {
  3511. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3512. if (ret < 0)
  3513. dev_err(codec->dev, "ASoC: Failed to regster"
  3514. " DAIs: %d\n", ret);
  3515. }
  3516. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
  3517. return 0;
  3518. fail:
  3519. kfree(codec->name);
  3520. kfree(codec);
  3521. return ret;
  3522. }
  3523. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  3524. /**
  3525. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  3526. *
  3527. * @codec: codec to unregister
  3528. */
  3529. void snd_soc_unregister_codec(struct device *dev)
  3530. {
  3531. struct snd_soc_codec *codec;
  3532. int i;
  3533. list_for_each_entry(codec, &codec_list, list) {
  3534. if (dev == codec->dev)
  3535. goto found;
  3536. }
  3537. return;
  3538. found:
  3539. if (codec->num_dai)
  3540. for (i = 0; i < codec->num_dai; i++)
  3541. snd_soc_unregister_dai(dev);
  3542. mutex_lock(&client_mutex);
  3543. list_del(&codec->list);
  3544. mutex_unlock(&client_mutex);
  3545. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
  3546. snd_soc_cache_exit(codec);
  3547. kfree(codec->reg_def_copy);
  3548. kfree(codec->name);
  3549. kfree(codec);
  3550. }
  3551. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  3552. /**
  3553. * snd_soc_register_component - Register a component with the ASoC core
  3554. *
  3555. */
  3556. int snd_soc_register_component(struct device *dev,
  3557. const struct snd_soc_component_driver *cmpnt_drv,
  3558. struct snd_soc_dai_driver *dai_drv,
  3559. int num_dai)
  3560. {
  3561. struct snd_soc_component *cmpnt;
  3562. int ret;
  3563. dev_dbg(dev, "component register %s\n", dev_name(dev));
  3564. cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
  3565. if (!cmpnt) {
  3566. dev_err(dev, "ASoC: Failed to allocate memory\n");
  3567. return -ENOMEM;
  3568. }
  3569. cmpnt->name = fmt_single_name(dev, &cmpnt->id);
  3570. if (!cmpnt->name) {
  3571. dev_err(dev, "ASoC: Failed to simplifying name\n");
  3572. return -ENOMEM;
  3573. }
  3574. cmpnt->dev = dev;
  3575. cmpnt->driver = cmpnt_drv;
  3576. cmpnt->num_dai = num_dai;
  3577. /*
  3578. * snd_soc_register_dai() uses fmt_single_name(), and
  3579. * snd_soc_register_dais() uses fmt_multiple_name()
  3580. * for dai->name which is used for name based matching
  3581. */
  3582. if (1 == num_dai)
  3583. ret = snd_soc_register_dai(dev, dai_drv);
  3584. else
  3585. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3586. if (ret < 0) {
  3587. dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
  3588. goto error_component_name;
  3589. }
  3590. mutex_lock(&client_mutex);
  3591. list_add(&cmpnt->list, &component_list);
  3592. mutex_unlock(&client_mutex);
  3593. dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
  3594. return ret;
  3595. error_component_name:
  3596. kfree(cmpnt->name);
  3597. return ret;
  3598. }
  3599. /**
  3600. * snd_soc_unregister_component - Unregister a component from the ASoC core
  3601. *
  3602. */
  3603. void snd_soc_unregister_component(struct device *dev)
  3604. {
  3605. struct snd_soc_component *cmpnt;
  3606. list_for_each_entry(cmpnt, &component_list, list) {
  3607. if (dev == cmpnt->dev)
  3608. goto found;
  3609. }
  3610. return;
  3611. found:
  3612. snd_soc_unregister_dais(dev, cmpnt->num_dai);
  3613. mutex_lock(&client_mutex);
  3614. list_del(&cmpnt->list);
  3615. mutex_unlock(&client_mutex);
  3616. dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
  3617. kfree(cmpnt->name);
  3618. }
  3619. /* Retrieve a card's name from device tree */
  3620. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  3621. const char *propname)
  3622. {
  3623. struct device_node *np = card->dev->of_node;
  3624. int ret;
  3625. ret = of_property_read_string_index(np, propname, 0, &card->name);
  3626. /*
  3627. * EINVAL means the property does not exist. This is fine providing
  3628. * card->name was previously set, which is checked later in
  3629. * snd_soc_register_card.
  3630. */
  3631. if (ret < 0 && ret != -EINVAL) {
  3632. dev_err(card->dev,
  3633. "ASoC: Property '%s' could not be read: %d\n",
  3634. propname, ret);
  3635. return ret;
  3636. }
  3637. return 0;
  3638. }
  3639. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  3640. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3641. const char *propname)
  3642. {
  3643. struct device_node *np = card->dev->of_node;
  3644. int num_routes;
  3645. struct snd_soc_dapm_route *routes;
  3646. int i, ret;
  3647. num_routes = of_property_count_strings(np, propname);
  3648. if (num_routes < 0 || num_routes & 1) {
  3649. dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
  3650. " length is not even\n", propname);
  3651. return -EINVAL;
  3652. }
  3653. num_routes /= 2;
  3654. if (!num_routes) {
  3655. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3656. propname);
  3657. return -EINVAL;
  3658. }
  3659. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3660. GFP_KERNEL);
  3661. if (!routes) {
  3662. dev_err(card->dev,
  3663. "ASoC: Could not allocate DAPM route table\n");
  3664. return -EINVAL;
  3665. }
  3666. for (i = 0; i < num_routes; i++) {
  3667. ret = of_property_read_string_index(np, propname,
  3668. 2 * i, &routes[i].sink);
  3669. if (ret) {
  3670. dev_err(card->dev,
  3671. "ASoC: Property '%s' index %d could not be read: %d\n",
  3672. propname, 2 * i, ret);
  3673. kfree(routes);
  3674. return -EINVAL;
  3675. }
  3676. ret = of_property_read_string_index(np, propname,
  3677. (2 * i) + 1, &routes[i].source);
  3678. if (ret) {
  3679. dev_err(card->dev,
  3680. "ASoC: Property '%s' index %d could not be read: %d\n",
  3681. propname, (2 * i) + 1, ret);
  3682. kfree(routes);
  3683. return -EINVAL;
  3684. }
  3685. }
  3686. card->num_dapm_routes = num_routes;
  3687. card->dapm_routes = routes;
  3688. return 0;
  3689. }
  3690. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3691. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3692. const char *prefix)
  3693. {
  3694. int ret, i;
  3695. char prop[128];
  3696. unsigned int format = 0;
  3697. int bit, frame;
  3698. const char *str;
  3699. struct {
  3700. char *name;
  3701. unsigned int val;
  3702. } of_fmt_table[] = {
  3703. { "i2s", SND_SOC_DAIFMT_I2S },
  3704. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3705. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3706. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3707. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3708. { "ac97", SND_SOC_DAIFMT_AC97 },
  3709. { "pdm", SND_SOC_DAIFMT_PDM},
  3710. { "msb", SND_SOC_DAIFMT_MSB },
  3711. { "lsb", SND_SOC_DAIFMT_LSB },
  3712. };
  3713. if (!prefix)
  3714. prefix = "";
  3715. /*
  3716. * check "[prefix]format = xxx"
  3717. * SND_SOC_DAIFMT_FORMAT_MASK area
  3718. */
  3719. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3720. ret = of_property_read_string(np, prop, &str);
  3721. if (ret == 0) {
  3722. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3723. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3724. format |= of_fmt_table[i].val;
  3725. break;
  3726. }
  3727. }
  3728. }
  3729. /*
  3730. * check "[prefix]continuous-clock"
  3731. * SND_SOC_DAIFMT_CLOCK_MASK area
  3732. */
  3733. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3734. if (of_get_property(np, prop, NULL))
  3735. format |= SND_SOC_DAIFMT_CONT;
  3736. else
  3737. format |= SND_SOC_DAIFMT_GATED;
  3738. /*
  3739. * check "[prefix]bitclock-inversion"
  3740. * check "[prefix]frame-inversion"
  3741. * SND_SOC_DAIFMT_INV_MASK area
  3742. */
  3743. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3744. bit = !!of_get_property(np, prop, NULL);
  3745. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3746. frame = !!of_get_property(np, prop, NULL);
  3747. switch ((bit << 4) + frame) {
  3748. case 0x11:
  3749. format |= SND_SOC_DAIFMT_IB_IF;
  3750. break;
  3751. case 0x10:
  3752. format |= SND_SOC_DAIFMT_IB_NF;
  3753. break;
  3754. case 0x01:
  3755. format |= SND_SOC_DAIFMT_NB_IF;
  3756. break;
  3757. default:
  3758. /* SND_SOC_DAIFMT_NB_NF is default */
  3759. break;
  3760. }
  3761. /*
  3762. * check "[prefix]bitclock-master"
  3763. * check "[prefix]frame-master"
  3764. * SND_SOC_DAIFMT_MASTER_MASK area
  3765. */
  3766. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3767. bit = !!of_get_property(np, prop, NULL);
  3768. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3769. frame = !!of_get_property(np, prop, NULL);
  3770. switch ((bit << 4) + frame) {
  3771. case 0x11:
  3772. format |= SND_SOC_DAIFMT_CBM_CFM;
  3773. break;
  3774. case 0x10:
  3775. format |= SND_SOC_DAIFMT_CBM_CFS;
  3776. break;
  3777. case 0x01:
  3778. format |= SND_SOC_DAIFMT_CBS_CFM;
  3779. break;
  3780. default:
  3781. format |= SND_SOC_DAIFMT_CBS_CFS;
  3782. break;
  3783. }
  3784. return format;
  3785. }
  3786. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3787. static int __init snd_soc_init(void)
  3788. {
  3789. #ifdef CONFIG_DEBUG_FS
  3790. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  3791. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  3792. pr_warn("ASoC: Failed to create debugfs directory\n");
  3793. snd_soc_debugfs_root = NULL;
  3794. }
  3795. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  3796. &codec_list_fops))
  3797. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  3798. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  3799. &dai_list_fops))
  3800. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  3801. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  3802. &platform_list_fops))
  3803. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  3804. #endif
  3805. snd_soc_util_init();
  3806. return platform_driver_register(&soc_driver);
  3807. }
  3808. module_init(snd_soc_init);
  3809. static void __exit snd_soc_exit(void)
  3810. {
  3811. snd_soc_util_exit();
  3812. #ifdef CONFIG_DEBUG_FS
  3813. debugfs_remove_recursive(snd_soc_debugfs_root);
  3814. #endif
  3815. platform_driver_unregister(&soc_driver);
  3816. }
  3817. module_exit(snd_soc_exit);
  3818. /* Module information */
  3819. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3820. MODULE_DESCRIPTION("ALSA SoC Core");
  3821. MODULE_LICENSE("GPL");
  3822. MODULE_ALIAS("platform:soc-audio");