soc-core.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  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_platform *platform;
  832. struct snd_soc_dai *dai;
  833. int i, found, ret, ac97;
  834. if (card->instantiated)
  835. return;
  836. found = 0;
  837. list_for_each_entry(platform, &platform_list, list)
  838. if (card->platform == platform) {
  839. found = 1;
  840. break;
  841. }
  842. if (!found) {
  843. dev_dbg(card->dev, "Platform %s not registered\n",
  844. card->platform->name);
  845. return;
  846. }
  847. ac97 = 0;
  848. for (i = 0; i < card->num_links; i++) {
  849. found = 0;
  850. list_for_each_entry(dai, &dai_list, list)
  851. if (card->dai_link[i].cpu_dai == dai) {
  852. found = 1;
  853. break;
  854. }
  855. if (!found) {
  856. dev_dbg(card->dev, "DAI %s not registered\n",
  857. card->dai_link[i].cpu_dai->name);
  858. return;
  859. }
  860. if (card->dai_link[i].cpu_dai->ac97_control)
  861. ac97 = 1;
  862. }
  863. for (i = 0; i < card->num_links; i++) {
  864. if (!card->dai_link[i].codec_dai->ops)
  865. card->dai_link[i].codec_dai->ops = &null_dai_ops;
  866. }
  867. /* If we have AC97 in the system then don't wait for the
  868. * codec. This will need revisiting if we have to handle
  869. * systems with mixed AC97 and non-AC97 parts. Only check for
  870. * DAIs currently; we can't do this per link since some AC97
  871. * codecs have non-AC97 DAIs.
  872. */
  873. if (!ac97)
  874. for (i = 0; i < card->num_links; i++) {
  875. found = 0;
  876. list_for_each_entry(dai, &dai_list, list)
  877. if (card->dai_link[i].codec_dai == dai) {
  878. found = 1;
  879. break;
  880. }
  881. if (!found) {
  882. dev_dbg(card->dev, "DAI %s not registered\n",
  883. card->dai_link[i].codec_dai->name);
  884. return;
  885. }
  886. }
  887. /* Note that we do not current check for codec components */
  888. dev_dbg(card->dev, "All components present, instantiating\n");
  889. /* Found everything, bring it up */
  890. if (card->probe) {
  891. ret = card->probe(pdev);
  892. if (ret < 0)
  893. return;
  894. }
  895. for (i = 0; i < card->num_links; i++) {
  896. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  897. if (cpu_dai->probe) {
  898. ret = cpu_dai->probe(pdev, cpu_dai);
  899. if (ret < 0)
  900. goto cpu_dai_err;
  901. }
  902. }
  903. if (codec_dev->probe) {
  904. ret = codec_dev->probe(pdev);
  905. if (ret < 0)
  906. goto cpu_dai_err;
  907. }
  908. if (platform->probe) {
  909. ret = platform->probe(pdev);
  910. if (ret < 0)
  911. goto platform_err;
  912. }
  913. /* DAPM stream work */
  914. INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
  915. #ifdef CONFIG_PM
  916. /* deferred resume work */
  917. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  918. #endif
  919. card->instantiated = 1;
  920. return;
  921. platform_err:
  922. if (codec_dev->remove)
  923. codec_dev->remove(pdev);
  924. cpu_dai_err:
  925. for (i--; i >= 0; i--) {
  926. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  927. if (cpu_dai->remove)
  928. cpu_dai->remove(pdev, cpu_dai);
  929. }
  930. if (card->remove)
  931. card->remove(pdev);
  932. }
  933. /*
  934. * Attempt to initialise any uninitalised cards. Must be called with
  935. * client_mutex.
  936. */
  937. static void snd_soc_instantiate_cards(void)
  938. {
  939. struct snd_soc_card *card;
  940. list_for_each_entry(card, &card_list, list)
  941. snd_soc_instantiate_card(card);
  942. }
  943. /* probes a new socdev */
  944. static int soc_probe(struct platform_device *pdev)
  945. {
  946. int ret = 0;
  947. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  948. struct snd_soc_card *card = socdev->card;
  949. /* Bodge while we push things out of socdev */
  950. card->socdev = socdev;
  951. /* Bodge while we unpick instantiation */
  952. card->dev = &pdev->dev;
  953. ret = snd_soc_register_card(card);
  954. if (ret != 0) {
  955. dev_err(&pdev->dev, "Failed to register card\n");
  956. return ret;
  957. }
  958. return 0;
  959. }
  960. /* removes a socdev */
  961. static int soc_remove(struct platform_device *pdev)
  962. {
  963. int i;
  964. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  965. struct snd_soc_card *card = socdev->card;
  966. struct snd_soc_platform *platform = card->platform;
  967. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  968. if (!card->instantiated)
  969. return 0;
  970. run_delayed_work(&card->delayed_work);
  971. if (platform->remove)
  972. platform->remove(pdev);
  973. if (codec_dev->remove)
  974. codec_dev->remove(pdev);
  975. for (i = 0; i < card->num_links; i++) {
  976. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  977. if (cpu_dai->remove)
  978. cpu_dai->remove(pdev, cpu_dai);
  979. }
  980. if (card->remove)
  981. card->remove(pdev);
  982. snd_soc_unregister_card(card);
  983. return 0;
  984. }
  985. static int soc_poweroff(struct device *dev)
  986. {
  987. struct platform_device *pdev = to_platform_device(dev);
  988. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  989. struct snd_soc_card *card = socdev->card;
  990. if (!card->instantiated)
  991. return 0;
  992. /* Flush out pmdown_time work - we actually do want to run it
  993. * now, we're shutting down so no imminent restart. */
  994. run_delayed_work(&card->delayed_work);
  995. snd_soc_dapm_shutdown(socdev);
  996. return 0;
  997. }
  998. static struct dev_pm_ops soc_pm_ops = {
  999. .suspend = soc_suspend,
  1000. .resume = soc_resume,
  1001. .poweroff = soc_poweroff,
  1002. };
  1003. /* ASoC platform driver */
  1004. static struct platform_driver soc_driver = {
  1005. .driver = {
  1006. .name = "soc-audio",
  1007. .owner = THIS_MODULE,
  1008. .pm = &soc_pm_ops,
  1009. },
  1010. .probe = soc_probe,
  1011. .remove = soc_remove,
  1012. };
  1013. /* create a new pcm */
  1014. static int soc_new_pcm(struct snd_soc_device *socdev,
  1015. struct snd_soc_dai_link *dai_link, int num)
  1016. {
  1017. struct snd_soc_card *card = socdev->card;
  1018. struct snd_soc_codec *codec = card->codec;
  1019. struct snd_soc_platform *platform = card->platform;
  1020. struct snd_soc_dai *codec_dai = dai_link->codec_dai;
  1021. struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
  1022. struct snd_soc_pcm_runtime *rtd;
  1023. struct snd_pcm *pcm;
  1024. char new_name[64];
  1025. int ret = 0, playback = 0, capture = 0;
  1026. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  1027. if (rtd == NULL)
  1028. return -ENOMEM;
  1029. rtd->dai = dai_link;
  1030. rtd->socdev = socdev;
  1031. codec_dai->codec = card->codec;
  1032. /* check client and interface hw capabilities */
  1033. sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
  1034. num);
  1035. if (codec_dai->playback.channels_min)
  1036. playback = 1;
  1037. if (codec_dai->capture.channels_min)
  1038. capture = 1;
  1039. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  1040. capture, &pcm);
  1041. if (ret < 0) {
  1042. printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
  1043. codec->name);
  1044. kfree(rtd);
  1045. return ret;
  1046. }
  1047. dai_link->pcm = pcm;
  1048. pcm->private_data = rtd;
  1049. soc_pcm_ops.mmap = platform->pcm_ops->mmap;
  1050. soc_pcm_ops.pointer = platform->pcm_ops->pointer;
  1051. soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
  1052. soc_pcm_ops.copy = platform->pcm_ops->copy;
  1053. soc_pcm_ops.silence = platform->pcm_ops->silence;
  1054. soc_pcm_ops.ack = platform->pcm_ops->ack;
  1055. soc_pcm_ops.page = platform->pcm_ops->page;
  1056. if (playback)
  1057. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  1058. if (capture)
  1059. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  1060. ret = platform->pcm_new(codec->card, codec_dai, pcm);
  1061. if (ret < 0) {
  1062. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  1063. kfree(rtd);
  1064. return ret;
  1065. }
  1066. pcm->private_free = platform->pcm_free;
  1067. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  1068. cpu_dai->name);
  1069. return ret;
  1070. }
  1071. /**
  1072. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1073. *
  1074. * @codec: CODEC to query.
  1075. * @reg: Register to query.
  1076. *
  1077. * Boolean function indiciating if a CODEC register is volatile.
  1078. */
  1079. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
  1080. {
  1081. if (codec->volatile_register)
  1082. return codec->volatile_register(reg);
  1083. else
  1084. return 0;
  1085. }
  1086. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1087. /**
  1088. * snd_soc_new_ac97_codec - initailise AC97 device
  1089. * @codec: audio codec
  1090. * @ops: AC97 bus operations
  1091. * @num: AC97 codec number
  1092. *
  1093. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1094. */
  1095. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1096. struct snd_ac97_bus_ops *ops, int num)
  1097. {
  1098. mutex_lock(&codec->mutex);
  1099. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1100. if (codec->ac97 == NULL) {
  1101. mutex_unlock(&codec->mutex);
  1102. return -ENOMEM;
  1103. }
  1104. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1105. if (codec->ac97->bus == NULL) {
  1106. kfree(codec->ac97);
  1107. codec->ac97 = NULL;
  1108. mutex_unlock(&codec->mutex);
  1109. return -ENOMEM;
  1110. }
  1111. codec->ac97->bus->ops = ops;
  1112. codec->ac97->num = num;
  1113. mutex_unlock(&codec->mutex);
  1114. return 0;
  1115. }
  1116. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1117. /**
  1118. * snd_soc_free_ac97_codec - free AC97 codec device
  1119. * @codec: audio codec
  1120. *
  1121. * Frees AC97 codec device resources.
  1122. */
  1123. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1124. {
  1125. mutex_lock(&codec->mutex);
  1126. kfree(codec->ac97->bus);
  1127. kfree(codec->ac97);
  1128. codec->ac97 = NULL;
  1129. mutex_unlock(&codec->mutex);
  1130. }
  1131. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1132. /**
  1133. * snd_soc_update_bits - update codec register bits
  1134. * @codec: audio codec
  1135. * @reg: codec register
  1136. * @mask: register mask
  1137. * @value: new value
  1138. *
  1139. * Writes new register value.
  1140. *
  1141. * Returns 1 for change else 0.
  1142. */
  1143. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1144. unsigned int mask, unsigned int value)
  1145. {
  1146. int change;
  1147. unsigned int old, new;
  1148. old = snd_soc_read(codec, reg);
  1149. new = (old & ~mask) | value;
  1150. change = old != new;
  1151. if (change)
  1152. snd_soc_write(codec, reg, new);
  1153. return change;
  1154. }
  1155. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1156. /**
  1157. * snd_soc_update_bits_locked - update codec register bits
  1158. * @codec: audio codec
  1159. * @reg: codec register
  1160. * @mask: register mask
  1161. * @value: new value
  1162. *
  1163. * Writes new register value, and takes the codec mutex.
  1164. *
  1165. * Returns 1 for change else 0.
  1166. */
  1167. static int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1168. unsigned short reg, unsigned int mask,
  1169. unsigned int value)
  1170. {
  1171. int change;
  1172. mutex_lock(&codec->mutex);
  1173. change = snd_soc_update_bits(codec, reg, mask, value);
  1174. mutex_unlock(&codec->mutex);
  1175. return change;
  1176. }
  1177. /**
  1178. * snd_soc_test_bits - test register for change
  1179. * @codec: audio codec
  1180. * @reg: codec register
  1181. * @mask: register mask
  1182. * @value: new value
  1183. *
  1184. * Tests a register with a new value and checks if the new value is
  1185. * different from the old value.
  1186. *
  1187. * Returns 1 for change else 0.
  1188. */
  1189. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1190. unsigned int mask, unsigned int value)
  1191. {
  1192. int change;
  1193. unsigned int old, new;
  1194. old = snd_soc_read(codec, reg);
  1195. new = (old & ~mask) | value;
  1196. change = old != new;
  1197. return change;
  1198. }
  1199. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1200. /**
  1201. * snd_soc_new_pcms - create new sound card and pcms
  1202. * @socdev: the SoC audio device
  1203. * @idx: ALSA card index
  1204. * @xid: card identification
  1205. *
  1206. * Create a new sound card based upon the codec and interface pcms.
  1207. *
  1208. * Returns 0 for success, else error.
  1209. */
  1210. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  1211. {
  1212. struct snd_soc_card *card = socdev->card;
  1213. struct snd_soc_codec *codec = card->codec;
  1214. int ret, i;
  1215. mutex_lock(&codec->mutex);
  1216. /* register a sound card */
  1217. ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
  1218. if (ret < 0) {
  1219. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1220. codec->name);
  1221. mutex_unlock(&codec->mutex);
  1222. return ret;
  1223. }
  1224. codec->socdev = socdev;
  1225. codec->card->dev = socdev->dev;
  1226. codec->card->private_data = codec;
  1227. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1228. /* create the pcms */
  1229. for (i = 0; i < card->num_links; i++) {
  1230. ret = soc_new_pcm(socdev, &card->dai_link[i], i);
  1231. if (ret < 0) {
  1232. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1233. card->dai_link[i].stream_name);
  1234. mutex_unlock(&codec->mutex);
  1235. return ret;
  1236. }
  1237. }
  1238. mutex_unlock(&codec->mutex);
  1239. return ret;
  1240. }
  1241. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1242. /**
  1243. * snd_soc_init_card - register sound card
  1244. * @socdev: the SoC audio device
  1245. *
  1246. * Register a SoC sound card. Also registers an AC97 device if the
  1247. * codec is AC97 for ad hoc devices.
  1248. *
  1249. * Returns 0 for success, else error.
  1250. */
  1251. int snd_soc_init_card(struct snd_soc_device *socdev)
  1252. {
  1253. struct snd_soc_card *card = socdev->card;
  1254. struct snd_soc_codec *codec = card->codec;
  1255. int ret = 0, i, ac97 = 0, err = 0;
  1256. for (i = 0; i < card->num_links; i++) {
  1257. if (card->dai_link[i].init) {
  1258. err = card->dai_link[i].init(codec);
  1259. if (err < 0) {
  1260. printk(KERN_ERR "asoc: failed to init %s\n",
  1261. card->dai_link[i].stream_name);
  1262. continue;
  1263. }
  1264. }
  1265. if (card->dai_link[i].codec_dai->ac97_control) {
  1266. ac97 = 1;
  1267. snd_ac97_dev_add_pdata(codec->ac97,
  1268. card->dai_link[i].cpu_dai->ac97_pdata);
  1269. }
  1270. }
  1271. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1272. "%s", card->name);
  1273. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1274. "%s (%s)", card->name, codec->name);
  1275. /* Make sure all DAPM widgets are instantiated */
  1276. snd_soc_dapm_new_widgets(codec);
  1277. ret = snd_card_register(codec->card);
  1278. if (ret < 0) {
  1279. printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
  1280. codec->name);
  1281. goto out;
  1282. }
  1283. mutex_lock(&codec->mutex);
  1284. #ifdef CONFIG_SND_SOC_AC97_BUS
  1285. /* Only instantiate AC97 if not already done by the adaptor
  1286. * for the generic AC97 subsystem.
  1287. */
  1288. if (ac97 && strcmp(codec->name, "AC97") != 0) {
  1289. ret = soc_ac97_dev_register(codec);
  1290. if (ret < 0) {
  1291. printk(KERN_ERR "asoc: AC97 device register failed\n");
  1292. snd_card_free(codec->card);
  1293. mutex_unlock(&codec->mutex);
  1294. goto out;
  1295. }
  1296. }
  1297. #endif
  1298. err = snd_soc_dapm_sys_add(socdev->dev);
  1299. if (err < 0)
  1300. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  1301. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  1302. if (err < 0)
  1303. printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
  1304. soc_init_codec_debugfs(codec);
  1305. mutex_unlock(&codec->mutex);
  1306. out:
  1307. return ret;
  1308. }
  1309. EXPORT_SYMBOL_GPL(snd_soc_init_card);
  1310. /**
  1311. * snd_soc_free_pcms - free sound card and pcms
  1312. * @socdev: the SoC audio device
  1313. *
  1314. * Frees sound card and pcms associated with the socdev.
  1315. * Also unregister the codec if it is an AC97 device.
  1316. */
  1317. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1318. {
  1319. struct snd_soc_codec *codec = socdev->card->codec;
  1320. #ifdef CONFIG_SND_SOC_AC97_BUS
  1321. struct snd_soc_dai *codec_dai;
  1322. int i;
  1323. #endif
  1324. mutex_lock(&codec->mutex);
  1325. soc_cleanup_codec_debugfs(codec);
  1326. #ifdef CONFIG_SND_SOC_AC97_BUS
  1327. for (i = 0; i < codec->num_dai; i++) {
  1328. codec_dai = &codec->dai[i];
  1329. if (codec_dai->ac97_control && codec->ac97 &&
  1330. strcmp(codec->name, "AC97") != 0) {
  1331. soc_ac97_dev_unregister(codec);
  1332. goto free_card;
  1333. }
  1334. }
  1335. free_card:
  1336. #endif
  1337. if (codec->card)
  1338. snd_card_free(codec->card);
  1339. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1340. mutex_unlock(&codec->mutex);
  1341. }
  1342. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1343. /**
  1344. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1345. * @substream: the pcm substream
  1346. * @hw: the hardware parameters
  1347. *
  1348. * Sets the substream runtime hardware parameters.
  1349. */
  1350. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1351. const struct snd_pcm_hardware *hw)
  1352. {
  1353. struct snd_pcm_runtime *runtime = substream->runtime;
  1354. runtime->hw.info = hw->info;
  1355. runtime->hw.formats = hw->formats;
  1356. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1357. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1358. runtime->hw.periods_min = hw->periods_min;
  1359. runtime->hw.periods_max = hw->periods_max;
  1360. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1361. runtime->hw.fifo_size = hw->fifo_size;
  1362. return 0;
  1363. }
  1364. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1365. /**
  1366. * snd_soc_cnew - create new control
  1367. * @_template: control template
  1368. * @data: control private data
  1369. * @long_name: control long name
  1370. *
  1371. * Create a new mixer control from a template control.
  1372. *
  1373. * Returns 0 for success, else error.
  1374. */
  1375. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1376. void *data, char *long_name)
  1377. {
  1378. struct snd_kcontrol_new template;
  1379. memcpy(&template, _template, sizeof(template));
  1380. if (long_name)
  1381. template.name = long_name;
  1382. template.index = 0;
  1383. return snd_ctl_new1(&template, data);
  1384. }
  1385. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1386. /**
  1387. * snd_soc_add_controls - add an array of controls to a codec.
  1388. * Convienience function to add a list of controls. Many codecs were
  1389. * duplicating this code.
  1390. *
  1391. * @codec: codec to add controls to
  1392. * @controls: array of controls to add
  1393. * @num_controls: number of elements in the array
  1394. *
  1395. * Return 0 for success, else error.
  1396. */
  1397. int snd_soc_add_controls(struct snd_soc_codec *codec,
  1398. const struct snd_kcontrol_new *controls, int num_controls)
  1399. {
  1400. struct snd_card *card = codec->card;
  1401. int err, i;
  1402. for (i = 0; i < num_controls; i++) {
  1403. const struct snd_kcontrol_new *control = &controls[i];
  1404. err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
  1405. if (err < 0) {
  1406. dev_err(codec->dev, "%s: Failed to add %s\n",
  1407. codec->name, control->name);
  1408. return err;
  1409. }
  1410. }
  1411. return 0;
  1412. }
  1413. EXPORT_SYMBOL_GPL(snd_soc_add_controls);
  1414. /**
  1415. * snd_soc_info_enum_double - enumerated double mixer info callback
  1416. * @kcontrol: mixer control
  1417. * @uinfo: control element information
  1418. *
  1419. * Callback to provide information about a double enumerated
  1420. * mixer control.
  1421. *
  1422. * Returns 0 for success.
  1423. */
  1424. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1425. struct snd_ctl_elem_info *uinfo)
  1426. {
  1427. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1428. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1429. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1430. uinfo->value.enumerated.items = e->max;
  1431. if (uinfo->value.enumerated.item > e->max - 1)
  1432. uinfo->value.enumerated.item = e->max - 1;
  1433. strcpy(uinfo->value.enumerated.name,
  1434. e->texts[uinfo->value.enumerated.item]);
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1438. /**
  1439. * snd_soc_get_enum_double - enumerated double mixer get callback
  1440. * @kcontrol: mixer control
  1441. * @ucontrol: control element information
  1442. *
  1443. * Callback to get the value of a double enumerated mixer.
  1444. *
  1445. * Returns 0 for success.
  1446. */
  1447. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1448. struct snd_ctl_elem_value *ucontrol)
  1449. {
  1450. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1451. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1452. unsigned int val, bitmask;
  1453. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1454. ;
  1455. val = snd_soc_read(codec, e->reg);
  1456. ucontrol->value.enumerated.item[0]
  1457. = (val >> e->shift_l) & (bitmask - 1);
  1458. if (e->shift_l != e->shift_r)
  1459. ucontrol->value.enumerated.item[1] =
  1460. (val >> e->shift_r) & (bitmask - 1);
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1464. /**
  1465. * snd_soc_put_enum_double - enumerated double mixer put callback
  1466. * @kcontrol: mixer control
  1467. * @ucontrol: control element information
  1468. *
  1469. * Callback to set the value of a double enumerated mixer.
  1470. *
  1471. * Returns 0 for success.
  1472. */
  1473. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1474. struct snd_ctl_elem_value *ucontrol)
  1475. {
  1476. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1477. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1478. unsigned int val;
  1479. unsigned int mask, bitmask;
  1480. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1481. ;
  1482. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1483. return -EINVAL;
  1484. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1485. mask = (bitmask - 1) << e->shift_l;
  1486. if (e->shift_l != e->shift_r) {
  1487. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1488. return -EINVAL;
  1489. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1490. mask |= (bitmask - 1) << e->shift_r;
  1491. }
  1492. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  1493. }
  1494. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1495. /**
  1496. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  1497. * @kcontrol: mixer control
  1498. * @ucontrol: control element information
  1499. *
  1500. * Callback to get the value of a double semi enumerated mixer.
  1501. *
  1502. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1503. * used for handling bitfield coded enumeration for example.
  1504. *
  1505. * Returns 0 for success.
  1506. */
  1507. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1508. struct snd_ctl_elem_value *ucontrol)
  1509. {
  1510. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1511. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1512. unsigned int reg_val, val, mux;
  1513. reg_val = snd_soc_read(codec, e->reg);
  1514. val = (reg_val >> e->shift_l) & e->mask;
  1515. for (mux = 0; mux < e->max; mux++) {
  1516. if (val == e->values[mux])
  1517. break;
  1518. }
  1519. ucontrol->value.enumerated.item[0] = mux;
  1520. if (e->shift_l != e->shift_r) {
  1521. val = (reg_val >> e->shift_r) & e->mask;
  1522. for (mux = 0; mux < e->max; mux++) {
  1523. if (val == e->values[mux])
  1524. break;
  1525. }
  1526. ucontrol->value.enumerated.item[1] = mux;
  1527. }
  1528. return 0;
  1529. }
  1530. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  1531. /**
  1532. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  1533. * @kcontrol: mixer control
  1534. * @ucontrol: control element information
  1535. *
  1536. * Callback to set the value of a double semi enumerated mixer.
  1537. *
  1538. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1539. * used for handling bitfield coded enumeration for example.
  1540. *
  1541. * Returns 0 for success.
  1542. */
  1543. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1544. struct snd_ctl_elem_value *ucontrol)
  1545. {
  1546. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1547. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1548. unsigned int val;
  1549. unsigned int mask;
  1550. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1551. return -EINVAL;
  1552. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1553. mask = e->mask << e->shift_l;
  1554. if (e->shift_l != e->shift_r) {
  1555. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1556. return -EINVAL;
  1557. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1558. mask |= e->mask << e->shift_r;
  1559. }
  1560. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  1561. }
  1562. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  1563. /**
  1564. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1565. * @kcontrol: mixer control
  1566. * @uinfo: control element information
  1567. *
  1568. * Callback to provide information about an external enumerated
  1569. * single mixer.
  1570. *
  1571. * Returns 0 for success.
  1572. */
  1573. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1574. struct snd_ctl_elem_info *uinfo)
  1575. {
  1576. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1577. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1578. uinfo->count = 1;
  1579. uinfo->value.enumerated.items = e->max;
  1580. if (uinfo->value.enumerated.item > e->max - 1)
  1581. uinfo->value.enumerated.item = e->max - 1;
  1582. strcpy(uinfo->value.enumerated.name,
  1583. e->texts[uinfo->value.enumerated.item]);
  1584. return 0;
  1585. }
  1586. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1587. /**
  1588. * snd_soc_info_volsw_ext - external single mixer info callback
  1589. * @kcontrol: mixer control
  1590. * @uinfo: control element information
  1591. *
  1592. * Callback to provide information about a single external mixer control.
  1593. *
  1594. * Returns 0 for success.
  1595. */
  1596. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1597. struct snd_ctl_elem_info *uinfo)
  1598. {
  1599. int max = kcontrol->private_value;
  1600. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1601. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1602. else
  1603. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1604. uinfo->count = 1;
  1605. uinfo->value.integer.min = 0;
  1606. uinfo->value.integer.max = max;
  1607. return 0;
  1608. }
  1609. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1610. /**
  1611. * snd_soc_info_volsw - single mixer info callback
  1612. * @kcontrol: mixer control
  1613. * @uinfo: control element information
  1614. *
  1615. * Callback to provide information about a single mixer control.
  1616. *
  1617. * Returns 0 for success.
  1618. */
  1619. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1620. struct snd_ctl_elem_info *uinfo)
  1621. {
  1622. struct soc_mixer_control *mc =
  1623. (struct soc_mixer_control *)kcontrol->private_value;
  1624. int max = mc->max;
  1625. unsigned int shift = mc->shift;
  1626. unsigned int rshift = mc->rshift;
  1627. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1628. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1629. else
  1630. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1631. uinfo->count = shift == rshift ? 1 : 2;
  1632. uinfo->value.integer.min = 0;
  1633. uinfo->value.integer.max = max;
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1637. /**
  1638. * snd_soc_get_volsw - single mixer get callback
  1639. * @kcontrol: mixer control
  1640. * @ucontrol: control element information
  1641. *
  1642. * Callback to get the value of a single mixer control.
  1643. *
  1644. * Returns 0 for success.
  1645. */
  1646. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1647. struct snd_ctl_elem_value *ucontrol)
  1648. {
  1649. struct soc_mixer_control *mc =
  1650. (struct soc_mixer_control *)kcontrol->private_value;
  1651. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1652. unsigned int reg = mc->reg;
  1653. unsigned int shift = mc->shift;
  1654. unsigned int rshift = mc->rshift;
  1655. int max = mc->max;
  1656. unsigned int mask = (1 << fls(max)) - 1;
  1657. unsigned int invert = mc->invert;
  1658. ucontrol->value.integer.value[0] =
  1659. (snd_soc_read(codec, reg) >> shift) & mask;
  1660. if (shift != rshift)
  1661. ucontrol->value.integer.value[1] =
  1662. (snd_soc_read(codec, reg) >> rshift) & mask;
  1663. if (invert) {
  1664. ucontrol->value.integer.value[0] =
  1665. max - ucontrol->value.integer.value[0];
  1666. if (shift != rshift)
  1667. ucontrol->value.integer.value[1] =
  1668. max - ucontrol->value.integer.value[1];
  1669. }
  1670. return 0;
  1671. }
  1672. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1673. /**
  1674. * snd_soc_put_volsw - single mixer put callback
  1675. * @kcontrol: mixer control
  1676. * @ucontrol: control element information
  1677. *
  1678. * Callback to set the value of a single mixer control.
  1679. *
  1680. * Returns 0 for success.
  1681. */
  1682. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1683. struct snd_ctl_elem_value *ucontrol)
  1684. {
  1685. struct soc_mixer_control *mc =
  1686. (struct soc_mixer_control *)kcontrol->private_value;
  1687. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1688. unsigned int reg = mc->reg;
  1689. unsigned int shift = mc->shift;
  1690. unsigned int rshift = mc->rshift;
  1691. int max = mc->max;
  1692. unsigned int mask = (1 << fls(max)) - 1;
  1693. unsigned int invert = mc->invert;
  1694. unsigned int val, val2, val_mask;
  1695. val = (ucontrol->value.integer.value[0] & mask);
  1696. if (invert)
  1697. val = max - val;
  1698. val_mask = mask << shift;
  1699. val = val << shift;
  1700. if (shift != rshift) {
  1701. val2 = (ucontrol->value.integer.value[1] & mask);
  1702. if (invert)
  1703. val2 = max - val2;
  1704. val_mask |= mask << rshift;
  1705. val |= val2 << rshift;
  1706. }
  1707. return snd_soc_update_bits_locked(codec, reg, val_mask, val);
  1708. }
  1709. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1710. /**
  1711. * snd_soc_info_volsw_2r - double mixer info callback
  1712. * @kcontrol: mixer control
  1713. * @uinfo: control element information
  1714. *
  1715. * Callback to provide information about a double mixer control that
  1716. * spans 2 codec registers.
  1717. *
  1718. * Returns 0 for success.
  1719. */
  1720. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1721. struct snd_ctl_elem_info *uinfo)
  1722. {
  1723. struct soc_mixer_control *mc =
  1724. (struct soc_mixer_control *)kcontrol->private_value;
  1725. int max = mc->max;
  1726. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1727. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1728. else
  1729. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1730. uinfo->count = 2;
  1731. uinfo->value.integer.min = 0;
  1732. uinfo->value.integer.max = max;
  1733. return 0;
  1734. }
  1735. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1736. /**
  1737. * snd_soc_get_volsw_2r - double mixer get callback
  1738. * @kcontrol: mixer control
  1739. * @ucontrol: control element information
  1740. *
  1741. * Callback to get the value of a double mixer control that spans 2 registers.
  1742. *
  1743. * Returns 0 for success.
  1744. */
  1745. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1746. struct snd_ctl_elem_value *ucontrol)
  1747. {
  1748. struct soc_mixer_control *mc =
  1749. (struct soc_mixer_control *)kcontrol->private_value;
  1750. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1751. unsigned int reg = mc->reg;
  1752. unsigned int reg2 = mc->rreg;
  1753. unsigned int shift = mc->shift;
  1754. int max = mc->max;
  1755. unsigned int mask = (1 << fls(max)) - 1;
  1756. unsigned int invert = mc->invert;
  1757. ucontrol->value.integer.value[0] =
  1758. (snd_soc_read(codec, reg) >> shift) & mask;
  1759. ucontrol->value.integer.value[1] =
  1760. (snd_soc_read(codec, reg2) >> shift) & mask;
  1761. if (invert) {
  1762. ucontrol->value.integer.value[0] =
  1763. max - ucontrol->value.integer.value[0];
  1764. ucontrol->value.integer.value[1] =
  1765. max - ucontrol->value.integer.value[1];
  1766. }
  1767. return 0;
  1768. }
  1769. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1770. /**
  1771. * snd_soc_put_volsw_2r - double mixer set callback
  1772. * @kcontrol: mixer control
  1773. * @ucontrol: control element information
  1774. *
  1775. * Callback to set the value of a double mixer control that spans 2 registers.
  1776. *
  1777. * Returns 0 for success.
  1778. */
  1779. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1780. struct snd_ctl_elem_value *ucontrol)
  1781. {
  1782. struct soc_mixer_control *mc =
  1783. (struct soc_mixer_control *)kcontrol->private_value;
  1784. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1785. unsigned int reg = mc->reg;
  1786. unsigned int reg2 = mc->rreg;
  1787. unsigned int shift = mc->shift;
  1788. int max = mc->max;
  1789. unsigned int mask = (1 << fls(max)) - 1;
  1790. unsigned int invert = mc->invert;
  1791. int err;
  1792. unsigned int val, val2, val_mask;
  1793. val_mask = mask << shift;
  1794. val = (ucontrol->value.integer.value[0] & mask);
  1795. val2 = (ucontrol->value.integer.value[1] & mask);
  1796. if (invert) {
  1797. val = max - val;
  1798. val2 = max - val2;
  1799. }
  1800. val = val << shift;
  1801. val2 = val2 << shift;
  1802. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  1803. if (err < 0)
  1804. return err;
  1805. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  1806. return err;
  1807. }
  1808. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1809. /**
  1810. * snd_soc_info_volsw_s8 - signed mixer info callback
  1811. * @kcontrol: mixer control
  1812. * @uinfo: control element information
  1813. *
  1814. * Callback to provide information about a signed mixer control.
  1815. *
  1816. * Returns 0 for success.
  1817. */
  1818. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  1819. struct snd_ctl_elem_info *uinfo)
  1820. {
  1821. struct soc_mixer_control *mc =
  1822. (struct soc_mixer_control *)kcontrol->private_value;
  1823. int max = mc->max;
  1824. int min = mc->min;
  1825. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1826. uinfo->count = 2;
  1827. uinfo->value.integer.min = 0;
  1828. uinfo->value.integer.max = max-min;
  1829. return 0;
  1830. }
  1831. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  1832. /**
  1833. * snd_soc_get_volsw_s8 - signed mixer get callback
  1834. * @kcontrol: mixer control
  1835. * @ucontrol: control element information
  1836. *
  1837. * Callback to get the value of a signed mixer control.
  1838. *
  1839. * Returns 0 for success.
  1840. */
  1841. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  1842. struct snd_ctl_elem_value *ucontrol)
  1843. {
  1844. struct soc_mixer_control *mc =
  1845. (struct soc_mixer_control *)kcontrol->private_value;
  1846. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1847. unsigned int reg = mc->reg;
  1848. int min = mc->min;
  1849. int val = snd_soc_read(codec, reg);
  1850. ucontrol->value.integer.value[0] =
  1851. ((signed char)(val & 0xff))-min;
  1852. ucontrol->value.integer.value[1] =
  1853. ((signed char)((val >> 8) & 0xff))-min;
  1854. return 0;
  1855. }
  1856. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  1857. /**
  1858. * snd_soc_put_volsw_sgn - signed mixer put callback
  1859. * @kcontrol: mixer control
  1860. * @ucontrol: control element information
  1861. *
  1862. * Callback to set the value of a signed mixer control.
  1863. *
  1864. * Returns 0 for success.
  1865. */
  1866. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  1867. struct snd_ctl_elem_value *ucontrol)
  1868. {
  1869. struct soc_mixer_control *mc =
  1870. (struct soc_mixer_control *)kcontrol->private_value;
  1871. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1872. unsigned int reg = mc->reg;
  1873. int min = mc->min;
  1874. unsigned int val;
  1875. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  1876. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  1877. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  1878. }
  1879. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  1880. /**
  1881. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1882. * @dai: DAI
  1883. * @clk_id: DAI specific clock ID
  1884. * @freq: new clock frequency in Hz
  1885. * @dir: new clock direction - input/output.
  1886. *
  1887. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1888. */
  1889. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1890. unsigned int freq, int dir)
  1891. {
  1892. if (dai->ops && dai->ops->set_sysclk)
  1893. return dai->ops->set_sysclk(dai, clk_id, freq, dir);
  1894. else
  1895. return -EINVAL;
  1896. }
  1897. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1898. /**
  1899. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1900. * @dai: DAI
  1901. * @div_id: DAI specific clock divider ID
  1902. * @div: new clock divisor.
  1903. *
  1904. * Configures the clock dividers. This is used to derive the best DAI bit and
  1905. * frame clocks from the system or master clock. It's best to set the DAI bit
  1906. * and frame clocks as low as possible to save system power.
  1907. */
  1908. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1909. int div_id, int div)
  1910. {
  1911. if (dai->ops && dai->ops->set_clkdiv)
  1912. return dai->ops->set_clkdiv(dai, div_id, div);
  1913. else
  1914. return -EINVAL;
  1915. }
  1916. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1917. /**
  1918. * snd_soc_dai_set_pll - configure DAI PLL.
  1919. * @dai: DAI
  1920. * @pll_id: DAI specific PLL ID
  1921. * @source: DAI specific source for the PLL
  1922. * @freq_in: PLL input clock frequency in Hz
  1923. * @freq_out: requested PLL output clock frequency in Hz
  1924. *
  1925. * Configures and enables PLL to generate output clock based on input clock.
  1926. */
  1927. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  1928. unsigned int freq_in, unsigned int freq_out)
  1929. {
  1930. if (dai->ops && dai->ops->set_pll)
  1931. return dai->ops->set_pll(dai, pll_id, source,
  1932. freq_in, freq_out);
  1933. else
  1934. return -EINVAL;
  1935. }
  1936. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  1937. /**
  1938. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  1939. * @dai: DAI
  1940. * @fmt: SND_SOC_DAIFMT_ format value.
  1941. *
  1942. * Configures the DAI hardware format and clocking.
  1943. */
  1944. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1945. {
  1946. if (dai->ops && dai->ops->set_fmt)
  1947. return dai->ops->set_fmt(dai, fmt);
  1948. else
  1949. return -EINVAL;
  1950. }
  1951. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  1952. /**
  1953. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  1954. * @dai: DAI
  1955. * @tx_mask: bitmask representing active TX slots.
  1956. * @rx_mask: bitmask representing active RX slots.
  1957. * @slots: Number of slots in use.
  1958. * @slot_width: Width in bits for each slot.
  1959. *
  1960. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  1961. * specific.
  1962. */
  1963. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  1964. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  1965. {
  1966. if (dai->ops && dai->ops->set_tdm_slot)
  1967. return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  1968. slots, slot_width);
  1969. else
  1970. return -EINVAL;
  1971. }
  1972. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  1973. /**
  1974. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  1975. * @dai: DAI
  1976. * @tx_num: how many TX channels
  1977. * @tx_slot: pointer to an array which imply the TX slot number channel
  1978. * 0~num-1 uses
  1979. * @rx_num: how many RX channels
  1980. * @rx_slot: pointer to an array which imply the RX slot number channel
  1981. * 0~num-1 uses
  1982. *
  1983. * configure the relationship between channel number and TDM slot number.
  1984. */
  1985. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  1986. unsigned int tx_num, unsigned int *tx_slot,
  1987. unsigned int rx_num, unsigned int *rx_slot)
  1988. {
  1989. if (dai->ops && dai->ops->set_channel_map)
  1990. return dai->ops->set_channel_map(dai, tx_num, tx_slot,
  1991. rx_num, rx_slot);
  1992. else
  1993. return -EINVAL;
  1994. }
  1995. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  1996. /**
  1997. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  1998. * @dai: DAI
  1999. * @tristate: tristate enable
  2000. *
  2001. * Tristates the DAI so that others can use it.
  2002. */
  2003. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  2004. {
  2005. if (dai->ops && dai->ops->set_tristate)
  2006. return dai->ops->set_tristate(dai, tristate);
  2007. else
  2008. return -EINVAL;
  2009. }
  2010. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  2011. /**
  2012. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  2013. * @dai: DAI
  2014. * @mute: mute enable
  2015. *
  2016. * Mutes the DAI DAC.
  2017. */
  2018. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  2019. {
  2020. if (dai->ops && dai->ops->digital_mute)
  2021. return dai->ops->digital_mute(dai, mute);
  2022. else
  2023. return -EINVAL;
  2024. }
  2025. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  2026. /**
  2027. * snd_soc_register_card - Register a card with the ASoC core
  2028. *
  2029. * @card: Card to register
  2030. *
  2031. * Note that currently this is an internal only function: it will be
  2032. * exposed to machine drivers after further backporting of ASoC v2
  2033. * registration APIs.
  2034. */
  2035. static int snd_soc_register_card(struct snd_soc_card *card)
  2036. {
  2037. if (!card->name || !card->dev)
  2038. return -EINVAL;
  2039. INIT_LIST_HEAD(&card->list);
  2040. card->instantiated = 0;
  2041. mutex_lock(&client_mutex);
  2042. list_add(&card->list, &card_list);
  2043. snd_soc_instantiate_cards();
  2044. mutex_unlock(&client_mutex);
  2045. dev_dbg(card->dev, "Registered card '%s'\n", card->name);
  2046. return 0;
  2047. }
  2048. /**
  2049. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2050. *
  2051. * @card: Card to unregister
  2052. *
  2053. * Note that currently this is an internal only function: it will be
  2054. * exposed to machine drivers after further backporting of ASoC v2
  2055. * registration APIs.
  2056. */
  2057. static int snd_soc_unregister_card(struct snd_soc_card *card)
  2058. {
  2059. mutex_lock(&client_mutex);
  2060. list_del(&card->list);
  2061. mutex_unlock(&client_mutex);
  2062. dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
  2063. return 0;
  2064. }
  2065. /**
  2066. * snd_soc_register_dai - Register a DAI with the ASoC core
  2067. *
  2068. * @dai: DAI to register
  2069. */
  2070. int snd_soc_register_dai(struct snd_soc_dai *dai)
  2071. {
  2072. if (!dai->name)
  2073. return -EINVAL;
  2074. /* The device should become mandatory over time */
  2075. if (!dai->dev)
  2076. printk(KERN_WARNING "No device for DAI %s\n", dai->name);
  2077. if (!dai->ops)
  2078. dai->ops = &null_dai_ops;
  2079. INIT_LIST_HEAD(&dai->list);
  2080. mutex_lock(&client_mutex);
  2081. list_add(&dai->list, &dai_list);
  2082. snd_soc_instantiate_cards();
  2083. mutex_unlock(&client_mutex);
  2084. pr_debug("Registered DAI '%s'\n", dai->name);
  2085. return 0;
  2086. }
  2087. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2088. /**
  2089. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  2090. *
  2091. * @dai: DAI to unregister
  2092. */
  2093. void snd_soc_unregister_dai(struct snd_soc_dai *dai)
  2094. {
  2095. mutex_lock(&client_mutex);
  2096. list_del(&dai->list);
  2097. mutex_unlock(&client_mutex);
  2098. pr_debug("Unregistered DAI '%s'\n", dai->name);
  2099. }
  2100. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  2101. /**
  2102. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  2103. *
  2104. * @dai: Array of DAIs to register
  2105. * @count: Number of DAIs
  2106. */
  2107. int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
  2108. {
  2109. int i, ret;
  2110. for (i = 0; i < count; i++) {
  2111. ret = snd_soc_register_dai(&dai[i]);
  2112. if (ret != 0)
  2113. goto err;
  2114. }
  2115. return 0;
  2116. err:
  2117. for (i--; i >= 0; i--)
  2118. snd_soc_unregister_dai(&dai[i]);
  2119. return ret;
  2120. }
  2121. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  2122. /**
  2123. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  2124. *
  2125. * @dai: Array of DAIs to unregister
  2126. * @count: Number of DAIs
  2127. */
  2128. void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
  2129. {
  2130. int i;
  2131. for (i = 0; i < count; i++)
  2132. snd_soc_unregister_dai(&dai[i]);
  2133. }
  2134. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  2135. /**
  2136. * snd_soc_register_platform - Register a platform with the ASoC core
  2137. *
  2138. * @platform: platform to register
  2139. */
  2140. int snd_soc_register_platform(struct snd_soc_platform *platform)
  2141. {
  2142. if (!platform->name)
  2143. return -EINVAL;
  2144. INIT_LIST_HEAD(&platform->list);
  2145. mutex_lock(&client_mutex);
  2146. list_add(&platform->list, &platform_list);
  2147. snd_soc_instantiate_cards();
  2148. mutex_unlock(&client_mutex);
  2149. pr_debug("Registered platform '%s'\n", platform->name);
  2150. return 0;
  2151. }
  2152. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2153. /**
  2154. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2155. *
  2156. * @platform: platform to unregister
  2157. */
  2158. void snd_soc_unregister_platform(struct snd_soc_platform *platform)
  2159. {
  2160. mutex_lock(&client_mutex);
  2161. list_del(&platform->list);
  2162. mutex_unlock(&client_mutex);
  2163. pr_debug("Unregistered platform '%s'\n", platform->name);
  2164. }
  2165. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2166. static u64 codec_format_map[] = {
  2167. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2168. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2169. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2170. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2171. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2172. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2173. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2174. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2175. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2176. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2177. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2178. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2179. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2180. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2181. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2182. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2183. };
  2184. /* Fix up the DAI formats for endianness: codecs don't actually see
  2185. * the endianness of the data but we're using the CPU format
  2186. * definitions which do need to include endianness so we ensure that
  2187. * codec DAIs always have both big and little endian variants set.
  2188. */
  2189. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2190. {
  2191. int i;
  2192. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2193. if (stream->formats & codec_format_map[i])
  2194. stream->formats |= codec_format_map[i];
  2195. }
  2196. /**
  2197. * snd_soc_register_codec - Register a codec with the ASoC core
  2198. *
  2199. * @codec: codec to register
  2200. */
  2201. int snd_soc_register_codec(struct snd_soc_codec *codec)
  2202. {
  2203. int i;
  2204. if (!codec->name)
  2205. return -EINVAL;
  2206. /* The device should become mandatory over time */
  2207. if (!codec->dev)
  2208. printk(KERN_WARNING "No device for codec %s\n", codec->name);
  2209. INIT_LIST_HEAD(&codec->list);
  2210. for (i = 0; i < codec->num_dai; i++) {
  2211. fixup_codec_formats(&codec->dai[i].playback);
  2212. fixup_codec_formats(&codec->dai[i].capture);
  2213. }
  2214. mutex_lock(&client_mutex);
  2215. list_add(&codec->list, &codec_list);
  2216. snd_soc_instantiate_cards();
  2217. mutex_unlock(&client_mutex);
  2218. pr_debug("Registered codec '%s'\n", codec->name);
  2219. return 0;
  2220. }
  2221. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2222. /**
  2223. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2224. *
  2225. * @codec: codec to unregister
  2226. */
  2227. void snd_soc_unregister_codec(struct snd_soc_codec *codec)
  2228. {
  2229. mutex_lock(&client_mutex);
  2230. list_del(&codec->list);
  2231. mutex_unlock(&client_mutex);
  2232. pr_debug("Unregistered codec '%s'\n", codec->name);
  2233. }
  2234. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2235. static int __init snd_soc_init(void)
  2236. {
  2237. #ifdef CONFIG_DEBUG_FS
  2238. debugfs_root = debugfs_create_dir("asoc", NULL);
  2239. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2240. printk(KERN_WARNING
  2241. "ASoC: Failed to create debugfs directory\n");
  2242. debugfs_root = NULL;
  2243. }
  2244. #endif
  2245. return platform_driver_register(&soc_driver);
  2246. }
  2247. static void __exit snd_soc_exit(void)
  2248. {
  2249. #ifdef CONFIG_DEBUG_FS
  2250. debugfs_remove_recursive(debugfs_root);
  2251. #endif
  2252. platform_driver_unregister(&soc_driver);
  2253. }
  2254. module_init(snd_soc_init);
  2255. module_exit(snd_soc_exit);
  2256. /* Module information */
  2257. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2258. MODULE_DESCRIPTION("ALSA SoC Core");
  2259. MODULE_LICENSE("GPL");
  2260. MODULE_ALIAS("platform:soc-audio");