soc-core.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643
  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/pinctrl/consumer.h>
  33. #include <linux/ctype.h>
  34. #include <linux/slab.h>
  35. #include <linux/of.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dpcm.h>
  42. #include <sound/soc-topology.h>
  43. #include <sound/initval.h>
  44. #define CREATE_TRACE_POINTS
  45. #include <trace/events/asoc.h>
  46. #define NAME_SIZE 32
  47. #ifdef CONFIG_DEBUG_FS
  48. struct dentry *snd_soc_debugfs_root;
  49. EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  50. #endif
  51. static DEFINE_MUTEX(client_mutex);
  52. static LIST_HEAD(platform_list);
  53. static LIST_HEAD(codec_list);
  54. static LIST_HEAD(component_list);
  55. /*
  56. * This is a timeout to do a DAPM powerdown after a stream is closed().
  57. * It can be used to eliminate pops between different playback streams, e.g.
  58. * between two audio tracks.
  59. */
  60. static int pmdown_time = 5000;
  61. module_param(pmdown_time, int, 0);
  62. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  63. /* returns the minimum number of bytes needed to represent
  64. * a particular given value */
  65. static int min_bytes_needed(unsigned long val)
  66. {
  67. int c = 0;
  68. int i;
  69. for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  70. if (val & (1UL << i))
  71. break;
  72. c = (sizeof val * 8) - c;
  73. if (!c || (c % 8))
  74. c = (c + 8) / 8;
  75. else
  76. c /= 8;
  77. return c;
  78. }
  79. /* fill buf which is 'len' bytes with a formatted
  80. * string of the form 'reg: value\n' */
  81. static int format_register_str(struct snd_soc_codec *codec,
  82. unsigned int reg, char *buf, size_t len)
  83. {
  84. int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  85. int regsize = codec->driver->reg_word_size * 2;
  86. int ret;
  87. /* +2 for ': ' and + 1 for '\n' */
  88. if (wordsize + regsize + 2 + 1 != len)
  89. return -EINVAL;
  90. sprintf(buf, "%.*x: ", wordsize, reg);
  91. buf += wordsize + 2;
  92. ret = snd_soc_read(codec, reg);
  93. if (ret < 0)
  94. memset(buf, 'X', regsize);
  95. else
  96. sprintf(buf, "%.*x", regsize, ret);
  97. buf[regsize] = '\n';
  98. /* no NUL-termination needed */
  99. return 0;
  100. }
  101. /* codec register dump */
  102. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  103. size_t count, loff_t pos)
  104. {
  105. int i, step = 1;
  106. int wordsize, regsize;
  107. int len;
  108. size_t total = 0;
  109. loff_t p = 0;
  110. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  111. regsize = codec->driver->reg_word_size * 2;
  112. len = wordsize + regsize + 2 + 1;
  113. if (!codec->driver->reg_cache_size)
  114. return 0;
  115. if (codec->driver->reg_cache_step)
  116. step = codec->driver->reg_cache_step;
  117. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  118. /* only support larger than PAGE_SIZE bytes debugfs
  119. * entries for the default case */
  120. if (p >= pos) {
  121. if (total + len >= count - 1)
  122. break;
  123. format_register_str(codec, i, buf + total, len);
  124. total += len;
  125. }
  126. p += len;
  127. }
  128. total = min(total, count - 1);
  129. return total;
  130. }
  131. static ssize_t codec_reg_show(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  135. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  136. }
  137. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  138. static ssize_t pmdown_time_show(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  142. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  143. }
  144. static ssize_t pmdown_time_set(struct device *dev,
  145. struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  149. int ret;
  150. ret = kstrtol(buf, 10, &rtd->pmdown_time);
  151. if (ret)
  152. return ret;
  153. return count;
  154. }
  155. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  156. static struct attribute *soc_dev_attrs[] = {
  157. &dev_attr_codec_reg.attr,
  158. &dev_attr_pmdown_time.attr,
  159. NULL
  160. };
  161. static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
  162. struct attribute *attr, int idx)
  163. {
  164. struct device *dev = kobj_to_dev(kobj);
  165. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  166. if (attr == &dev_attr_pmdown_time.attr)
  167. return attr->mode; /* always visible */
  168. return rtd->codec ? attr->mode : 0; /* enabled only with codec */
  169. }
  170. static const struct attribute_group soc_dapm_dev_group = {
  171. .attrs = soc_dapm_dev_attrs,
  172. .is_visible = soc_dev_attr_is_visible,
  173. };
  174. static const struct attribute_group soc_dev_roup = {
  175. .attrs = soc_dev_attrs,
  176. .is_visible = soc_dev_attr_is_visible,
  177. };
  178. static const struct attribute_group *soc_dev_attr_groups[] = {
  179. &soc_dapm_dev_group,
  180. &soc_dev_roup,
  181. NULL
  182. };
  183. #ifdef CONFIG_DEBUG_FS
  184. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. ssize_t ret;
  188. struct snd_soc_codec *codec = file->private_data;
  189. char *buf;
  190. if (*ppos < 0 || !count)
  191. return -EINVAL;
  192. buf = kmalloc(count, GFP_KERNEL);
  193. if (!buf)
  194. return -ENOMEM;
  195. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  196. if (ret >= 0) {
  197. if (copy_to_user(user_buf, buf, ret)) {
  198. kfree(buf);
  199. return -EFAULT;
  200. }
  201. *ppos += ret;
  202. }
  203. kfree(buf);
  204. return ret;
  205. }
  206. static ssize_t codec_reg_write_file(struct file *file,
  207. const char __user *user_buf, size_t count, loff_t *ppos)
  208. {
  209. char buf[32];
  210. size_t buf_size;
  211. char *start = buf;
  212. unsigned long reg, value;
  213. struct snd_soc_codec *codec = file->private_data;
  214. int ret;
  215. buf_size = min(count, (sizeof(buf)-1));
  216. if (copy_from_user(buf, user_buf, buf_size))
  217. return -EFAULT;
  218. buf[buf_size] = 0;
  219. while (*start == ' ')
  220. start++;
  221. reg = simple_strtoul(start, &start, 16);
  222. while (*start == ' ')
  223. start++;
  224. ret = kstrtoul(start, 16, &value);
  225. if (ret)
  226. return ret;
  227. /* Userspace has been fiddling around behind the kernel's back */
  228. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  229. snd_soc_write(codec, reg, value);
  230. return buf_size;
  231. }
  232. static const struct file_operations codec_reg_fops = {
  233. .open = simple_open,
  234. .read = codec_reg_read_file,
  235. .write = codec_reg_write_file,
  236. .llseek = default_llseek,
  237. };
  238. static void soc_init_component_debugfs(struct snd_soc_component *component)
  239. {
  240. if (!component->card->debugfs_card_root)
  241. return;
  242. if (component->debugfs_prefix) {
  243. char *name;
  244. name = kasprintf(GFP_KERNEL, "%s:%s",
  245. component->debugfs_prefix, component->name);
  246. if (name) {
  247. component->debugfs_root = debugfs_create_dir(name,
  248. component->card->debugfs_card_root);
  249. kfree(name);
  250. }
  251. } else {
  252. component->debugfs_root = debugfs_create_dir(component->name,
  253. component->card->debugfs_card_root);
  254. }
  255. if (!component->debugfs_root) {
  256. dev_warn(component->dev,
  257. "ASoC: Failed to create component debugfs directory\n");
  258. return;
  259. }
  260. snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
  261. component->debugfs_root);
  262. if (component->init_debugfs)
  263. component->init_debugfs(component);
  264. }
  265. static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
  266. {
  267. debugfs_remove_recursive(component->debugfs_root);
  268. }
  269. static void soc_init_codec_debugfs(struct snd_soc_component *component)
  270. {
  271. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  272. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  273. codec->component.debugfs_root,
  274. codec, &codec_reg_fops);
  275. if (!codec->debugfs_reg)
  276. dev_warn(codec->dev,
  277. "ASoC: Failed to create codec register debugfs file\n");
  278. }
  279. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  280. size_t count, loff_t *ppos)
  281. {
  282. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  283. ssize_t len, ret = 0;
  284. struct snd_soc_codec *codec;
  285. if (!buf)
  286. return -ENOMEM;
  287. mutex_lock(&client_mutex);
  288. list_for_each_entry(codec, &codec_list, list) {
  289. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  290. codec->component.name);
  291. if (len >= 0)
  292. ret += len;
  293. if (ret > PAGE_SIZE) {
  294. ret = PAGE_SIZE;
  295. break;
  296. }
  297. }
  298. mutex_unlock(&client_mutex);
  299. if (ret >= 0)
  300. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  301. kfree(buf);
  302. return ret;
  303. }
  304. static const struct file_operations codec_list_fops = {
  305. .read = codec_list_read_file,
  306. .llseek = default_llseek,/* read accesses f_pos */
  307. };
  308. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  309. size_t count, loff_t *ppos)
  310. {
  311. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  312. ssize_t len, ret = 0;
  313. struct snd_soc_component *component;
  314. struct snd_soc_dai *dai;
  315. if (!buf)
  316. return -ENOMEM;
  317. mutex_lock(&client_mutex);
  318. list_for_each_entry(component, &component_list, list) {
  319. list_for_each_entry(dai, &component->dai_list, list) {
  320. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  321. dai->name);
  322. if (len >= 0)
  323. ret += len;
  324. if (ret > PAGE_SIZE) {
  325. ret = PAGE_SIZE;
  326. break;
  327. }
  328. }
  329. }
  330. mutex_unlock(&client_mutex);
  331. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  332. kfree(buf);
  333. return ret;
  334. }
  335. static const struct file_operations dai_list_fops = {
  336. .read = dai_list_read_file,
  337. .llseek = default_llseek,/* read accesses f_pos */
  338. };
  339. static ssize_t platform_list_read_file(struct file *file,
  340. char __user *user_buf,
  341. size_t count, loff_t *ppos)
  342. {
  343. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  344. ssize_t len, ret = 0;
  345. struct snd_soc_platform *platform;
  346. if (!buf)
  347. return -ENOMEM;
  348. mutex_lock(&client_mutex);
  349. list_for_each_entry(platform, &platform_list, list) {
  350. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  351. platform->component.name);
  352. if (len >= 0)
  353. ret += len;
  354. if (ret > PAGE_SIZE) {
  355. ret = PAGE_SIZE;
  356. break;
  357. }
  358. }
  359. mutex_unlock(&client_mutex);
  360. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  361. kfree(buf);
  362. return ret;
  363. }
  364. static const struct file_operations platform_list_fops = {
  365. .read = platform_list_read_file,
  366. .llseek = default_llseek,/* read accesses f_pos */
  367. };
  368. static void soc_init_card_debugfs(struct snd_soc_card *card)
  369. {
  370. if (!snd_soc_debugfs_root)
  371. return;
  372. card->debugfs_card_root = debugfs_create_dir(card->name,
  373. snd_soc_debugfs_root);
  374. if (!card->debugfs_card_root) {
  375. dev_warn(card->dev,
  376. "ASoC: Failed to create card debugfs directory\n");
  377. return;
  378. }
  379. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  380. card->debugfs_card_root,
  381. &card->pop_time);
  382. if (!card->debugfs_pop_time)
  383. dev_warn(card->dev,
  384. "ASoC: Failed to create pop time debugfs file\n");
  385. }
  386. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  387. {
  388. debugfs_remove_recursive(card->debugfs_card_root);
  389. }
  390. static void snd_soc_debugfs_init(void)
  391. {
  392. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  393. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  394. pr_warn("ASoC: Failed to create debugfs directory\n");
  395. snd_soc_debugfs_root = NULL;
  396. return;
  397. }
  398. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  399. &codec_list_fops))
  400. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  401. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  402. &dai_list_fops))
  403. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  404. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  405. &platform_list_fops))
  406. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  407. }
  408. static void snd_soc_debugfs_exit(void)
  409. {
  410. debugfs_remove_recursive(snd_soc_debugfs_root);
  411. }
  412. #else
  413. #define soc_init_codec_debugfs NULL
  414. static inline void soc_init_component_debugfs(
  415. struct snd_soc_component *component)
  416. {
  417. }
  418. static inline void soc_cleanup_component_debugfs(
  419. struct snd_soc_component *component)
  420. {
  421. }
  422. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  423. {
  424. }
  425. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  426. {
  427. }
  428. static inline void snd_soc_debugfs_init(void)
  429. {
  430. }
  431. static inline void snd_soc_debugfs_exit(void)
  432. {
  433. }
  434. #endif
  435. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  436. const char *dai_link, int stream)
  437. {
  438. int i;
  439. for (i = 0; i < card->num_links; i++) {
  440. if (card->rtd[i].dai_link->no_pcm &&
  441. !strcmp(card->rtd[i].dai_link->name, dai_link))
  442. return card->rtd[i].pcm->streams[stream].substream;
  443. }
  444. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  445. return NULL;
  446. }
  447. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  448. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  449. const char *dai_link)
  450. {
  451. int i;
  452. for (i = 0; i < card->num_links; i++) {
  453. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  454. return &card->rtd[i];
  455. }
  456. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  457. return NULL;
  458. }
  459. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  460. static void codec2codec_close_delayed_work(struct work_struct *work)
  461. {
  462. /* Currently nothing to do for c2c links
  463. * Since c2c links are internal nodes in the DAPM graph and
  464. * don't interface with the outside world or application layer
  465. * we don't have to do any special handling on close.
  466. */
  467. }
  468. #ifdef CONFIG_PM_SLEEP
  469. /* powers down audio subsystem for suspend */
  470. int snd_soc_suspend(struct device *dev)
  471. {
  472. struct snd_soc_card *card = dev_get_drvdata(dev);
  473. struct snd_soc_codec *codec;
  474. int i, j;
  475. /* If the card is not initialized yet there is nothing to do */
  476. if (!card->instantiated)
  477. return 0;
  478. /* Due to the resume being scheduled into a workqueue we could
  479. * suspend before that's finished - wait for it to complete.
  480. */
  481. snd_power_lock(card->snd_card);
  482. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  483. snd_power_unlock(card->snd_card);
  484. /* we're going to block userspace touching us until resume completes */
  485. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  486. /* mute any active DACs */
  487. for (i = 0; i < card->num_rtd; i++) {
  488. if (card->rtd[i].dai_link->ignore_suspend)
  489. continue;
  490. for (j = 0; j < card->rtd[i].num_codecs; j++) {
  491. struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
  492. struct snd_soc_dai_driver *drv = dai->driver;
  493. if (drv->ops->digital_mute && dai->playback_active)
  494. drv->ops->digital_mute(dai, 1);
  495. }
  496. }
  497. /* suspend all pcms */
  498. for (i = 0; i < card->num_rtd; i++) {
  499. if (card->rtd[i].dai_link->ignore_suspend)
  500. continue;
  501. snd_pcm_suspend_all(card->rtd[i].pcm);
  502. }
  503. if (card->suspend_pre)
  504. card->suspend_pre(card);
  505. for (i = 0; i < card->num_rtd; i++) {
  506. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  507. if (card->rtd[i].dai_link->ignore_suspend)
  508. continue;
  509. if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
  510. cpu_dai->driver->suspend(cpu_dai);
  511. }
  512. /* close any waiting streams */
  513. for (i = 0; i < card->num_rtd; i++)
  514. flush_delayed_work(&card->rtd[i].delayed_work);
  515. for (i = 0; i < card->num_rtd; i++) {
  516. if (card->rtd[i].dai_link->ignore_suspend)
  517. continue;
  518. snd_soc_dapm_stream_event(&card->rtd[i],
  519. SNDRV_PCM_STREAM_PLAYBACK,
  520. SND_SOC_DAPM_STREAM_SUSPEND);
  521. snd_soc_dapm_stream_event(&card->rtd[i],
  522. SNDRV_PCM_STREAM_CAPTURE,
  523. SND_SOC_DAPM_STREAM_SUSPEND);
  524. }
  525. /* Recheck all endpoints too, their state is affected by suspend */
  526. dapm_mark_endpoints_dirty(card);
  527. snd_soc_dapm_sync(&card->dapm);
  528. /* suspend all CODECs */
  529. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  530. /* If there are paths active then the CODEC will be held with
  531. * bias _ON and should not be suspended. */
  532. if (!codec->suspended) {
  533. switch (codec->dapm.bias_level) {
  534. case SND_SOC_BIAS_STANDBY:
  535. /*
  536. * If the CODEC is capable of idle
  537. * bias off then being in STANDBY
  538. * means it's doing something,
  539. * otherwise fall through.
  540. */
  541. if (codec->dapm.idle_bias_off) {
  542. dev_dbg(codec->dev,
  543. "ASoC: idle_bias_off CODEC on over suspend\n");
  544. break;
  545. }
  546. case SND_SOC_BIAS_OFF:
  547. if (codec->driver->suspend)
  548. codec->driver->suspend(codec);
  549. codec->suspended = 1;
  550. if (codec->component.regmap)
  551. regcache_mark_dirty(codec->component.regmap);
  552. /* deactivate pins to sleep state */
  553. pinctrl_pm_select_sleep_state(codec->dev);
  554. break;
  555. default:
  556. dev_dbg(codec->dev,
  557. "ASoC: CODEC is on over suspend\n");
  558. break;
  559. }
  560. }
  561. }
  562. for (i = 0; i < card->num_rtd; i++) {
  563. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  564. if (card->rtd[i].dai_link->ignore_suspend)
  565. continue;
  566. if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
  567. cpu_dai->driver->suspend(cpu_dai);
  568. /* deactivate pins to sleep state */
  569. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  570. }
  571. if (card->suspend_post)
  572. card->suspend_post(card);
  573. return 0;
  574. }
  575. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  576. /* deferred resume work, so resume can complete before we finished
  577. * setting our codec back up, which can be very slow on I2C
  578. */
  579. static void soc_resume_deferred(struct work_struct *work)
  580. {
  581. struct snd_soc_card *card =
  582. container_of(work, struct snd_soc_card, deferred_resume_work);
  583. struct snd_soc_codec *codec;
  584. int i, j;
  585. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  586. * so userspace apps are blocked from touching us
  587. */
  588. dev_dbg(card->dev, "ASoC: starting resume work\n");
  589. /* Bring us up into D2 so that DAPM starts enabling things */
  590. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  591. if (card->resume_pre)
  592. card->resume_pre(card);
  593. /* resume control bus DAIs */
  594. for (i = 0; i < card->num_rtd; i++) {
  595. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  596. if (card->rtd[i].dai_link->ignore_suspend)
  597. continue;
  598. if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
  599. cpu_dai->driver->resume(cpu_dai);
  600. }
  601. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  602. if (codec->suspended) {
  603. if (codec->driver->resume)
  604. codec->driver->resume(codec);
  605. codec->suspended = 0;
  606. }
  607. }
  608. for (i = 0; i < card->num_rtd; i++) {
  609. if (card->rtd[i].dai_link->ignore_suspend)
  610. continue;
  611. snd_soc_dapm_stream_event(&card->rtd[i],
  612. SNDRV_PCM_STREAM_PLAYBACK,
  613. SND_SOC_DAPM_STREAM_RESUME);
  614. snd_soc_dapm_stream_event(&card->rtd[i],
  615. SNDRV_PCM_STREAM_CAPTURE,
  616. SND_SOC_DAPM_STREAM_RESUME);
  617. }
  618. /* unmute any active DACs */
  619. for (i = 0; i < card->num_rtd; i++) {
  620. if (card->rtd[i].dai_link->ignore_suspend)
  621. continue;
  622. for (j = 0; j < card->rtd[i].num_codecs; j++) {
  623. struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
  624. struct snd_soc_dai_driver *drv = dai->driver;
  625. if (drv->ops->digital_mute && dai->playback_active)
  626. drv->ops->digital_mute(dai, 0);
  627. }
  628. }
  629. for (i = 0; i < card->num_rtd; i++) {
  630. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  631. if (card->rtd[i].dai_link->ignore_suspend)
  632. continue;
  633. if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
  634. cpu_dai->driver->resume(cpu_dai);
  635. }
  636. if (card->resume_post)
  637. card->resume_post(card);
  638. dev_dbg(card->dev, "ASoC: resume work completed\n");
  639. /* userspace can access us now we are back as we were before */
  640. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  641. /* Recheck all endpoints too, their state is affected by suspend */
  642. dapm_mark_endpoints_dirty(card);
  643. snd_soc_dapm_sync(&card->dapm);
  644. }
  645. /* powers up audio subsystem after a suspend */
  646. int snd_soc_resume(struct device *dev)
  647. {
  648. struct snd_soc_card *card = dev_get_drvdata(dev);
  649. bool bus_control = false;
  650. int i;
  651. /* If the card is not initialized yet there is nothing to do */
  652. if (!card->instantiated)
  653. return 0;
  654. /* activate pins from sleep state */
  655. for (i = 0; i < card->num_rtd; i++) {
  656. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  657. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  658. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  659. int j;
  660. if (cpu_dai->active)
  661. pinctrl_pm_select_default_state(cpu_dai->dev);
  662. for (j = 0; j < rtd->num_codecs; j++) {
  663. struct snd_soc_dai *codec_dai = codec_dais[j];
  664. if (codec_dai->active)
  665. pinctrl_pm_select_default_state(codec_dai->dev);
  666. }
  667. }
  668. /*
  669. * DAIs that also act as the control bus master might have other drivers
  670. * hanging off them so need to resume immediately. Other drivers don't
  671. * have that problem and may take a substantial amount of time to resume
  672. * due to I/O costs and anti-pop so handle them out of line.
  673. */
  674. for (i = 0; i < card->num_rtd; i++) {
  675. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  676. bus_control |= cpu_dai->driver->bus_control;
  677. }
  678. if (bus_control) {
  679. dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
  680. soc_resume_deferred(&card->deferred_resume_work);
  681. } else {
  682. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  683. if (!schedule_work(&card->deferred_resume_work))
  684. dev_err(dev, "ASoC: resume work item may be lost\n");
  685. }
  686. return 0;
  687. }
  688. EXPORT_SYMBOL_GPL(snd_soc_resume);
  689. #else
  690. #define snd_soc_suspend NULL
  691. #define snd_soc_resume NULL
  692. #endif
  693. static const struct snd_soc_dai_ops null_dai_ops = {
  694. };
  695. static struct snd_soc_component *soc_find_component(
  696. const struct device_node *of_node, const char *name)
  697. {
  698. struct snd_soc_component *component;
  699. lockdep_assert_held(&client_mutex);
  700. list_for_each_entry(component, &component_list, list) {
  701. if (of_node) {
  702. if (component->dev->of_node == of_node)
  703. return component;
  704. } else if (strcmp(component->name, name) == 0) {
  705. return component;
  706. }
  707. }
  708. return NULL;
  709. }
  710. static struct snd_soc_dai *snd_soc_find_dai(
  711. const struct snd_soc_dai_link_component *dlc)
  712. {
  713. struct snd_soc_component *component;
  714. struct snd_soc_dai *dai;
  715. struct device_node *component_of_node;
  716. lockdep_assert_held(&client_mutex);
  717. /* Find CPU DAI from registered DAIs*/
  718. list_for_each_entry(component, &component_list, list) {
  719. component_of_node = component->dev->of_node;
  720. if (!component_of_node && component->dev->parent)
  721. component_of_node = component->dev->parent->of_node;
  722. if (dlc->of_node && component_of_node != dlc->of_node)
  723. continue;
  724. if (dlc->name && strcmp(component->name, dlc->name))
  725. continue;
  726. list_for_each_entry(dai, &component->dai_list, list) {
  727. if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
  728. continue;
  729. return dai;
  730. }
  731. }
  732. return NULL;
  733. }
  734. static int soc_bind_dai_link(struct snd_soc_card *card, int num)
  735. {
  736. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  737. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  738. struct snd_soc_dai_link_component *codecs = dai_link->codecs;
  739. struct snd_soc_dai_link_component cpu_dai_component;
  740. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  741. struct snd_soc_platform *platform;
  742. const char *platform_name;
  743. int i;
  744. dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
  745. cpu_dai_component.name = dai_link->cpu_name;
  746. cpu_dai_component.of_node = dai_link->cpu_of_node;
  747. cpu_dai_component.dai_name = dai_link->cpu_dai_name;
  748. rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
  749. if (!rtd->cpu_dai) {
  750. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  751. dai_link->cpu_dai_name);
  752. return -EPROBE_DEFER;
  753. }
  754. rtd->num_codecs = dai_link->num_codecs;
  755. /* Find CODEC from registered CODECs */
  756. for (i = 0; i < rtd->num_codecs; i++) {
  757. codec_dais[i] = snd_soc_find_dai(&codecs[i]);
  758. if (!codec_dais[i]) {
  759. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  760. codecs[i].dai_name);
  761. return -EPROBE_DEFER;
  762. }
  763. }
  764. /* Single codec links expect codec and codec_dai in runtime data */
  765. rtd->codec_dai = codec_dais[0];
  766. rtd->codec = rtd->codec_dai->codec;
  767. /* if there's no platform we match on the empty platform */
  768. platform_name = dai_link->platform_name;
  769. if (!platform_name && !dai_link->platform_of_node)
  770. platform_name = "snd-soc-dummy";
  771. /* find one from the set of registered platforms */
  772. list_for_each_entry(platform, &platform_list, list) {
  773. if (dai_link->platform_of_node) {
  774. if (platform->dev->of_node !=
  775. dai_link->platform_of_node)
  776. continue;
  777. } else {
  778. if (strcmp(platform->component.name, platform_name))
  779. continue;
  780. }
  781. rtd->platform = platform;
  782. }
  783. if (!rtd->platform) {
  784. dev_err(card->dev, "ASoC: platform %s not registered\n",
  785. dai_link->platform_name);
  786. return -EPROBE_DEFER;
  787. }
  788. card->num_rtd++;
  789. return 0;
  790. }
  791. static void soc_remove_component(struct snd_soc_component *component)
  792. {
  793. if (!component->probed)
  794. return;
  795. /* This is a HACK and will be removed soon */
  796. if (component->codec)
  797. list_del(&component->codec->card_list);
  798. if (component->remove)
  799. component->remove(component);
  800. snd_soc_dapm_free(snd_soc_component_get_dapm(component));
  801. soc_cleanup_component_debugfs(component);
  802. component->probed = 0;
  803. module_put(component->dev->driver->owner);
  804. }
  805. static void soc_remove_dai(struct snd_soc_dai *dai, int order)
  806. {
  807. int err;
  808. if (dai && dai->probed &&
  809. dai->driver->remove_order == order) {
  810. if (dai->driver->remove) {
  811. err = dai->driver->remove(dai);
  812. if (err < 0)
  813. dev_err(dai->dev,
  814. "ASoC: failed to remove %s: %d\n",
  815. dai->name, err);
  816. }
  817. dai->probed = 0;
  818. }
  819. }
  820. static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
  821. {
  822. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  823. int i;
  824. /* unregister the rtd device */
  825. if (rtd->dev_registered) {
  826. device_unregister(rtd->dev);
  827. rtd->dev_registered = 0;
  828. }
  829. /* remove the CODEC DAI */
  830. for (i = 0; i < rtd->num_codecs; i++)
  831. soc_remove_dai(rtd->codec_dais[i], order);
  832. soc_remove_dai(rtd->cpu_dai, order);
  833. }
  834. static void soc_remove_link_components(struct snd_soc_card *card, int num,
  835. int order)
  836. {
  837. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  838. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  839. struct snd_soc_platform *platform = rtd->platform;
  840. struct snd_soc_component *component;
  841. int i;
  842. /* remove the platform */
  843. if (platform && platform->component.driver->remove_order == order)
  844. soc_remove_component(&platform->component);
  845. /* remove the CODEC-side CODEC */
  846. for (i = 0; i < rtd->num_codecs; i++) {
  847. component = rtd->codec_dais[i]->component;
  848. if (component->driver->remove_order == order)
  849. soc_remove_component(component);
  850. }
  851. /* remove any CPU-side CODEC */
  852. if (cpu_dai) {
  853. if (cpu_dai->component->driver->remove_order == order)
  854. soc_remove_component(cpu_dai->component);
  855. }
  856. }
  857. static void soc_remove_dai_links(struct snd_soc_card *card)
  858. {
  859. int dai, order;
  860. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  861. order++) {
  862. for (dai = 0; dai < card->num_rtd; dai++)
  863. soc_remove_link_dais(card, dai, order);
  864. }
  865. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  866. order++) {
  867. for (dai = 0; dai < card->num_rtd; dai++)
  868. soc_remove_link_components(card, dai, order);
  869. }
  870. card->num_rtd = 0;
  871. }
  872. static void soc_set_name_prefix(struct snd_soc_card *card,
  873. struct snd_soc_component *component)
  874. {
  875. int i;
  876. if (card->codec_conf == NULL)
  877. return;
  878. for (i = 0; i < card->num_configs; i++) {
  879. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  880. if (map->of_node && component->dev->of_node != map->of_node)
  881. continue;
  882. if (map->dev_name && strcmp(component->name, map->dev_name))
  883. continue;
  884. component->name_prefix = map->name_prefix;
  885. break;
  886. }
  887. }
  888. static int soc_probe_component(struct snd_soc_card *card,
  889. struct snd_soc_component *component)
  890. {
  891. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  892. struct snd_soc_dai *dai;
  893. int ret;
  894. if (component->probed)
  895. return 0;
  896. component->card = card;
  897. dapm->card = card;
  898. soc_set_name_prefix(card, component);
  899. if (!try_module_get(component->dev->driver->owner))
  900. return -ENODEV;
  901. soc_init_component_debugfs(component);
  902. if (component->dapm_widgets) {
  903. ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
  904. component->num_dapm_widgets);
  905. if (ret != 0) {
  906. dev_err(component->dev,
  907. "Failed to create new controls %d\n", ret);
  908. goto err_probe;
  909. }
  910. }
  911. list_for_each_entry(dai, &component->dai_list, list) {
  912. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  913. if (ret != 0) {
  914. dev_err(component->dev,
  915. "Failed to create DAI widgets %d\n", ret);
  916. goto err_probe;
  917. }
  918. }
  919. if (component->probe) {
  920. ret = component->probe(component);
  921. if (ret < 0) {
  922. dev_err(component->dev,
  923. "ASoC: failed to probe component %d\n", ret);
  924. goto err_probe;
  925. }
  926. WARN(dapm->idle_bias_off &&
  927. dapm->bias_level != SND_SOC_BIAS_OFF,
  928. "codec %s can not start from non-off bias with idle_bias_off==1\n",
  929. component->name);
  930. }
  931. if (component->controls)
  932. snd_soc_add_component_controls(component, component->controls,
  933. component->num_controls);
  934. if (component->dapm_routes)
  935. snd_soc_dapm_add_routes(dapm, component->dapm_routes,
  936. component->num_dapm_routes);
  937. component->probed = 1;
  938. list_add(&dapm->list, &card->dapm_list);
  939. /* This is a HACK and will be removed soon */
  940. if (component->codec)
  941. list_add(&component->codec->card_list, &card->codec_dev_list);
  942. return 0;
  943. err_probe:
  944. soc_cleanup_component_debugfs(component);
  945. module_put(component->dev->driver->owner);
  946. return ret;
  947. }
  948. static void rtd_release(struct device *dev)
  949. {
  950. kfree(dev);
  951. }
  952. static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
  953. const char *name)
  954. {
  955. int ret = 0;
  956. /* register the rtd device */
  957. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  958. if (!rtd->dev)
  959. return -ENOMEM;
  960. device_initialize(rtd->dev);
  961. rtd->dev->parent = rtd->card->dev;
  962. rtd->dev->release = rtd_release;
  963. rtd->dev->groups = soc_dev_attr_groups;
  964. dev_set_name(rtd->dev, "%s", name);
  965. dev_set_drvdata(rtd->dev, rtd);
  966. mutex_init(&rtd->pcm_mutex);
  967. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  968. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  969. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  970. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  971. ret = device_add(rtd->dev);
  972. if (ret < 0) {
  973. /* calling put_device() here to free the rtd->dev */
  974. put_device(rtd->dev);
  975. dev_err(rtd->card->dev,
  976. "ASoC: failed to register runtime device: %d\n", ret);
  977. return ret;
  978. }
  979. rtd->dev_registered = 1;
  980. return 0;
  981. }
  982. static int soc_probe_link_components(struct snd_soc_card *card, int num,
  983. int order)
  984. {
  985. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  986. struct snd_soc_platform *platform = rtd->platform;
  987. struct snd_soc_component *component;
  988. int i, ret;
  989. /* probe the CPU-side component, if it is a CODEC */
  990. component = rtd->cpu_dai->component;
  991. if (component->driver->probe_order == order) {
  992. ret = soc_probe_component(card, component);
  993. if (ret < 0)
  994. return ret;
  995. }
  996. /* probe the CODEC-side components */
  997. for (i = 0; i < rtd->num_codecs; i++) {
  998. component = rtd->codec_dais[i]->component;
  999. if (component->driver->probe_order == order) {
  1000. ret = soc_probe_component(card, component);
  1001. if (ret < 0)
  1002. return ret;
  1003. }
  1004. }
  1005. /* probe the platform */
  1006. if (platform->component.driver->probe_order == order) {
  1007. ret = soc_probe_component(card, &platform->component);
  1008. if (ret < 0)
  1009. return ret;
  1010. }
  1011. return 0;
  1012. }
  1013. static int soc_probe_dai(struct snd_soc_dai *dai, int order)
  1014. {
  1015. int ret;
  1016. if (!dai->probed && dai->driver->probe_order == order) {
  1017. if (dai->driver->probe) {
  1018. ret = dai->driver->probe(dai);
  1019. if (ret < 0) {
  1020. dev_err(dai->dev,
  1021. "ASoC: failed to probe DAI %s: %d\n",
  1022. dai->name, ret);
  1023. return ret;
  1024. }
  1025. }
  1026. dai->probed = 1;
  1027. }
  1028. return 0;
  1029. }
  1030. static int soc_link_dai_widgets(struct snd_soc_card *card,
  1031. struct snd_soc_dai_link *dai_link,
  1032. struct snd_soc_pcm_runtime *rtd)
  1033. {
  1034. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1035. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1036. struct snd_soc_dapm_widget *play_w, *capture_w;
  1037. int ret;
  1038. if (rtd->num_codecs > 1)
  1039. dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
  1040. /* link the DAI widgets */
  1041. play_w = codec_dai->playback_widget;
  1042. capture_w = cpu_dai->capture_widget;
  1043. if (play_w && capture_w) {
  1044. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1045. dai_link->num_params, capture_w,
  1046. play_w);
  1047. if (ret != 0) {
  1048. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1049. play_w->name, capture_w->name, ret);
  1050. return ret;
  1051. }
  1052. }
  1053. play_w = cpu_dai->playback_widget;
  1054. capture_w = codec_dai->capture_widget;
  1055. if (play_w && capture_w) {
  1056. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1057. dai_link->num_params, capture_w,
  1058. play_w);
  1059. if (ret != 0) {
  1060. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1061. play_w->name, capture_w->name, ret);
  1062. return ret;
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
  1068. {
  1069. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  1070. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1071. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1072. int i, ret;
  1073. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1074. card->name, num, order);
  1075. /* set default power off timeout */
  1076. rtd->pmdown_time = pmdown_time;
  1077. ret = soc_probe_dai(cpu_dai, order);
  1078. if (ret)
  1079. return ret;
  1080. /* probe the CODEC DAI */
  1081. for (i = 0; i < rtd->num_codecs; i++) {
  1082. ret = soc_probe_dai(rtd->codec_dais[i], order);
  1083. if (ret)
  1084. return ret;
  1085. }
  1086. /* complete DAI probe during last probe */
  1087. if (order != SND_SOC_COMP_ORDER_LAST)
  1088. return 0;
  1089. /* do machine specific initialization */
  1090. if (dai_link->init) {
  1091. ret = dai_link->init(rtd);
  1092. if (ret < 0) {
  1093. dev_err(card->dev, "ASoC: failed to init %s: %d\n",
  1094. dai_link->name, ret);
  1095. return ret;
  1096. }
  1097. }
  1098. if (dai_link->dai_fmt)
  1099. snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
  1100. ret = soc_post_component_init(rtd, dai_link->name);
  1101. if (ret)
  1102. return ret;
  1103. #ifdef CONFIG_DEBUG_FS
  1104. /* add DPCM sysfs entries */
  1105. if (dai_link->dynamic)
  1106. soc_dpcm_debugfs_add(rtd);
  1107. #endif
  1108. if (cpu_dai->driver->compress_dai) {
  1109. /*create compress_device"*/
  1110. ret = soc_new_compress(rtd, num);
  1111. if (ret < 0) {
  1112. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1113. dai_link->stream_name);
  1114. return ret;
  1115. }
  1116. } else {
  1117. if (!dai_link->params) {
  1118. /* create the pcm */
  1119. ret = soc_new_pcm(rtd, num);
  1120. if (ret < 0) {
  1121. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1122. dai_link->stream_name, ret);
  1123. return ret;
  1124. }
  1125. } else {
  1126. INIT_DELAYED_WORK(&rtd->delayed_work,
  1127. codec2codec_close_delayed_work);
  1128. /* link the DAI widgets */
  1129. ret = soc_link_dai_widgets(card, dai_link, rtd);
  1130. if (ret)
  1131. return ret;
  1132. }
  1133. }
  1134. return 0;
  1135. }
  1136. static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
  1137. {
  1138. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1139. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1140. const char *name = aux_dev->codec_name;
  1141. rtd->component = soc_find_component(aux_dev->codec_of_node, name);
  1142. if (!rtd->component) {
  1143. if (aux_dev->codec_of_node)
  1144. name = of_node_full_name(aux_dev->codec_of_node);
  1145. dev_err(card->dev, "ASoC: %s not registered\n", name);
  1146. return -EPROBE_DEFER;
  1147. }
  1148. /*
  1149. * Some places still reference rtd->codec, so we have to keep that
  1150. * initialized if the component is a CODEC. Once all those references
  1151. * have been removed, this code can be removed as well.
  1152. */
  1153. rtd->codec = rtd->component->codec;
  1154. return 0;
  1155. }
  1156. static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
  1157. {
  1158. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1159. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1160. int ret;
  1161. ret = soc_probe_component(card, rtd->component);
  1162. if (ret < 0)
  1163. return ret;
  1164. /* do machine specific initialization */
  1165. if (aux_dev->init) {
  1166. ret = aux_dev->init(rtd->component);
  1167. if (ret < 0) {
  1168. dev_err(card->dev, "ASoC: failed to init %s: %d\n",
  1169. aux_dev->name, ret);
  1170. return ret;
  1171. }
  1172. }
  1173. return soc_post_component_init(rtd, aux_dev->name);
  1174. }
  1175. static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
  1176. {
  1177. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1178. struct snd_soc_component *component = rtd->component;
  1179. /* unregister the rtd device */
  1180. if (rtd->dev_registered) {
  1181. device_unregister(rtd->dev);
  1182. rtd->dev_registered = 0;
  1183. }
  1184. if (component && component->probed)
  1185. soc_remove_component(component);
  1186. }
  1187. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
  1188. {
  1189. int ret;
  1190. if (codec->cache_init)
  1191. return 0;
  1192. ret = snd_soc_cache_init(codec);
  1193. if (ret < 0) {
  1194. dev_err(codec->dev,
  1195. "ASoC: Failed to set cache compression type: %d\n",
  1196. ret);
  1197. return ret;
  1198. }
  1199. codec->cache_init = 1;
  1200. return 0;
  1201. }
  1202. /**
  1203. * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
  1204. * @rtd: The runtime for which the DAI link format should be changed
  1205. * @dai_fmt: The new DAI link format
  1206. *
  1207. * This function updates the DAI link format for all DAIs connected to the DAI
  1208. * link for the specified runtime.
  1209. *
  1210. * Note: For setups with a static format set the dai_fmt field in the
  1211. * corresponding snd_dai_link struct instead of using this function.
  1212. *
  1213. * Returns 0 on success, otherwise a negative error code.
  1214. */
  1215. int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
  1216. unsigned int dai_fmt)
  1217. {
  1218. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  1219. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1220. unsigned int i;
  1221. int ret;
  1222. for (i = 0; i < rtd->num_codecs; i++) {
  1223. struct snd_soc_dai *codec_dai = codec_dais[i];
  1224. ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
  1225. if (ret != 0 && ret != -ENOTSUPP) {
  1226. dev_warn(codec_dai->dev,
  1227. "ASoC: Failed to set DAI format: %d\n", ret);
  1228. return ret;
  1229. }
  1230. }
  1231. /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
  1232. if (cpu_dai->codec) {
  1233. unsigned int inv_dai_fmt;
  1234. inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
  1235. switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1236. case SND_SOC_DAIFMT_CBM_CFM:
  1237. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1238. break;
  1239. case SND_SOC_DAIFMT_CBM_CFS:
  1240. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1241. break;
  1242. case SND_SOC_DAIFMT_CBS_CFM:
  1243. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1244. break;
  1245. case SND_SOC_DAIFMT_CBS_CFS:
  1246. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1247. break;
  1248. }
  1249. dai_fmt = inv_dai_fmt;
  1250. }
  1251. ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
  1252. if (ret != 0 && ret != -ENOTSUPP) {
  1253. dev_warn(cpu_dai->dev,
  1254. "ASoC: Failed to set DAI format: %d\n", ret);
  1255. return ret;
  1256. }
  1257. return 0;
  1258. }
  1259. EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
  1260. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1261. {
  1262. struct snd_soc_codec *codec;
  1263. int ret, i, order;
  1264. mutex_lock(&client_mutex);
  1265. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1266. /* bind DAIs */
  1267. for (i = 0; i < card->num_links; i++) {
  1268. ret = soc_bind_dai_link(card, i);
  1269. if (ret != 0)
  1270. goto base_error;
  1271. }
  1272. /* bind aux_devs too */
  1273. for (i = 0; i < card->num_aux_devs; i++) {
  1274. ret = soc_bind_aux_dev(card, i);
  1275. if (ret != 0)
  1276. goto base_error;
  1277. }
  1278. /* initialize the register cache for each available codec */
  1279. list_for_each_entry(codec, &codec_list, list) {
  1280. if (codec->cache_init)
  1281. continue;
  1282. ret = snd_soc_init_codec_cache(codec);
  1283. if (ret < 0)
  1284. goto base_error;
  1285. }
  1286. /* card bind complete so register a sound card */
  1287. ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1288. card->owner, 0, &card->snd_card);
  1289. if (ret < 0) {
  1290. dev_err(card->dev,
  1291. "ASoC: can't create sound card for card %s: %d\n",
  1292. card->name, ret);
  1293. goto base_error;
  1294. }
  1295. soc_init_card_debugfs(card);
  1296. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1297. card->dapm.dev = card->dev;
  1298. card->dapm.card = card;
  1299. list_add(&card->dapm.list, &card->dapm_list);
  1300. #ifdef CONFIG_DEBUG_FS
  1301. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1302. #endif
  1303. #ifdef CONFIG_PM_SLEEP
  1304. /* deferred resume work */
  1305. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1306. #endif
  1307. if (card->dapm_widgets)
  1308. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1309. card->num_dapm_widgets);
  1310. if (card->of_dapm_widgets)
  1311. snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
  1312. card->num_of_dapm_widgets);
  1313. /* initialise the sound card only once */
  1314. if (card->probe) {
  1315. ret = card->probe(card);
  1316. if (ret < 0)
  1317. goto card_probe_error;
  1318. }
  1319. /* probe all components used by DAI links on this card */
  1320. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1321. order++) {
  1322. for (i = 0; i < card->num_links; i++) {
  1323. ret = soc_probe_link_components(card, i, order);
  1324. if (ret < 0) {
  1325. dev_err(card->dev,
  1326. "ASoC: failed to instantiate card %d\n",
  1327. ret);
  1328. goto probe_dai_err;
  1329. }
  1330. }
  1331. }
  1332. /* probe all DAI links on this card */
  1333. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1334. order++) {
  1335. for (i = 0; i < card->num_links; i++) {
  1336. ret = soc_probe_link_dais(card, i, order);
  1337. if (ret < 0) {
  1338. dev_err(card->dev,
  1339. "ASoC: failed to instantiate card %d\n",
  1340. ret);
  1341. goto probe_dai_err;
  1342. }
  1343. }
  1344. }
  1345. for (i = 0; i < card->num_aux_devs; i++) {
  1346. ret = soc_probe_aux_dev(card, i);
  1347. if (ret < 0) {
  1348. dev_err(card->dev,
  1349. "ASoC: failed to add auxiliary devices %d\n",
  1350. ret);
  1351. goto probe_aux_dev_err;
  1352. }
  1353. }
  1354. snd_soc_dapm_link_dai_widgets(card);
  1355. snd_soc_dapm_connect_dai_link_widgets(card);
  1356. if (card->controls)
  1357. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1358. if (card->dapm_routes)
  1359. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1360. card->num_dapm_routes);
  1361. if (card->of_dapm_routes)
  1362. snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
  1363. card->num_of_dapm_routes);
  1364. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1365. "%s", card->name);
  1366. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1367. "%s", card->long_name ? card->long_name : card->name);
  1368. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1369. "%s", card->driver_name ? card->driver_name : card->name);
  1370. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1371. switch (card->snd_card->driver[i]) {
  1372. case '_':
  1373. case '-':
  1374. case '\0':
  1375. break;
  1376. default:
  1377. if (!isalnum(card->snd_card->driver[i]))
  1378. card->snd_card->driver[i] = '_';
  1379. break;
  1380. }
  1381. }
  1382. if (card->late_probe) {
  1383. ret = card->late_probe(card);
  1384. if (ret < 0) {
  1385. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1386. card->name, ret);
  1387. goto probe_aux_dev_err;
  1388. }
  1389. }
  1390. snd_soc_dapm_new_widgets(card);
  1391. ret = snd_card_register(card->snd_card);
  1392. if (ret < 0) {
  1393. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1394. ret);
  1395. goto probe_aux_dev_err;
  1396. }
  1397. card->instantiated = 1;
  1398. snd_soc_dapm_sync(&card->dapm);
  1399. mutex_unlock(&card->mutex);
  1400. mutex_unlock(&client_mutex);
  1401. return 0;
  1402. probe_aux_dev_err:
  1403. for (i = 0; i < card->num_aux_devs; i++)
  1404. soc_remove_aux_dev(card, i);
  1405. probe_dai_err:
  1406. soc_remove_dai_links(card);
  1407. card_probe_error:
  1408. if (card->remove)
  1409. card->remove(card);
  1410. soc_cleanup_card_debugfs(card);
  1411. snd_card_free(card->snd_card);
  1412. base_error:
  1413. mutex_unlock(&card->mutex);
  1414. mutex_unlock(&client_mutex);
  1415. return ret;
  1416. }
  1417. /* probes a new socdev */
  1418. static int soc_probe(struct platform_device *pdev)
  1419. {
  1420. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1421. /*
  1422. * no card, so machine driver should be registering card
  1423. * we should not be here in that case so ret error
  1424. */
  1425. if (!card)
  1426. return -EINVAL;
  1427. dev_warn(&pdev->dev,
  1428. "ASoC: machine %s should use snd_soc_register_card()\n",
  1429. card->name);
  1430. /* Bodge while we unpick instantiation */
  1431. card->dev = &pdev->dev;
  1432. return snd_soc_register_card(card);
  1433. }
  1434. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1435. {
  1436. int i;
  1437. /* make sure any delayed work runs */
  1438. for (i = 0; i < card->num_rtd; i++) {
  1439. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1440. flush_delayed_work(&rtd->delayed_work);
  1441. }
  1442. /* remove auxiliary devices */
  1443. for (i = 0; i < card->num_aux_devs; i++)
  1444. soc_remove_aux_dev(card, i);
  1445. /* remove and free each DAI */
  1446. soc_remove_dai_links(card);
  1447. soc_cleanup_card_debugfs(card);
  1448. /* remove the card */
  1449. if (card->remove)
  1450. card->remove(card);
  1451. snd_soc_dapm_free(&card->dapm);
  1452. snd_card_free(card->snd_card);
  1453. return 0;
  1454. }
  1455. /* removes a socdev */
  1456. static int soc_remove(struct platform_device *pdev)
  1457. {
  1458. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1459. snd_soc_unregister_card(card);
  1460. return 0;
  1461. }
  1462. int snd_soc_poweroff(struct device *dev)
  1463. {
  1464. struct snd_soc_card *card = dev_get_drvdata(dev);
  1465. int i;
  1466. if (!card->instantiated)
  1467. return 0;
  1468. /* Flush out pmdown_time work - we actually do want to run it
  1469. * now, we're shutting down so no imminent restart. */
  1470. for (i = 0; i < card->num_rtd; i++) {
  1471. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1472. flush_delayed_work(&rtd->delayed_work);
  1473. }
  1474. snd_soc_dapm_shutdown(card);
  1475. /* deactivate pins to sleep state */
  1476. for (i = 0; i < card->num_rtd; i++) {
  1477. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1478. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1479. int j;
  1480. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  1481. for (j = 0; j < rtd->num_codecs; j++) {
  1482. struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
  1483. pinctrl_pm_select_sleep_state(codec_dai->dev);
  1484. }
  1485. }
  1486. return 0;
  1487. }
  1488. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1489. const struct dev_pm_ops snd_soc_pm_ops = {
  1490. .suspend = snd_soc_suspend,
  1491. .resume = snd_soc_resume,
  1492. .freeze = snd_soc_suspend,
  1493. .thaw = snd_soc_resume,
  1494. .poweroff = snd_soc_poweroff,
  1495. .restore = snd_soc_resume,
  1496. };
  1497. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1498. /* ASoC platform driver */
  1499. static struct platform_driver soc_driver = {
  1500. .driver = {
  1501. .name = "soc-audio",
  1502. .pm = &snd_soc_pm_ops,
  1503. },
  1504. .probe = soc_probe,
  1505. .remove = soc_remove,
  1506. };
  1507. /**
  1508. * snd_soc_cnew - create new control
  1509. * @_template: control template
  1510. * @data: control private data
  1511. * @long_name: control long name
  1512. * @prefix: control name prefix
  1513. *
  1514. * Create a new mixer control from a template control.
  1515. *
  1516. * Returns 0 for success, else error.
  1517. */
  1518. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1519. void *data, const char *long_name,
  1520. const char *prefix)
  1521. {
  1522. struct snd_kcontrol_new template;
  1523. struct snd_kcontrol *kcontrol;
  1524. char *name = NULL;
  1525. memcpy(&template, _template, sizeof(template));
  1526. template.index = 0;
  1527. if (!long_name)
  1528. long_name = template.name;
  1529. if (prefix) {
  1530. name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
  1531. if (!name)
  1532. return NULL;
  1533. template.name = name;
  1534. } else {
  1535. template.name = long_name;
  1536. }
  1537. kcontrol = snd_ctl_new1(&template, data);
  1538. kfree(name);
  1539. return kcontrol;
  1540. }
  1541. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1542. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1543. const struct snd_kcontrol_new *controls, int num_controls,
  1544. const char *prefix, void *data)
  1545. {
  1546. int err, i;
  1547. for (i = 0; i < num_controls; i++) {
  1548. const struct snd_kcontrol_new *control = &controls[i];
  1549. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1550. control->name, prefix));
  1551. if (err < 0) {
  1552. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1553. control->name, err);
  1554. return err;
  1555. }
  1556. }
  1557. return 0;
  1558. }
  1559. struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
  1560. const char *name)
  1561. {
  1562. struct snd_card *card = soc_card->snd_card;
  1563. struct snd_kcontrol *kctl;
  1564. if (unlikely(!name))
  1565. return NULL;
  1566. list_for_each_entry(kctl, &card->controls, list)
  1567. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
  1568. return kctl;
  1569. return NULL;
  1570. }
  1571. EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
  1572. /**
  1573. * snd_soc_add_component_controls - Add an array of controls to a component.
  1574. *
  1575. * @component: Component to add controls to
  1576. * @controls: Array of controls to add
  1577. * @num_controls: Number of elements in the array
  1578. *
  1579. * Return: 0 for success, else error.
  1580. */
  1581. int snd_soc_add_component_controls(struct snd_soc_component *component,
  1582. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1583. {
  1584. struct snd_card *card = component->card->snd_card;
  1585. return snd_soc_add_controls(card, component->dev, controls,
  1586. num_controls, component->name_prefix, component);
  1587. }
  1588. EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
  1589. /**
  1590. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1591. * Convenience function to add a list of controls. Many codecs were
  1592. * duplicating this code.
  1593. *
  1594. * @codec: codec to add controls to
  1595. * @controls: array of controls to add
  1596. * @num_controls: number of elements in the array
  1597. *
  1598. * Return 0 for success, else error.
  1599. */
  1600. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1601. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1602. {
  1603. return snd_soc_add_component_controls(&codec->component, controls,
  1604. num_controls);
  1605. }
  1606. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1607. /**
  1608. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1609. * Convenience function to add a list of controls.
  1610. *
  1611. * @platform: platform to add controls to
  1612. * @controls: array of controls to add
  1613. * @num_controls: number of elements in the array
  1614. *
  1615. * Return 0 for success, else error.
  1616. */
  1617. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1618. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1619. {
  1620. return snd_soc_add_component_controls(&platform->component, controls,
  1621. num_controls);
  1622. }
  1623. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1624. /**
  1625. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1626. * Convenience function to add a list of controls.
  1627. *
  1628. * @soc_card: SoC card to add controls to
  1629. * @controls: array of controls to add
  1630. * @num_controls: number of elements in the array
  1631. *
  1632. * Return 0 for success, else error.
  1633. */
  1634. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1635. const struct snd_kcontrol_new *controls, int num_controls)
  1636. {
  1637. struct snd_card *card = soc_card->snd_card;
  1638. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1639. NULL, soc_card);
  1640. }
  1641. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1642. /**
  1643. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1644. * Convienience function to add a list of controls.
  1645. *
  1646. * @dai: DAI to add controls to
  1647. * @controls: array of controls to add
  1648. * @num_controls: number of elements in the array
  1649. *
  1650. * Return 0 for success, else error.
  1651. */
  1652. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1653. const struct snd_kcontrol_new *controls, int num_controls)
  1654. {
  1655. struct snd_card *card = dai->component->card->snd_card;
  1656. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  1657. NULL, dai);
  1658. }
  1659. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  1660. /**
  1661. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1662. * @dai: DAI
  1663. * @clk_id: DAI specific clock ID
  1664. * @freq: new clock frequency in Hz
  1665. * @dir: new clock direction - input/output.
  1666. *
  1667. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1668. */
  1669. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1670. unsigned int freq, int dir)
  1671. {
  1672. if (dai->driver && dai->driver->ops->set_sysclk)
  1673. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  1674. else if (dai->codec && dai->codec->driver->set_sysclk)
  1675. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  1676. freq, dir);
  1677. else
  1678. return -ENOTSUPP;
  1679. }
  1680. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1681. /**
  1682. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  1683. * @codec: CODEC
  1684. * @clk_id: DAI specific clock ID
  1685. * @source: Source for the clock
  1686. * @freq: new clock frequency in Hz
  1687. * @dir: new clock direction - input/output.
  1688. *
  1689. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  1690. */
  1691. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  1692. int source, unsigned int freq, int dir)
  1693. {
  1694. if (codec->driver->set_sysclk)
  1695. return codec->driver->set_sysclk(codec, clk_id, source,
  1696. freq, dir);
  1697. else
  1698. return -ENOTSUPP;
  1699. }
  1700. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  1701. /**
  1702. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1703. * @dai: DAI
  1704. * @div_id: DAI specific clock divider ID
  1705. * @div: new clock divisor.
  1706. *
  1707. * Configures the clock dividers. This is used to derive the best DAI bit and
  1708. * frame clocks from the system or master clock. It's best to set the DAI bit
  1709. * and frame clocks as low as possible to save system power.
  1710. */
  1711. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1712. int div_id, int div)
  1713. {
  1714. if (dai->driver && dai->driver->ops->set_clkdiv)
  1715. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  1716. else
  1717. return -EINVAL;
  1718. }
  1719. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1720. /**
  1721. * snd_soc_dai_set_pll - configure DAI PLL.
  1722. * @dai: DAI
  1723. * @pll_id: DAI specific PLL ID
  1724. * @source: DAI specific source for the PLL
  1725. * @freq_in: PLL input clock frequency in Hz
  1726. * @freq_out: requested PLL output clock frequency in Hz
  1727. *
  1728. * Configures and enables PLL to generate output clock based on input clock.
  1729. */
  1730. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  1731. unsigned int freq_in, unsigned int freq_out)
  1732. {
  1733. if (dai->driver && dai->driver->ops->set_pll)
  1734. return dai->driver->ops->set_pll(dai, pll_id, source,
  1735. freq_in, freq_out);
  1736. else if (dai->codec && dai->codec->driver->set_pll)
  1737. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  1738. freq_in, freq_out);
  1739. else
  1740. return -EINVAL;
  1741. }
  1742. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  1743. /*
  1744. * snd_soc_codec_set_pll - configure codec PLL.
  1745. * @codec: CODEC
  1746. * @pll_id: DAI specific PLL ID
  1747. * @source: DAI specific source for the PLL
  1748. * @freq_in: PLL input clock frequency in Hz
  1749. * @freq_out: requested PLL output clock frequency in Hz
  1750. *
  1751. * Configures and enables PLL to generate output clock based on input clock.
  1752. */
  1753. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  1754. unsigned int freq_in, unsigned int freq_out)
  1755. {
  1756. if (codec->driver->set_pll)
  1757. return codec->driver->set_pll(codec, pll_id, source,
  1758. freq_in, freq_out);
  1759. else
  1760. return -EINVAL;
  1761. }
  1762. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  1763. /**
  1764. * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  1765. * @dai: DAI
  1766. * @ratio Ratio of BCLK to Sample rate.
  1767. *
  1768. * Configures the DAI for a preset BCLK to sample rate ratio.
  1769. */
  1770. int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  1771. {
  1772. if (dai->driver && dai->driver->ops->set_bclk_ratio)
  1773. return dai->driver->ops->set_bclk_ratio(dai, ratio);
  1774. else
  1775. return -EINVAL;
  1776. }
  1777. EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  1778. /**
  1779. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  1780. * @dai: DAI
  1781. * @fmt: SND_SOC_DAIFMT_ format value.
  1782. *
  1783. * Configures the DAI hardware format and clocking.
  1784. */
  1785. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1786. {
  1787. if (dai->driver == NULL)
  1788. return -EINVAL;
  1789. if (dai->driver->ops->set_fmt == NULL)
  1790. return -ENOTSUPP;
  1791. return dai->driver->ops->set_fmt(dai, fmt);
  1792. }
  1793. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  1794. /**
  1795. * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
  1796. * @slots: Number of slots in use.
  1797. * @tx_mask: bitmask representing active TX slots.
  1798. * @rx_mask: bitmask representing active RX slots.
  1799. *
  1800. * Generates the TDM tx and rx slot default masks for DAI.
  1801. */
  1802. static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
  1803. unsigned int *tx_mask,
  1804. unsigned int *rx_mask)
  1805. {
  1806. if (*tx_mask || *rx_mask)
  1807. return 0;
  1808. if (!slots)
  1809. return -EINVAL;
  1810. *tx_mask = (1 << slots) - 1;
  1811. *rx_mask = (1 << slots) - 1;
  1812. return 0;
  1813. }
  1814. /**
  1815. * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
  1816. * @dai: The DAI to configure
  1817. * @tx_mask: bitmask representing active TX slots.
  1818. * @rx_mask: bitmask representing active RX slots.
  1819. * @slots: Number of slots in use.
  1820. * @slot_width: Width in bits for each slot.
  1821. *
  1822. * This function configures the specified DAI for TDM operation. @slot contains
  1823. * the total number of slots of the TDM stream and @slot_with the width of each
  1824. * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
  1825. * active slots of the TDM stream for the specified DAI, i.e. which slots the
  1826. * DAI should write to or read from. If a bit is set the corresponding slot is
  1827. * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
  1828. * the first slot, bit 1 to the second slot and so on. The first active slot
  1829. * maps to the first channel of the DAI, the second active slot to the second
  1830. * channel and so on.
  1831. *
  1832. * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
  1833. * @rx_mask and @slot_width will be ignored.
  1834. *
  1835. * Returns 0 on success, a negative error code otherwise.
  1836. */
  1837. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  1838. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  1839. {
  1840. if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
  1841. dai->driver->ops->xlate_tdm_slot_mask(slots,
  1842. &tx_mask, &rx_mask);
  1843. else
  1844. snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
  1845. dai->tx_mask = tx_mask;
  1846. dai->rx_mask = rx_mask;
  1847. if (dai->driver && dai->driver->ops->set_tdm_slot)
  1848. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  1849. slots, slot_width);
  1850. else
  1851. return -ENOTSUPP;
  1852. }
  1853. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  1854. /**
  1855. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  1856. * @dai: DAI
  1857. * @tx_num: how many TX channels
  1858. * @tx_slot: pointer to an array which imply the TX slot number channel
  1859. * 0~num-1 uses
  1860. * @rx_num: how many RX channels
  1861. * @rx_slot: pointer to an array which imply the RX slot number channel
  1862. * 0~num-1 uses
  1863. *
  1864. * configure the relationship between channel number and TDM slot number.
  1865. */
  1866. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  1867. unsigned int tx_num, unsigned int *tx_slot,
  1868. unsigned int rx_num, unsigned int *rx_slot)
  1869. {
  1870. if (dai->driver && dai->driver->ops->set_channel_map)
  1871. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  1872. rx_num, rx_slot);
  1873. else
  1874. return -EINVAL;
  1875. }
  1876. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  1877. /**
  1878. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  1879. * @dai: DAI
  1880. * @tristate: tristate enable
  1881. *
  1882. * Tristates the DAI so that others can use it.
  1883. */
  1884. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  1885. {
  1886. if (dai->driver && dai->driver->ops->set_tristate)
  1887. return dai->driver->ops->set_tristate(dai, tristate);
  1888. else
  1889. return -EINVAL;
  1890. }
  1891. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  1892. /**
  1893. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  1894. * @dai: DAI
  1895. * @mute: mute enable
  1896. * @direction: stream to mute
  1897. *
  1898. * Mutes the DAI DAC.
  1899. */
  1900. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  1901. int direction)
  1902. {
  1903. if (!dai->driver)
  1904. return -ENOTSUPP;
  1905. if (dai->driver->ops->mute_stream)
  1906. return dai->driver->ops->mute_stream(dai, mute, direction);
  1907. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  1908. dai->driver->ops->digital_mute)
  1909. return dai->driver->ops->digital_mute(dai, mute);
  1910. else
  1911. return -ENOTSUPP;
  1912. }
  1913. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  1914. static int snd_soc_init_multicodec(struct snd_soc_card *card,
  1915. struct snd_soc_dai_link *dai_link)
  1916. {
  1917. /* Legacy codec/codec_dai link is a single entry in multicodec */
  1918. if (dai_link->codec_name || dai_link->codec_of_node ||
  1919. dai_link->codec_dai_name) {
  1920. dai_link->num_codecs = 1;
  1921. dai_link->codecs = devm_kzalloc(card->dev,
  1922. sizeof(struct snd_soc_dai_link_component),
  1923. GFP_KERNEL);
  1924. if (!dai_link->codecs)
  1925. return -ENOMEM;
  1926. dai_link->codecs[0].name = dai_link->codec_name;
  1927. dai_link->codecs[0].of_node = dai_link->codec_of_node;
  1928. dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
  1929. }
  1930. if (!dai_link->codecs) {
  1931. dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
  1932. return -EINVAL;
  1933. }
  1934. return 0;
  1935. }
  1936. /**
  1937. * snd_soc_register_card - Register a card with the ASoC core
  1938. *
  1939. * @card: Card to register
  1940. *
  1941. */
  1942. int snd_soc_register_card(struct snd_soc_card *card)
  1943. {
  1944. int i, j, ret;
  1945. if (!card->name || !card->dev)
  1946. return -EINVAL;
  1947. for (i = 0; i < card->num_links; i++) {
  1948. struct snd_soc_dai_link *link = &card->dai_link[i];
  1949. ret = snd_soc_init_multicodec(card, link);
  1950. if (ret) {
  1951. dev_err(card->dev, "ASoC: failed to init multicodec\n");
  1952. return ret;
  1953. }
  1954. for (j = 0; j < link->num_codecs; j++) {
  1955. /*
  1956. * Codec must be specified by 1 of name or OF node,
  1957. * not both or neither.
  1958. */
  1959. if (!!link->codecs[j].name ==
  1960. !!link->codecs[j].of_node) {
  1961. dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
  1962. link->name);
  1963. return -EINVAL;
  1964. }
  1965. /* Codec DAI name must be specified */
  1966. if (!link->codecs[j].dai_name) {
  1967. dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
  1968. link->name);
  1969. return -EINVAL;
  1970. }
  1971. }
  1972. /*
  1973. * Platform may be specified by either name or OF node, but
  1974. * can be left unspecified, and a dummy platform will be used.
  1975. */
  1976. if (link->platform_name && link->platform_of_node) {
  1977. dev_err(card->dev,
  1978. "ASoC: Both platform name/of_node are set for %s\n",
  1979. link->name);
  1980. return -EINVAL;
  1981. }
  1982. /*
  1983. * CPU device may be specified by either name or OF node, but
  1984. * can be left unspecified, and will be matched based on DAI
  1985. * name alone..
  1986. */
  1987. if (link->cpu_name && link->cpu_of_node) {
  1988. dev_err(card->dev,
  1989. "ASoC: Neither/both cpu name/of_node are set for %s\n",
  1990. link->name);
  1991. return -EINVAL;
  1992. }
  1993. /*
  1994. * At least one of CPU DAI name or CPU device name/node must be
  1995. * specified
  1996. */
  1997. if (!link->cpu_dai_name &&
  1998. !(link->cpu_name || link->cpu_of_node)) {
  1999. dev_err(card->dev,
  2000. "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
  2001. link->name);
  2002. return -EINVAL;
  2003. }
  2004. }
  2005. dev_set_drvdata(card->dev, card);
  2006. snd_soc_initialize_card_lists(card);
  2007. card->rtd = devm_kzalloc(card->dev,
  2008. sizeof(struct snd_soc_pcm_runtime) *
  2009. (card->num_links + card->num_aux_devs),
  2010. GFP_KERNEL);
  2011. if (card->rtd == NULL)
  2012. return -ENOMEM;
  2013. card->num_rtd = 0;
  2014. card->rtd_aux = &card->rtd[card->num_links];
  2015. for (i = 0; i < card->num_links; i++) {
  2016. card->rtd[i].card = card;
  2017. card->rtd[i].dai_link = &card->dai_link[i];
  2018. card->rtd[i].codec_dais = devm_kzalloc(card->dev,
  2019. sizeof(struct snd_soc_dai *) *
  2020. (card->rtd[i].dai_link->num_codecs),
  2021. GFP_KERNEL);
  2022. if (card->rtd[i].codec_dais == NULL)
  2023. return -ENOMEM;
  2024. }
  2025. for (i = 0; i < card->num_aux_devs; i++)
  2026. card->rtd_aux[i].card = card;
  2027. INIT_LIST_HEAD(&card->dapm_dirty);
  2028. INIT_LIST_HEAD(&card->dobj_list);
  2029. card->instantiated = 0;
  2030. mutex_init(&card->mutex);
  2031. mutex_init(&card->dapm_mutex);
  2032. ret = snd_soc_instantiate_card(card);
  2033. if (ret != 0)
  2034. return ret;
  2035. /* deactivate pins to sleep state */
  2036. for (i = 0; i < card->num_rtd; i++) {
  2037. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  2038. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2039. int j;
  2040. for (j = 0; j < rtd->num_codecs; j++) {
  2041. struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
  2042. if (!codec_dai->active)
  2043. pinctrl_pm_select_sleep_state(codec_dai->dev);
  2044. }
  2045. if (!cpu_dai->active)
  2046. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  2047. }
  2048. return ret;
  2049. }
  2050. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  2051. /**
  2052. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2053. *
  2054. * @card: Card to unregister
  2055. *
  2056. */
  2057. int snd_soc_unregister_card(struct snd_soc_card *card)
  2058. {
  2059. if (card->instantiated) {
  2060. card->instantiated = false;
  2061. snd_soc_dapm_shutdown(card);
  2062. soc_cleanup_card_resources(card);
  2063. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  2064. }
  2065. return 0;
  2066. }
  2067. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  2068. /*
  2069. * Simplify DAI link configuration by removing ".-1" from device names
  2070. * and sanitizing names.
  2071. */
  2072. static char *fmt_single_name(struct device *dev, int *id)
  2073. {
  2074. char *found, name[NAME_SIZE];
  2075. int id1, id2;
  2076. if (dev_name(dev) == NULL)
  2077. return NULL;
  2078. strlcpy(name, dev_name(dev), NAME_SIZE);
  2079. /* are we a "%s.%d" name (platform and SPI components) */
  2080. found = strstr(name, dev->driver->name);
  2081. if (found) {
  2082. /* get ID */
  2083. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  2084. /* discard ID from name if ID == -1 */
  2085. if (*id == -1)
  2086. found[strlen(dev->driver->name)] = '\0';
  2087. }
  2088. } else {
  2089. /* I2C component devices are named "bus-addr" */
  2090. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  2091. char tmp[NAME_SIZE];
  2092. /* create unique ID number from I2C addr and bus */
  2093. *id = ((id1 & 0xffff) << 16) + id2;
  2094. /* sanitize component name for DAI link creation */
  2095. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  2096. strlcpy(name, tmp, NAME_SIZE);
  2097. } else
  2098. *id = 0;
  2099. }
  2100. return kstrdup(name, GFP_KERNEL);
  2101. }
  2102. /*
  2103. * Simplify DAI link naming for single devices with multiple DAIs by removing
  2104. * any ".-1" and using the DAI name (instead of device name).
  2105. */
  2106. static inline char *fmt_multiple_name(struct device *dev,
  2107. struct snd_soc_dai_driver *dai_drv)
  2108. {
  2109. if (dai_drv->name == NULL) {
  2110. dev_err(dev,
  2111. "ASoC: error - multiple DAI %s registered with no name\n",
  2112. dev_name(dev));
  2113. return NULL;
  2114. }
  2115. return kstrdup(dai_drv->name, GFP_KERNEL);
  2116. }
  2117. /**
  2118. * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
  2119. *
  2120. * @component: The component for which the DAIs should be unregistered
  2121. */
  2122. static void snd_soc_unregister_dais(struct snd_soc_component *component)
  2123. {
  2124. struct snd_soc_dai *dai, *_dai;
  2125. list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
  2126. dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
  2127. dai->name);
  2128. list_del(&dai->list);
  2129. kfree(dai->name);
  2130. kfree(dai);
  2131. }
  2132. }
  2133. /**
  2134. * snd_soc_register_dais - Register a DAI with the ASoC core
  2135. *
  2136. * @component: The component the DAIs are registered for
  2137. * @dai_drv: DAI driver to use for the DAIs
  2138. * @count: Number of DAIs
  2139. * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
  2140. * parent's name.
  2141. */
  2142. static int snd_soc_register_dais(struct snd_soc_component *component,
  2143. struct snd_soc_dai_driver *dai_drv, size_t count,
  2144. bool legacy_dai_naming)
  2145. {
  2146. struct device *dev = component->dev;
  2147. struct snd_soc_dai *dai;
  2148. unsigned int i;
  2149. int ret;
  2150. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  2151. component->dai_drv = dai_drv;
  2152. component->num_dai = count;
  2153. for (i = 0; i < count; i++) {
  2154. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  2155. if (dai == NULL) {
  2156. ret = -ENOMEM;
  2157. goto err;
  2158. }
  2159. /*
  2160. * Back in the old days when we still had component-less DAIs,
  2161. * instead of having a static name, component-less DAIs would
  2162. * inherit the name of the parent device so it is possible to
  2163. * register multiple instances of the DAI. We still need to keep
  2164. * the same naming style even though those DAIs are not
  2165. * component-less anymore.
  2166. */
  2167. if (count == 1 && legacy_dai_naming &&
  2168. (dai_drv[i].id == 0 || dai_drv[i].name == NULL)) {
  2169. dai->name = fmt_single_name(dev, &dai->id);
  2170. } else {
  2171. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  2172. if (dai_drv[i].id)
  2173. dai->id = dai_drv[i].id;
  2174. else
  2175. dai->id = i;
  2176. }
  2177. if (dai->name == NULL) {
  2178. kfree(dai);
  2179. ret = -ENOMEM;
  2180. goto err;
  2181. }
  2182. dai->component = component;
  2183. dai->dev = dev;
  2184. dai->driver = &dai_drv[i];
  2185. if (!dai->driver->ops)
  2186. dai->driver->ops = &null_dai_ops;
  2187. list_add(&dai->list, &component->dai_list);
  2188. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  2189. }
  2190. return 0;
  2191. err:
  2192. snd_soc_unregister_dais(component);
  2193. return ret;
  2194. }
  2195. static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
  2196. enum snd_soc_dapm_type type, int subseq)
  2197. {
  2198. struct snd_soc_component *component = dapm->component;
  2199. component->driver->seq_notifier(component, type, subseq);
  2200. }
  2201. static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
  2202. int event)
  2203. {
  2204. struct snd_soc_component *component = dapm->component;
  2205. return component->driver->stream_event(component, event);
  2206. }
  2207. static int snd_soc_component_initialize(struct snd_soc_component *component,
  2208. const struct snd_soc_component_driver *driver, struct device *dev)
  2209. {
  2210. struct snd_soc_dapm_context *dapm;
  2211. component->name = fmt_single_name(dev, &component->id);
  2212. if (!component->name) {
  2213. dev_err(dev, "ASoC: Failed to allocate name\n");
  2214. return -ENOMEM;
  2215. }
  2216. component->dev = dev;
  2217. component->driver = driver;
  2218. component->probe = component->driver->probe;
  2219. component->remove = component->driver->remove;
  2220. if (!component->dapm_ptr)
  2221. component->dapm_ptr = &component->dapm;
  2222. dapm = component->dapm_ptr;
  2223. dapm->dev = dev;
  2224. dapm->component = component;
  2225. dapm->bias_level = SND_SOC_BIAS_OFF;
  2226. dapm->idle_bias_off = true;
  2227. if (driver->seq_notifier)
  2228. dapm->seq_notifier = snd_soc_component_seq_notifier;
  2229. if (driver->stream_event)
  2230. dapm->stream_event = snd_soc_component_stream_event;
  2231. component->controls = driver->controls;
  2232. component->num_controls = driver->num_controls;
  2233. component->dapm_widgets = driver->dapm_widgets;
  2234. component->num_dapm_widgets = driver->num_dapm_widgets;
  2235. component->dapm_routes = driver->dapm_routes;
  2236. component->num_dapm_routes = driver->num_dapm_routes;
  2237. INIT_LIST_HEAD(&component->dai_list);
  2238. mutex_init(&component->io_mutex);
  2239. return 0;
  2240. }
  2241. static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
  2242. {
  2243. int val_bytes = regmap_get_val_bytes(component->regmap);
  2244. /* Errors are legitimate for non-integer byte multiples */
  2245. if (val_bytes > 0)
  2246. component->val_bytes = val_bytes;
  2247. }
  2248. #ifdef CONFIG_REGMAP
  2249. /**
  2250. * snd_soc_component_init_regmap() - Initialize regmap instance for the component
  2251. * @component: The component for which to initialize the regmap instance
  2252. * @regmap: The regmap instance that should be used by the component
  2253. *
  2254. * This function allows deferred assignment of the regmap instance that is
  2255. * associated with the component. Only use this if the regmap instance is not
  2256. * yet ready when the component is registered. The function must also be called
  2257. * before the first IO attempt of the component.
  2258. */
  2259. void snd_soc_component_init_regmap(struct snd_soc_component *component,
  2260. struct regmap *regmap)
  2261. {
  2262. component->regmap = regmap;
  2263. snd_soc_component_setup_regmap(component);
  2264. }
  2265. EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
  2266. /**
  2267. * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
  2268. * @component: The component for which to de-initialize the regmap instance
  2269. *
  2270. * Calls regmap_exit() on the regmap instance associated to the component and
  2271. * removes the regmap instance from the component.
  2272. *
  2273. * This function should only be used if snd_soc_component_init_regmap() was used
  2274. * to initialize the regmap instance.
  2275. */
  2276. void snd_soc_component_exit_regmap(struct snd_soc_component *component)
  2277. {
  2278. regmap_exit(component->regmap);
  2279. component->regmap = NULL;
  2280. }
  2281. EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
  2282. #endif
  2283. static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
  2284. {
  2285. if (!component->write && !component->read) {
  2286. if (!component->regmap)
  2287. component->regmap = dev_get_regmap(component->dev, NULL);
  2288. if (component->regmap)
  2289. snd_soc_component_setup_regmap(component);
  2290. }
  2291. list_add(&component->list, &component_list);
  2292. INIT_LIST_HEAD(&component->dobj_list);
  2293. }
  2294. static void snd_soc_component_add(struct snd_soc_component *component)
  2295. {
  2296. mutex_lock(&client_mutex);
  2297. snd_soc_component_add_unlocked(component);
  2298. mutex_unlock(&client_mutex);
  2299. }
  2300. static void snd_soc_component_cleanup(struct snd_soc_component *component)
  2301. {
  2302. snd_soc_unregister_dais(component);
  2303. kfree(component->name);
  2304. }
  2305. static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
  2306. {
  2307. list_del(&component->list);
  2308. }
  2309. int snd_soc_register_component(struct device *dev,
  2310. const struct snd_soc_component_driver *cmpnt_drv,
  2311. struct snd_soc_dai_driver *dai_drv,
  2312. int num_dai)
  2313. {
  2314. struct snd_soc_component *cmpnt;
  2315. int ret;
  2316. cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
  2317. if (!cmpnt) {
  2318. dev_err(dev, "ASoC: Failed to allocate memory\n");
  2319. return -ENOMEM;
  2320. }
  2321. ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
  2322. if (ret)
  2323. goto err_free;
  2324. cmpnt->ignore_pmdown_time = true;
  2325. cmpnt->registered_as_component = true;
  2326. ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
  2327. if (ret < 0) {
  2328. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2329. goto err_cleanup;
  2330. }
  2331. snd_soc_component_add(cmpnt);
  2332. return 0;
  2333. err_cleanup:
  2334. snd_soc_component_cleanup(cmpnt);
  2335. err_free:
  2336. kfree(cmpnt);
  2337. return ret;
  2338. }
  2339. EXPORT_SYMBOL_GPL(snd_soc_register_component);
  2340. /**
  2341. * snd_soc_unregister_component - Unregister a component from the ASoC core
  2342. *
  2343. */
  2344. void snd_soc_unregister_component(struct device *dev)
  2345. {
  2346. struct snd_soc_component *cmpnt;
  2347. mutex_lock(&client_mutex);
  2348. list_for_each_entry(cmpnt, &component_list, list) {
  2349. if (dev == cmpnt->dev && cmpnt->registered_as_component)
  2350. goto found;
  2351. }
  2352. mutex_unlock(&client_mutex);
  2353. return;
  2354. found:
  2355. snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
  2356. snd_soc_component_del_unlocked(cmpnt);
  2357. mutex_unlock(&client_mutex);
  2358. snd_soc_component_cleanup(cmpnt);
  2359. kfree(cmpnt);
  2360. }
  2361. EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
  2362. static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
  2363. {
  2364. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2365. return platform->driver->probe(platform);
  2366. }
  2367. static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
  2368. {
  2369. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2370. platform->driver->remove(platform);
  2371. }
  2372. /**
  2373. * snd_soc_add_platform - Add a platform to the ASoC core
  2374. * @dev: The parent device for the platform
  2375. * @platform: The platform to add
  2376. * @platform_driver: The driver for the platform
  2377. */
  2378. int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
  2379. const struct snd_soc_platform_driver *platform_drv)
  2380. {
  2381. int ret;
  2382. ret = snd_soc_component_initialize(&platform->component,
  2383. &platform_drv->component_driver, dev);
  2384. if (ret)
  2385. return ret;
  2386. platform->dev = dev;
  2387. platform->driver = platform_drv;
  2388. if (platform_drv->probe)
  2389. platform->component.probe = snd_soc_platform_drv_probe;
  2390. if (platform_drv->remove)
  2391. platform->component.remove = snd_soc_platform_drv_remove;
  2392. #ifdef CONFIG_DEBUG_FS
  2393. platform->component.debugfs_prefix = "platform";
  2394. #endif
  2395. mutex_lock(&client_mutex);
  2396. snd_soc_component_add_unlocked(&platform->component);
  2397. list_add(&platform->list, &platform_list);
  2398. mutex_unlock(&client_mutex);
  2399. dev_dbg(dev, "ASoC: Registered platform '%s'\n",
  2400. platform->component.name);
  2401. return 0;
  2402. }
  2403. EXPORT_SYMBOL_GPL(snd_soc_add_platform);
  2404. /**
  2405. * snd_soc_register_platform - Register a platform with the ASoC core
  2406. *
  2407. * @platform: platform to register
  2408. */
  2409. int snd_soc_register_platform(struct device *dev,
  2410. const struct snd_soc_platform_driver *platform_drv)
  2411. {
  2412. struct snd_soc_platform *platform;
  2413. int ret;
  2414. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  2415. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  2416. if (platform == NULL)
  2417. return -ENOMEM;
  2418. ret = snd_soc_add_platform(dev, platform, platform_drv);
  2419. if (ret)
  2420. kfree(platform);
  2421. return ret;
  2422. }
  2423. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2424. /**
  2425. * snd_soc_remove_platform - Remove a platform from the ASoC core
  2426. * @platform: the platform to remove
  2427. */
  2428. void snd_soc_remove_platform(struct snd_soc_platform *platform)
  2429. {
  2430. mutex_lock(&client_mutex);
  2431. list_del(&platform->list);
  2432. snd_soc_component_del_unlocked(&platform->component);
  2433. mutex_unlock(&client_mutex);
  2434. dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
  2435. platform->component.name);
  2436. snd_soc_component_cleanup(&platform->component);
  2437. }
  2438. EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
  2439. struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
  2440. {
  2441. struct snd_soc_platform *platform;
  2442. mutex_lock(&client_mutex);
  2443. list_for_each_entry(platform, &platform_list, list) {
  2444. if (dev == platform->dev) {
  2445. mutex_unlock(&client_mutex);
  2446. return platform;
  2447. }
  2448. }
  2449. mutex_unlock(&client_mutex);
  2450. return NULL;
  2451. }
  2452. EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
  2453. /**
  2454. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2455. *
  2456. * @platform: platform to unregister
  2457. */
  2458. void snd_soc_unregister_platform(struct device *dev)
  2459. {
  2460. struct snd_soc_platform *platform;
  2461. platform = snd_soc_lookup_platform(dev);
  2462. if (!platform)
  2463. return;
  2464. snd_soc_remove_platform(platform);
  2465. kfree(platform);
  2466. }
  2467. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2468. static u64 codec_format_map[] = {
  2469. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2470. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2471. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2472. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2473. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2474. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2475. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2476. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2477. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2478. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2479. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2480. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2481. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2482. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2483. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2484. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2485. };
  2486. /* Fix up the DAI formats for endianness: codecs don't actually see
  2487. * the endianness of the data but we're using the CPU format
  2488. * definitions which do need to include endianness so we ensure that
  2489. * codec DAIs always have both big and little endian variants set.
  2490. */
  2491. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2492. {
  2493. int i;
  2494. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2495. if (stream->formats & codec_format_map[i])
  2496. stream->formats |= codec_format_map[i];
  2497. }
  2498. static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
  2499. {
  2500. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2501. return codec->driver->probe(codec);
  2502. }
  2503. static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
  2504. {
  2505. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2506. codec->driver->remove(codec);
  2507. }
  2508. static int snd_soc_codec_drv_write(struct snd_soc_component *component,
  2509. unsigned int reg, unsigned int val)
  2510. {
  2511. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2512. return codec->driver->write(codec, reg, val);
  2513. }
  2514. static int snd_soc_codec_drv_read(struct snd_soc_component *component,
  2515. unsigned int reg, unsigned int *val)
  2516. {
  2517. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2518. *val = codec->driver->read(codec, reg);
  2519. return 0;
  2520. }
  2521. static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
  2522. enum snd_soc_bias_level level)
  2523. {
  2524. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
  2525. return codec->driver->set_bias_level(codec, level);
  2526. }
  2527. /**
  2528. * snd_soc_register_codec - Register a codec with the ASoC core
  2529. *
  2530. * @codec: codec to register
  2531. */
  2532. int snd_soc_register_codec(struct device *dev,
  2533. const struct snd_soc_codec_driver *codec_drv,
  2534. struct snd_soc_dai_driver *dai_drv,
  2535. int num_dai)
  2536. {
  2537. struct snd_soc_codec *codec;
  2538. struct snd_soc_dai *dai;
  2539. int ret, i;
  2540. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  2541. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  2542. if (codec == NULL)
  2543. return -ENOMEM;
  2544. codec->component.dapm_ptr = &codec->dapm;
  2545. codec->component.codec = codec;
  2546. ret = snd_soc_component_initialize(&codec->component,
  2547. &codec_drv->component_driver, dev);
  2548. if (ret)
  2549. goto err_free;
  2550. if (codec_drv->controls) {
  2551. codec->component.controls = codec_drv->controls;
  2552. codec->component.num_controls = codec_drv->num_controls;
  2553. }
  2554. if (codec_drv->dapm_widgets) {
  2555. codec->component.dapm_widgets = codec_drv->dapm_widgets;
  2556. codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
  2557. }
  2558. if (codec_drv->dapm_routes) {
  2559. codec->component.dapm_routes = codec_drv->dapm_routes;
  2560. codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
  2561. }
  2562. if (codec_drv->probe)
  2563. codec->component.probe = snd_soc_codec_drv_probe;
  2564. if (codec_drv->remove)
  2565. codec->component.remove = snd_soc_codec_drv_remove;
  2566. if (codec_drv->write)
  2567. codec->component.write = snd_soc_codec_drv_write;
  2568. if (codec_drv->read)
  2569. codec->component.read = snd_soc_codec_drv_read;
  2570. codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  2571. codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
  2572. codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
  2573. if (codec_drv->seq_notifier)
  2574. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  2575. if (codec_drv->set_bias_level)
  2576. codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
  2577. codec->dev = dev;
  2578. codec->driver = codec_drv;
  2579. codec->component.val_bytes = codec_drv->reg_word_size;
  2580. #ifdef CONFIG_DEBUG_FS
  2581. codec->component.init_debugfs = soc_init_codec_debugfs;
  2582. codec->component.debugfs_prefix = "codec";
  2583. #endif
  2584. if (codec_drv->get_regmap)
  2585. codec->component.regmap = codec_drv->get_regmap(dev);
  2586. for (i = 0; i < num_dai; i++) {
  2587. fixup_codec_formats(&dai_drv[i].playback);
  2588. fixup_codec_formats(&dai_drv[i].capture);
  2589. }
  2590. ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
  2591. if (ret < 0) {
  2592. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2593. goto err_cleanup;
  2594. }
  2595. list_for_each_entry(dai, &codec->component.dai_list, list)
  2596. dai->codec = codec;
  2597. mutex_lock(&client_mutex);
  2598. snd_soc_component_add_unlocked(&codec->component);
  2599. list_add(&codec->list, &codec_list);
  2600. mutex_unlock(&client_mutex);
  2601. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
  2602. codec->component.name);
  2603. return 0;
  2604. err_cleanup:
  2605. snd_soc_component_cleanup(&codec->component);
  2606. err_free:
  2607. kfree(codec);
  2608. return ret;
  2609. }
  2610. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2611. /**
  2612. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2613. *
  2614. * @codec: codec to unregister
  2615. */
  2616. void snd_soc_unregister_codec(struct device *dev)
  2617. {
  2618. struct snd_soc_codec *codec;
  2619. mutex_lock(&client_mutex);
  2620. list_for_each_entry(codec, &codec_list, list) {
  2621. if (dev == codec->dev)
  2622. goto found;
  2623. }
  2624. mutex_unlock(&client_mutex);
  2625. return;
  2626. found:
  2627. list_del(&codec->list);
  2628. snd_soc_component_del_unlocked(&codec->component);
  2629. mutex_unlock(&client_mutex);
  2630. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
  2631. codec->component.name);
  2632. snd_soc_component_cleanup(&codec->component);
  2633. snd_soc_cache_exit(codec);
  2634. kfree(codec);
  2635. }
  2636. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2637. /* Retrieve a card's name from device tree */
  2638. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  2639. const char *propname)
  2640. {
  2641. struct device_node *np;
  2642. int ret;
  2643. if (!card->dev) {
  2644. pr_err("card->dev is not set before calling %s\n", __func__);
  2645. return -EINVAL;
  2646. }
  2647. np = card->dev->of_node;
  2648. ret = of_property_read_string_index(np, propname, 0, &card->name);
  2649. /*
  2650. * EINVAL means the property does not exist. This is fine providing
  2651. * card->name was previously set, which is checked later in
  2652. * snd_soc_register_card.
  2653. */
  2654. if (ret < 0 && ret != -EINVAL) {
  2655. dev_err(card->dev,
  2656. "ASoC: Property '%s' could not be read: %d\n",
  2657. propname, ret);
  2658. return ret;
  2659. }
  2660. return 0;
  2661. }
  2662. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  2663. static const struct snd_soc_dapm_widget simple_widgets[] = {
  2664. SND_SOC_DAPM_MIC("Microphone", NULL),
  2665. SND_SOC_DAPM_LINE("Line", NULL),
  2666. SND_SOC_DAPM_HP("Headphone", NULL),
  2667. SND_SOC_DAPM_SPK("Speaker", NULL),
  2668. };
  2669. int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
  2670. const char *propname)
  2671. {
  2672. struct device_node *np = card->dev->of_node;
  2673. struct snd_soc_dapm_widget *widgets;
  2674. const char *template, *wname;
  2675. int i, j, num_widgets, ret;
  2676. num_widgets = of_property_count_strings(np, propname);
  2677. if (num_widgets < 0) {
  2678. dev_err(card->dev,
  2679. "ASoC: Property '%s' does not exist\n", propname);
  2680. return -EINVAL;
  2681. }
  2682. if (num_widgets & 1) {
  2683. dev_err(card->dev,
  2684. "ASoC: Property '%s' length is not even\n", propname);
  2685. return -EINVAL;
  2686. }
  2687. num_widgets /= 2;
  2688. if (!num_widgets) {
  2689. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  2690. propname);
  2691. return -EINVAL;
  2692. }
  2693. widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
  2694. GFP_KERNEL);
  2695. if (!widgets) {
  2696. dev_err(card->dev,
  2697. "ASoC: Could not allocate memory for widgets\n");
  2698. return -ENOMEM;
  2699. }
  2700. for (i = 0; i < num_widgets; i++) {
  2701. ret = of_property_read_string_index(np, propname,
  2702. 2 * i, &template);
  2703. if (ret) {
  2704. dev_err(card->dev,
  2705. "ASoC: Property '%s' index %d read error:%d\n",
  2706. propname, 2 * i, ret);
  2707. return -EINVAL;
  2708. }
  2709. for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
  2710. if (!strncmp(template, simple_widgets[j].name,
  2711. strlen(simple_widgets[j].name))) {
  2712. widgets[i] = simple_widgets[j];
  2713. break;
  2714. }
  2715. }
  2716. if (j >= ARRAY_SIZE(simple_widgets)) {
  2717. dev_err(card->dev,
  2718. "ASoC: DAPM widget '%s' is not supported\n",
  2719. template);
  2720. return -EINVAL;
  2721. }
  2722. ret = of_property_read_string_index(np, propname,
  2723. (2 * i) + 1,
  2724. &wname);
  2725. if (ret) {
  2726. dev_err(card->dev,
  2727. "ASoC: Property '%s' index %d read error:%d\n",
  2728. propname, (2 * i) + 1, ret);
  2729. return -EINVAL;
  2730. }
  2731. widgets[i].name = wname;
  2732. }
  2733. card->of_dapm_widgets = widgets;
  2734. card->num_of_dapm_widgets = num_widgets;
  2735. return 0;
  2736. }
  2737. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
  2738. int snd_soc_of_parse_tdm_slot(struct device_node *np,
  2739. unsigned int *slots,
  2740. unsigned int *slot_width)
  2741. {
  2742. u32 val;
  2743. int ret;
  2744. if (of_property_read_bool(np, "dai-tdm-slot-num")) {
  2745. ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
  2746. if (ret)
  2747. return ret;
  2748. if (slots)
  2749. *slots = val;
  2750. }
  2751. if (of_property_read_bool(np, "dai-tdm-slot-width")) {
  2752. ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
  2753. if (ret)
  2754. return ret;
  2755. if (slot_width)
  2756. *slot_width = val;
  2757. }
  2758. return 0;
  2759. }
  2760. EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
  2761. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  2762. const char *propname)
  2763. {
  2764. struct device_node *np = card->dev->of_node;
  2765. int num_routes;
  2766. struct snd_soc_dapm_route *routes;
  2767. int i, ret;
  2768. num_routes = of_property_count_strings(np, propname);
  2769. if (num_routes < 0 || num_routes & 1) {
  2770. dev_err(card->dev,
  2771. "ASoC: Property '%s' does not exist or its length is not even\n",
  2772. propname);
  2773. return -EINVAL;
  2774. }
  2775. num_routes /= 2;
  2776. if (!num_routes) {
  2777. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  2778. propname);
  2779. return -EINVAL;
  2780. }
  2781. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  2782. GFP_KERNEL);
  2783. if (!routes) {
  2784. dev_err(card->dev,
  2785. "ASoC: Could not allocate DAPM route table\n");
  2786. return -EINVAL;
  2787. }
  2788. for (i = 0; i < num_routes; i++) {
  2789. ret = of_property_read_string_index(np, propname,
  2790. 2 * i, &routes[i].sink);
  2791. if (ret) {
  2792. dev_err(card->dev,
  2793. "ASoC: Property '%s' index %d could not be read: %d\n",
  2794. propname, 2 * i, ret);
  2795. return -EINVAL;
  2796. }
  2797. ret = of_property_read_string_index(np, propname,
  2798. (2 * i) + 1, &routes[i].source);
  2799. if (ret) {
  2800. dev_err(card->dev,
  2801. "ASoC: Property '%s' index %d could not be read: %d\n",
  2802. propname, (2 * i) + 1, ret);
  2803. return -EINVAL;
  2804. }
  2805. }
  2806. card->num_of_dapm_routes = num_routes;
  2807. card->of_dapm_routes = routes;
  2808. return 0;
  2809. }
  2810. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  2811. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  2812. const char *prefix,
  2813. struct device_node **bitclkmaster,
  2814. struct device_node **framemaster)
  2815. {
  2816. int ret, i;
  2817. char prop[128];
  2818. unsigned int format = 0;
  2819. int bit, frame;
  2820. const char *str;
  2821. struct {
  2822. char *name;
  2823. unsigned int val;
  2824. } of_fmt_table[] = {
  2825. { "i2s", SND_SOC_DAIFMT_I2S },
  2826. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  2827. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  2828. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  2829. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  2830. { "ac97", SND_SOC_DAIFMT_AC97 },
  2831. { "pdm", SND_SOC_DAIFMT_PDM},
  2832. { "msb", SND_SOC_DAIFMT_MSB },
  2833. { "lsb", SND_SOC_DAIFMT_LSB },
  2834. };
  2835. if (!prefix)
  2836. prefix = "";
  2837. /*
  2838. * check "[prefix]format = xxx"
  2839. * SND_SOC_DAIFMT_FORMAT_MASK area
  2840. */
  2841. snprintf(prop, sizeof(prop), "%sformat", prefix);
  2842. ret = of_property_read_string(np, prop, &str);
  2843. if (ret == 0) {
  2844. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  2845. if (strcmp(str, of_fmt_table[i].name) == 0) {
  2846. format |= of_fmt_table[i].val;
  2847. break;
  2848. }
  2849. }
  2850. }
  2851. /*
  2852. * check "[prefix]continuous-clock"
  2853. * SND_SOC_DAIFMT_CLOCK_MASK area
  2854. */
  2855. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  2856. if (of_get_property(np, prop, NULL))
  2857. format |= SND_SOC_DAIFMT_CONT;
  2858. else
  2859. format |= SND_SOC_DAIFMT_GATED;
  2860. /*
  2861. * check "[prefix]bitclock-inversion"
  2862. * check "[prefix]frame-inversion"
  2863. * SND_SOC_DAIFMT_INV_MASK area
  2864. */
  2865. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  2866. bit = !!of_get_property(np, prop, NULL);
  2867. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  2868. frame = !!of_get_property(np, prop, NULL);
  2869. switch ((bit << 4) + frame) {
  2870. case 0x11:
  2871. format |= SND_SOC_DAIFMT_IB_IF;
  2872. break;
  2873. case 0x10:
  2874. format |= SND_SOC_DAIFMT_IB_NF;
  2875. break;
  2876. case 0x01:
  2877. format |= SND_SOC_DAIFMT_NB_IF;
  2878. break;
  2879. default:
  2880. /* SND_SOC_DAIFMT_NB_NF is default */
  2881. break;
  2882. }
  2883. /*
  2884. * check "[prefix]bitclock-master"
  2885. * check "[prefix]frame-master"
  2886. * SND_SOC_DAIFMT_MASTER_MASK area
  2887. */
  2888. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  2889. bit = !!of_get_property(np, prop, NULL);
  2890. if (bit && bitclkmaster)
  2891. *bitclkmaster = of_parse_phandle(np, prop, 0);
  2892. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  2893. frame = !!of_get_property(np, prop, NULL);
  2894. if (frame && framemaster)
  2895. *framemaster = of_parse_phandle(np, prop, 0);
  2896. switch ((bit << 4) + frame) {
  2897. case 0x11:
  2898. format |= SND_SOC_DAIFMT_CBM_CFM;
  2899. break;
  2900. case 0x10:
  2901. format |= SND_SOC_DAIFMT_CBM_CFS;
  2902. break;
  2903. case 0x01:
  2904. format |= SND_SOC_DAIFMT_CBS_CFM;
  2905. break;
  2906. default:
  2907. format |= SND_SOC_DAIFMT_CBS_CFS;
  2908. break;
  2909. }
  2910. return format;
  2911. }
  2912. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  2913. static int snd_soc_get_dai_name(struct of_phandle_args *args,
  2914. const char **dai_name)
  2915. {
  2916. struct snd_soc_component *pos;
  2917. struct device_node *component_of_node;
  2918. int ret = -EPROBE_DEFER;
  2919. mutex_lock(&client_mutex);
  2920. list_for_each_entry(pos, &component_list, list) {
  2921. component_of_node = pos->dev->of_node;
  2922. if (!component_of_node && pos->dev->parent)
  2923. component_of_node = pos->dev->parent->of_node;
  2924. if (component_of_node != args->np)
  2925. continue;
  2926. if (pos->driver->of_xlate_dai_name) {
  2927. ret = pos->driver->of_xlate_dai_name(pos,
  2928. args,
  2929. dai_name);
  2930. } else {
  2931. int id = -1;
  2932. switch (args->args_count) {
  2933. case 0:
  2934. id = 0; /* same as dai_drv[0] */
  2935. break;
  2936. case 1:
  2937. id = args->args[0];
  2938. break;
  2939. default:
  2940. /* not supported */
  2941. break;
  2942. }
  2943. if (id < 0 || id >= pos->num_dai) {
  2944. ret = -EINVAL;
  2945. continue;
  2946. }
  2947. ret = 0;
  2948. *dai_name = pos->dai_drv[id].name;
  2949. if (!*dai_name)
  2950. *dai_name = pos->name;
  2951. }
  2952. break;
  2953. }
  2954. mutex_unlock(&client_mutex);
  2955. return ret;
  2956. }
  2957. int snd_soc_of_get_dai_name(struct device_node *of_node,
  2958. const char **dai_name)
  2959. {
  2960. struct of_phandle_args args;
  2961. int ret;
  2962. ret = of_parse_phandle_with_args(of_node, "sound-dai",
  2963. "#sound-dai-cells", 0, &args);
  2964. if (ret)
  2965. return ret;
  2966. ret = snd_soc_get_dai_name(&args, dai_name);
  2967. of_node_put(args.np);
  2968. return ret;
  2969. }
  2970. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
  2971. /*
  2972. * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
  2973. * @dev: Card device
  2974. * @of_node: Device node
  2975. * @dai_link: DAI link
  2976. *
  2977. * Builds an array of CODEC DAI components from the DAI link property
  2978. * 'sound-dai'.
  2979. * The array is set in the DAI link and the number of DAIs is set accordingly.
  2980. * The device nodes in the array (of_node) must be dereferenced by the caller.
  2981. *
  2982. * Returns 0 for success
  2983. */
  2984. int snd_soc_of_get_dai_link_codecs(struct device *dev,
  2985. struct device_node *of_node,
  2986. struct snd_soc_dai_link *dai_link)
  2987. {
  2988. struct of_phandle_args args;
  2989. struct snd_soc_dai_link_component *component;
  2990. char *name;
  2991. int index, num_codecs, ret;
  2992. /* Count the number of CODECs */
  2993. name = "sound-dai";
  2994. num_codecs = of_count_phandle_with_args(of_node, name,
  2995. "#sound-dai-cells");
  2996. if (num_codecs <= 0) {
  2997. if (num_codecs == -ENOENT)
  2998. dev_err(dev, "No 'sound-dai' property\n");
  2999. else
  3000. dev_err(dev, "Bad phandle in 'sound-dai'\n");
  3001. return num_codecs;
  3002. }
  3003. component = devm_kzalloc(dev,
  3004. sizeof *component * num_codecs,
  3005. GFP_KERNEL);
  3006. if (!component)
  3007. return -ENOMEM;
  3008. dai_link->codecs = component;
  3009. dai_link->num_codecs = num_codecs;
  3010. /* Parse the list */
  3011. for (index = 0, component = dai_link->codecs;
  3012. index < dai_link->num_codecs;
  3013. index++, component++) {
  3014. ret = of_parse_phandle_with_args(of_node, name,
  3015. "#sound-dai-cells",
  3016. index, &args);
  3017. if (ret)
  3018. goto err;
  3019. component->of_node = args.np;
  3020. ret = snd_soc_get_dai_name(&args, &component->dai_name);
  3021. if (ret < 0)
  3022. goto err;
  3023. }
  3024. return 0;
  3025. err:
  3026. for (index = 0, component = dai_link->codecs;
  3027. index < dai_link->num_codecs;
  3028. index++, component++) {
  3029. if (!component->of_node)
  3030. break;
  3031. of_node_put(component->of_node);
  3032. component->of_node = NULL;
  3033. }
  3034. dai_link->codecs = NULL;
  3035. dai_link->num_codecs = 0;
  3036. return ret;
  3037. }
  3038. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
  3039. static int __init snd_soc_init(void)
  3040. {
  3041. snd_soc_debugfs_init();
  3042. snd_soc_util_init();
  3043. return platform_driver_register(&soc_driver);
  3044. }
  3045. module_init(snd_soc_init);
  3046. static void __exit snd_soc_exit(void)
  3047. {
  3048. snd_soc_util_exit();
  3049. snd_soc_debugfs_exit();
  3050. #ifdef CONFIG_DEBUG_FS
  3051. #endif
  3052. platform_driver_unregister(&soc_driver);
  3053. }
  3054. module_exit(snd_soc_exit);
  3055. /* Module information */
  3056. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3057. MODULE_DESCRIPTION("ALSA SoC Core");
  3058. MODULE_LICENSE("GPL");
  3059. MODULE_ALIAS("platform:soc-audio");