soc-core.c 94 KB

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