soc-core.c 87 KB

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