soc-core.c 94 KB

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