soc-core.c 93 KB

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