soc-core.c 95 KB

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