soc-core.c 99 KB

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