soc-core.c 69 KB

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