soc-core.c 98 KB

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