soc-core.c 73 KB

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