soc-core.c 87 KB

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