soc-core.c 105 KB

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