soc-core.c 96 KB

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