soc-core.c 91 KB

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