soc-core.c 68 KB

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