soc-core.c 76 KB

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