soc-core.c 68 KB

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