soc-core.c 87 KB

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