soc-core.c 86 KB

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