soc-core.c 94 KB

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