soc-core.c 89 KB

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