soc-core.c 95 KB

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