soc-core.c 86 KB

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