soc-core.c 92 KB

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