soc-core.c 84 KB

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