soc-core.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956
  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/pinctrl/consumer.h>
  33. #include <linux/ctype.h>
  34. #include <linux/slab.h>
  35. #include <linux/of.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/soc-topology.h>
  43. #include <sound/initval.h>
  44. #define CREATE_TRACE_POINTS
  45. #include <trace/events/asoc.h>
  46. #define NAME_SIZE 32
  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(platform_list);
  53. static LIST_HEAD(codec_list);
  54. static LIST_HEAD(component_list);
  55. /*
  56. * This is a timeout to do a DAPM powerdown after a stream is closed().
  57. * It can be used to eliminate pops between different playback streams, e.g.
  58. * between two audio tracks.
  59. */
  60. static int pmdown_time = 5000;
  61. module_param(pmdown_time, int, 0);
  62. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  63. /* returns the minimum number of bytes needed to represent
  64. * a particular given value */
  65. static int min_bytes_needed(unsigned long val)
  66. {
  67. int c = 0;
  68. int i;
  69. for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  70. if (val & (1UL << i))
  71. break;
  72. c = (sizeof val * 8) - c;
  73. if (!c || (c % 8))
  74. c = (c + 8) / 8;
  75. else
  76. c /= 8;
  77. return c;
  78. }
  79. /* fill buf which is 'len' bytes with a formatted
  80. * string of the form 'reg: value\n' */
  81. static int format_register_str(struct snd_soc_codec *codec,
  82. unsigned int reg, char *buf, size_t len)
  83. {
  84. int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  85. int regsize = codec->driver->reg_word_size * 2;
  86. int ret;
  87. /* +2 for ': ' and + 1 for '\n' */
  88. if (wordsize + regsize + 2 + 1 != len)
  89. return -EINVAL;
  90. sprintf(buf, "%.*x: ", wordsize, reg);
  91. buf += wordsize + 2;
  92. ret = snd_soc_read(codec, reg);
  93. if (ret < 0)
  94. memset(buf, 'X', regsize);
  95. else
  96. sprintf(buf, "%.*x", regsize, ret);
  97. buf[regsize] = '\n';
  98. /* no NUL-termination needed */
  99. return 0;
  100. }
  101. /* codec register dump */
  102. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  103. size_t count, loff_t pos)
  104. {
  105. int i, step = 1;
  106. int wordsize, regsize;
  107. int len;
  108. size_t total = 0;
  109. loff_t p = 0;
  110. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  111. regsize = codec->driver->reg_word_size * 2;
  112. len = wordsize + regsize + 2 + 1;
  113. if (!codec->driver->reg_cache_size)
  114. return 0;
  115. if (codec->driver->reg_cache_step)
  116. step = codec->driver->reg_cache_step;
  117. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  118. /* only support larger than PAGE_SIZE bytes debugfs
  119. * entries for the default case */
  120. if (p >= pos) {
  121. if (total + len >= count - 1)
  122. break;
  123. format_register_str(codec, i, buf + total, len);
  124. total += len;
  125. }
  126. p += len;
  127. }
  128. total = min(total, count - 1);
  129. return total;
  130. }
  131. static ssize_t codec_reg_show(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  135. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  136. }
  137. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  138. static ssize_t pmdown_time_show(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  142. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  143. }
  144. static ssize_t pmdown_time_set(struct device *dev,
  145. struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  149. int ret;
  150. ret = kstrtol(buf, 10, &rtd->pmdown_time);
  151. if (ret)
  152. return ret;
  153. return count;
  154. }
  155. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  156. static struct attribute *soc_dev_attrs[] = {
  157. &dev_attr_codec_reg.attr,
  158. &dev_attr_pmdown_time.attr,
  159. NULL
  160. };
  161. static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
  162. struct attribute *attr, int idx)
  163. {
  164. struct device *dev = kobj_to_dev(kobj);
  165. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  166. if (attr == &dev_attr_pmdown_time.attr)
  167. return attr->mode; /* always visible */
  168. return rtd->codec ? attr->mode : 0; /* enabled only with codec */
  169. }
  170. static const struct attribute_group soc_dapm_dev_group = {
  171. .attrs = soc_dapm_dev_attrs,
  172. .is_visible = soc_dev_attr_is_visible,
  173. };
  174. static const struct attribute_group soc_dev_roup = {
  175. .attrs = soc_dev_attrs,
  176. .is_visible = soc_dev_attr_is_visible,
  177. };
  178. static const struct attribute_group *soc_dev_attr_groups[] = {
  179. &soc_dapm_dev_group,
  180. &soc_dev_roup,
  181. NULL
  182. };
  183. #ifdef CONFIG_DEBUG_FS
  184. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. ssize_t ret;
  188. struct snd_soc_codec *codec = file->private_data;
  189. char *buf;
  190. if (*ppos < 0 || !count)
  191. return -EINVAL;
  192. buf = kmalloc(count, GFP_KERNEL);
  193. if (!buf)
  194. return -ENOMEM;
  195. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  196. if (ret >= 0) {
  197. if (copy_to_user(user_buf, buf, ret)) {
  198. kfree(buf);
  199. return -EFAULT;
  200. }
  201. *ppos += ret;
  202. }
  203. kfree(buf);
  204. return ret;
  205. }
  206. static ssize_t codec_reg_write_file(struct file *file,
  207. const char __user *user_buf, size_t count, loff_t *ppos)
  208. {
  209. char buf[32];
  210. size_t buf_size;
  211. char *start = buf;
  212. unsigned long reg, value;
  213. struct snd_soc_codec *codec = file->private_data;
  214. int ret;
  215. buf_size = min(count, (sizeof(buf)-1));
  216. if (copy_from_user(buf, user_buf, buf_size))
  217. return -EFAULT;
  218. buf[buf_size] = 0;
  219. while (*start == ' ')
  220. start++;
  221. reg = simple_strtoul(start, &start, 16);
  222. while (*start == ' ')
  223. start++;
  224. ret = kstrtoul(start, 16, &value);
  225. if (ret)
  226. return ret;
  227. /* Userspace has been fiddling around behind the kernel's back */
  228. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  229. snd_soc_write(codec, reg, value);
  230. return buf_size;
  231. }
  232. static const struct file_operations codec_reg_fops = {
  233. .open = simple_open,
  234. .read = codec_reg_read_file,
  235. .write = codec_reg_write_file,
  236. .llseek = default_llseek,
  237. };
  238. static void soc_init_component_debugfs(struct snd_soc_component *component)
  239. {
  240. if (!component->card->debugfs_card_root)
  241. return;
  242. if (component->debugfs_prefix) {
  243. char *name;
  244. name = kasprintf(GFP_KERNEL, "%s:%s",
  245. component->debugfs_prefix, component->name);
  246. if (name) {
  247. component->debugfs_root = debugfs_create_dir(name,
  248. component->card->debugfs_card_root);
  249. kfree(name);
  250. }
  251. } else {
  252. component->debugfs_root = debugfs_create_dir(component->name,
  253. component->card->debugfs_card_root);
  254. }
  255. if (!component->debugfs_root) {
  256. dev_warn(component->dev,
  257. "ASoC: Failed to create component debugfs directory\n");
  258. return;
  259. }
  260. snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
  261. component->debugfs_root);
  262. if (component->init_debugfs)
  263. component->init_debugfs(component);
  264. }
  265. static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
  266. {
  267. debugfs_remove_recursive(component->debugfs_root);
  268. }
  269. static void soc_init_codec_debugfs(struct snd_soc_component *component)
  270. {
  271. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  272. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  273. codec->component.debugfs_root,
  274. codec, &codec_reg_fops);
  275. if (!codec->debugfs_reg)
  276. dev_warn(codec->dev,
  277. "ASoC: Failed to create codec register debugfs file\n");
  278. }
  279. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  280. size_t count, loff_t *ppos)
  281. {
  282. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  283. ssize_t len, ret = 0;
  284. struct snd_soc_codec *codec;
  285. if (!buf)
  286. return -ENOMEM;
  287. mutex_lock(&client_mutex);
  288. list_for_each_entry(codec, &codec_list, list) {
  289. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  290. codec->component.name);
  291. if (len >= 0)
  292. ret += len;
  293. if (ret > PAGE_SIZE) {
  294. ret = PAGE_SIZE;
  295. break;
  296. }
  297. }
  298. mutex_unlock(&client_mutex);
  299. if (ret >= 0)
  300. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  301. kfree(buf);
  302. return ret;
  303. }
  304. static const struct file_operations codec_list_fops = {
  305. .read = codec_list_read_file,
  306. .llseek = default_llseek,/* read accesses f_pos */
  307. };
  308. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  309. size_t count, loff_t *ppos)
  310. {
  311. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  312. ssize_t len, ret = 0;
  313. struct snd_soc_component *component;
  314. struct snd_soc_dai *dai;
  315. if (!buf)
  316. return -ENOMEM;
  317. mutex_lock(&client_mutex);
  318. list_for_each_entry(component, &component_list, list) {
  319. list_for_each_entry(dai, &component->dai_list, list) {
  320. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  321. dai->name);
  322. if (len >= 0)
  323. ret += len;
  324. if (ret > PAGE_SIZE) {
  325. ret = PAGE_SIZE;
  326. break;
  327. }
  328. }
  329. }
  330. mutex_unlock(&client_mutex);
  331. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  332. kfree(buf);
  333. return ret;
  334. }
  335. static const struct file_operations dai_list_fops = {
  336. .read = dai_list_read_file,
  337. .llseek = default_llseek,/* read accesses f_pos */
  338. };
  339. static ssize_t platform_list_read_file(struct file *file,
  340. char __user *user_buf,
  341. size_t count, loff_t *ppos)
  342. {
  343. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  344. ssize_t len, ret = 0;
  345. struct snd_soc_platform *platform;
  346. if (!buf)
  347. return -ENOMEM;
  348. mutex_lock(&client_mutex);
  349. list_for_each_entry(platform, &platform_list, list) {
  350. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  351. platform->component.name);
  352. if (len >= 0)
  353. ret += len;
  354. if (ret > PAGE_SIZE) {
  355. ret = PAGE_SIZE;
  356. break;
  357. }
  358. }
  359. mutex_unlock(&client_mutex);
  360. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  361. kfree(buf);
  362. return ret;
  363. }
  364. static const struct file_operations platform_list_fops = {
  365. .read = platform_list_read_file,
  366. .llseek = default_llseek,/* read accesses f_pos */
  367. };
  368. static void soc_init_card_debugfs(struct snd_soc_card *card)
  369. {
  370. if (!snd_soc_debugfs_root)
  371. return;
  372. card->debugfs_card_root = debugfs_create_dir(card->name,
  373. snd_soc_debugfs_root);
  374. if (!card->debugfs_card_root) {
  375. dev_warn(card->dev,
  376. "ASoC: Failed to create card debugfs directory\n");
  377. return;
  378. }
  379. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  380. card->debugfs_card_root,
  381. &card->pop_time);
  382. if (!card->debugfs_pop_time)
  383. dev_warn(card->dev,
  384. "ASoC: Failed to create pop time debugfs file\n");
  385. }
  386. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  387. {
  388. debugfs_remove_recursive(card->debugfs_card_root);
  389. }
  390. static void snd_soc_debugfs_init(void)
  391. {
  392. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  393. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  394. pr_warn("ASoC: Failed to create debugfs directory\n");
  395. snd_soc_debugfs_root = NULL;
  396. return;
  397. }
  398. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  399. &codec_list_fops))
  400. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  401. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  402. &dai_list_fops))
  403. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  404. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  405. &platform_list_fops))
  406. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  407. }
  408. static void snd_soc_debugfs_exit(void)
  409. {
  410. debugfs_remove_recursive(snd_soc_debugfs_root);
  411. }
  412. #else
  413. #define soc_init_codec_debugfs NULL
  414. static inline void soc_init_component_debugfs(
  415. struct snd_soc_component *component)
  416. {
  417. }
  418. static inline void soc_cleanup_component_debugfs(
  419. struct snd_soc_component *component)
  420. {
  421. }
  422. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  423. {
  424. }
  425. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  426. {
  427. }
  428. static inline void snd_soc_debugfs_init(void)
  429. {
  430. }
  431. static inline void snd_soc_debugfs_exit(void)
  432. {
  433. }
  434. #endif
  435. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  436. const char *dai_link, int stream)
  437. {
  438. struct snd_soc_pcm_runtime *rtd;
  439. list_for_each_entry(rtd, &card->rtd_list, list) {
  440. if (rtd->dai_link->no_pcm &&
  441. !strcmp(rtd->dai_link->name, dai_link))
  442. return rtd->pcm->streams[stream].substream;
  443. }
  444. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  445. return NULL;
  446. }
  447. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  448. static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
  449. struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
  450. {
  451. struct snd_soc_pcm_runtime *rtd;
  452. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  453. if (!rtd)
  454. return NULL;
  455. rtd->card = card;
  456. rtd->dai_link = dai_link;
  457. rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
  458. dai_link->num_codecs,
  459. GFP_KERNEL);
  460. if (!rtd->codec_dais) {
  461. kfree(rtd);
  462. return NULL;
  463. }
  464. return rtd;
  465. }
  466. static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
  467. {
  468. if (rtd && rtd->codec_dais)
  469. kfree(rtd->codec_dais);
  470. kfree(rtd);
  471. }
  472. static void soc_add_pcm_runtime(struct snd_soc_card *card,
  473. struct snd_soc_pcm_runtime *rtd)
  474. {
  475. list_add_tail(&rtd->list, &card->rtd_list);
  476. rtd->num = card->num_rtd;
  477. card->num_rtd++;
  478. }
  479. static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
  480. {
  481. struct snd_soc_pcm_runtime *rtd, *_rtd;
  482. list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
  483. list_del(&rtd->list);
  484. soc_free_pcm_runtime(rtd);
  485. }
  486. card->num_rtd = 0;
  487. }
  488. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  489. const char *dai_link)
  490. {
  491. struct snd_soc_pcm_runtime *rtd;
  492. list_for_each_entry(rtd, &card->rtd_list, list) {
  493. if (!strcmp(rtd->dai_link->name, dai_link))
  494. return rtd;
  495. }
  496. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  497. return NULL;
  498. }
  499. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  500. static void codec2codec_close_delayed_work(struct work_struct *work)
  501. {
  502. /* Currently nothing to do for c2c links
  503. * Since c2c links are internal nodes in the DAPM graph and
  504. * don't interface with the outside world or application layer
  505. * we don't have to do any special handling on close.
  506. */
  507. }
  508. #ifdef CONFIG_PM_SLEEP
  509. /* powers down audio subsystem for suspend */
  510. int snd_soc_suspend(struct device *dev)
  511. {
  512. struct snd_soc_card *card = dev_get_drvdata(dev);
  513. struct snd_soc_codec *codec;
  514. struct snd_soc_pcm_runtime *rtd;
  515. int i;
  516. /* If the card is not initialized yet there is nothing to do */
  517. if (!card->instantiated)
  518. return 0;
  519. /* Due to the resume being scheduled into a workqueue we could
  520. * suspend before that's finished - wait for it to complete.
  521. */
  522. snd_power_lock(card->snd_card);
  523. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  524. snd_power_unlock(card->snd_card);
  525. /* we're going to block userspace touching us until resume completes */
  526. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  527. /* mute any active DACs */
  528. list_for_each_entry(rtd, &card->rtd_list, list) {
  529. if (rtd->dai_link->ignore_suspend)
  530. continue;
  531. for (i = 0; i < rtd->num_codecs; i++) {
  532. struct snd_soc_dai *dai = rtd->codec_dais[i];
  533. struct snd_soc_dai_driver *drv = dai->driver;
  534. if (drv->ops->digital_mute && dai->playback_active)
  535. drv->ops->digital_mute(dai, 1);
  536. }
  537. }
  538. /* suspend all pcms */
  539. list_for_each_entry(rtd, &card->rtd_list, list) {
  540. if (rtd->dai_link->ignore_suspend)
  541. continue;
  542. snd_pcm_suspend_all(rtd->pcm);
  543. }
  544. if (card->suspend_pre)
  545. card->suspend_pre(card);
  546. list_for_each_entry(rtd, &card->rtd_list, list) {
  547. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  548. if (rtd->dai_link->ignore_suspend)
  549. continue;
  550. if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
  551. cpu_dai->driver->suspend(cpu_dai);
  552. }
  553. /* close any waiting streams */
  554. list_for_each_entry(rtd, &card->rtd_list, list)
  555. flush_delayed_work(&rtd->delayed_work);
  556. list_for_each_entry(rtd, &card->rtd_list, list) {
  557. if (rtd->dai_link->ignore_suspend)
  558. continue;
  559. snd_soc_dapm_stream_event(rtd,
  560. SNDRV_PCM_STREAM_PLAYBACK,
  561. SND_SOC_DAPM_STREAM_SUSPEND);
  562. snd_soc_dapm_stream_event(rtd,
  563. SNDRV_PCM_STREAM_CAPTURE,
  564. SND_SOC_DAPM_STREAM_SUSPEND);
  565. }
  566. /* Recheck all endpoints too, their state is affected by suspend */
  567. dapm_mark_endpoints_dirty(card);
  568. snd_soc_dapm_sync(&card->dapm);
  569. /* suspend all CODECs */
  570. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  571. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  572. /* If there are paths active then the CODEC will be held with
  573. * bias _ON and should not be suspended. */
  574. if (!codec->suspended) {
  575. switch (snd_soc_dapm_get_bias_level(dapm)) {
  576. case SND_SOC_BIAS_STANDBY:
  577. /*
  578. * If the CODEC is capable of idle
  579. * bias off then being in STANDBY
  580. * means it's doing something,
  581. * otherwise fall through.
  582. */
  583. if (dapm->idle_bias_off) {
  584. dev_dbg(codec->dev,
  585. "ASoC: idle_bias_off CODEC on over suspend\n");
  586. break;
  587. }
  588. case SND_SOC_BIAS_OFF:
  589. if (codec->driver->suspend)
  590. codec->driver->suspend(codec);
  591. codec->suspended = 1;
  592. if (codec->component.regmap)
  593. regcache_mark_dirty(codec->component.regmap);
  594. /* deactivate pins to sleep state */
  595. pinctrl_pm_select_sleep_state(codec->dev);
  596. break;
  597. default:
  598. dev_dbg(codec->dev,
  599. "ASoC: CODEC is on over suspend\n");
  600. break;
  601. }
  602. }
  603. }
  604. list_for_each_entry(rtd, &card->rtd_list, list) {
  605. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  606. if (rtd->dai_link->ignore_suspend)
  607. continue;
  608. if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
  609. cpu_dai->driver->suspend(cpu_dai);
  610. /* deactivate pins to sleep state */
  611. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  612. }
  613. if (card->suspend_post)
  614. card->suspend_post(card);
  615. return 0;
  616. }
  617. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  618. /* deferred resume work, so resume can complete before we finished
  619. * setting our codec back up, which can be very slow on I2C
  620. */
  621. static void soc_resume_deferred(struct work_struct *work)
  622. {
  623. struct snd_soc_card *card =
  624. container_of(work, struct snd_soc_card, deferred_resume_work);
  625. struct snd_soc_pcm_runtime *rtd;
  626. struct snd_soc_codec *codec;
  627. int i;
  628. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  629. * so userspace apps are blocked from touching us
  630. */
  631. dev_dbg(card->dev, "ASoC: starting resume work\n");
  632. /* Bring us up into D2 so that DAPM starts enabling things */
  633. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  634. if (card->resume_pre)
  635. card->resume_pre(card);
  636. /* resume control bus DAIs */
  637. list_for_each_entry(rtd, &card->rtd_list, list) {
  638. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  639. if (rtd->dai_link->ignore_suspend)
  640. continue;
  641. if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
  642. cpu_dai->driver->resume(cpu_dai);
  643. }
  644. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  645. if (codec->suspended) {
  646. if (codec->driver->resume)
  647. codec->driver->resume(codec);
  648. codec->suspended = 0;
  649. }
  650. }
  651. list_for_each_entry(rtd, &card->rtd_list, list) {
  652. if (rtd->dai_link->ignore_suspend)
  653. continue;
  654. snd_soc_dapm_stream_event(rtd,
  655. SNDRV_PCM_STREAM_PLAYBACK,
  656. SND_SOC_DAPM_STREAM_RESUME);
  657. snd_soc_dapm_stream_event(rtd,
  658. SNDRV_PCM_STREAM_CAPTURE,
  659. SND_SOC_DAPM_STREAM_RESUME);
  660. }
  661. /* unmute any active DACs */
  662. list_for_each_entry(rtd, &card->rtd_list, list) {
  663. if (rtd->dai_link->ignore_suspend)
  664. continue;
  665. for (i = 0; i < rtd->num_codecs; i++) {
  666. struct snd_soc_dai *dai = rtd->codec_dais[i];
  667. struct snd_soc_dai_driver *drv = dai->driver;
  668. if (drv->ops->digital_mute && dai->playback_active)
  669. drv->ops->digital_mute(dai, 0);
  670. }
  671. }
  672. list_for_each_entry(rtd, &card->rtd_list, list) {
  673. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  674. if (rtd->dai_link->ignore_suspend)
  675. continue;
  676. if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
  677. cpu_dai->driver->resume(cpu_dai);
  678. }
  679. if (card->resume_post)
  680. card->resume_post(card);
  681. dev_dbg(card->dev, "ASoC: resume work completed\n");
  682. /* Recheck all endpoints too, their state is affected by suspend */
  683. dapm_mark_endpoints_dirty(card);
  684. snd_soc_dapm_sync(&card->dapm);
  685. /* userspace can access us now we are back as we were before */
  686. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  687. }
  688. /* powers up audio subsystem after a suspend */
  689. int snd_soc_resume(struct device *dev)
  690. {
  691. struct snd_soc_card *card = dev_get_drvdata(dev);
  692. bool bus_control = false;
  693. struct snd_soc_pcm_runtime *rtd;
  694. /* If the card is not initialized yet there is nothing to do */
  695. if (!card->instantiated)
  696. return 0;
  697. /* activate pins from sleep state */
  698. list_for_each_entry(rtd, &card->rtd_list, list) {
  699. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  700. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  701. int j;
  702. if (cpu_dai->active)
  703. pinctrl_pm_select_default_state(cpu_dai->dev);
  704. for (j = 0; j < rtd->num_codecs; j++) {
  705. struct snd_soc_dai *codec_dai = codec_dais[j];
  706. if (codec_dai->active)
  707. pinctrl_pm_select_default_state(codec_dai->dev);
  708. }
  709. }
  710. /*
  711. * DAIs that also act as the control bus master might have other drivers
  712. * hanging off them so need to resume immediately. Other drivers don't
  713. * have that problem and may take a substantial amount of time to resume
  714. * due to I/O costs and anti-pop so handle them out of line.
  715. */
  716. list_for_each_entry(rtd, &card->rtd_list, list) {
  717. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  718. bus_control |= cpu_dai->driver->bus_control;
  719. }
  720. if (bus_control) {
  721. dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
  722. soc_resume_deferred(&card->deferred_resume_work);
  723. } else {
  724. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  725. if (!schedule_work(&card->deferred_resume_work))
  726. dev_err(dev, "ASoC: resume work item may be lost\n");
  727. }
  728. return 0;
  729. }
  730. EXPORT_SYMBOL_GPL(snd_soc_resume);
  731. #else
  732. #define snd_soc_suspend NULL
  733. #define snd_soc_resume NULL
  734. #endif
  735. static const struct snd_soc_dai_ops null_dai_ops = {
  736. };
  737. static struct snd_soc_component *soc_find_component(
  738. const struct device_node *of_node, const char *name)
  739. {
  740. struct snd_soc_component *component;
  741. lockdep_assert_held(&client_mutex);
  742. list_for_each_entry(component, &component_list, list) {
  743. if (of_node) {
  744. if (component->dev->of_node == of_node)
  745. return component;
  746. } else if (strcmp(component->name, name) == 0) {
  747. return component;
  748. }
  749. }
  750. return NULL;
  751. }
  752. /**
  753. * snd_soc_find_dai - Find a registered DAI
  754. *
  755. * @dlc: name of the DAI and optional component info to match
  756. *
  757. * This function will search all regsitered components and their DAIs to
  758. * find the DAI of the same name. The component's of_node and name
  759. * should also match if being specified.
  760. *
  761. * Return: pointer of DAI, or NULL if not found.
  762. */
  763. struct snd_soc_dai *snd_soc_find_dai(
  764. const struct snd_soc_dai_link_component *dlc)
  765. {
  766. struct snd_soc_component *component;
  767. struct snd_soc_dai *dai;
  768. struct device_node *component_of_node;
  769. lockdep_assert_held(&client_mutex);
  770. /* Find CPU DAI from registered DAIs*/
  771. list_for_each_entry(component, &component_list, list) {
  772. component_of_node = component->dev->of_node;
  773. if (!component_of_node && component->dev->parent)
  774. component_of_node = component->dev->parent->of_node;
  775. if (dlc->of_node && component_of_node != dlc->of_node)
  776. continue;
  777. if (dlc->name && strcmp(component->name, dlc->name))
  778. continue;
  779. list_for_each_entry(dai, &component->dai_list, list) {
  780. if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
  781. continue;
  782. return dai;
  783. }
  784. }
  785. return NULL;
  786. }
  787. EXPORT_SYMBOL_GPL(snd_soc_find_dai);
  788. static bool soc_is_dai_link_bound(struct snd_soc_card *card,
  789. struct snd_soc_dai_link *dai_link)
  790. {
  791. struct snd_soc_pcm_runtime *rtd;
  792. list_for_each_entry(rtd, &card->rtd_list, list) {
  793. if (rtd->dai_link == dai_link)
  794. return true;
  795. }
  796. return false;
  797. }
  798. static int soc_bind_dai_link(struct snd_soc_card *card,
  799. struct snd_soc_dai_link *dai_link)
  800. {
  801. struct snd_soc_pcm_runtime *rtd;
  802. struct snd_soc_dai_link_component *codecs = dai_link->codecs;
  803. struct snd_soc_dai_link_component cpu_dai_component;
  804. struct snd_soc_dai **codec_dais;
  805. struct snd_soc_platform *platform;
  806. const char *platform_name;
  807. int i;
  808. dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
  809. if (soc_is_dai_link_bound(card, dai_link)) {
  810. dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
  811. dai_link->name);
  812. return 0;
  813. }
  814. rtd = soc_new_pcm_runtime(card, dai_link);
  815. if (!rtd)
  816. return -ENOMEM;
  817. cpu_dai_component.name = dai_link->cpu_name;
  818. cpu_dai_component.of_node = dai_link->cpu_of_node;
  819. cpu_dai_component.dai_name = dai_link->cpu_dai_name;
  820. rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
  821. if (!rtd->cpu_dai) {
  822. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  823. dai_link->cpu_dai_name);
  824. goto _err_defer;
  825. }
  826. rtd->num_codecs = dai_link->num_codecs;
  827. /* Find CODEC from registered CODECs */
  828. codec_dais = rtd->codec_dais;
  829. for (i = 0; i < rtd->num_codecs; i++) {
  830. codec_dais[i] = snd_soc_find_dai(&codecs[i]);
  831. if (!codec_dais[i]) {
  832. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  833. codecs[i].dai_name);
  834. goto _err_defer;
  835. }
  836. }
  837. /* Single codec links expect codec and codec_dai in runtime data */
  838. rtd->codec_dai = codec_dais[0];
  839. rtd->codec = rtd->codec_dai->codec;
  840. /* if there's no platform we match on the empty platform */
  841. platform_name = dai_link->platform_name;
  842. if (!platform_name && !dai_link->platform_of_node)
  843. platform_name = "snd-soc-dummy";
  844. /* find one from the set of registered platforms */
  845. list_for_each_entry(platform, &platform_list, list) {
  846. if (dai_link->platform_of_node) {
  847. if (platform->dev->of_node !=
  848. dai_link->platform_of_node)
  849. continue;
  850. } else {
  851. if (strcmp(platform->component.name, platform_name))
  852. continue;
  853. }
  854. rtd->platform = platform;
  855. }
  856. if (!rtd->platform) {
  857. dev_err(card->dev, "ASoC: platform %s not registered\n",
  858. dai_link->platform_name);
  859. goto _err_defer;
  860. }
  861. soc_add_pcm_runtime(card, rtd);
  862. return 0;
  863. _err_defer:
  864. soc_free_pcm_runtime(rtd);
  865. return -EPROBE_DEFER;
  866. }
  867. static void soc_remove_component(struct snd_soc_component *component)
  868. {
  869. if (!component->card)
  870. return;
  871. /* This is a HACK and will be removed soon */
  872. if (component->codec)
  873. list_del(&component->codec->card_list);
  874. if (component->remove)
  875. component->remove(component);
  876. snd_soc_dapm_free(snd_soc_component_get_dapm(component));
  877. soc_cleanup_component_debugfs(component);
  878. component->card = NULL;
  879. module_put(component->dev->driver->owner);
  880. }
  881. static void soc_remove_dai(struct snd_soc_dai *dai, int order)
  882. {
  883. int err;
  884. if (dai && dai->probed &&
  885. dai->driver->remove_order == order) {
  886. if (dai->driver->remove) {
  887. err = dai->driver->remove(dai);
  888. if (err < 0)
  889. dev_err(dai->dev,
  890. "ASoC: failed to remove %s: %d\n",
  891. dai->name, err);
  892. }
  893. dai->probed = 0;
  894. }
  895. }
  896. static void soc_remove_link_dais(struct snd_soc_card *card,
  897. struct snd_soc_pcm_runtime *rtd, int order)
  898. {
  899. int i;
  900. /* unregister the rtd device */
  901. if (rtd->dev_registered) {
  902. device_unregister(rtd->dev);
  903. rtd->dev_registered = 0;
  904. }
  905. /* remove the CODEC DAI */
  906. for (i = 0; i < rtd->num_codecs; i++)
  907. soc_remove_dai(rtd->codec_dais[i], order);
  908. soc_remove_dai(rtd->cpu_dai, order);
  909. }
  910. static void soc_remove_link_components(struct snd_soc_card *card,
  911. struct snd_soc_pcm_runtime *rtd, int order)
  912. {
  913. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  914. struct snd_soc_platform *platform = rtd->platform;
  915. struct snd_soc_component *component;
  916. int i;
  917. /* remove the platform */
  918. if (platform && platform->component.driver->remove_order == order)
  919. soc_remove_component(&platform->component);
  920. /* remove the CODEC-side CODEC */
  921. for (i = 0; i < rtd->num_codecs; i++) {
  922. component = rtd->codec_dais[i]->component;
  923. if (component->driver->remove_order == order)
  924. soc_remove_component(component);
  925. }
  926. /* remove any CPU-side CODEC */
  927. if (cpu_dai) {
  928. if (cpu_dai->component->driver->remove_order == order)
  929. soc_remove_component(cpu_dai->component);
  930. }
  931. }
  932. static void soc_remove_dai_links(struct snd_soc_card *card)
  933. {
  934. int order;
  935. struct snd_soc_pcm_runtime *rtd;
  936. struct snd_soc_dai_link *link, *_link;
  937. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  938. order++) {
  939. list_for_each_entry(rtd, &card->rtd_list, list)
  940. soc_remove_link_dais(card, rtd, order);
  941. }
  942. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  943. order++) {
  944. list_for_each_entry(rtd, &card->rtd_list, list)
  945. soc_remove_link_components(card, rtd, order);
  946. }
  947. list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
  948. if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
  949. dev_warn(card->dev, "Topology forgot to remove link %s?\n",
  950. link->name);
  951. list_del(&link->list);
  952. card->num_dai_links--;
  953. }
  954. }
  955. static int snd_soc_init_multicodec(struct snd_soc_card *card,
  956. struct snd_soc_dai_link *dai_link)
  957. {
  958. /* Legacy codec/codec_dai link is a single entry in multicodec */
  959. if (dai_link->codec_name || dai_link->codec_of_node ||
  960. dai_link->codec_dai_name) {
  961. dai_link->num_codecs = 1;
  962. dai_link->codecs = devm_kzalloc(card->dev,
  963. sizeof(struct snd_soc_dai_link_component),
  964. GFP_KERNEL);
  965. if (!dai_link->codecs)
  966. return -ENOMEM;
  967. dai_link->codecs[0].name = dai_link->codec_name;
  968. dai_link->codecs[0].of_node = dai_link->codec_of_node;
  969. dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
  970. }
  971. if (!dai_link->codecs) {
  972. dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
  973. return -EINVAL;
  974. }
  975. return 0;
  976. }
  977. static int soc_init_dai_link(struct snd_soc_card *card,
  978. struct snd_soc_dai_link *link)
  979. {
  980. int i, ret;
  981. ret = snd_soc_init_multicodec(card, link);
  982. if (ret) {
  983. dev_err(card->dev, "ASoC: failed to init multicodec\n");
  984. return ret;
  985. }
  986. for (i = 0; i < link->num_codecs; i++) {
  987. /*
  988. * Codec must be specified by 1 of name or OF node,
  989. * not both or neither.
  990. */
  991. if (!!link->codecs[i].name ==
  992. !!link->codecs[i].of_node) {
  993. dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
  994. link->name);
  995. return -EINVAL;
  996. }
  997. /* Codec DAI name must be specified */
  998. if (!link->codecs[i].dai_name) {
  999. dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
  1000. link->name);
  1001. return -EINVAL;
  1002. }
  1003. }
  1004. /*
  1005. * Platform may be specified by either name or OF node, but
  1006. * can be left unspecified, and a dummy platform will be used.
  1007. */
  1008. if (link->platform_name && link->platform_of_node) {
  1009. dev_err(card->dev,
  1010. "ASoC: Both platform name/of_node are set for %s\n",
  1011. link->name);
  1012. return -EINVAL;
  1013. }
  1014. /*
  1015. * CPU device may be specified by either name or OF node, but
  1016. * can be left unspecified, and will be matched based on DAI
  1017. * name alone..
  1018. */
  1019. if (link->cpu_name && link->cpu_of_node) {
  1020. dev_err(card->dev,
  1021. "ASoC: Neither/both cpu name/of_node are set for %s\n",
  1022. link->name);
  1023. return -EINVAL;
  1024. }
  1025. /*
  1026. * At least one of CPU DAI name or CPU device name/node must be
  1027. * specified
  1028. */
  1029. if (!link->cpu_dai_name &&
  1030. !(link->cpu_name || link->cpu_of_node)) {
  1031. dev_err(card->dev,
  1032. "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
  1033. link->name);
  1034. return -EINVAL;
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * snd_soc_add_dai_link - Add a DAI link dynamically
  1040. * @card: The ASoC card to which the DAI link is added
  1041. * @dai_link: The new DAI link to add
  1042. *
  1043. * This function adds a DAI link to the ASoC card's link list.
  1044. *
  1045. * Note: Topology can use this API to add DAI links when probing the
  1046. * topology component. And machine drivers can still define static
  1047. * DAI links in dai_link array.
  1048. */
  1049. int snd_soc_add_dai_link(struct snd_soc_card *card,
  1050. struct snd_soc_dai_link *dai_link)
  1051. {
  1052. if (dai_link->dobj.type
  1053. && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
  1054. dev_err(card->dev, "Invalid dai link type %d\n",
  1055. dai_link->dobj.type);
  1056. return -EINVAL;
  1057. }
  1058. lockdep_assert_held(&client_mutex);
  1059. /* Notify the machine driver for extra initialization
  1060. * on the link created by topology.
  1061. */
  1062. if (dai_link->dobj.type && card->add_dai_link)
  1063. card->add_dai_link(card, dai_link);
  1064. list_add_tail(&dai_link->list, &card->dai_link_list);
  1065. card->num_dai_links++;
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
  1069. /**
  1070. * snd_soc_remove_dai_link - Remove a DAI link from the list
  1071. * @card: The ASoC card that owns the link
  1072. * @dai_link: The DAI link to remove
  1073. *
  1074. * This function removes a DAI link from the ASoC card's link list.
  1075. *
  1076. * For DAI links previously added by topology, topology should
  1077. * remove them by using the dobj embedded in the link.
  1078. */
  1079. void snd_soc_remove_dai_link(struct snd_soc_card *card,
  1080. struct snd_soc_dai_link *dai_link)
  1081. {
  1082. struct snd_soc_dai_link *link, *_link;
  1083. if (dai_link->dobj.type
  1084. && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
  1085. dev_err(card->dev, "Invalid dai link type %d\n",
  1086. dai_link->dobj.type);
  1087. return;
  1088. }
  1089. lockdep_assert_held(&client_mutex);
  1090. /* Notify the machine driver for extra destruction
  1091. * on the link created by topology.
  1092. */
  1093. if (dai_link->dobj.type && card->remove_dai_link)
  1094. card->remove_dai_link(card, dai_link);
  1095. list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
  1096. if (link == dai_link) {
  1097. list_del(&link->list);
  1098. card->num_dai_links--;
  1099. return;
  1100. }
  1101. }
  1102. }
  1103. EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
  1104. static void soc_set_name_prefix(struct snd_soc_card *card,
  1105. struct snd_soc_component *component)
  1106. {
  1107. int i;
  1108. if (card->codec_conf == NULL)
  1109. return;
  1110. for (i = 0; i < card->num_configs; i++) {
  1111. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  1112. if (map->of_node && component->dev->of_node != map->of_node)
  1113. continue;
  1114. if (map->dev_name && strcmp(component->name, map->dev_name))
  1115. continue;
  1116. component->name_prefix = map->name_prefix;
  1117. break;
  1118. }
  1119. }
  1120. static int soc_probe_component(struct snd_soc_card *card,
  1121. struct snd_soc_component *component)
  1122. {
  1123. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  1124. struct snd_soc_dai *dai;
  1125. int ret;
  1126. if (!strcmp(component->name, "snd-soc-dummy"))
  1127. return 0;
  1128. if (component->card) {
  1129. if (component->card != card) {
  1130. dev_err(component->dev,
  1131. "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
  1132. card->name, component->card->name);
  1133. return -ENODEV;
  1134. }
  1135. return 0;
  1136. }
  1137. if (!try_module_get(component->dev->driver->owner))
  1138. return -ENODEV;
  1139. component->card = card;
  1140. dapm->card = card;
  1141. soc_set_name_prefix(card, component);
  1142. soc_init_component_debugfs(component);
  1143. if (component->dapm_widgets) {
  1144. ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
  1145. component->num_dapm_widgets);
  1146. if (ret != 0) {
  1147. dev_err(component->dev,
  1148. "Failed to create new controls %d\n", ret);
  1149. goto err_probe;
  1150. }
  1151. }
  1152. list_for_each_entry(dai, &component->dai_list, list) {
  1153. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  1154. if (ret != 0) {
  1155. dev_err(component->dev,
  1156. "Failed to create DAI widgets %d\n", ret);
  1157. goto err_probe;
  1158. }
  1159. }
  1160. if (component->probe) {
  1161. ret = component->probe(component);
  1162. if (ret < 0) {
  1163. dev_err(component->dev,
  1164. "ASoC: failed to probe component %d\n", ret);
  1165. goto err_probe;
  1166. }
  1167. WARN(dapm->idle_bias_off &&
  1168. dapm->bias_level != SND_SOC_BIAS_OFF,
  1169. "codec %s can not start from non-off bias with idle_bias_off==1\n",
  1170. component->name);
  1171. }
  1172. /* machine specific init */
  1173. if (component->init) {
  1174. ret = component->init(component);
  1175. if (ret < 0) {
  1176. dev_err(component->dev,
  1177. "Failed to do machine specific init %d\n", ret);
  1178. goto err_probe;
  1179. }
  1180. }
  1181. if (component->controls)
  1182. snd_soc_add_component_controls(component, component->controls,
  1183. component->num_controls);
  1184. if (component->dapm_routes)
  1185. snd_soc_dapm_add_routes(dapm, component->dapm_routes,
  1186. component->num_dapm_routes);
  1187. list_add(&dapm->list, &card->dapm_list);
  1188. /* This is a HACK and will be removed soon */
  1189. if (component->codec)
  1190. list_add(&component->codec->card_list, &card->codec_dev_list);
  1191. return 0;
  1192. err_probe:
  1193. soc_cleanup_component_debugfs(component);
  1194. component->card = NULL;
  1195. module_put(component->dev->driver->owner);
  1196. return ret;
  1197. }
  1198. static void rtd_release(struct device *dev)
  1199. {
  1200. kfree(dev);
  1201. }
  1202. static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
  1203. const char *name)
  1204. {
  1205. int ret = 0;
  1206. /* register the rtd device */
  1207. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1208. if (!rtd->dev)
  1209. return -ENOMEM;
  1210. device_initialize(rtd->dev);
  1211. rtd->dev->parent = rtd->card->dev;
  1212. rtd->dev->release = rtd_release;
  1213. rtd->dev->groups = soc_dev_attr_groups;
  1214. dev_set_name(rtd->dev, "%s", name);
  1215. dev_set_drvdata(rtd->dev, rtd);
  1216. mutex_init(&rtd->pcm_mutex);
  1217. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1218. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1219. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1220. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1221. ret = device_add(rtd->dev);
  1222. if (ret < 0) {
  1223. /* calling put_device() here to free the rtd->dev */
  1224. put_device(rtd->dev);
  1225. dev_err(rtd->card->dev,
  1226. "ASoC: failed to register runtime device: %d\n", ret);
  1227. return ret;
  1228. }
  1229. rtd->dev_registered = 1;
  1230. return 0;
  1231. }
  1232. static int soc_probe_link_components(struct snd_soc_card *card,
  1233. struct snd_soc_pcm_runtime *rtd,
  1234. int order)
  1235. {
  1236. struct snd_soc_platform *platform = rtd->platform;
  1237. struct snd_soc_component *component;
  1238. int i, ret;
  1239. /* probe the CPU-side component, if it is a CODEC */
  1240. component = rtd->cpu_dai->component;
  1241. if (component->driver->probe_order == order) {
  1242. ret = soc_probe_component(card, component);
  1243. if (ret < 0)
  1244. return ret;
  1245. }
  1246. /* probe the CODEC-side components */
  1247. for (i = 0; i < rtd->num_codecs; i++) {
  1248. component = rtd->codec_dais[i]->component;
  1249. if (component->driver->probe_order == order) {
  1250. ret = soc_probe_component(card, component);
  1251. if (ret < 0)
  1252. return ret;
  1253. }
  1254. }
  1255. /* probe the platform */
  1256. if (platform->component.driver->probe_order == order) {
  1257. ret = soc_probe_component(card, &platform->component);
  1258. if (ret < 0)
  1259. return ret;
  1260. }
  1261. return 0;
  1262. }
  1263. static int soc_probe_dai(struct snd_soc_dai *dai, int order)
  1264. {
  1265. int ret;
  1266. if (!dai->probed && dai->driver->probe_order == order) {
  1267. if (dai->driver->probe) {
  1268. ret = dai->driver->probe(dai);
  1269. if (ret < 0) {
  1270. dev_err(dai->dev,
  1271. "ASoC: failed to probe DAI %s: %d\n",
  1272. dai->name, ret);
  1273. return ret;
  1274. }
  1275. }
  1276. dai->probed = 1;
  1277. }
  1278. return 0;
  1279. }
  1280. static int soc_link_dai_widgets(struct snd_soc_card *card,
  1281. struct snd_soc_dai_link *dai_link,
  1282. struct snd_soc_pcm_runtime *rtd)
  1283. {
  1284. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1285. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1286. struct snd_soc_dapm_widget *sink, *source;
  1287. int ret;
  1288. if (rtd->num_codecs > 1)
  1289. dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
  1290. /* link the DAI widgets */
  1291. sink = codec_dai->playback_widget;
  1292. source = cpu_dai->capture_widget;
  1293. if (sink && source) {
  1294. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1295. dai_link->num_params,
  1296. source, sink);
  1297. if (ret != 0) {
  1298. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1299. sink->name, source->name, ret);
  1300. return ret;
  1301. }
  1302. }
  1303. sink = cpu_dai->playback_widget;
  1304. source = codec_dai->capture_widget;
  1305. if (sink && source) {
  1306. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1307. dai_link->num_params,
  1308. source, sink);
  1309. if (ret != 0) {
  1310. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1311. sink->name, source->name, ret);
  1312. return ret;
  1313. }
  1314. }
  1315. return 0;
  1316. }
  1317. static int soc_probe_link_dais(struct snd_soc_card *card,
  1318. struct snd_soc_pcm_runtime *rtd, int order)
  1319. {
  1320. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  1321. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1322. int i, ret;
  1323. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1324. card->name, rtd->num, order);
  1325. /* set default power off timeout */
  1326. rtd->pmdown_time = pmdown_time;
  1327. ret = soc_probe_dai(cpu_dai, order);
  1328. if (ret)
  1329. return ret;
  1330. /* probe the CODEC DAI */
  1331. for (i = 0; i < rtd->num_codecs; i++) {
  1332. ret = soc_probe_dai(rtd->codec_dais[i], order);
  1333. if (ret)
  1334. return ret;
  1335. }
  1336. /* complete DAI probe during last probe */
  1337. if (order != SND_SOC_COMP_ORDER_LAST)
  1338. return 0;
  1339. /* do machine specific initialization */
  1340. if (dai_link->init) {
  1341. ret = dai_link->init(rtd);
  1342. if (ret < 0) {
  1343. dev_err(card->dev, "ASoC: failed to init %s: %d\n",
  1344. dai_link->name, ret);
  1345. return ret;
  1346. }
  1347. }
  1348. if (dai_link->dai_fmt)
  1349. snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
  1350. ret = soc_post_component_init(rtd, dai_link->name);
  1351. if (ret)
  1352. return ret;
  1353. #ifdef CONFIG_DEBUG_FS
  1354. /* add DPCM sysfs entries */
  1355. if (dai_link->dynamic)
  1356. soc_dpcm_debugfs_add(rtd);
  1357. #endif
  1358. if (cpu_dai->driver->compress_new) {
  1359. /*create compress_device"*/
  1360. ret = cpu_dai->driver->compress_new(rtd, rtd->num);
  1361. if (ret < 0) {
  1362. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1363. dai_link->stream_name);
  1364. return ret;
  1365. }
  1366. } else {
  1367. if (!dai_link->params) {
  1368. /* create the pcm */
  1369. ret = soc_new_pcm(rtd, rtd->num);
  1370. if (ret < 0) {
  1371. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1372. dai_link->stream_name, ret);
  1373. return ret;
  1374. }
  1375. } else {
  1376. INIT_DELAYED_WORK(&rtd->delayed_work,
  1377. codec2codec_close_delayed_work);
  1378. /* link the DAI widgets */
  1379. ret = soc_link_dai_widgets(card, dai_link, rtd);
  1380. if (ret)
  1381. return ret;
  1382. }
  1383. }
  1384. return 0;
  1385. }
  1386. static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
  1387. {
  1388. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1389. struct snd_soc_component *component;
  1390. const char *name;
  1391. struct device_node *codec_of_node;
  1392. if (aux_dev->codec_of_node || aux_dev->codec_name) {
  1393. /* codecs, usually analog devices */
  1394. name = aux_dev->codec_name;
  1395. codec_of_node = aux_dev->codec_of_node;
  1396. component = soc_find_component(codec_of_node, name);
  1397. if (!component) {
  1398. if (codec_of_node)
  1399. name = of_node_full_name(codec_of_node);
  1400. goto err_defer;
  1401. }
  1402. } else if (aux_dev->name) {
  1403. /* generic components */
  1404. name = aux_dev->name;
  1405. component = soc_find_component(NULL, name);
  1406. if (!component)
  1407. goto err_defer;
  1408. } else {
  1409. dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
  1410. return -EINVAL;
  1411. }
  1412. component->init = aux_dev->init;
  1413. list_add(&component->list_aux, &card->aux_comp_list);
  1414. return 0;
  1415. err_defer:
  1416. dev_err(card->dev, "ASoC: %s not registered\n", name);
  1417. return -EPROBE_DEFER;
  1418. }
  1419. static int soc_probe_aux_devices(struct snd_soc_card *card)
  1420. {
  1421. struct snd_soc_component *comp;
  1422. int order;
  1423. int ret;
  1424. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1425. order++) {
  1426. list_for_each_entry(comp, &card->aux_comp_list, list_aux) {
  1427. if (comp->driver->probe_order == order) {
  1428. ret = soc_probe_component(card, comp);
  1429. if (ret < 0) {
  1430. dev_err(card->dev,
  1431. "ASoC: failed to probe aux component %s %d\n",
  1432. comp->name, ret);
  1433. return ret;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. return 0;
  1439. }
  1440. static void soc_remove_aux_devices(struct snd_soc_card *card)
  1441. {
  1442. struct snd_soc_component *comp, *_comp;
  1443. int order;
  1444. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1445. order++) {
  1446. list_for_each_entry_safe(comp, _comp,
  1447. &card->aux_comp_list, list_aux) {
  1448. if (comp->driver->remove_order == order) {
  1449. soc_remove_component(comp);
  1450. /* remove it from the card's aux_comp_list */
  1451. list_del(&comp->list_aux);
  1452. }
  1453. }
  1454. }
  1455. }
  1456. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
  1457. {
  1458. int ret;
  1459. if (codec->cache_init)
  1460. return 0;
  1461. ret = snd_soc_cache_init(codec);
  1462. if (ret < 0) {
  1463. dev_err(codec->dev,
  1464. "ASoC: Failed to set cache compression type: %d\n",
  1465. ret);
  1466. return ret;
  1467. }
  1468. codec->cache_init = 1;
  1469. return 0;
  1470. }
  1471. /**
  1472. * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
  1473. * @rtd: The runtime for which the DAI link format should be changed
  1474. * @dai_fmt: The new DAI link format
  1475. *
  1476. * This function updates the DAI link format for all DAIs connected to the DAI
  1477. * link for the specified runtime.
  1478. *
  1479. * Note: For setups with a static format set the dai_fmt field in the
  1480. * corresponding snd_dai_link struct instead of using this function.
  1481. *
  1482. * Returns 0 on success, otherwise a negative error code.
  1483. */
  1484. int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
  1485. unsigned int dai_fmt)
  1486. {
  1487. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  1488. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1489. unsigned int i;
  1490. int ret;
  1491. for (i = 0; i < rtd->num_codecs; i++) {
  1492. struct snd_soc_dai *codec_dai = codec_dais[i];
  1493. ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
  1494. if (ret != 0 && ret != -ENOTSUPP) {
  1495. dev_warn(codec_dai->dev,
  1496. "ASoC: Failed to set DAI format: %d\n", ret);
  1497. return ret;
  1498. }
  1499. }
  1500. /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
  1501. if (cpu_dai->codec) {
  1502. unsigned int inv_dai_fmt;
  1503. inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
  1504. switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1505. case SND_SOC_DAIFMT_CBM_CFM:
  1506. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1507. break;
  1508. case SND_SOC_DAIFMT_CBM_CFS:
  1509. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1510. break;
  1511. case SND_SOC_DAIFMT_CBS_CFM:
  1512. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1513. break;
  1514. case SND_SOC_DAIFMT_CBS_CFS:
  1515. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1516. break;
  1517. }
  1518. dai_fmt = inv_dai_fmt;
  1519. }
  1520. ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
  1521. if (ret != 0 && ret != -ENOTSUPP) {
  1522. dev_warn(cpu_dai->dev,
  1523. "ASoC: Failed to set DAI format: %d\n", ret);
  1524. return ret;
  1525. }
  1526. return 0;
  1527. }
  1528. EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
  1529. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1530. {
  1531. struct snd_soc_codec *codec;
  1532. struct snd_soc_pcm_runtime *rtd;
  1533. struct snd_soc_dai_link *dai_link;
  1534. int ret, i, order;
  1535. mutex_lock(&client_mutex);
  1536. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1537. /* bind DAIs */
  1538. for (i = 0; i < card->num_links; i++) {
  1539. ret = soc_bind_dai_link(card, &card->dai_link[i]);
  1540. if (ret != 0)
  1541. goto base_error;
  1542. }
  1543. /* bind aux_devs too */
  1544. for (i = 0; i < card->num_aux_devs; i++) {
  1545. ret = soc_bind_aux_dev(card, i);
  1546. if (ret != 0)
  1547. goto base_error;
  1548. }
  1549. /* add predefined DAI links to the list */
  1550. for (i = 0; i < card->num_links; i++)
  1551. snd_soc_add_dai_link(card, card->dai_link+i);
  1552. /* initialize the register cache for each available codec */
  1553. list_for_each_entry(codec, &codec_list, list) {
  1554. if (codec->cache_init)
  1555. continue;
  1556. ret = snd_soc_init_codec_cache(codec);
  1557. if (ret < 0)
  1558. goto base_error;
  1559. }
  1560. /* card bind complete so register a sound card */
  1561. ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1562. card->owner, 0, &card->snd_card);
  1563. if (ret < 0) {
  1564. dev_err(card->dev,
  1565. "ASoC: can't create sound card for card %s: %d\n",
  1566. card->name, ret);
  1567. goto base_error;
  1568. }
  1569. soc_init_card_debugfs(card);
  1570. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1571. card->dapm.dev = card->dev;
  1572. card->dapm.card = card;
  1573. list_add(&card->dapm.list, &card->dapm_list);
  1574. #ifdef CONFIG_DEBUG_FS
  1575. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1576. #endif
  1577. #ifdef CONFIG_PM_SLEEP
  1578. /* deferred resume work */
  1579. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1580. #endif
  1581. if (card->dapm_widgets)
  1582. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1583. card->num_dapm_widgets);
  1584. if (card->of_dapm_widgets)
  1585. snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
  1586. card->num_of_dapm_widgets);
  1587. /* initialise the sound card only once */
  1588. if (card->probe) {
  1589. ret = card->probe(card);
  1590. if (ret < 0)
  1591. goto card_probe_error;
  1592. }
  1593. /* probe all components used by DAI links on this card */
  1594. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1595. order++) {
  1596. list_for_each_entry(rtd, &card->rtd_list, list) {
  1597. ret = soc_probe_link_components(card, rtd, order);
  1598. if (ret < 0) {
  1599. dev_err(card->dev,
  1600. "ASoC: failed to instantiate card %d\n",
  1601. ret);
  1602. goto probe_dai_err;
  1603. }
  1604. }
  1605. }
  1606. /* probe auxiliary components */
  1607. ret = soc_probe_aux_devices(card);
  1608. if (ret < 0)
  1609. goto probe_dai_err;
  1610. /* Find new DAI links added during probing components and bind them.
  1611. * Components with topology may bring new DAIs and DAI links.
  1612. */
  1613. list_for_each_entry(dai_link, &card->dai_link_list, list) {
  1614. if (soc_is_dai_link_bound(card, dai_link))
  1615. continue;
  1616. ret = soc_init_dai_link(card, dai_link);
  1617. if (ret)
  1618. goto probe_dai_err;
  1619. ret = soc_bind_dai_link(card, dai_link);
  1620. if (ret)
  1621. goto probe_dai_err;
  1622. }
  1623. /* probe all DAI links on this card */
  1624. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1625. order++) {
  1626. list_for_each_entry(rtd, &card->rtd_list, list) {
  1627. ret = soc_probe_link_dais(card, rtd, order);
  1628. if (ret < 0) {
  1629. dev_err(card->dev,
  1630. "ASoC: failed to instantiate card %d\n",
  1631. ret);
  1632. goto probe_dai_err;
  1633. }
  1634. }
  1635. }
  1636. snd_soc_dapm_link_dai_widgets(card);
  1637. snd_soc_dapm_connect_dai_link_widgets(card);
  1638. if (card->controls)
  1639. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1640. if (card->dapm_routes)
  1641. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1642. card->num_dapm_routes);
  1643. if (card->of_dapm_routes)
  1644. snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
  1645. card->num_of_dapm_routes);
  1646. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1647. "%s", card->name);
  1648. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1649. "%s", card->long_name ? card->long_name : card->name);
  1650. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1651. "%s", card->driver_name ? card->driver_name : card->name);
  1652. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1653. switch (card->snd_card->driver[i]) {
  1654. case '_':
  1655. case '-':
  1656. case '\0':
  1657. break;
  1658. default:
  1659. if (!isalnum(card->snd_card->driver[i]))
  1660. card->snd_card->driver[i] = '_';
  1661. break;
  1662. }
  1663. }
  1664. if (card->late_probe) {
  1665. ret = card->late_probe(card);
  1666. if (ret < 0) {
  1667. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1668. card->name, ret);
  1669. goto probe_aux_dev_err;
  1670. }
  1671. }
  1672. snd_soc_dapm_new_widgets(card);
  1673. ret = snd_card_register(card->snd_card);
  1674. if (ret < 0) {
  1675. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1676. ret);
  1677. goto probe_aux_dev_err;
  1678. }
  1679. card->instantiated = 1;
  1680. snd_soc_dapm_sync(&card->dapm);
  1681. mutex_unlock(&card->mutex);
  1682. mutex_unlock(&client_mutex);
  1683. return 0;
  1684. probe_aux_dev_err:
  1685. soc_remove_aux_devices(card);
  1686. probe_dai_err:
  1687. soc_remove_dai_links(card);
  1688. card_probe_error:
  1689. if (card->remove)
  1690. card->remove(card);
  1691. snd_soc_dapm_free(&card->dapm);
  1692. soc_cleanup_card_debugfs(card);
  1693. snd_card_free(card->snd_card);
  1694. base_error:
  1695. soc_remove_pcm_runtimes(card);
  1696. mutex_unlock(&card->mutex);
  1697. mutex_unlock(&client_mutex);
  1698. return ret;
  1699. }
  1700. /* probes a new socdev */
  1701. static int soc_probe(struct platform_device *pdev)
  1702. {
  1703. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1704. /*
  1705. * no card, so machine driver should be registering card
  1706. * we should not be here in that case so ret error
  1707. */
  1708. if (!card)
  1709. return -EINVAL;
  1710. dev_warn(&pdev->dev,
  1711. "ASoC: machine %s should use snd_soc_register_card()\n",
  1712. card->name);
  1713. /* Bodge while we unpick instantiation */
  1714. card->dev = &pdev->dev;
  1715. return snd_soc_register_card(card);
  1716. }
  1717. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1718. {
  1719. struct snd_soc_pcm_runtime *rtd;
  1720. /* make sure any delayed work runs */
  1721. list_for_each_entry(rtd, &card->rtd_list, list)
  1722. flush_delayed_work(&rtd->delayed_work);
  1723. /* free the ALSA card at first; this syncs with pending operations */
  1724. snd_card_free(card->snd_card);
  1725. /* remove and free each DAI */
  1726. soc_remove_dai_links(card);
  1727. soc_remove_pcm_runtimes(card);
  1728. /* remove auxiliary devices */
  1729. soc_remove_aux_devices(card);
  1730. snd_soc_dapm_free(&card->dapm);
  1731. soc_cleanup_card_debugfs(card);
  1732. /* remove the card */
  1733. if (card->remove)
  1734. card->remove(card);
  1735. return 0;
  1736. }
  1737. /* removes a socdev */
  1738. static int soc_remove(struct platform_device *pdev)
  1739. {
  1740. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1741. snd_soc_unregister_card(card);
  1742. return 0;
  1743. }
  1744. int snd_soc_poweroff(struct device *dev)
  1745. {
  1746. struct snd_soc_card *card = dev_get_drvdata(dev);
  1747. struct snd_soc_pcm_runtime *rtd;
  1748. if (!card->instantiated)
  1749. return 0;
  1750. /* Flush out pmdown_time work - we actually do want to run it
  1751. * now, we're shutting down so no imminent restart. */
  1752. list_for_each_entry(rtd, &card->rtd_list, list)
  1753. flush_delayed_work(&rtd->delayed_work);
  1754. snd_soc_dapm_shutdown(card);
  1755. /* deactivate pins to sleep state */
  1756. list_for_each_entry(rtd, &card->rtd_list, list) {
  1757. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1758. int i;
  1759. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  1760. for (i = 0; i < rtd->num_codecs; i++) {
  1761. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  1762. pinctrl_pm_select_sleep_state(codec_dai->dev);
  1763. }
  1764. }
  1765. return 0;
  1766. }
  1767. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1768. const struct dev_pm_ops snd_soc_pm_ops = {
  1769. .suspend = snd_soc_suspend,
  1770. .resume = snd_soc_resume,
  1771. .freeze = snd_soc_suspend,
  1772. .thaw = snd_soc_resume,
  1773. .poweroff = snd_soc_poweroff,
  1774. .restore = snd_soc_resume,
  1775. };
  1776. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1777. /* ASoC platform driver */
  1778. static struct platform_driver soc_driver = {
  1779. .driver = {
  1780. .name = "soc-audio",
  1781. .pm = &snd_soc_pm_ops,
  1782. },
  1783. .probe = soc_probe,
  1784. .remove = soc_remove,
  1785. };
  1786. /**
  1787. * snd_soc_cnew - create new control
  1788. * @_template: control template
  1789. * @data: control private data
  1790. * @long_name: control long name
  1791. * @prefix: control name prefix
  1792. *
  1793. * Create a new mixer control from a template control.
  1794. *
  1795. * Returns 0 for success, else error.
  1796. */
  1797. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1798. void *data, const char *long_name,
  1799. const char *prefix)
  1800. {
  1801. struct snd_kcontrol_new template;
  1802. struct snd_kcontrol *kcontrol;
  1803. char *name = NULL;
  1804. memcpy(&template, _template, sizeof(template));
  1805. template.index = 0;
  1806. if (!long_name)
  1807. long_name = template.name;
  1808. if (prefix) {
  1809. name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
  1810. if (!name)
  1811. return NULL;
  1812. template.name = name;
  1813. } else {
  1814. template.name = long_name;
  1815. }
  1816. kcontrol = snd_ctl_new1(&template, data);
  1817. kfree(name);
  1818. return kcontrol;
  1819. }
  1820. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1821. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1822. const struct snd_kcontrol_new *controls, int num_controls,
  1823. const char *prefix, void *data)
  1824. {
  1825. int err, i;
  1826. for (i = 0; i < num_controls; i++) {
  1827. const struct snd_kcontrol_new *control = &controls[i];
  1828. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1829. control->name, prefix));
  1830. if (err < 0) {
  1831. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1832. control->name, err);
  1833. return err;
  1834. }
  1835. }
  1836. return 0;
  1837. }
  1838. struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
  1839. const char *name)
  1840. {
  1841. struct snd_card *card = soc_card->snd_card;
  1842. struct snd_kcontrol *kctl;
  1843. if (unlikely(!name))
  1844. return NULL;
  1845. list_for_each_entry(kctl, &card->controls, list)
  1846. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
  1847. return kctl;
  1848. return NULL;
  1849. }
  1850. EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
  1851. /**
  1852. * snd_soc_add_component_controls - Add an array of controls to a component.
  1853. *
  1854. * @component: Component to add controls to
  1855. * @controls: Array of controls to add
  1856. * @num_controls: Number of elements in the array
  1857. *
  1858. * Return: 0 for success, else error.
  1859. */
  1860. int snd_soc_add_component_controls(struct snd_soc_component *component,
  1861. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1862. {
  1863. struct snd_card *card = component->card->snd_card;
  1864. return snd_soc_add_controls(card, component->dev, controls,
  1865. num_controls, component->name_prefix, component);
  1866. }
  1867. EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
  1868. /**
  1869. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1870. * Convenience function to add a list of controls. Many codecs were
  1871. * duplicating this code.
  1872. *
  1873. * @codec: codec to add controls to
  1874. * @controls: array of controls to add
  1875. * @num_controls: number of elements in the array
  1876. *
  1877. * Return 0 for success, else error.
  1878. */
  1879. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1880. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1881. {
  1882. return snd_soc_add_component_controls(&codec->component, controls,
  1883. num_controls);
  1884. }
  1885. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1886. /**
  1887. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1888. * Convenience function to add a list of controls.
  1889. *
  1890. * @platform: platform to add controls to
  1891. * @controls: array of controls to add
  1892. * @num_controls: number of elements in the array
  1893. *
  1894. * Return 0 for success, else error.
  1895. */
  1896. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1897. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1898. {
  1899. return snd_soc_add_component_controls(&platform->component, controls,
  1900. num_controls);
  1901. }
  1902. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1903. /**
  1904. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1905. * Convenience function to add a list of controls.
  1906. *
  1907. * @soc_card: SoC card to add controls to
  1908. * @controls: array of controls to add
  1909. * @num_controls: number of elements in the array
  1910. *
  1911. * Return 0 for success, else error.
  1912. */
  1913. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1914. const struct snd_kcontrol_new *controls, int num_controls)
  1915. {
  1916. struct snd_card *card = soc_card->snd_card;
  1917. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1918. NULL, soc_card);
  1919. }
  1920. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1921. /**
  1922. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1923. * Convienience function to add a list of controls.
  1924. *
  1925. * @dai: DAI to add controls to
  1926. * @controls: array of controls to add
  1927. * @num_controls: number of elements in the array
  1928. *
  1929. * Return 0 for success, else error.
  1930. */
  1931. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1932. const struct snd_kcontrol_new *controls, int num_controls)
  1933. {
  1934. struct snd_card *card = dai->component->card->snd_card;
  1935. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  1936. NULL, dai);
  1937. }
  1938. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  1939. /**
  1940. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1941. * @dai: DAI
  1942. * @clk_id: DAI specific clock ID
  1943. * @freq: new clock frequency in Hz
  1944. * @dir: new clock direction - input/output.
  1945. *
  1946. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1947. */
  1948. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1949. unsigned int freq, int dir)
  1950. {
  1951. if (dai->driver && dai->driver->ops->set_sysclk)
  1952. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  1953. else if (dai->codec && dai->codec->driver->set_sysclk)
  1954. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  1955. freq, dir);
  1956. else
  1957. return -ENOTSUPP;
  1958. }
  1959. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1960. /**
  1961. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  1962. * @codec: CODEC
  1963. * @clk_id: DAI specific clock ID
  1964. * @source: Source for the clock
  1965. * @freq: new clock frequency in Hz
  1966. * @dir: new clock direction - input/output.
  1967. *
  1968. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  1969. */
  1970. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  1971. int source, unsigned int freq, int dir)
  1972. {
  1973. if (codec->driver->set_sysclk)
  1974. return codec->driver->set_sysclk(codec, clk_id, source,
  1975. freq, dir);
  1976. else
  1977. return -ENOTSUPP;
  1978. }
  1979. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  1980. /**
  1981. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1982. * @dai: DAI
  1983. * @div_id: DAI specific clock divider ID
  1984. * @div: new clock divisor.
  1985. *
  1986. * Configures the clock dividers. This is used to derive the best DAI bit and
  1987. * frame clocks from the system or master clock. It's best to set the DAI bit
  1988. * and frame clocks as low as possible to save system power.
  1989. */
  1990. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1991. int div_id, int div)
  1992. {
  1993. if (dai->driver && dai->driver->ops->set_clkdiv)
  1994. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  1995. else
  1996. return -EINVAL;
  1997. }
  1998. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1999. /**
  2000. * snd_soc_dai_set_pll - configure DAI PLL.
  2001. * @dai: DAI
  2002. * @pll_id: DAI specific PLL ID
  2003. * @source: DAI specific source for the PLL
  2004. * @freq_in: PLL input clock frequency in Hz
  2005. * @freq_out: requested PLL output clock frequency in Hz
  2006. *
  2007. * Configures and enables PLL to generate output clock based on input clock.
  2008. */
  2009. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2010. unsigned int freq_in, unsigned int freq_out)
  2011. {
  2012. if (dai->driver && dai->driver->ops->set_pll)
  2013. return dai->driver->ops->set_pll(dai, pll_id, source,
  2014. freq_in, freq_out);
  2015. else if (dai->codec && dai->codec->driver->set_pll)
  2016. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2017. freq_in, freq_out);
  2018. else
  2019. return -EINVAL;
  2020. }
  2021. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2022. /*
  2023. * snd_soc_codec_set_pll - configure codec PLL.
  2024. * @codec: CODEC
  2025. * @pll_id: DAI specific PLL ID
  2026. * @source: DAI specific source for the PLL
  2027. * @freq_in: PLL input clock frequency in Hz
  2028. * @freq_out: requested PLL output clock frequency in Hz
  2029. *
  2030. * Configures and enables PLL to generate output clock based on input clock.
  2031. */
  2032. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2033. unsigned int freq_in, unsigned int freq_out)
  2034. {
  2035. if (codec->driver->set_pll)
  2036. return codec->driver->set_pll(codec, pll_id, source,
  2037. freq_in, freq_out);
  2038. else
  2039. return -EINVAL;
  2040. }
  2041. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2042. /**
  2043. * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  2044. * @dai: DAI
  2045. * @ratio: Ratio of BCLK to Sample rate.
  2046. *
  2047. * Configures the DAI for a preset BCLK to sample rate ratio.
  2048. */
  2049. int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  2050. {
  2051. if (dai->driver && dai->driver->ops->set_bclk_ratio)
  2052. return dai->driver->ops->set_bclk_ratio(dai, ratio);
  2053. else
  2054. return -EINVAL;
  2055. }
  2056. EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  2057. /**
  2058. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2059. * @dai: DAI
  2060. * @fmt: SND_SOC_DAIFMT_ format value.
  2061. *
  2062. * Configures the DAI hardware format and clocking.
  2063. */
  2064. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2065. {
  2066. if (dai->driver == NULL)
  2067. return -EINVAL;
  2068. if (dai->driver->ops->set_fmt == NULL)
  2069. return -ENOTSUPP;
  2070. return dai->driver->ops->set_fmt(dai, fmt);
  2071. }
  2072. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2073. /**
  2074. * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
  2075. * @slots: Number of slots in use.
  2076. * @tx_mask: bitmask representing active TX slots.
  2077. * @rx_mask: bitmask representing active RX slots.
  2078. *
  2079. * Generates the TDM tx and rx slot default masks for DAI.
  2080. */
  2081. static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
  2082. unsigned int *tx_mask,
  2083. unsigned int *rx_mask)
  2084. {
  2085. if (*tx_mask || *rx_mask)
  2086. return 0;
  2087. if (!slots)
  2088. return -EINVAL;
  2089. *tx_mask = (1 << slots) - 1;
  2090. *rx_mask = (1 << slots) - 1;
  2091. return 0;
  2092. }
  2093. /**
  2094. * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
  2095. * @dai: The DAI to configure
  2096. * @tx_mask: bitmask representing active TX slots.
  2097. * @rx_mask: bitmask representing active RX slots.
  2098. * @slots: Number of slots in use.
  2099. * @slot_width: Width in bits for each slot.
  2100. *
  2101. * This function configures the specified DAI for TDM operation. @slot contains
  2102. * the total number of slots of the TDM stream and @slot_with the width of each
  2103. * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
  2104. * active slots of the TDM stream for the specified DAI, i.e. which slots the
  2105. * DAI should write to or read from. If a bit is set the corresponding slot is
  2106. * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
  2107. * the first slot, bit 1 to the second slot and so on. The first active slot
  2108. * maps to the first channel of the DAI, the second active slot to the second
  2109. * channel and so on.
  2110. *
  2111. * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
  2112. * @rx_mask and @slot_width will be ignored.
  2113. *
  2114. * Returns 0 on success, a negative error code otherwise.
  2115. */
  2116. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2117. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2118. {
  2119. if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
  2120. dai->driver->ops->xlate_tdm_slot_mask(slots,
  2121. &tx_mask, &rx_mask);
  2122. else
  2123. snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
  2124. dai->tx_mask = tx_mask;
  2125. dai->rx_mask = rx_mask;
  2126. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2127. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2128. slots, slot_width);
  2129. else
  2130. return -ENOTSUPP;
  2131. }
  2132. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  2133. /**
  2134. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  2135. * @dai: DAI
  2136. * @tx_num: how many TX channels
  2137. * @tx_slot: pointer to an array which imply the TX slot number channel
  2138. * 0~num-1 uses
  2139. * @rx_num: how many RX channels
  2140. * @rx_slot: pointer to an array which imply the RX slot number channel
  2141. * 0~num-1 uses
  2142. *
  2143. * configure the relationship between channel number and TDM slot number.
  2144. */
  2145. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  2146. unsigned int tx_num, unsigned int *tx_slot,
  2147. unsigned int rx_num, unsigned int *rx_slot)
  2148. {
  2149. if (dai->driver && dai->driver->ops->set_channel_map)
  2150. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  2151. rx_num, rx_slot);
  2152. else
  2153. return -EINVAL;
  2154. }
  2155. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  2156. /**
  2157. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  2158. * @dai: DAI
  2159. * @tristate: tristate enable
  2160. *
  2161. * Tristates the DAI so that others can use it.
  2162. */
  2163. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  2164. {
  2165. if (dai->driver && dai->driver->ops->set_tristate)
  2166. return dai->driver->ops->set_tristate(dai, tristate);
  2167. else
  2168. return -EINVAL;
  2169. }
  2170. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  2171. /**
  2172. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  2173. * @dai: DAI
  2174. * @mute: mute enable
  2175. * @direction: stream to mute
  2176. *
  2177. * Mutes the DAI DAC.
  2178. */
  2179. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  2180. int direction)
  2181. {
  2182. if (!dai->driver)
  2183. return -ENOTSUPP;
  2184. if (dai->driver->ops->mute_stream)
  2185. return dai->driver->ops->mute_stream(dai, mute, direction);
  2186. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  2187. dai->driver->ops->digital_mute)
  2188. return dai->driver->ops->digital_mute(dai, mute);
  2189. else
  2190. return -ENOTSUPP;
  2191. }
  2192. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  2193. /**
  2194. * snd_soc_register_card - Register a card with the ASoC core
  2195. *
  2196. * @card: Card to register
  2197. *
  2198. */
  2199. int snd_soc_register_card(struct snd_soc_card *card)
  2200. {
  2201. int i, ret;
  2202. struct snd_soc_pcm_runtime *rtd;
  2203. if (!card->name || !card->dev)
  2204. return -EINVAL;
  2205. for (i = 0; i < card->num_links; i++) {
  2206. struct snd_soc_dai_link *link = &card->dai_link[i];
  2207. ret = soc_init_dai_link(card, link);
  2208. if (ret) {
  2209. dev_err(card->dev, "ASoC: failed to init link %s\n",
  2210. link->name);
  2211. return ret;
  2212. }
  2213. }
  2214. dev_set_drvdata(card->dev, card);
  2215. snd_soc_initialize_card_lists(card);
  2216. INIT_LIST_HEAD(&card->dai_link_list);
  2217. card->num_dai_links = 0;
  2218. INIT_LIST_HEAD(&card->rtd_list);
  2219. card->num_rtd = 0;
  2220. INIT_LIST_HEAD(&card->dapm_dirty);
  2221. INIT_LIST_HEAD(&card->dobj_list);
  2222. card->instantiated = 0;
  2223. mutex_init(&card->mutex);
  2224. mutex_init(&card->dapm_mutex);
  2225. ret = snd_soc_instantiate_card(card);
  2226. if (ret != 0)
  2227. return ret;
  2228. /* deactivate pins to sleep state */
  2229. list_for_each_entry(rtd, &card->rtd_list, list) {
  2230. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2231. int j;
  2232. for (j = 0; j < rtd->num_codecs; j++) {
  2233. struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
  2234. if (!codec_dai->active)
  2235. pinctrl_pm_select_sleep_state(codec_dai->dev);
  2236. }
  2237. if (!cpu_dai->active)
  2238. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  2239. }
  2240. return ret;
  2241. }
  2242. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  2243. /**
  2244. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2245. *
  2246. * @card: Card to unregister
  2247. *
  2248. */
  2249. int snd_soc_unregister_card(struct snd_soc_card *card)
  2250. {
  2251. if (card->instantiated) {
  2252. card->instantiated = false;
  2253. snd_soc_dapm_shutdown(card);
  2254. soc_cleanup_card_resources(card);
  2255. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  2256. }
  2257. return 0;
  2258. }
  2259. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  2260. /*
  2261. * Simplify DAI link configuration by removing ".-1" from device names
  2262. * and sanitizing names.
  2263. */
  2264. static char *fmt_single_name(struct device *dev, int *id)
  2265. {
  2266. char *found, name[NAME_SIZE];
  2267. int id1, id2;
  2268. if (dev_name(dev) == NULL)
  2269. return NULL;
  2270. strlcpy(name, dev_name(dev), NAME_SIZE);
  2271. /* are we a "%s.%d" name (platform and SPI components) */
  2272. found = strstr(name, dev->driver->name);
  2273. if (found) {
  2274. /* get ID */
  2275. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  2276. /* discard ID from name if ID == -1 */
  2277. if (*id == -1)
  2278. found[strlen(dev->driver->name)] = '\0';
  2279. }
  2280. } else {
  2281. /* I2C component devices are named "bus-addr" */
  2282. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  2283. char tmp[NAME_SIZE];
  2284. /* create unique ID number from I2C addr and bus */
  2285. *id = ((id1 & 0xffff) << 16) + id2;
  2286. /* sanitize component name for DAI link creation */
  2287. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  2288. strlcpy(name, tmp, NAME_SIZE);
  2289. } else
  2290. *id = 0;
  2291. }
  2292. return kstrdup(name, GFP_KERNEL);
  2293. }
  2294. /*
  2295. * Simplify DAI link naming for single devices with multiple DAIs by removing
  2296. * any ".-1" and using the DAI name (instead of device name).
  2297. */
  2298. static inline char *fmt_multiple_name(struct device *dev,
  2299. struct snd_soc_dai_driver *dai_drv)
  2300. {
  2301. if (dai_drv->name == NULL) {
  2302. dev_err(dev,
  2303. "ASoC: error - multiple DAI %s registered with no name\n",
  2304. dev_name(dev));
  2305. return NULL;
  2306. }
  2307. return kstrdup(dai_drv->name, GFP_KERNEL);
  2308. }
  2309. /**
  2310. * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
  2311. *
  2312. * @component: The component for which the DAIs should be unregistered
  2313. */
  2314. static void snd_soc_unregister_dais(struct snd_soc_component *component)
  2315. {
  2316. struct snd_soc_dai *dai, *_dai;
  2317. list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
  2318. dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
  2319. dai->name);
  2320. list_del(&dai->list);
  2321. kfree(dai->name);
  2322. kfree(dai);
  2323. }
  2324. }
  2325. /* Create a DAI and add it to the component's DAI list */
  2326. static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
  2327. struct snd_soc_dai_driver *dai_drv,
  2328. bool legacy_dai_naming)
  2329. {
  2330. struct device *dev = component->dev;
  2331. struct snd_soc_dai *dai;
  2332. dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
  2333. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  2334. if (dai == NULL)
  2335. return NULL;
  2336. /*
  2337. * Back in the old days when we still had component-less DAIs,
  2338. * instead of having a static name, component-less DAIs would
  2339. * inherit the name of the parent device so it is possible to
  2340. * register multiple instances of the DAI. We still need to keep
  2341. * the same naming style even though those DAIs are not
  2342. * component-less anymore.
  2343. */
  2344. if (legacy_dai_naming &&
  2345. (dai_drv->id == 0 || dai_drv->name == NULL)) {
  2346. dai->name = fmt_single_name(dev, &dai->id);
  2347. } else {
  2348. dai->name = fmt_multiple_name(dev, dai_drv);
  2349. if (dai_drv->id)
  2350. dai->id = dai_drv->id;
  2351. else
  2352. dai->id = component->num_dai;
  2353. }
  2354. if (dai->name == NULL) {
  2355. kfree(dai);
  2356. return NULL;
  2357. }
  2358. dai->component = component;
  2359. dai->dev = dev;
  2360. dai->driver = dai_drv;
  2361. if (!dai->driver->ops)
  2362. dai->driver->ops = &null_dai_ops;
  2363. list_add(&dai->list, &component->dai_list);
  2364. component->num_dai++;
  2365. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  2366. return dai;
  2367. }
  2368. /**
  2369. * snd_soc_register_dais - Register a DAI with the ASoC core
  2370. *
  2371. * @component: The component the DAIs are registered for
  2372. * @dai_drv: DAI driver to use for the DAIs
  2373. * @count: Number of DAIs
  2374. * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
  2375. * parent's name.
  2376. */
  2377. static int snd_soc_register_dais(struct snd_soc_component *component,
  2378. struct snd_soc_dai_driver *dai_drv, size_t count,
  2379. bool legacy_dai_naming)
  2380. {
  2381. struct device *dev = component->dev;
  2382. struct snd_soc_dai *dai;
  2383. unsigned int i;
  2384. int ret;
  2385. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  2386. component->dai_drv = dai_drv;
  2387. for (i = 0; i < count; i++) {
  2388. dai = soc_add_dai(component, dai_drv + i,
  2389. count == 1 && legacy_dai_naming);
  2390. if (dai == NULL) {
  2391. ret = -ENOMEM;
  2392. goto err;
  2393. }
  2394. }
  2395. return 0;
  2396. err:
  2397. snd_soc_unregister_dais(component);
  2398. return ret;
  2399. }
  2400. /**
  2401. * snd_soc_register_dai - Register a DAI dynamically & create its widgets
  2402. *
  2403. * @component: The component the DAIs are registered for
  2404. * @dai_drv: DAI driver to use for the DAI
  2405. *
  2406. * Topology can use this API to register DAIs when probing a component.
  2407. * These DAIs's widgets will be freed in the card cleanup and the DAIs
  2408. * will be freed in the component cleanup.
  2409. */
  2410. int snd_soc_register_dai(struct snd_soc_component *component,
  2411. struct snd_soc_dai_driver *dai_drv)
  2412. {
  2413. struct snd_soc_dapm_context *dapm =
  2414. snd_soc_component_get_dapm(component);
  2415. struct snd_soc_dai *dai;
  2416. int ret;
  2417. if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
  2418. dev_err(component->dev, "Invalid dai type %d\n",
  2419. dai_drv->dobj.type);
  2420. return -EINVAL;
  2421. }
  2422. lockdep_assert_held(&client_mutex);
  2423. dai = soc_add_dai(component, dai_drv, false);
  2424. if (!dai)
  2425. return -ENOMEM;
  2426. /* Create the DAI widgets here. After adding DAIs, topology may
  2427. * also add routes that need these widgets as source or sink.
  2428. */
  2429. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  2430. if (ret != 0) {
  2431. dev_err(component->dev,
  2432. "Failed to create DAI widgets %d\n", ret);
  2433. }
  2434. return ret;
  2435. }
  2436. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2437. static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
  2438. enum snd_soc_dapm_type type, int subseq)
  2439. {
  2440. struct snd_soc_component *component = dapm->component;
  2441. component->driver->seq_notifier(component, type, subseq);
  2442. }
  2443. static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
  2444. int event)
  2445. {
  2446. struct snd_soc_component *component = dapm->component;
  2447. return component->driver->stream_event(component, event);
  2448. }
  2449. static int snd_soc_component_initialize(struct snd_soc_component *component,
  2450. const struct snd_soc_component_driver *driver, struct device *dev)
  2451. {
  2452. struct snd_soc_dapm_context *dapm;
  2453. component->name = fmt_single_name(dev, &component->id);
  2454. if (!component->name) {
  2455. dev_err(dev, "ASoC: Failed to allocate name\n");
  2456. return -ENOMEM;
  2457. }
  2458. component->dev = dev;
  2459. component->driver = driver;
  2460. component->probe = component->driver->probe;
  2461. component->remove = component->driver->remove;
  2462. dapm = &component->dapm;
  2463. dapm->dev = dev;
  2464. dapm->component = component;
  2465. dapm->bias_level = SND_SOC_BIAS_OFF;
  2466. dapm->idle_bias_off = true;
  2467. if (driver->seq_notifier)
  2468. dapm->seq_notifier = snd_soc_component_seq_notifier;
  2469. if (driver->stream_event)
  2470. dapm->stream_event = snd_soc_component_stream_event;
  2471. component->controls = driver->controls;
  2472. component->num_controls = driver->num_controls;
  2473. component->dapm_widgets = driver->dapm_widgets;
  2474. component->num_dapm_widgets = driver->num_dapm_widgets;
  2475. component->dapm_routes = driver->dapm_routes;
  2476. component->num_dapm_routes = driver->num_dapm_routes;
  2477. INIT_LIST_HEAD(&component->dai_list);
  2478. mutex_init(&component->io_mutex);
  2479. return 0;
  2480. }
  2481. static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
  2482. {
  2483. int val_bytes = regmap_get_val_bytes(component->regmap);
  2484. /* Errors are legitimate for non-integer byte multiples */
  2485. if (val_bytes > 0)
  2486. component->val_bytes = val_bytes;
  2487. }
  2488. #ifdef CONFIG_REGMAP
  2489. /**
  2490. * snd_soc_component_init_regmap() - Initialize regmap instance for the component
  2491. * @component: The component for which to initialize the regmap instance
  2492. * @regmap: The regmap instance that should be used by the component
  2493. *
  2494. * This function allows deferred assignment of the regmap instance that is
  2495. * associated with the component. Only use this if the regmap instance is not
  2496. * yet ready when the component is registered. The function must also be called
  2497. * before the first IO attempt of the component.
  2498. */
  2499. void snd_soc_component_init_regmap(struct snd_soc_component *component,
  2500. struct regmap *regmap)
  2501. {
  2502. component->regmap = regmap;
  2503. snd_soc_component_setup_regmap(component);
  2504. }
  2505. EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
  2506. /**
  2507. * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
  2508. * @component: The component for which to de-initialize the regmap instance
  2509. *
  2510. * Calls regmap_exit() on the regmap instance associated to the component and
  2511. * removes the regmap instance from the component.
  2512. *
  2513. * This function should only be used if snd_soc_component_init_regmap() was used
  2514. * to initialize the regmap instance.
  2515. */
  2516. void snd_soc_component_exit_regmap(struct snd_soc_component *component)
  2517. {
  2518. regmap_exit(component->regmap);
  2519. component->regmap = NULL;
  2520. }
  2521. EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
  2522. #endif
  2523. static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
  2524. {
  2525. if (!component->write && !component->read) {
  2526. if (!component->regmap)
  2527. component->regmap = dev_get_regmap(component->dev, NULL);
  2528. if (component->regmap)
  2529. snd_soc_component_setup_regmap(component);
  2530. }
  2531. list_add(&component->list, &component_list);
  2532. INIT_LIST_HEAD(&component->dobj_list);
  2533. }
  2534. static void snd_soc_component_add(struct snd_soc_component *component)
  2535. {
  2536. mutex_lock(&client_mutex);
  2537. snd_soc_component_add_unlocked(component);
  2538. mutex_unlock(&client_mutex);
  2539. }
  2540. static void snd_soc_component_cleanup(struct snd_soc_component *component)
  2541. {
  2542. snd_soc_unregister_dais(component);
  2543. kfree(component->name);
  2544. }
  2545. static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
  2546. {
  2547. list_del(&component->list);
  2548. }
  2549. int snd_soc_register_component(struct device *dev,
  2550. const struct snd_soc_component_driver *cmpnt_drv,
  2551. struct snd_soc_dai_driver *dai_drv,
  2552. int num_dai)
  2553. {
  2554. struct snd_soc_component *cmpnt;
  2555. int ret;
  2556. cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
  2557. if (!cmpnt) {
  2558. dev_err(dev, "ASoC: Failed to allocate memory\n");
  2559. return -ENOMEM;
  2560. }
  2561. ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
  2562. if (ret)
  2563. goto err_free;
  2564. cmpnt->ignore_pmdown_time = true;
  2565. cmpnt->registered_as_component = true;
  2566. ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
  2567. if (ret < 0) {
  2568. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2569. goto err_cleanup;
  2570. }
  2571. snd_soc_component_add(cmpnt);
  2572. return 0;
  2573. err_cleanup:
  2574. snd_soc_component_cleanup(cmpnt);
  2575. err_free:
  2576. kfree(cmpnt);
  2577. return ret;
  2578. }
  2579. EXPORT_SYMBOL_GPL(snd_soc_register_component);
  2580. /**
  2581. * snd_soc_unregister_component - Unregister a component from the ASoC core
  2582. *
  2583. * @dev: The device to unregister
  2584. */
  2585. void snd_soc_unregister_component(struct device *dev)
  2586. {
  2587. struct snd_soc_component *cmpnt;
  2588. mutex_lock(&client_mutex);
  2589. list_for_each_entry(cmpnt, &component_list, list) {
  2590. if (dev == cmpnt->dev && cmpnt->registered_as_component)
  2591. goto found;
  2592. }
  2593. mutex_unlock(&client_mutex);
  2594. return;
  2595. found:
  2596. snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
  2597. snd_soc_component_del_unlocked(cmpnt);
  2598. mutex_unlock(&client_mutex);
  2599. snd_soc_component_cleanup(cmpnt);
  2600. kfree(cmpnt);
  2601. }
  2602. EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
  2603. static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
  2604. {
  2605. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2606. return platform->driver->probe(platform);
  2607. }
  2608. static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
  2609. {
  2610. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2611. platform->driver->remove(platform);
  2612. }
  2613. /**
  2614. * snd_soc_add_platform - Add a platform to the ASoC core
  2615. * @dev: The parent device for the platform
  2616. * @platform: The platform to add
  2617. * @platform_drv: The driver for the platform
  2618. */
  2619. int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
  2620. const struct snd_soc_platform_driver *platform_drv)
  2621. {
  2622. int ret;
  2623. ret = snd_soc_component_initialize(&platform->component,
  2624. &platform_drv->component_driver, dev);
  2625. if (ret)
  2626. return ret;
  2627. platform->dev = dev;
  2628. platform->driver = platform_drv;
  2629. if (platform_drv->probe)
  2630. platform->component.probe = snd_soc_platform_drv_probe;
  2631. if (platform_drv->remove)
  2632. platform->component.remove = snd_soc_platform_drv_remove;
  2633. #ifdef CONFIG_DEBUG_FS
  2634. platform->component.debugfs_prefix = "platform";
  2635. #endif
  2636. mutex_lock(&client_mutex);
  2637. snd_soc_component_add_unlocked(&platform->component);
  2638. list_add(&platform->list, &platform_list);
  2639. mutex_unlock(&client_mutex);
  2640. dev_dbg(dev, "ASoC: Registered platform '%s'\n",
  2641. platform->component.name);
  2642. return 0;
  2643. }
  2644. EXPORT_SYMBOL_GPL(snd_soc_add_platform);
  2645. /**
  2646. * snd_soc_register_platform - Register a platform with the ASoC core
  2647. *
  2648. * @dev: The device for the platform
  2649. * @platform_drv: The driver for the platform
  2650. */
  2651. int snd_soc_register_platform(struct device *dev,
  2652. const struct snd_soc_platform_driver *platform_drv)
  2653. {
  2654. struct snd_soc_platform *platform;
  2655. int ret;
  2656. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  2657. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  2658. if (platform == NULL)
  2659. return -ENOMEM;
  2660. ret = snd_soc_add_platform(dev, platform, platform_drv);
  2661. if (ret)
  2662. kfree(platform);
  2663. return ret;
  2664. }
  2665. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2666. /**
  2667. * snd_soc_remove_platform - Remove a platform from the ASoC core
  2668. * @platform: the platform to remove
  2669. */
  2670. void snd_soc_remove_platform(struct snd_soc_platform *platform)
  2671. {
  2672. mutex_lock(&client_mutex);
  2673. list_del(&platform->list);
  2674. snd_soc_component_del_unlocked(&platform->component);
  2675. mutex_unlock(&client_mutex);
  2676. dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
  2677. platform->component.name);
  2678. snd_soc_component_cleanup(&platform->component);
  2679. }
  2680. EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
  2681. struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
  2682. {
  2683. struct snd_soc_platform *platform;
  2684. mutex_lock(&client_mutex);
  2685. list_for_each_entry(platform, &platform_list, list) {
  2686. if (dev == platform->dev) {
  2687. mutex_unlock(&client_mutex);
  2688. return platform;
  2689. }
  2690. }
  2691. mutex_unlock(&client_mutex);
  2692. return NULL;
  2693. }
  2694. EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
  2695. /**
  2696. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2697. *
  2698. * @dev: platform to unregister
  2699. */
  2700. void snd_soc_unregister_platform(struct device *dev)
  2701. {
  2702. struct snd_soc_platform *platform;
  2703. platform = snd_soc_lookup_platform(dev);
  2704. if (!platform)
  2705. return;
  2706. snd_soc_remove_platform(platform);
  2707. kfree(platform);
  2708. }
  2709. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2710. static u64 codec_format_map[] = {
  2711. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2712. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2713. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2714. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2715. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2716. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2717. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2718. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2719. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2720. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2721. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2722. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2723. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2724. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2725. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2726. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2727. };
  2728. /* Fix up the DAI formats for endianness: codecs don't actually see
  2729. * the endianness of the data but we're using the CPU format
  2730. * definitions which do need to include endianness so we ensure that
  2731. * codec DAIs always have both big and little endian variants set.
  2732. */
  2733. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2734. {
  2735. int i;
  2736. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2737. if (stream->formats & codec_format_map[i])
  2738. stream->formats |= codec_format_map[i];
  2739. }
  2740. static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
  2741. {
  2742. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2743. return codec->driver->probe(codec);
  2744. }
  2745. static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
  2746. {
  2747. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2748. codec->driver->remove(codec);
  2749. }
  2750. static int snd_soc_codec_drv_write(struct snd_soc_component *component,
  2751. unsigned int reg, unsigned int val)
  2752. {
  2753. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2754. return codec->driver->write(codec, reg, val);
  2755. }
  2756. static int snd_soc_codec_drv_read(struct snd_soc_component *component,
  2757. unsigned int reg, unsigned int *val)
  2758. {
  2759. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2760. *val = codec->driver->read(codec, reg);
  2761. return 0;
  2762. }
  2763. static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
  2764. enum snd_soc_bias_level level)
  2765. {
  2766. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
  2767. return codec->driver->set_bias_level(codec, level);
  2768. }
  2769. /**
  2770. * snd_soc_register_codec - Register a codec with the ASoC core
  2771. *
  2772. * @dev: The parent device for this codec
  2773. * @codec_drv: Codec driver
  2774. * @dai_drv: The associated DAI driver
  2775. * @num_dai: Number of DAIs
  2776. */
  2777. int snd_soc_register_codec(struct device *dev,
  2778. const struct snd_soc_codec_driver *codec_drv,
  2779. struct snd_soc_dai_driver *dai_drv,
  2780. int num_dai)
  2781. {
  2782. struct snd_soc_dapm_context *dapm;
  2783. struct snd_soc_codec *codec;
  2784. struct snd_soc_dai *dai;
  2785. int ret, i;
  2786. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  2787. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  2788. if (codec == NULL)
  2789. return -ENOMEM;
  2790. codec->component.codec = codec;
  2791. ret = snd_soc_component_initialize(&codec->component,
  2792. &codec_drv->component_driver, dev);
  2793. if (ret)
  2794. goto err_free;
  2795. if (codec_drv->probe)
  2796. codec->component.probe = snd_soc_codec_drv_probe;
  2797. if (codec_drv->remove)
  2798. codec->component.remove = snd_soc_codec_drv_remove;
  2799. if (codec_drv->write)
  2800. codec->component.write = snd_soc_codec_drv_write;
  2801. if (codec_drv->read)
  2802. codec->component.read = snd_soc_codec_drv_read;
  2803. codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  2804. dapm = snd_soc_codec_get_dapm(codec);
  2805. dapm->idle_bias_off = codec_drv->idle_bias_off;
  2806. dapm->suspend_bias_off = codec_drv->suspend_bias_off;
  2807. if (codec_drv->seq_notifier)
  2808. dapm->seq_notifier = codec_drv->seq_notifier;
  2809. if (codec_drv->set_bias_level)
  2810. dapm->set_bias_level = snd_soc_codec_set_bias_level;
  2811. codec->dev = dev;
  2812. codec->driver = codec_drv;
  2813. codec->component.val_bytes = codec_drv->reg_word_size;
  2814. #ifdef CONFIG_DEBUG_FS
  2815. codec->component.init_debugfs = soc_init_codec_debugfs;
  2816. codec->component.debugfs_prefix = "codec";
  2817. #endif
  2818. if (codec_drv->get_regmap)
  2819. codec->component.regmap = codec_drv->get_regmap(dev);
  2820. for (i = 0; i < num_dai; i++) {
  2821. fixup_codec_formats(&dai_drv[i].playback);
  2822. fixup_codec_formats(&dai_drv[i].capture);
  2823. }
  2824. ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
  2825. if (ret < 0) {
  2826. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2827. goto err_cleanup;
  2828. }
  2829. list_for_each_entry(dai, &codec->component.dai_list, list)
  2830. dai->codec = codec;
  2831. mutex_lock(&client_mutex);
  2832. snd_soc_component_add_unlocked(&codec->component);
  2833. list_add(&codec->list, &codec_list);
  2834. mutex_unlock(&client_mutex);
  2835. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
  2836. codec->component.name);
  2837. return 0;
  2838. err_cleanup:
  2839. snd_soc_component_cleanup(&codec->component);
  2840. err_free:
  2841. kfree(codec);
  2842. return ret;
  2843. }
  2844. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2845. /**
  2846. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2847. *
  2848. * @dev: codec to unregister
  2849. */
  2850. void snd_soc_unregister_codec(struct device *dev)
  2851. {
  2852. struct snd_soc_codec *codec;
  2853. mutex_lock(&client_mutex);
  2854. list_for_each_entry(codec, &codec_list, list) {
  2855. if (dev == codec->dev)
  2856. goto found;
  2857. }
  2858. mutex_unlock(&client_mutex);
  2859. return;
  2860. found:
  2861. list_del(&codec->list);
  2862. snd_soc_component_del_unlocked(&codec->component);
  2863. mutex_unlock(&client_mutex);
  2864. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
  2865. codec->component.name);
  2866. snd_soc_component_cleanup(&codec->component);
  2867. snd_soc_cache_exit(codec);
  2868. kfree(codec);
  2869. }
  2870. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2871. /* Retrieve a card's name from device tree */
  2872. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  2873. const char *propname)
  2874. {
  2875. struct device_node *np;
  2876. int ret;
  2877. if (!card->dev) {
  2878. pr_err("card->dev is not set before calling %s\n", __func__);
  2879. return -EINVAL;
  2880. }
  2881. np = card->dev->of_node;
  2882. ret = of_property_read_string_index(np, propname, 0, &card->name);
  2883. /*
  2884. * EINVAL means the property does not exist. This is fine providing
  2885. * card->name was previously set, which is checked later in
  2886. * snd_soc_register_card.
  2887. */
  2888. if (ret < 0 && ret != -EINVAL) {
  2889. dev_err(card->dev,
  2890. "ASoC: Property '%s' could not be read: %d\n",
  2891. propname, ret);
  2892. return ret;
  2893. }
  2894. return 0;
  2895. }
  2896. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  2897. static const struct snd_soc_dapm_widget simple_widgets[] = {
  2898. SND_SOC_DAPM_MIC("Microphone", NULL),
  2899. SND_SOC_DAPM_LINE("Line", NULL),
  2900. SND_SOC_DAPM_HP("Headphone", NULL),
  2901. SND_SOC_DAPM_SPK("Speaker", NULL),
  2902. };
  2903. int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
  2904. const char *propname)
  2905. {
  2906. struct device_node *np = card->dev->of_node;
  2907. struct snd_soc_dapm_widget *widgets;
  2908. const char *template, *wname;
  2909. int i, j, num_widgets, ret;
  2910. num_widgets = of_property_count_strings(np, propname);
  2911. if (num_widgets < 0) {
  2912. dev_err(card->dev,
  2913. "ASoC: Property '%s' does not exist\n", propname);
  2914. return -EINVAL;
  2915. }
  2916. if (num_widgets & 1) {
  2917. dev_err(card->dev,
  2918. "ASoC: Property '%s' length is not even\n", propname);
  2919. return -EINVAL;
  2920. }
  2921. num_widgets /= 2;
  2922. if (!num_widgets) {
  2923. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  2924. propname);
  2925. return -EINVAL;
  2926. }
  2927. widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
  2928. GFP_KERNEL);
  2929. if (!widgets) {
  2930. dev_err(card->dev,
  2931. "ASoC: Could not allocate memory for widgets\n");
  2932. return -ENOMEM;
  2933. }
  2934. for (i = 0; i < num_widgets; i++) {
  2935. ret = of_property_read_string_index(np, propname,
  2936. 2 * i, &template);
  2937. if (ret) {
  2938. dev_err(card->dev,
  2939. "ASoC: Property '%s' index %d read error:%d\n",
  2940. propname, 2 * i, ret);
  2941. return -EINVAL;
  2942. }
  2943. for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
  2944. if (!strncmp(template, simple_widgets[j].name,
  2945. strlen(simple_widgets[j].name))) {
  2946. widgets[i] = simple_widgets[j];
  2947. break;
  2948. }
  2949. }
  2950. if (j >= ARRAY_SIZE(simple_widgets)) {
  2951. dev_err(card->dev,
  2952. "ASoC: DAPM widget '%s' is not supported\n",
  2953. template);
  2954. return -EINVAL;
  2955. }
  2956. ret = of_property_read_string_index(np, propname,
  2957. (2 * i) + 1,
  2958. &wname);
  2959. if (ret) {
  2960. dev_err(card->dev,
  2961. "ASoC: Property '%s' index %d read error:%d\n",
  2962. propname, (2 * i) + 1, ret);
  2963. return -EINVAL;
  2964. }
  2965. widgets[i].name = wname;
  2966. }
  2967. card->of_dapm_widgets = widgets;
  2968. card->num_of_dapm_widgets = num_widgets;
  2969. return 0;
  2970. }
  2971. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
  2972. static int snd_soc_of_get_slot_mask(struct device_node *np,
  2973. const char *prop_name,
  2974. unsigned int *mask)
  2975. {
  2976. u32 val;
  2977. const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
  2978. int i;
  2979. if (!of_slot_mask)
  2980. return 0;
  2981. val /= sizeof(u32);
  2982. for (i = 0; i < val; i++)
  2983. if (be32_to_cpup(&of_slot_mask[i]))
  2984. *mask |= (1 << i);
  2985. return val;
  2986. }
  2987. int snd_soc_of_parse_tdm_slot(struct device_node *np,
  2988. unsigned int *tx_mask,
  2989. unsigned int *rx_mask,
  2990. unsigned int *slots,
  2991. unsigned int *slot_width)
  2992. {
  2993. u32 val;
  2994. int ret;
  2995. if (tx_mask)
  2996. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
  2997. if (rx_mask)
  2998. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
  2999. if (of_property_read_bool(np, "dai-tdm-slot-num")) {
  3000. ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
  3001. if (ret)
  3002. return ret;
  3003. if (slots)
  3004. *slots = val;
  3005. }
  3006. if (of_property_read_bool(np, "dai-tdm-slot-width")) {
  3007. ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
  3008. if (ret)
  3009. return ret;
  3010. if (slot_width)
  3011. *slot_width = val;
  3012. }
  3013. return 0;
  3014. }
  3015. EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
  3016. void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
  3017. struct snd_soc_codec_conf *codec_conf,
  3018. struct device_node *of_node,
  3019. const char *propname)
  3020. {
  3021. struct device_node *np = card->dev->of_node;
  3022. const char *str;
  3023. int ret;
  3024. ret = of_property_read_string(np, propname, &str);
  3025. if (ret < 0) {
  3026. /* no prefix is not error */
  3027. return;
  3028. }
  3029. codec_conf->of_node = of_node;
  3030. codec_conf->name_prefix = str;
  3031. }
  3032. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
  3033. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3034. const char *propname)
  3035. {
  3036. struct device_node *np = card->dev->of_node;
  3037. int num_routes;
  3038. struct snd_soc_dapm_route *routes;
  3039. int i, ret;
  3040. num_routes = of_property_count_strings(np, propname);
  3041. if (num_routes < 0 || num_routes & 1) {
  3042. dev_err(card->dev,
  3043. "ASoC: Property '%s' does not exist or its length is not even\n",
  3044. propname);
  3045. return -EINVAL;
  3046. }
  3047. num_routes /= 2;
  3048. if (!num_routes) {
  3049. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3050. propname);
  3051. return -EINVAL;
  3052. }
  3053. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3054. GFP_KERNEL);
  3055. if (!routes) {
  3056. dev_err(card->dev,
  3057. "ASoC: Could not allocate DAPM route table\n");
  3058. return -EINVAL;
  3059. }
  3060. for (i = 0; i < num_routes; i++) {
  3061. ret = of_property_read_string_index(np, propname,
  3062. 2 * i, &routes[i].sink);
  3063. if (ret) {
  3064. dev_err(card->dev,
  3065. "ASoC: Property '%s' index %d could not be read: %d\n",
  3066. propname, 2 * i, ret);
  3067. return -EINVAL;
  3068. }
  3069. ret = of_property_read_string_index(np, propname,
  3070. (2 * i) + 1, &routes[i].source);
  3071. if (ret) {
  3072. dev_err(card->dev,
  3073. "ASoC: Property '%s' index %d could not be read: %d\n",
  3074. propname, (2 * i) + 1, ret);
  3075. return -EINVAL;
  3076. }
  3077. }
  3078. card->num_of_dapm_routes = num_routes;
  3079. card->of_dapm_routes = routes;
  3080. return 0;
  3081. }
  3082. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3083. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3084. const char *prefix,
  3085. struct device_node **bitclkmaster,
  3086. struct device_node **framemaster)
  3087. {
  3088. int ret, i;
  3089. char prop[128];
  3090. unsigned int format = 0;
  3091. int bit, frame;
  3092. const char *str;
  3093. struct {
  3094. char *name;
  3095. unsigned int val;
  3096. } of_fmt_table[] = {
  3097. { "i2s", SND_SOC_DAIFMT_I2S },
  3098. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3099. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3100. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3101. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3102. { "ac97", SND_SOC_DAIFMT_AC97 },
  3103. { "pdm", SND_SOC_DAIFMT_PDM},
  3104. { "msb", SND_SOC_DAIFMT_MSB },
  3105. { "lsb", SND_SOC_DAIFMT_LSB },
  3106. };
  3107. if (!prefix)
  3108. prefix = "";
  3109. /*
  3110. * check "[prefix]format = xxx"
  3111. * SND_SOC_DAIFMT_FORMAT_MASK area
  3112. */
  3113. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3114. ret = of_property_read_string(np, prop, &str);
  3115. if (ret == 0) {
  3116. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3117. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3118. format |= of_fmt_table[i].val;
  3119. break;
  3120. }
  3121. }
  3122. }
  3123. /*
  3124. * check "[prefix]continuous-clock"
  3125. * SND_SOC_DAIFMT_CLOCK_MASK area
  3126. */
  3127. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3128. if (of_property_read_bool(np, prop))
  3129. format |= SND_SOC_DAIFMT_CONT;
  3130. else
  3131. format |= SND_SOC_DAIFMT_GATED;
  3132. /*
  3133. * check "[prefix]bitclock-inversion"
  3134. * check "[prefix]frame-inversion"
  3135. * SND_SOC_DAIFMT_INV_MASK area
  3136. */
  3137. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3138. bit = !!of_get_property(np, prop, NULL);
  3139. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3140. frame = !!of_get_property(np, prop, NULL);
  3141. switch ((bit << 4) + frame) {
  3142. case 0x11:
  3143. format |= SND_SOC_DAIFMT_IB_IF;
  3144. break;
  3145. case 0x10:
  3146. format |= SND_SOC_DAIFMT_IB_NF;
  3147. break;
  3148. case 0x01:
  3149. format |= SND_SOC_DAIFMT_NB_IF;
  3150. break;
  3151. default:
  3152. /* SND_SOC_DAIFMT_NB_NF is default */
  3153. break;
  3154. }
  3155. /*
  3156. * check "[prefix]bitclock-master"
  3157. * check "[prefix]frame-master"
  3158. * SND_SOC_DAIFMT_MASTER_MASK area
  3159. */
  3160. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3161. bit = !!of_get_property(np, prop, NULL);
  3162. if (bit && bitclkmaster)
  3163. *bitclkmaster = of_parse_phandle(np, prop, 0);
  3164. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3165. frame = !!of_get_property(np, prop, NULL);
  3166. if (frame && framemaster)
  3167. *framemaster = of_parse_phandle(np, prop, 0);
  3168. switch ((bit << 4) + frame) {
  3169. case 0x11:
  3170. format |= SND_SOC_DAIFMT_CBM_CFM;
  3171. break;
  3172. case 0x10:
  3173. format |= SND_SOC_DAIFMT_CBM_CFS;
  3174. break;
  3175. case 0x01:
  3176. format |= SND_SOC_DAIFMT_CBS_CFM;
  3177. break;
  3178. default:
  3179. format |= SND_SOC_DAIFMT_CBS_CFS;
  3180. break;
  3181. }
  3182. return format;
  3183. }
  3184. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3185. static int snd_soc_get_dai_name(struct of_phandle_args *args,
  3186. const char **dai_name)
  3187. {
  3188. struct snd_soc_component *pos;
  3189. struct device_node *component_of_node;
  3190. int ret = -EPROBE_DEFER;
  3191. mutex_lock(&client_mutex);
  3192. list_for_each_entry(pos, &component_list, list) {
  3193. component_of_node = pos->dev->of_node;
  3194. if (!component_of_node && pos->dev->parent)
  3195. component_of_node = pos->dev->parent->of_node;
  3196. if (component_of_node != args->np)
  3197. continue;
  3198. if (pos->driver->of_xlate_dai_name) {
  3199. ret = pos->driver->of_xlate_dai_name(pos,
  3200. args,
  3201. dai_name);
  3202. } else {
  3203. int id = -1;
  3204. switch (args->args_count) {
  3205. case 0:
  3206. id = 0; /* same as dai_drv[0] */
  3207. break;
  3208. case 1:
  3209. id = args->args[0];
  3210. break;
  3211. default:
  3212. /* not supported */
  3213. break;
  3214. }
  3215. if (id < 0 || id >= pos->num_dai) {
  3216. ret = -EINVAL;
  3217. continue;
  3218. }
  3219. ret = 0;
  3220. *dai_name = pos->dai_drv[id].name;
  3221. if (!*dai_name)
  3222. *dai_name = pos->name;
  3223. }
  3224. break;
  3225. }
  3226. mutex_unlock(&client_mutex);
  3227. return ret;
  3228. }
  3229. int snd_soc_of_get_dai_name(struct device_node *of_node,
  3230. const char **dai_name)
  3231. {
  3232. struct of_phandle_args args;
  3233. int ret;
  3234. ret = of_parse_phandle_with_args(of_node, "sound-dai",
  3235. "#sound-dai-cells", 0, &args);
  3236. if (ret)
  3237. return ret;
  3238. ret = snd_soc_get_dai_name(&args, dai_name);
  3239. of_node_put(args.np);
  3240. return ret;
  3241. }
  3242. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
  3243. /*
  3244. * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
  3245. * @dev: Card device
  3246. * @of_node: Device node
  3247. * @dai_link: DAI link
  3248. *
  3249. * Builds an array of CODEC DAI components from the DAI link property
  3250. * 'sound-dai'.
  3251. * The array is set in the DAI link and the number of DAIs is set accordingly.
  3252. * The device nodes in the array (of_node) must be dereferenced by the caller.
  3253. *
  3254. * Returns 0 for success
  3255. */
  3256. int snd_soc_of_get_dai_link_codecs(struct device *dev,
  3257. struct device_node *of_node,
  3258. struct snd_soc_dai_link *dai_link)
  3259. {
  3260. struct of_phandle_args args;
  3261. struct snd_soc_dai_link_component *component;
  3262. char *name;
  3263. int index, num_codecs, ret;
  3264. /* Count the number of CODECs */
  3265. name = "sound-dai";
  3266. num_codecs = of_count_phandle_with_args(of_node, name,
  3267. "#sound-dai-cells");
  3268. if (num_codecs <= 0) {
  3269. if (num_codecs == -ENOENT)
  3270. dev_err(dev, "No 'sound-dai' property\n");
  3271. else
  3272. dev_err(dev, "Bad phandle in 'sound-dai'\n");
  3273. return num_codecs;
  3274. }
  3275. component = devm_kzalloc(dev,
  3276. sizeof *component * num_codecs,
  3277. GFP_KERNEL);
  3278. if (!component)
  3279. return -ENOMEM;
  3280. dai_link->codecs = component;
  3281. dai_link->num_codecs = num_codecs;
  3282. /* Parse the list */
  3283. for (index = 0, component = dai_link->codecs;
  3284. index < dai_link->num_codecs;
  3285. index++, component++) {
  3286. ret = of_parse_phandle_with_args(of_node, name,
  3287. "#sound-dai-cells",
  3288. index, &args);
  3289. if (ret)
  3290. goto err;
  3291. component->of_node = args.np;
  3292. ret = snd_soc_get_dai_name(&args, &component->dai_name);
  3293. if (ret < 0)
  3294. goto err;
  3295. }
  3296. return 0;
  3297. err:
  3298. for (index = 0, component = dai_link->codecs;
  3299. index < dai_link->num_codecs;
  3300. index++, component++) {
  3301. if (!component->of_node)
  3302. break;
  3303. of_node_put(component->of_node);
  3304. component->of_node = NULL;
  3305. }
  3306. dai_link->codecs = NULL;
  3307. dai_link->num_codecs = 0;
  3308. return ret;
  3309. }
  3310. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
  3311. static int __init snd_soc_init(void)
  3312. {
  3313. snd_soc_debugfs_init();
  3314. snd_soc_util_init();
  3315. return platform_driver_register(&soc_driver);
  3316. }
  3317. module_init(snd_soc_init);
  3318. static void __exit snd_soc_exit(void)
  3319. {
  3320. snd_soc_util_exit();
  3321. snd_soc_debugfs_exit();
  3322. #ifdef CONFIG_DEBUG_FS
  3323. #endif
  3324. platform_driver_unregister(&soc_driver);
  3325. }
  3326. module_exit(snd_soc_exit);
  3327. /* Module information */
  3328. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3329. MODULE_DESCRIPTION("ALSA SoC Core");
  3330. MODULE_LICENSE("GPL");
  3331. MODULE_ALIAS("platform:soc-audio");