soc-core.c 76 KB

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