soc-core.c 70 KB

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