soc-core.c 100 KB

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