soc-core.c 88 KB

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