soc-core.c 75 KB

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