soc-core.c 94 KB

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