soc-core.c 69 KB

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