soc-core.c 100 KB

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