soc-core.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033
  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. struct snd_soc_pcm_runtime *rtd;
  439. list_for_each_entry(rtd, &card->rtd_list, list) {
  440. if (rtd->dai_link->no_pcm &&
  441. !strcmp(rtd->dai_link->name, dai_link))
  442. return rtd->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. static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
  449. struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
  450. {
  451. struct snd_soc_pcm_runtime *rtd;
  452. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  453. if (!rtd)
  454. return NULL;
  455. rtd->card = card;
  456. rtd->dai_link = dai_link;
  457. rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
  458. dai_link->num_codecs,
  459. GFP_KERNEL);
  460. if (!rtd->codec_dais) {
  461. kfree(rtd);
  462. return NULL;
  463. }
  464. return rtd;
  465. }
  466. static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
  467. {
  468. if (rtd && rtd->codec_dais)
  469. kfree(rtd->codec_dais);
  470. kfree(rtd);
  471. }
  472. static void soc_add_pcm_runtime(struct snd_soc_card *card,
  473. struct snd_soc_pcm_runtime *rtd)
  474. {
  475. list_add_tail(&rtd->list, &card->rtd_list);
  476. rtd->num = card->num_rtd;
  477. card->num_rtd++;
  478. }
  479. static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
  480. {
  481. struct snd_soc_pcm_runtime *rtd, *_rtd;
  482. list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
  483. list_del(&rtd->list);
  484. soc_free_pcm_runtime(rtd);
  485. }
  486. card->num_rtd = 0;
  487. }
  488. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  489. const char *dai_link)
  490. {
  491. struct snd_soc_pcm_runtime *rtd;
  492. list_for_each_entry(rtd, &card->rtd_list, list) {
  493. if (!strcmp(rtd->dai_link->name, dai_link))
  494. return rtd;
  495. }
  496. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  497. return NULL;
  498. }
  499. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  500. static void codec2codec_close_delayed_work(struct work_struct *work)
  501. {
  502. /* Currently nothing to do for c2c links
  503. * Since c2c links are internal nodes in the DAPM graph and
  504. * don't interface with the outside world or application layer
  505. * we don't have to do any special handling on close.
  506. */
  507. }
  508. #ifdef CONFIG_PM_SLEEP
  509. /* powers down audio subsystem for suspend */
  510. int snd_soc_suspend(struct device *dev)
  511. {
  512. struct snd_soc_card *card = dev_get_drvdata(dev);
  513. struct snd_soc_component *component;
  514. struct snd_soc_pcm_runtime *rtd;
  515. int i;
  516. /* If the card is not initialized yet there is nothing to do */
  517. if (!card->instantiated)
  518. return 0;
  519. /* Due to the resume being scheduled into a workqueue we could
  520. * suspend before that's finished - wait for it to complete.
  521. */
  522. snd_power_lock(card->snd_card);
  523. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  524. snd_power_unlock(card->snd_card);
  525. /* we're going to block userspace touching us until resume completes */
  526. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  527. /* mute any active DACs */
  528. list_for_each_entry(rtd, &card->rtd_list, list) {
  529. if (rtd->dai_link->ignore_suspend)
  530. continue;
  531. for (i = 0; i < rtd->num_codecs; i++) {
  532. struct snd_soc_dai *dai = rtd->codec_dais[i];
  533. struct snd_soc_dai_driver *drv = dai->driver;
  534. if (drv->ops->digital_mute && dai->playback_active)
  535. drv->ops->digital_mute(dai, 1);
  536. }
  537. }
  538. /* suspend all pcms */
  539. list_for_each_entry(rtd, &card->rtd_list, list) {
  540. if (rtd->dai_link->ignore_suspend)
  541. continue;
  542. snd_pcm_suspend_all(rtd->pcm);
  543. }
  544. if (card->suspend_pre)
  545. card->suspend_pre(card);
  546. list_for_each_entry(rtd, &card->rtd_list, list) {
  547. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  548. if (rtd->dai_link->ignore_suspend)
  549. continue;
  550. if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
  551. cpu_dai->driver->suspend(cpu_dai);
  552. }
  553. /* close any waiting streams */
  554. list_for_each_entry(rtd, &card->rtd_list, list)
  555. flush_delayed_work(&rtd->delayed_work);
  556. list_for_each_entry(rtd, &card->rtd_list, list) {
  557. if (rtd->dai_link->ignore_suspend)
  558. continue;
  559. snd_soc_dapm_stream_event(rtd,
  560. SNDRV_PCM_STREAM_PLAYBACK,
  561. SND_SOC_DAPM_STREAM_SUSPEND);
  562. snd_soc_dapm_stream_event(rtd,
  563. SNDRV_PCM_STREAM_CAPTURE,
  564. SND_SOC_DAPM_STREAM_SUSPEND);
  565. }
  566. /* Recheck all endpoints too, their state is affected by suspend */
  567. dapm_mark_endpoints_dirty(card);
  568. snd_soc_dapm_sync(&card->dapm);
  569. /* suspend all COMPONENTs */
  570. list_for_each_entry(component, &card->component_dev_list, card_list) {
  571. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  572. /* If there are paths active then the COMPONENT will be held with
  573. * bias _ON and should not be suspended. */
  574. if (!component->suspended) {
  575. switch (snd_soc_dapm_get_bias_level(dapm)) {
  576. case SND_SOC_BIAS_STANDBY:
  577. /*
  578. * If the COMPONENT is capable of idle
  579. * bias off then being in STANDBY
  580. * means it's doing something,
  581. * otherwise fall through.
  582. */
  583. if (dapm->idle_bias_off) {
  584. dev_dbg(component->dev,
  585. "ASoC: idle_bias_off CODEC on over suspend\n");
  586. break;
  587. }
  588. case SND_SOC_BIAS_OFF:
  589. if (component->suspend)
  590. component->suspend(component);
  591. component->suspended = 1;
  592. if (component->regmap)
  593. regcache_mark_dirty(component->regmap);
  594. /* deactivate pins to sleep state */
  595. pinctrl_pm_select_sleep_state(component->dev);
  596. break;
  597. default:
  598. dev_dbg(component->dev,
  599. "ASoC: COMPONENT is on over suspend\n");
  600. break;
  601. }
  602. }
  603. }
  604. list_for_each_entry(rtd, &card->rtd_list, list) {
  605. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  606. if (rtd->dai_link->ignore_suspend)
  607. continue;
  608. if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
  609. cpu_dai->driver->suspend(cpu_dai);
  610. /* deactivate pins to sleep state */
  611. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  612. }
  613. if (card->suspend_post)
  614. card->suspend_post(card);
  615. return 0;
  616. }
  617. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  618. /* deferred resume work, so resume can complete before we finished
  619. * setting our codec back up, which can be very slow on I2C
  620. */
  621. static void soc_resume_deferred(struct work_struct *work)
  622. {
  623. struct snd_soc_card *card =
  624. container_of(work, struct snd_soc_card, deferred_resume_work);
  625. struct snd_soc_pcm_runtime *rtd;
  626. struct snd_soc_component *component;
  627. int i;
  628. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  629. * so userspace apps are blocked from touching us
  630. */
  631. dev_dbg(card->dev, "ASoC: starting resume work\n");
  632. /* Bring us up into D2 so that DAPM starts enabling things */
  633. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  634. if (card->resume_pre)
  635. card->resume_pre(card);
  636. /* resume control bus DAIs */
  637. list_for_each_entry(rtd, &card->rtd_list, list) {
  638. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  639. if (rtd->dai_link->ignore_suspend)
  640. continue;
  641. if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
  642. cpu_dai->driver->resume(cpu_dai);
  643. }
  644. list_for_each_entry(component, &card->component_dev_list, card_list) {
  645. if (component->suspended) {
  646. if (component->resume)
  647. component->resume(component);
  648. component->suspended = 0;
  649. }
  650. }
  651. list_for_each_entry(rtd, &card->rtd_list, list) {
  652. if (rtd->dai_link->ignore_suspend)
  653. continue;
  654. snd_soc_dapm_stream_event(rtd,
  655. SNDRV_PCM_STREAM_PLAYBACK,
  656. SND_SOC_DAPM_STREAM_RESUME);
  657. snd_soc_dapm_stream_event(rtd,
  658. SNDRV_PCM_STREAM_CAPTURE,
  659. SND_SOC_DAPM_STREAM_RESUME);
  660. }
  661. /* unmute any active DACs */
  662. list_for_each_entry(rtd, &card->rtd_list, list) {
  663. if (rtd->dai_link->ignore_suspend)
  664. continue;
  665. for (i = 0; i < rtd->num_codecs; i++) {
  666. struct snd_soc_dai *dai = rtd->codec_dais[i];
  667. struct snd_soc_dai_driver *drv = dai->driver;
  668. if (drv->ops->digital_mute && dai->playback_active)
  669. drv->ops->digital_mute(dai, 0);
  670. }
  671. }
  672. list_for_each_entry(rtd, &card->rtd_list, list) {
  673. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  674. if (rtd->dai_link->ignore_suspend)
  675. continue;
  676. if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
  677. cpu_dai->driver->resume(cpu_dai);
  678. }
  679. if (card->resume_post)
  680. card->resume_post(card);
  681. dev_dbg(card->dev, "ASoC: resume work completed\n");
  682. /* Recheck all endpoints too, their state is affected by suspend */
  683. dapm_mark_endpoints_dirty(card);
  684. snd_soc_dapm_sync(&card->dapm);
  685. /* userspace can access us now we are back as we were before */
  686. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  687. }
  688. /* powers up audio subsystem after a suspend */
  689. int snd_soc_resume(struct device *dev)
  690. {
  691. struct snd_soc_card *card = dev_get_drvdata(dev);
  692. bool bus_control = false;
  693. struct snd_soc_pcm_runtime *rtd;
  694. /* If the card is not initialized yet there is nothing to do */
  695. if (!card->instantiated)
  696. return 0;
  697. /* activate pins from sleep state */
  698. list_for_each_entry(rtd, &card->rtd_list, list) {
  699. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  700. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  701. int j;
  702. if (cpu_dai->active)
  703. pinctrl_pm_select_default_state(cpu_dai->dev);
  704. for (j = 0; j < rtd->num_codecs; j++) {
  705. struct snd_soc_dai *codec_dai = codec_dais[j];
  706. if (codec_dai->active)
  707. pinctrl_pm_select_default_state(codec_dai->dev);
  708. }
  709. }
  710. /*
  711. * DAIs that also act as the control bus master might have other drivers
  712. * hanging off them so need to resume immediately. Other drivers don't
  713. * have that problem and may take a substantial amount of time to resume
  714. * due to I/O costs and anti-pop so handle them out of line.
  715. */
  716. list_for_each_entry(rtd, &card->rtd_list, list) {
  717. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  718. bus_control |= cpu_dai->driver->bus_control;
  719. }
  720. if (bus_control) {
  721. dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
  722. soc_resume_deferred(&card->deferred_resume_work);
  723. } else {
  724. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  725. if (!schedule_work(&card->deferred_resume_work))
  726. dev_err(dev, "ASoC: resume work item may be lost\n");
  727. }
  728. return 0;
  729. }
  730. EXPORT_SYMBOL_GPL(snd_soc_resume);
  731. #else
  732. #define snd_soc_suspend NULL
  733. #define snd_soc_resume NULL
  734. #endif
  735. static const struct snd_soc_dai_ops null_dai_ops = {
  736. };
  737. static struct snd_soc_component *soc_find_component(
  738. const struct device_node *of_node, const char *name)
  739. {
  740. struct snd_soc_component *component;
  741. lockdep_assert_held(&client_mutex);
  742. list_for_each_entry(component, &component_list, list) {
  743. if (of_node) {
  744. if (component->dev->of_node == of_node)
  745. return component;
  746. } else if (strcmp(component->name, name) == 0) {
  747. return component;
  748. }
  749. }
  750. return NULL;
  751. }
  752. /**
  753. * snd_soc_find_dai - Find a registered DAI
  754. *
  755. * @dlc: name of the DAI and optional component info to match
  756. *
  757. * This function will search all regsitered components and their DAIs to
  758. * find the DAI of the same name. The component's of_node and name
  759. * should also match if being specified.
  760. *
  761. * Return: pointer of DAI, or NULL if not found.
  762. */
  763. struct snd_soc_dai *snd_soc_find_dai(
  764. const struct snd_soc_dai_link_component *dlc)
  765. {
  766. struct snd_soc_component *component;
  767. struct snd_soc_dai *dai;
  768. struct device_node *component_of_node;
  769. lockdep_assert_held(&client_mutex);
  770. /* Find CPU DAI from registered DAIs*/
  771. list_for_each_entry(component, &component_list, list) {
  772. component_of_node = component->dev->of_node;
  773. if (!component_of_node && component->dev->parent)
  774. component_of_node = component->dev->parent->of_node;
  775. if (dlc->of_node && component_of_node != dlc->of_node)
  776. continue;
  777. if (dlc->name && strcmp(component->name, dlc->name))
  778. continue;
  779. list_for_each_entry(dai, &component->dai_list, list) {
  780. if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
  781. continue;
  782. return dai;
  783. }
  784. }
  785. return NULL;
  786. }
  787. EXPORT_SYMBOL_GPL(snd_soc_find_dai);
  788. /**
  789. * snd_soc_find_dai_link - Find a DAI link
  790. *
  791. * @card: soc card
  792. * @id: DAI link ID to match
  793. * @name: DAI link name to match, optional
  794. * @stream name: DAI link stream name to match, optional
  795. *
  796. * This function will search all existing DAI links of the soc card to
  797. * find the link of the same ID. Since DAI links may not have their
  798. * unique ID, so name and stream name should also match if being
  799. * specified.
  800. *
  801. * Return: pointer of DAI link, or NULL if not found.
  802. */
  803. struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
  804. int id, const char *name,
  805. const char *stream_name)
  806. {
  807. struct snd_soc_dai_link *link, *_link;
  808. lockdep_assert_held(&client_mutex);
  809. list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
  810. if (link->id != id)
  811. continue;
  812. if (name && (!link->name || strcmp(name, link->name)))
  813. continue;
  814. if (stream_name && (!link->stream_name
  815. || strcmp(stream_name, link->stream_name)))
  816. continue;
  817. return link;
  818. }
  819. return NULL;
  820. }
  821. EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
  822. static bool soc_is_dai_link_bound(struct snd_soc_card *card,
  823. struct snd_soc_dai_link *dai_link)
  824. {
  825. struct snd_soc_pcm_runtime *rtd;
  826. list_for_each_entry(rtd, &card->rtd_list, list) {
  827. if (rtd->dai_link == dai_link)
  828. return true;
  829. }
  830. return false;
  831. }
  832. static int soc_bind_dai_link(struct snd_soc_card *card,
  833. struct snd_soc_dai_link *dai_link)
  834. {
  835. struct snd_soc_pcm_runtime *rtd;
  836. struct snd_soc_dai_link_component *codecs = dai_link->codecs;
  837. struct snd_soc_dai_link_component cpu_dai_component;
  838. struct snd_soc_dai **codec_dais;
  839. struct snd_soc_platform *platform;
  840. struct device_node *platform_of_node;
  841. const char *platform_name;
  842. int i;
  843. dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
  844. if (soc_is_dai_link_bound(card, dai_link)) {
  845. dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
  846. dai_link->name);
  847. return 0;
  848. }
  849. rtd = soc_new_pcm_runtime(card, dai_link);
  850. if (!rtd)
  851. return -ENOMEM;
  852. cpu_dai_component.name = dai_link->cpu_name;
  853. cpu_dai_component.of_node = dai_link->cpu_of_node;
  854. cpu_dai_component.dai_name = dai_link->cpu_dai_name;
  855. rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
  856. if (!rtd->cpu_dai) {
  857. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  858. dai_link->cpu_dai_name);
  859. goto _err_defer;
  860. }
  861. rtd->num_codecs = dai_link->num_codecs;
  862. /* Find CODEC from registered CODECs */
  863. codec_dais = rtd->codec_dais;
  864. for (i = 0; i < rtd->num_codecs; i++) {
  865. codec_dais[i] = snd_soc_find_dai(&codecs[i]);
  866. if (!codec_dais[i]) {
  867. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  868. codecs[i].dai_name);
  869. goto _err_defer;
  870. }
  871. }
  872. /* Single codec links expect codec and codec_dai in runtime data */
  873. rtd->codec_dai = codec_dais[0];
  874. rtd->codec = rtd->codec_dai->codec;
  875. /* if there's no platform we match on the empty platform */
  876. platform_name = dai_link->platform_name;
  877. if (!platform_name && !dai_link->platform_of_node)
  878. platform_name = "snd-soc-dummy";
  879. /* find one from the set of registered platforms */
  880. list_for_each_entry(platform, &platform_list, list) {
  881. platform_of_node = platform->dev->of_node;
  882. if (!platform_of_node && platform->dev->parent->of_node)
  883. platform_of_node = platform->dev->parent->of_node;
  884. if (dai_link->platform_of_node) {
  885. if (platform_of_node != dai_link->platform_of_node)
  886. continue;
  887. } else {
  888. if (strcmp(platform->component.name, platform_name))
  889. continue;
  890. }
  891. rtd->platform = platform;
  892. }
  893. if (!rtd->platform) {
  894. dev_err(card->dev, "ASoC: platform %s not registered\n",
  895. dai_link->platform_name);
  896. goto _err_defer;
  897. }
  898. soc_add_pcm_runtime(card, rtd);
  899. return 0;
  900. _err_defer:
  901. soc_free_pcm_runtime(rtd);
  902. return -EPROBE_DEFER;
  903. }
  904. static void soc_remove_component(struct snd_soc_component *component)
  905. {
  906. if (!component->card)
  907. return;
  908. list_del(&component->card_list);
  909. if (component->remove)
  910. component->remove(component);
  911. snd_soc_dapm_free(snd_soc_component_get_dapm(component));
  912. soc_cleanup_component_debugfs(component);
  913. component->card = NULL;
  914. module_put(component->dev->driver->owner);
  915. }
  916. static void soc_remove_dai(struct snd_soc_dai *dai, int order)
  917. {
  918. int err;
  919. if (dai && dai->probed &&
  920. dai->driver->remove_order == order) {
  921. if (dai->driver->remove) {
  922. err = dai->driver->remove(dai);
  923. if (err < 0)
  924. dev_err(dai->dev,
  925. "ASoC: failed to remove %s: %d\n",
  926. dai->name, err);
  927. }
  928. dai->probed = 0;
  929. }
  930. }
  931. static void soc_remove_link_dais(struct snd_soc_card *card,
  932. struct snd_soc_pcm_runtime *rtd, int order)
  933. {
  934. int i;
  935. /* unregister the rtd device */
  936. if (rtd->dev_registered) {
  937. device_unregister(rtd->dev);
  938. rtd->dev_registered = 0;
  939. }
  940. /* remove the CODEC DAI */
  941. for (i = 0; i < rtd->num_codecs; i++)
  942. soc_remove_dai(rtd->codec_dais[i], order);
  943. soc_remove_dai(rtd->cpu_dai, order);
  944. }
  945. static void soc_remove_link_components(struct snd_soc_card *card,
  946. struct snd_soc_pcm_runtime *rtd, int order)
  947. {
  948. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  949. struct snd_soc_platform *platform = rtd->platform;
  950. struct snd_soc_component *component;
  951. int i;
  952. /* remove the platform */
  953. if (platform && platform->component.driver->remove_order == order)
  954. soc_remove_component(&platform->component);
  955. /* remove the CODEC-side CODEC */
  956. for (i = 0; i < rtd->num_codecs; i++) {
  957. component = rtd->codec_dais[i]->component;
  958. if (component->driver->remove_order == order)
  959. soc_remove_component(component);
  960. }
  961. /* remove any CPU-side CODEC */
  962. if (cpu_dai) {
  963. if (cpu_dai->component->driver->remove_order == order)
  964. soc_remove_component(cpu_dai->component);
  965. }
  966. }
  967. static void soc_remove_dai_links(struct snd_soc_card *card)
  968. {
  969. int order;
  970. struct snd_soc_pcm_runtime *rtd;
  971. struct snd_soc_dai_link *link, *_link;
  972. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  973. order++) {
  974. list_for_each_entry(rtd, &card->rtd_list, list)
  975. soc_remove_link_dais(card, rtd, order);
  976. }
  977. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  978. order++) {
  979. list_for_each_entry(rtd, &card->rtd_list, list)
  980. soc_remove_link_components(card, rtd, order);
  981. }
  982. list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
  983. if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
  984. dev_warn(card->dev, "Topology forgot to remove link %s?\n",
  985. link->name);
  986. list_del(&link->list);
  987. card->num_dai_links--;
  988. }
  989. }
  990. static int snd_soc_init_multicodec(struct snd_soc_card *card,
  991. struct snd_soc_dai_link *dai_link)
  992. {
  993. /* Legacy codec/codec_dai link is a single entry in multicodec */
  994. if (dai_link->codec_name || dai_link->codec_of_node ||
  995. dai_link->codec_dai_name) {
  996. dai_link->num_codecs = 1;
  997. dai_link->codecs = devm_kzalloc(card->dev,
  998. sizeof(struct snd_soc_dai_link_component),
  999. GFP_KERNEL);
  1000. if (!dai_link->codecs)
  1001. return -ENOMEM;
  1002. dai_link->codecs[0].name = dai_link->codec_name;
  1003. dai_link->codecs[0].of_node = dai_link->codec_of_node;
  1004. dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
  1005. }
  1006. if (!dai_link->codecs) {
  1007. dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
  1008. return -EINVAL;
  1009. }
  1010. return 0;
  1011. }
  1012. static int soc_init_dai_link(struct snd_soc_card *card,
  1013. struct snd_soc_dai_link *link)
  1014. {
  1015. int i, ret;
  1016. ret = snd_soc_init_multicodec(card, link);
  1017. if (ret) {
  1018. dev_err(card->dev, "ASoC: failed to init multicodec\n");
  1019. return ret;
  1020. }
  1021. for (i = 0; i < link->num_codecs; i++) {
  1022. /*
  1023. * Codec must be specified by 1 of name or OF node,
  1024. * not both or neither.
  1025. */
  1026. if (!!link->codecs[i].name ==
  1027. !!link->codecs[i].of_node) {
  1028. dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
  1029. link->name);
  1030. return -EINVAL;
  1031. }
  1032. /* Codec DAI name must be specified */
  1033. if (!link->codecs[i].dai_name) {
  1034. dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
  1035. link->name);
  1036. return -EINVAL;
  1037. }
  1038. }
  1039. /*
  1040. * Platform may be specified by either name or OF node, but
  1041. * can be left unspecified, and a dummy platform will be used.
  1042. */
  1043. if (link->platform_name && link->platform_of_node) {
  1044. dev_err(card->dev,
  1045. "ASoC: Both platform name/of_node are set for %s\n",
  1046. link->name);
  1047. return -EINVAL;
  1048. }
  1049. /*
  1050. * CPU device may be specified by either name or OF node, but
  1051. * can be left unspecified, and will be matched based on DAI
  1052. * name alone..
  1053. */
  1054. if (link->cpu_name && link->cpu_of_node) {
  1055. dev_err(card->dev,
  1056. "ASoC: Neither/both cpu name/of_node are set for %s\n",
  1057. link->name);
  1058. return -EINVAL;
  1059. }
  1060. /*
  1061. * At least one of CPU DAI name or CPU device name/node must be
  1062. * specified
  1063. */
  1064. if (!link->cpu_dai_name &&
  1065. !(link->cpu_name || link->cpu_of_node)) {
  1066. dev_err(card->dev,
  1067. "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
  1068. link->name);
  1069. return -EINVAL;
  1070. }
  1071. return 0;
  1072. }
  1073. /**
  1074. * snd_soc_add_dai_link - Add a DAI link dynamically
  1075. * @card: The ASoC card to which the DAI link is added
  1076. * @dai_link: The new DAI link to add
  1077. *
  1078. * This function adds a DAI link to the ASoC card's link list.
  1079. *
  1080. * Note: Topology can use this API to add DAI links when probing the
  1081. * topology component. And machine drivers can still define static
  1082. * DAI links in dai_link array.
  1083. */
  1084. int snd_soc_add_dai_link(struct snd_soc_card *card,
  1085. struct snd_soc_dai_link *dai_link)
  1086. {
  1087. if (dai_link->dobj.type
  1088. && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
  1089. dev_err(card->dev, "Invalid dai link type %d\n",
  1090. dai_link->dobj.type);
  1091. return -EINVAL;
  1092. }
  1093. lockdep_assert_held(&client_mutex);
  1094. /* Notify the machine driver for extra initialization
  1095. * on the link created by topology.
  1096. */
  1097. if (dai_link->dobj.type && card->add_dai_link)
  1098. card->add_dai_link(card, dai_link);
  1099. list_add_tail(&dai_link->list, &card->dai_link_list);
  1100. card->num_dai_links++;
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
  1104. /**
  1105. * snd_soc_remove_dai_link - Remove a DAI link from the list
  1106. * @card: The ASoC card that owns the link
  1107. * @dai_link: The DAI link to remove
  1108. *
  1109. * This function removes a DAI link from the ASoC card's link list.
  1110. *
  1111. * For DAI links previously added by topology, topology should
  1112. * remove them by using the dobj embedded in the link.
  1113. */
  1114. void snd_soc_remove_dai_link(struct snd_soc_card *card,
  1115. struct snd_soc_dai_link *dai_link)
  1116. {
  1117. struct snd_soc_dai_link *link, *_link;
  1118. if (dai_link->dobj.type
  1119. && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
  1120. dev_err(card->dev, "Invalid dai link type %d\n",
  1121. dai_link->dobj.type);
  1122. return;
  1123. }
  1124. lockdep_assert_held(&client_mutex);
  1125. /* Notify the machine driver for extra destruction
  1126. * on the link created by topology.
  1127. */
  1128. if (dai_link->dobj.type && card->remove_dai_link)
  1129. card->remove_dai_link(card, dai_link);
  1130. list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
  1131. if (link == dai_link) {
  1132. list_del(&link->list);
  1133. card->num_dai_links--;
  1134. return;
  1135. }
  1136. }
  1137. }
  1138. EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
  1139. static void soc_set_name_prefix(struct snd_soc_card *card,
  1140. struct snd_soc_component *component)
  1141. {
  1142. int i;
  1143. if (card->codec_conf == NULL)
  1144. return;
  1145. for (i = 0; i < card->num_configs; i++) {
  1146. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  1147. if (map->of_node && component->dev->of_node != map->of_node)
  1148. continue;
  1149. if (map->dev_name && strcmp(component->name, map->dev_name))
  1150. continue;
  1151. component->name_prefix = map->name_prefix;
  1152. break;
  1153. }
  1154. }
  1155. static int soc_probe_component(struct snd_soc_card *card,
  1156. struct snd_soc_component *component)
  1157. {
  1158. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  1159. struct snd_soc_dai *dai;
  1160. int ret;
  1161. if (!strcmp(component->name, "snd-soc-dummy"))
  1162. return 0;
  1163. if (component->card) {
  1164. if (component->card != card) {
  1165. dev_err(component->dev,
  1166. "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
  1167. card->name, component->card->name);
  1168. return -ENODEV;
  1169. }
  1170. return 0;
  1171. }
  1172. if (!try_module_get(component->dev->driver->owner))
  1173. return -ENODEV;
  1174. component->card = card;
  1175. dapm->card = card;
  1176. soc_set_name_prefix(card, component);
  1177. soc_init_component_debugfs(component);
  1178. if (component->dapm_widgets) {
  1179. ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
  1180. component->num_dapm_widgets);
  1181. if (ret != 0) {
  1182. dev_err(component->dev,
  1183. "Failed to create new controls %d\n", ret);
  1184. goto err_probe;
  1185. }
  1186. }
  1187. list_for_each_entry(dai, &component->dai_list, list) {
  1188. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  1189. if (ret != 0) {
  1190. dev_err(component->dev,
  1191. "Failed to create DAI widgets %d\n", ret);
  1192. goto err_probe;
  1193. }
  1194. }
  1195. if (component->probe) {
  1196. ret = component->probe(component);
  1197. if (ret < 0) {
  1198. dev_err(component->dev,
  1199. "ASoC: failed to probe component %d\n", ret);
  1200. goto err_probe;
  1201. }
  1202. WARN(dapm->idle_bias_off &&
  1203. dapm->bias_level != SND_SOC_BIAS_OFF,
  1204. "codec %s can not start from non-off bias with idle_bias_off==1\n",
  1205. component->name);
  1206. }
  1207. /* machine specific init */
  1208. if (component->init) {
  1209. ret = component->init(component);
  1210. if (ret < 0) {
  1211. dev_err(component->dev,
  1212. "Failed to do machine specific init %d\n", ret);
  1213. goto err_probe;
  1214. }
  1215. }
  1216. if (component->controls)
  1217. snd_soc_add_component_controls(component, component->controls,
  1218. component->num_controls);
  1219. if (component->dapm_routes)
  1220. snd_soc_dapm_add_routes(dapm, component->dapm_routes,
  1221. component->num_dapm_routes);
  1222. list_add(&dapm->list, &card->dapm_list);
  1223. list_add(&component->card_list, &card->component_dev_list);
  1224. return 0;
  1225. err_probe:
  1226. soc_cleanup_component_debugfs(component);
  1227. component->card = NULL;
  1228. module_put(component->dev->driver->owner);
  1229. return ret;
  1230. }
  1231. static void rtd_release(struct device *dev)
  1232. {
  1233. kfree(dev);
  1234. }
  1235. static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
  1236. const char *name)
  1237. {
  1238. int ret = 0;
  1239. /* register the rtd device */
  1240. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1241. if (!rtd->dev)
  1242. return -ENOMEM;
  1243. device_initialize(rtd->dev);
  1244. rtd->dev->parent = rtd->card->dev;
  1245. rtd->dev->release = rtd_release;
  1246. rtd->dev->groups = soc_dev_attr_groups;
  1247. dev_set_name(rtd->dev, "%s", name);
  1248. dev_set_drvdata(rtd->dev, rtd);
  1249. mutex_init(&rtd->pcm_mutex);
  1250. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1251. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1252. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1253. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1254. ret = device_add(rtd->dev);
  1255. if (ret < 0) {
  1256. /* calling put_device() here to free the rtd->dev */
  1257. put_device(rtd->dev);
  1258. dev_err(rtd->card->dev,
  1259. "ASoC: failed to register runtime device: %d\n", ret);
  1260. return ret;
  1261. }
  1262. rtd->dev_registered = 1;
  1263. return 0;
  1264. }
  1265. static int soc_probe_link_components(struct snd_soc_card *card,
  1266. struct snd_soc_pcm_runtime *rtd,
  1267. int order)
  1268. {
  1269. struct snd_soc_platform *platform = rtd->platform;
  1270. struct snd_soc_component *component;
  1271. int i, ret;
  1272. /* probe the CPU-side component, if it is a CODEC */
  1273. component = rtd->cpu_dai->component;
  1274. if (component->driver->probe_order == order) {
  1275. ret = soc_probe_component(card, component);
  1276. if (ret < 0)
  1277. return ret;
  1278. }
  1279. /* probe the CODEC-side components */
  1280. for (i = 0; i < rtd->num_codecs; i++) {
  1281. component = rtd->codec_dais[i]->component;
  1282. if (component->driver->probe_order == order) {
  1283. ret = soc_probe_component(card, component);
  1284. if (ret < 0)
  1285. return ret;
  1286. }
  1287. }
  1288. /* probe the platform */
  1289. if (platform->component.driver->probe_order == order) {
  1290. ret = soc_probe_component(card, &platform->component);
  1291. if (ret < 0)
  1292. return ret;
  1293. }
  1294. return 0;
  1295. }
  1296. static int soc_probe_dai(struct snd_soc_dai *dai, int order)
  1297. {
  1298. int ret;
  1299. if (!dai->probed && dai->driver->probe_order == order) {
  1300. if (dai->driver->probe) {
  1301. ret = dai->driver->probe(dai);
  1302. if (ret < 0) {
  1303. dev_err(dai->dev,
  1304. "ASoC: failed to probe DAI %s: %d\n",
  1305. dai->name, ret);
  1306. return ret;
  1307. }
  1308. }
  1309. dai->probed = 1;
  1310. }
  1311. return 0;
  1312. }
  1313. static int soc_link_dai_widgets(struct snd_soc_card *card,
  1314. struct snd_soc_dai_link *dai_link,
  1315. struct snd_soc_pcm_runtime *rtd)
  1316. {
  1317. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1318. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1319. struct snd_soc_dapm_widget *sink, *source;
  1320. int ret;
  1321. if (rtd->num_codecs > 1)
  1322. dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
  1323. /* link the DAI widgets */
  1324. sink = codec_dai->playback_widget;
  1325. source = cpu_dai->capture_widget;
  1326. if (sink && source) {
  1327. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1328. dai_link->num_params,
  1329. source, sink);
  1330. if (ret != 0) {
  1331. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1332. sink->name, source->name, ret);
  1333. return ret;
  1334. }
  1335. }
  1336. sink = cpu_dai->playback_widget;
  1337. source = codec_dai->capture_widget;
  1338. if (sink && source) {
  1339. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1340. dai_link->num_params,
  1341. source, sink);
  1342. if (ret != 0) {
  1343. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1344. sink->name, source->name, ret);
  1345. return ret;
  1346. }
  1347. }
  1348. return 0;
  1349. }
  1350. static int soc_probe_link_dais(struct snd_soc_card *card,
  1351. struct snd_soc_pcm_runtime *rtd, int order)
  1352. {
  1353. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  1354. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1355. int i, ret;
  1356. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1357. card->name, rtd->num, order);
  1358. /* set default power off timeout */
  1359. rtd->pmdown_time = pmdown_time;
  1360. ret = soc_probe_dai(cpu_dai, order);
  1361. if (ret)
  1362. return ret;
  1363. /* probe the CODEC DAI */
  1364. for (i = 0; i < rtd->num_codecs; i++) {
  1365. ret = soc_probe_dai(rtd->codec_dais[i], order);
  1366. if (ret)
  1367. return ret;
  1368. }
  1369. /* complete DAI probe during last probe */
  1370. if (order != SND_SOC_COMP_ORDER_LAST)
  1371. return 0;
  1372. /* do machine specific initialization */
  1373. if (dai_link->init) {
  1374. ret = dai_link->init(rtd);
  1375. if (ret < 0) {
  1376. dev_err(card->dev, "ASoC: failed to init %s: %d\n",
  1377. dai_link->name, ret);
  1378. return ret;
  1379. }
  1380. }
  1381. if (dai_link->dai_fmt)
  1382. snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
  1383. ret = soc_post_component_init(rtd, dai_link->name);
  1384. if (ret)
  1385. return ret;
  1386. #ifdef CONFIG_DEBUG_FS
  1387. /* add DPCM sysfs entries */
  1388. if (dai_link->dynamic)
  1389. soc_dpcm_debugfs_add(rtd);
  1390. #endif
  1391. if (cpu_dai->driver->compress_new) {
  1392. /*create compress_device"*/
  1393. ret = cpu_dai->driver->compress_new(rtd, rtd->num);
  1394. if (ret < 0) {
  1395. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1396. dai_link->stream_name);
  1397. return ret;
  1398. }
  1399. } else {
  1400. if (!dai_link->params) {
  1401. /* create the pcm */
  1402. ret = soc_new_pcm(rtd, rtd->num);
  1403. if (ret < 0) {
  1404. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1405. dai_link->stream_name, ret);
  1406. return ret;
  1407. }
  1408. } else {
  1409. INIT_DELAYED_WORK(&rtd->delayed_work,
  1410. codec2codec_close_delayed_work);
  1411. /* link the DAI widgets */
  1412. ret = soc_link_dai_widgets(card, dai_link, rtd);
  1413. if (ret)
  1414. return ret;
  1415. }
  1416. }
  1417. return 0;
  1418. }
  1419. static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
  1420. {
  1421. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1422. struct snd_soc_component *component;
  1423. const char *name;
  1424. struct device_node *codec_of_node;
  1425. if (aux_dev->codec_of_node || aux_dev->codec_name) {
  1426. /* codecs, usually analog devices */
  1427. name = aux_dev->codec_name;
  1428. codec_of_node = aux_dev->codec_of_node;
  1429. component = soc_find_component(codec_of_node, name);
  1430. if (!component) {
  1431. if (codec_of_node)
  1432. name = of_node_full_name(codec_of_node);
  1433. goto err_defer;
  1434. }
  1435. } else if (aux_dev->name) {
  1436. /* generic components */
  1437. name = aux_dev->name;
  1438. component = soc_find_component(NULL, name);
  1439. if (!component)
  1440. goto err_defer;
  1441. } else {
  1442. dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
  1443. return -EINVAL;
  1444. }
  1445. component->init = aux_dev->init;
  1446. component->auxiliary = 1;
  1447. list_add(&component->card_aux_list, &card->aux_comp_list);
  1448. return 0;
  1449. err_defer:
  1450. dev_err(card->dev, "ASoC: %s not registered\n", name);
  1451. return -EPROBE_DEFER;
  1452. }
  1453. static int soc_probe_aux_devices(struct snd_soc_card *card)
  1454. {
  1455. struct snd_soc_component *comp, *tmp;
  1456. int order;
  1457. int ret;
  1458. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1459. order++) {
  1460. list_for_each_entry_safe(comp, tmp, &card->aux_comp_list,
  1461. card_aux_list) {
  1462. if (comp->driver->probe_order == order) {
  1463. ret = soc_probe_component(card, comp);
  1464. if (ret < 0) {
  1465. dev_err(card->dev,
  1466. "ASoC: failed to probe aux component %s %d\n",
  1467. comp->name, ret);
  1468. return ret;
  1469. }
  1470. list_del(&comp->card_aux_list);
  1471. }
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. static void soc_remove_aux_devices(struct snd_soc_card *card)
  1477. {
  1478. struct snd_soc_component *comp, *_comp;
  1479. int order;
  1480. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1481. order++) {
  1482. list_for_each_entry_safe(comp, _comp,
  1483. &card->component_dev_list, card_list) {
  1484. if (!comp->auxiliary)
  1485. continue;
  1486. if (comp->driver->remove_order == order) {
  1487. soc_remove_component(comp);
  1488. comp->auxiliary = 0;
  1489. }
  1490. }
  1491. }
  1492. }
  1493. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
  1494. {
  1495. int ret;
  1496. if (codec->cache_init)
  1497. return 0;
  1498. ret = snd_soc_cache_init(codec);
  1499. if (ret < 0) {
  1500. dev_err(codec->dev,
  1501. "ASoC: Failed to set cache compression type: %d\n",
  1502. ret);
  1503. return ret;
  1504. }
  1505. codec->cache_init = 1;
  1506. return 0;
  1507. }
  1508. /**
  1509. * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
  1510. * @rtd: The runtime for which the DAI link format should be changed
  1511. * @dai_fmt: The new DAI link format
  1512. *
  1513. * This function updates the DAI link format for all DAIs connected to the DAI
  1514. * link for the specified runtime.
  1515. *
  1516. * Note: For setups with a static format set the dai_fmt field in the
  1517. * corresponding snd_dai_link struct instead of using this function.
  1518. *
  1519. * Returns 0 on success, otherwise a negative error code.
  1520. */
  1521. int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
  1522. unsigned int dai_fmt)
  1523. {
  1524. struct snd_soc_dai **codec_dais = rtd->codec_dais;
  1525. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1526. unsigned int i;
  1527. int ret;
  1528. for (i = 0; i < rtd->num_codecs; i++) {
  1529. struct snd_soc_dai *codec_dai = codec_dais[i];
  1530. ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
  1531. if (ret != 0 && ret != -ENOTSUPP) {
  1532. dev_warn(codec_dai->dev,
  1533. "ASoC: Failed to set DAI format: %d\n", ret);
  1534. return ret;
  1535. }
  1536. }
  1537. /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
  1538. if (cpu_dai->codec) {
  1539. unsigned int inv_dai_fmt;
  1540. inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
  1541. switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1542. case SND_SOC_DAIFMT_CBM_CFM:
  1543. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1544. break;
  1545. case SND_SOC_DAIFMT_CBM_CFS:
  1546. inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1547. break;
  1548. case SND_SOC_DAIFMT_CBS_CFM:
  1549. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1550. break;
  1551. case SND_SOC_DAIFMT_CBS_CFS:
  1552. inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1553. break;
  1554. }
  1555. dai_fmt = inv_dai_fmt;
  1556. }
  1557. ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
  1558. if (ret != 0 && ret != -ENOTSUPP) {
  1559. dev_warn(cpu_dai->dev,
  1560. "ASoC: Failed to set DAI format: %d\n", ret);
  1561. return ret;
  1562. }
  1563. return 0;
  1564. }
  1565. EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
  1566. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1567. {
  1568. struct snd_soc_codec *codec;
  1569. struct snd_soc_pcm_runtime *rtd;
  1570. struct snd_soc_dai_link *dai_link;
  1571. int ret, i, order;
  1572. mutex_lock(&client_mutex);
  1573. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1574. /* bind DAIs */
  1575. for (i = 0; i < card->num_links; i++) {
  1576. ret = soc_bind_dai_link(card, &card->dai_link[i]);
  1577. if (ret != 0)
  1578. goto base_error;
  1579. }
  1580. /* bind aux_devs too */
  1581. for (i = 0; i < card->num_aux_devs; i++) {
  1582. ret = soc_bind_aux_dev(card, i);
  1583. if (ret != 0)
  1584. goto base_error;
  1585. }
  1586. /* add predefined DAI links to the list */
  1587. for (i = 0; i < card->num_links; i++)
  1588. snd_soc_add_dai_link(card, card->dai_link+i);
  1589. /* initialize the register cache for each available codec */
  1590. list_for_each_entry(codec, &codec_list, list) {
  1591. if (codec->cache_init)
  1592. continue;
  1593. ret = snd_soc_init_codec_cache(codec);
  1594. if (ret < 0)
  1595. goto base_error;
  1596. }
  1597. /* card bind complete so register a sound card */
  1598. ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1599. card->owner, 0, &card->snd_card);
  1600. if (ret < 0) {
  1601. dev_err(card->dev,
  1602. "ASoC: can't create sound card for card %s: %d\n",
  1603. card->name, ret);
  1604. goto base_error;
  1605. }
  1606. soc_init_card_debugfs(card);
  1607. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1608. card->dapm.dev = card->dev;
  1609. card->dapm.card = card;
  1610. list_add(&card->dapm.list, &card->dapm_list);
  1611. #ifdef CONFIG_DEBUG_FS
  1612. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1613. #endif
  1614. #ifdef CONFIG_PM_SLEEP
  1615. /* deferred resume work */
  1616. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1617. #endif
  1618. if (card->dapm_widgets)
  1619. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1620. card->num_dapm_widgets);
  1621. if (card->of_dapm_widgets)
  1622. snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
  1623. card->num_of_dapm_widgets);
  1624. /* initialise the sound card only once */
  1625. if (card->probe) {
  1626. ret = card->probe(card);
  1627. if (ret < 0)
  1628. goto card_probe_error;
  1629. }
  1630. /* probe all components used by DAI links on this card */
  1631. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1632. order++) {
  1633. list_for_each_entry(rtd, &card->rtd_list, list) {
  1634. ret = soc_probe_link_components(card, rtd, order);
  1635. if (ret < 0) {
  1636. dev_err(card->dev,
  1637. "ASoC: failed to instantiate card %d\n",
  1638. ret);
  1639. goto probe_dai_err;
  1640. }
  1641. }
  1642. }
  1643. /* probe auxiliary components */
  1644. ret = soc_probe_aux_devices(card);
  1645. if (ret < 0)
  1646. goto probe_dai_err;
  1647. /* Find new DAI links added during probing components and bind them.
  1648. * Components with topology may bring new DAIs and DAI links.
  1649. */
  1650. list_for_each_entry(dai_link, &card->dai_link_list, list) {
  1651. if (soc_is_dai_link_bound(card, dai_link))
  1652. continue;
  1653. ret = soc_init_dai_link(card, dai_link);
  1654. if (ret)
  1655. goto probe_dai_err;
  1656. ret = soc_bind_dai_link(card, dai_link);
  1657. if (ret)
  1658. goto probe_dai_err;
  1659. }
  1660. /* probe all DAI links on this card */
  1661. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1662. order++) {
  1663. list_for_each_entry(rtd, &card->rtd_list, list) {
  1664. ret = soc_probe_link_dais(card, rtd, order);
  1665. if (ret < 0) {
  1666. dev_err(card->dev,
  1667. "ASoC: failed to instantiate card %d\n",
  1668. ret);
  1669. goto probe_dai_err;
  1670. }
  1671. }
  1672. }
  1673. snd_soc_dapm_link_dai_widgets(card);
  1674. snd_soc_dapm_connect_dai_link_widgets(card);
  1675. if (card->controls)
  1676. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1677. if (card->dapm_routes)
  1678. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1679. card->num_dapm_routes);
  1680. if (card->of_dapm_routes)
  1681. snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
  1682. card->num_of_dapm_routes);
  1683. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1684. "%s", card->name);
  1685. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1686. "%s", card->long_name ? card->long_name : card->name);
  1687. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1688. "%s", card->driver_name ? card->driver_name : card->name);
  1689. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1690. switch (card->snd_card->driver[i]) {
  1691. case '_':
  1692. case '-':
  1693. case '\0':
  1694. break;
  1695. default:
  1696. if (!isalnum(card->snd_card->driver[i]))
  1697. card->snd_card->driver[i] = '_';
  1698. break;
  1699. }
  1700. }
  1701. if (card->late_probe) {
  1702. ret = card->late_probe(card);
  1703. if (ret < 0) {
  1704. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1705. card->name, ret);
  1706. goto probe_aux_dev_err;
  1707. }
  1708. }
  1709. snd_soc_dapm_new_widgets(card);
  1710. ret = snd_card_register(card->snd_card);
  1711. if (ret < 0) {
  1712. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1713. ret);
  1714. goto probe_aux_dev_err;
  1715. }
  1716. card->instantiated = 1;
  1717. snd_soc_dapm_sync(&card->dapm);
  1718. mutex_unlock(&card->mutex);
  1719. mutex_unlock(&client_mutex);
  1720. return 0;
  1721. probe_aux_dev_err:
  1722. soc_remove_aux_devices(card);
  1723. probe_dai_err:
  1724. soc_remove_dai_links(card);
  1725. card_probe_error:
  1726. if (card->remove)
  1727. card->remove(card);
  1728. snd_soc_dapm_free(&card->dapm);
  1729. soc_cleanup_card_debugfs(card);
  1730. snd_card_free(card->snd_card);
  1731. base_error:
  1732. soc_remove_pcm_runtimes(card);
  1733. mutex_unlock(&card->mutex);
  1734. mutex_unlock(&client_mutex);
  1735. return ret;
  1736. }
  1737. /* probes a new socdev */
  1738. static int soc_probe(struct platform_device *pdev)
  1739. {
  1740. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1741. /*
  1742. * no card, so machine driver should be registering card
  1743. * we should not be here in that case so ret error
  1744. */
  1745. if (!card)
  1746. return -EINVAL;
  1747. dev_warn(&pdev->dev,
  1748. "ASoC: machine %s should use snd_soc_register_card()\n",
  1749. card->name);
  1750. /* Bodge while we unpick instantiation */
  1751. card->dev = &pdev->dev;
  1752. return snd_soc_register_card(card);
  1753. }
  1754. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1755. {
  1756. struct snd_soc_pcm_runtime *rtd;
  1757. /* make sure any delayed work runs */
  1758. list_for_each_entry(rtd, &card->rtd_list, list)
  1759. flush_delayed_work(&rtd->delayed_work);
  1760. /* remove and free each DAI */
  1761. soc_remove_dai_links(card);
  1762. soc_remove_pcm_runtimes(card);
  1763. /* remove auxiliary devices */
  1764. soc_remove_aux_devices(card);
  1765. snd_soc_dapm_free(&card->dapm);
  1766. soc_cleanup_card_debugfs(card);
  1767. /* remove the card */
  1768. if (card->remove)
  1769. card->remove(card);
  1770. snd_card_free(card->snd_card);
  1771. return 0;
  1772. }
  1773. /* removes a socdev */
  1774. static int soc_remove(struct platform_device *pdev)
  1775. {
  1776. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1777. snd_soc_unregister_card(card);
  1778. return 0;
  1779. }
  1780. int snd_soc_poweroff(struct device *dev)
  1781. {
  1782. struct snd_soc_card *card = dev_get_drvdata(dev);
  1783. struct snd_soc_pcm_runtime *rtd;
  1784. if (!card->instantiated)
  1785. return 0;
  1786. /* Flush out pmdown_time work - we actually do want to run it
  1787. * now, we're shutting down so no imminent restart. */
  1788. list_for_each_entry(rtd, &card->rtd_list, list)
  1789. flush_delayed_work(&rtd->delayed_work);
  1790. snd_soc_dapm_shutdown(card);
  1791. /* deactivate pins to sleep state */
  1792. list_for_each_entry(rtd, &card->rtd_list, list) {
  1793. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1794. int i;
  1795. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  1796. for (i = 0; i < rtd->num_codecs; i++) {
  1797. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  1798. pinctrl_pm_select_sleep_state(codec_dai->dev);
  1799. }
  1800. }
  1801. return 0;
  1802. }
  1803. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1804. const struct dev_pm_ops snd_soc_pm_ops = {
  1805. .suspend = snd_soc_suspend,
  1806. .resume = snd_soc_resume,
  1807. .freeze = snd_soc_suspend,
  1808. .thaw = snd_soc_resume,
  1809. .poweroff = snd_soc_poweroff,
  1810. .restore = snd_soc_resume,
  1811. };
  1812. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1813. /* ASoC platform driver */
  1814. static struct platform_driver soc_driver = {
  1815. .driver = {
  1816. .name = "soc-audio",
  1817. .pm = &snd_soc_pm_ops,
  1818. },
  1819. .probe = soc_probe,
  1820. .remove = soc_remove,
  1821. };
  1822. /**
  1823. * snd_soc_cnew - create new control
  1824. * @_template: control template
  1825. * @data: control private data
  1826. * @long_name: control long name
  1827. * @prefix: control name prefix
  1828. *
  1829. * Create a new mixer control from a template control.
  1830. *
  1831. * Returns 0 for success, else error.
  1832. */
  1833. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1834. void *data, const char *long_name,
  1835. const char *prefix)
  1836. {
  1837. struct snd_kcontrol_new template;
  1838. struct snd_kcontrol *kcontrol;
  1839. char *name = NULL;
  1840. memcpy(&template, _template, sizeof(template));
  1841. template.index = 0;
  1842. if (!long_name)
  1843. long_name = template.name;
  1844. if (prefix) {
  1845. name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
  1846. if (!name)
  1847. return NULL;
  1848. template.name = name;
  1849. } else {
  1850. template.name = long_name;
  1851. }
  1852. kcontrol = snd_ctl_new1(&template, data);
  1853. kfree(name);
  1854. return kcontrol;
  1855. }
  1856. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1857. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1858. const struct snd_kcontrol_new *controls, int num_controls,
  1859. const char *prefix, void *data)
  1860. {
  1861. int err, i;
  1862. for (i = 0; i < num_controls; i++) {
  1863. const struct snd_kcontrol_new *control = &controls[i];
  1864. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1865. control->name, prefix));
  1866. if (err < 0) {
  1867. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1868. control->name, err);
  1869. return err;
  1870. }
  1871. }
  1872. return 0;
  1873. }
  1874. struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
  1875. const char *name)
  1876. {
  1877. struct snd_card *card = soc_card->snd_card;
  1878. struct snd_kcontrol *kctl;
  1879. if (unlikely(!name))
  1880. return NULL;
  1881. list_for_each_entry(kctl, &card->controls, list)
  1882. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
  1883. return kctl;
  1884. return NULL;
  1885. }
  1886. EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
  1887. /**
  1888. * snd_soc_add_component_controls - Add an array of controls to a component.
  1889. *
  1890. * @component: Component to add controls to
  1891. * @controls: Array of controls to add
  1892. * @num_controls: Number of elements in the array
  1893. *
  1894. * Return: 0 for success, else error.
  1895. */
  1896. int snd_soc_add_component_controls(struct snd_soc_component *component,
  1897. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1898. {
  1899. struct snd_card *card = component->card->snd_card;
  1900. return snd_soc_add_controls(card, component->dev, controls,
  1901. num_controls, component->name_prefix, component);
  1902. }
  1903. EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
  1904. /**
  1905. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1906. * Convenience function to add a list of controls. Many codecs were
  1907. * duplicating this code.
  1908. *
  1909. * @codec: codec to add controls to
  1910. * @controls: array of controls to add
  1911. * @num_controls: number of elements in the array
  1912. *
  1913. * Return 0 for success, else error.
  1914. */
  1915. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1916. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1917. {
  1918. return snd_soc_add_component_controls(&codec->component, controls,
  1919. num_controls);
  1920. }
  1921. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1922. /**
  1923. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1924. * Convenience function to add a list of controls.
  1925. *
  1926. * @platform: platform to add controls to
  1927. * @controls: array of controls to add
  1928. * @num_controls: number of elements in the array
  1929. *
  1930. * Return 0 for success, else error.
  1931. */
  1932. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1933. const struct snd_kcontrol_new *controls, unsigned int num_controls)
  1934. {
  1935. return snd_soc_add_component_controls(&platform->component, controls,
  1936. num_controls);
  1937. }
  1938. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1939. /**
  1940. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1941. * Convenience function to add a list of controls.
  1942. *
  1943. * @soc_card: SoC card to add controls to
  1944. * @controls: array of controls to add
  1945. * @num_controls: number of elements in the array
  1946. *
  1947. * Return 0 for success, else error.
  1948. */
  1949. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1950. const struct snd_kcontrol_new *controls, int num_controls)
  1951. {
  1952. struct snd_card *card = soc_card->snd_card;
  1953. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1954. NULL, soc_card);
  1955. }
  1956. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1957. /**
  1958. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1959. * Convienience function to add a list of controls.
  1960. *
  1961. * @dai: DAI to add controls to
  1962. * @controls: array of controls to add
  1963. * @num_controls: number of elements in the array
  1964. *
  1965. * Return 0 for success, else error.
  1966. */
  1967. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1968. const struct snd_kcontrol_new *controls, int num_controls)
  1969. {
  1970. struct snd_card *card = dai->component->card->snd_card;
  1971. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  1972. NULL, dai);
  1973. }
  1974. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  1975. /**
  1976. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1977. * @dai: DAI
  1978. * @clk_id: DAI specific clock ID
  1979. * @freq: new clock frequency in Hz
  1980. * @dir: new clock direction - input/output.
  1981. *
  1982. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1983. */
  1984. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1985. unsigned int freq, int dir)
  1986. {
  1987. if (dai->driver && dai->driver->ops->set_sysclk)
  1988. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  1989. else if (dai->codec && dai->codec->driver->set_sysclk)
  1990. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  1991. freq, dir);
  1992. else
  1993. return -ENOTSUPP;
  1994. }
  1995. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1996. /**
  1997. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  1998. * @codec: CODEC
  1999. * @clk_id: DAI specific clock ID
  2000. * @source: Source for the clock
  2001. * @freq: new clock frequency in Hz
  2002. * @dir: new clock direction - input/output.
  2003. *
  2004. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2005. */
  2006. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2007. int source, unsigned int freq, int dir)
  2008. {
  2009. if (codec->driver->set_sysclk)
  2010. return codec->driver->set_sysclk(codec, clk_id, source,
  2011. freq, dir);
  2012. else
  2013. return -ENOTSUPP;
  2014. }
  2015. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2016. /**
  2017. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2018. * @dai: DAI
  2019. * @div_id: DAI specific clock divider ID
  2020. * @div: new clock divisor.
  2021. *
  2022. * Configures the clock dividers. This is used to derive the best DAI bit and
  2023. * frame clocks from the system or master clock. It's best to set the DAI bit
  2024. * and frame clocks as low as possible to save system power.
  2025. */
  2026. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2027. int div_id, int div)
  2028. {
  2029. if (dai->driver && dai->driver->ops->set_clkdiv)
  2030. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2031. else
  2032. return -EINVAL;
  2033. }
  2034. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2035. /**
  2036. * snd_soc_dai_set_pll - configure DAI PLL.
  2037. * @dai: DAI
  2038. * @pll_id: DAI specific PLL ID
  2039. * @source: DAI specific source for the PLL
  2040. * @freq_in: PLL input clock frequency in Hz
  2041. * @freq_out: requested PLL output clock frequency in Hz
  2042. *
  2043. * Configures and enables PLL to generate output clock based on input clock.
  2044. */
  2045. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2046. unsigned int freq_in, unsigned int freq_out)
  2047. {
  2048. if (dai->driver && dai->driver->ops->set_pll)
  2049. return dai->driver->ops->set_pll(dai, pll_id, source,
  2050. freq_in, freq_out);
  2051. else if (dai->codec && dai->codec->driver->set_pll)
  2052. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2053. freq_in, freq_out);
  2054. else
  2055. return -EINVAL;
  2056. }
  2057. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2058. /*
  2059. * snd_soc_codec_set_pll - configure codec PLL.
  2060. * @codec: CODEC
  2061. * @pll_id: DAI specific PLL ID
  2062. * @source: DAI specific source for the PLL
  2063. * @freq_in: PLL input clock frequency in Hz
  2064. * @freq_out: requested PLL output clock frequency in Hz
  2065. *
  2066. * Configures and enables PLL to generate output clock based on input clock.
  2067. */
  2068. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2069. unsigned int freq_in, unsigned int freq_out)
  2070. {
  2071. if (codec->driver->set_pll)
  2072. return codec->driver->set_pll(codec, pll_id, source,
  2073. freq_in, freq_out);
  2074. else
  2075. return -EINVAL;
  2076. }
  2077. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2078. /**
  2079. * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  2080. * @dai: DAI
  2081. * @ratio: Ratio of BCLK to Sample rate.
  2082. *
  2083. * Configures the DAI for a preset BCLK to sample rate ratio.
  2084. */
  2085. int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  2086. {
  2087. if (dai->driver && dai->driver->ops->set_bclk_ratio)
  2088. return dai->driver->ops->set_bclk_ratio(dai, ratio);
  2089. else
  2090. return -EINVAL;
  2091. }
  2092. EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  2093. /**
  2094. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2095. * @dai: DAI
  2096. * @fmt: SND_SOC_DAIFMT_ format value.
  2097. *
  2098. * Configures the DAI hardware format and clocking.
  2099. */
  2100. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2101. {
  2102. if (dai->driver == NULL)
  2103. return -EINVAL;
  2104. if (dai->driver->ops->set_fmt == NULL)
  2105. return -ENOTSUPP;
  2106. return dai->driver->ops->set_fmt(dai, fmt);
  2107. }
  2108. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2109. /**
  2110. * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
  2111. * @slots: Number of slots in use.
  2112. * @tx_mask: bitmask representing active TX slots.
  2113. * @rx_mask: bitmask representing active RX slots.
  2114. *
  2115. * Generates the TDM tx and rx slot default masks for DAI.
  2116. */
  2117. static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
  2118. unsigned int *tx_mask,
  2119. unsigned int *rx_mask)
  2120. {
  2121. if (*tx_mask || *rx_mask)
  2122. return 0;
  2123. if (!slots)
  2124. return -EINVAL;
  2125. *tx_mask = (1 << slots) - 1;
  2126. *rx_mask = (1 << slots) - 1;
  2127. return 0;
  2128. }
  2129. /**
  2130. * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
  2131. * @dai: The DAI to configure
  2132. * @tx_mask: bitmask representing active TX slots.
  2133. * @rx_mask: bitmask representing active RX slots.
  2134. * @slots: Number of slots in use.
  2135. * @slot_width: Width in bits for each slot.
  2136. *
  2137. * This function configures the specified DAI for TDM operation. @slot contains
  2138. * the total number of slots of the TDM stream and @slot_with the width of each
  2139. * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
  2140. * active slots of the TDM stream for the specified DAI, i.e. which slots the
  2141. * DAI should write to or read from. If a bit is set the corresponding slot is
  2142. * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
  2143. * the first slot, bit 1 to the second slot and so on. The first active slot
  2144. * maps to the first channel of the DAI, the second active slot to the second
  2145. * channel and so on.
  2146. *
  2147. * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
  2148. * @rx_mask and @slot_width will be ignored.
  2149. *
  2150. * Returns 0 on success, a negative error code otherwise.
  2151. */
  2152. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2153. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2154. {
  2155. if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
  2156. dai->driver->ops->xlate_tdm_slot_mask(slots,
  2157. &tx_mask, &rx_mask);
  2158. else
  2159. snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
  2160. dai->tx_mask = tx_mask;
  2161. dai->rx_mask = rx_mask;
  2162. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2163. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2164. slots, slot_width);
  2165. else
  2166. return -ENOTSUPP;
  2167. }
  2168. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  2169. /**
  2170. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  2171. * @dai: DAI
  2172. * @tx_num: how many TX channels
  2173. * @tx_slot: pointer to an array which imply the TX slot number channel
  2174. * 0~num-1 uses
  2175. * @rx_num: how many RX channels
  2176. * @rx_slot: pointer to an array which imply the RX slot number channel
  2177. * 0~num-1 uses
  2178. *
  2179. * configure the relationship between channel number and TDM slot number.
  2180. */
  2181. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  2182. unsigned int tx_num, unsigned int *tx_slot,
  2183. unsigned int rx_num, unsigned int *rx_slot)
  2184. {
  2185. if (dai->driver && dai->driver->ops->set_channel_map)
  2186. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  2187. rx_num, rx_slot);
  2188. else
  2189. return -EINVAL;
  2190. }
  2191. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  2192. /**
  2193. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  2194. * @dai: DAI
  2195. * @tristate: tristate enable
  2196. *
  2197. * Tristates the DAI so that others can use it.
  2198. */
  2199. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  2200. {
  2201. if (dai->driver && dai->driver->ops->set_tristate)
  2202. return dai->driver->ops->set_tristate(dai, tristate);
  2203. else
  2204. return -EINVAL;
  2205. }
  2206. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  2207. /**
  2208. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  2209. * @dai: DAI
  2210. * @mute: mute enable
  2211. * @direction: stream to mute
  2212. *
  2213. * Mutes the DAI DAC.
  2214. */
  2215. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  2216. int direction)
  2217. {
  2218. if (!dai->driver)
  2219. return -ENOTSUPP;
  2220. if (dai->driver->ops->mute_stream)
  2221. return dai->driver->ops->mute_stream(dai, mute, direction);
  2222. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  2223. dai->driver->ops->digital_mute)
  2224. return dai->driver->ops->digital_mute(dai, mute);
  2225. else
  2226. return -ENOTSUPP;
  2227. }
  2228. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  2229. /**
  2230. * snd_soc_register_card - Register a card with the ASoC core
  2231. *
  2232. * @card: Card to register
  2233. *
  2234. */
  2235. int snd_soc_register_card(struct snd_soc_card *card)
  2236. {
  2237. int i, ret;
  2238. struct snd_soc_pcm_runtime *rtd;
  2239. if (!card->name || !card->dev)
  2240. return -EINVAL;
  2241. for (i = 0; i < card->num_links; i++) {
  2242. struct snd_soc_dai_link *link = &card->dai_link[i];
  2243. ret = soc_init_dai_link(card, link);
  2244. if (ret) {
  2245. dev_err(card->dev, "ASoC: failed to init link %s\n",
  2246. link->name);
  2247. return ret;
  2248. }
  2249. }
  2250. dev_set_drvdata(card->dev, card);
  2251. snd_soc_initialize_card_lists(card);
  2252. INIT_LIST_HEAD(&card->dai_link_list);
  2253. card->num_dai_links = 0;
  2254. INIT_LIST_HEAD(&card->rtd_list);
  2255. card->num_rtd = 0;
  2256. INIT_LIST_HEAD(&card->dapm_dirty);
  2257. INIT_LIST_HEAD(&card->dobj_list);
  2258. card->instantiated = 0;
  2259. mutex_init(&card->mutex);
  2260. mutex_init(&card->dapm_mutex);
  2261. ret = snd_soc_instantiate_card(card);
  2262. if (ret != 0)
  2263. return ret;
  2264. /* deactivate pins to sleep state */
  2265. list_for_each_entry(rtd, &card->rtd_list, list) {
  2266. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2267. int j;
  2268. for (j = 0; j < rtd->num_codecs; j++) {
  2269. struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
  2270. if (!codec_dai->active)
  2271. pinctrl_pm_select_sleep_state(codec_dai->dev);
  2272. }
  2273. if (!cpu_dai->active)
  2274. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  2275. }
  2276. return ret;
  2277. }
  2278. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  2279. /**
  2280. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2281. *
  2282. * @card: Card to unregister
  2283. *
  2284. */
  2285. int snd_soc_unregister_card(struct snd_soc_card *card)
  2286. {
  2287. if (card->instantiated) {
  2288. card->instantiated = false;
  2289. snd_soc_dapm_shutdown(card);
  2290. soc_cleanup_card_resources(card);
  2291. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  2292. }
  2293. return 0;
  2294. }
  2295. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  2296. /*
  2297. * Simplify DAI link configuration by removing ".-1" from device names
  2298. * and sanitizing names.
  2299. */
  2300. static char *fmt_single_name(struct device *dev, int *id)
  2301. {
  2302. char *found, name[NAME_SIZE];
  2303. int id1, id2;
  2304. if (dev_name(dev) == NULL)
  2305. return NULL;
  2306. strlcpy(name, dev_name(dev), NAME_SIZE);
  2307. /* are we a "%s.%d" name (platform and SPI components) */
  2308. found = strstr(name, dev->driver->name);
  2309. if (found) {
  2310. /* get ID */
  2311. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  2312. /* discard ID from name if ID == -1 */
  2313. if (*id == -1)
  2314. found[strlen(dev->driver->name)] = '\0';
  2315. }
  2316. } else {
  2317. /* I2C component devices are named "bus-addr" */
  2318. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  2319. char tmp[NAME_SIZE];
  2320. /* create unique ID number from I2C addr and bus */
  2321. *id = ((id1 & 0xffff) << 16) + id2;
  2322. /* sanitize component name for DAI link creation */
  2323. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  2324. strlcpy(name, tmp, NAME_SIZE);
  2325. } else
  2326. *id = 0;
  2327. }
  2328. return kstrdup(name, GFP_KERNEL);
  2329. }
  2330. /*
  2331. * Simplify DAI link naming for single devices with multiple DAIs by removing
  2332. * any ".-1" and using the DAI name (instead of device name).
  2333. */
  2334. static inline char *fmt_multiple_name(struct device *dev,
  2335. struct snd_soc_dai_driver *dai_drv)
  2336. {
  2337. if (dai_drv->name == NULL) {
  2338. dev_err(dev,
  2339. "ASoC: error - multiple DAI %s registered with no name\n",
  2340. dev_name(dev));
  2341. return NULL;
  2342. }
  2343. return kstrdup(dai_drv->name, GFP_KERNEL);
  2344. }
  2345. /**
  2346. * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
  2347. *
  2348. * @component: The component for which the DAIs should be unregistered
  2349. */
  2350. static void snd_soc_unregister_dais(struct snd_soc_component *component)
  2351. {
  2352. struct snd_soc_dai *dai, *_dai;
  2353. list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
  2354. dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
  2355. dai->name);
  2356. list_del(&dai->list);
  2357. kfree(dai->name);
  2358. kfree(dai);
  2359. }
  2360. }
  2361. /* Create a DAI and add it to the component's DAI list */
  2362. static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
  2363. struct snd_soc_dai_driver *dai_drv,
  2364. bool legacy_dai_naming)
  2365. {
  2366. struct device *dev = component->dev;
  2367. struct snd_soc_dai *dai;
  2368. dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
  2369. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  2370. if (dai == NULL)
  2371. return NULL;
  2372. /*
  2373. * Back in the old days when we still had component-less DAIs,
  2374. * instead of having a static name, component-less DAIs would
  2375. * inherit the name of the parent device so it is possible to
  2376. * register multiple instances of the DAI. We still need to keep
  2377. * the same naming style even though those DAIs are not
  2378. * component-less anymore.
  2379. */
  2380. if (legacy_dai_naming &&
  2381. (dai_drv->id == 0 || dai_drv->name == NULL)) {
  2382. dai->name = fmt_single_name(dev, &dai->id);
  2383. } else {
  2384. dai->name = fmt_multiple_name(dev, dai_drv);
  2385. if (dai_drv->id)
  2386. dai->id = dai_drv->id;
  2387. else
  2388. dai->id = component->num_dai;
  2389. }
  2390. if (dai->name == NULL) {
  2391. kfree(dai);
  2392. return NULL;
  2393. }
  2394. dai->component = component;
  2395. dai->dev = dev;
  2396. dai->driver = dai_drv;
  2397. if (!dai->driver->ops)
  2398. dai->driver->ops = &null_dai_ops;
  2399. list_add(&dai->list, &component->dai_list);
  2400. component->num_dai++;
  2401. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  2402. return dai;
  2403. }
  2404. /**
  2405. * snd_soc_register_dais - Register a DAI with the ASoC core
  2406. *
  2407. * @component: The component the DAIs are registered for
  2408. * @dai_drv: DAI driver to use for the DAIs
  2409. * @count: Number of DAIs
  2410. * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
  2411. * parent's name.
  2412. */
  2413. static int snd_soc_register_dais(struct snd_soc_component *component,
  2414. struct snd_soc_dai_driver *dai_drv, size_t count,
  2415. bool legacy_dai_naming)
  2416. {
  2417. struct device *dev = component->dev;
  2418. struct snd_soc_dai *dai;
  2419. unsigned int i;
  2420. int ret;
  2421. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  2422. component->dai_drv = dai_drv;
  2423. for (i = 0; i < count; i++) {
  2424. dai = soc_add_dai(component, dai_drv + i,
  2425. count == 1 && legacy_dai_naming);
  2426. if (dai == NULL) {
  2427. ret = -ENOMEM;
  2428. goto err;
  2429. }
  2430. }
  2431. return 0;
  2432. err:
  2433. snd_soc_unregister_dais(component);
  2434. return ret;
  2435. }
  2436. /**
  2437. * snd_soc_register_dai - Register a DAI dynamically & create its widgets
  2438. *
  2439. * @component: The component the DAIs are registered for
  2440. * @dai_drv: DAI driver to use for the DAI
  2441. *
  2442. * Topology can use this API to register DAIs when probing a component.
  2443. * These DAIs's widgets will be freed in the card cleanup and the DAIs
  2444. * will be freed in the component cleanup.
  2445. */
  2446. int snd_soc_register_dai(struct snd_soc_component *component,
  2447. struct snd_soc_dai_driver *dai_drv)
  2448. {
  2449. struct snd_soc_dapm_context *dapm =
  2450. snd_soc_component_get_dapm(component);
  2451. struct snd_soc_dai *dai;
  2452. int ret;
  2453. if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
  2454. dev_err(component->dev, "Invalid dai type %d\n",
  2455. dai_drv->dobj.type);
  2456. return -EINVAL;
  2457. }
  2458. lockdep_assert_held(&client_mutex);
  2459. dai = soc_add_dai(component, dai_drv, false);
  2460. if (!dai)
  2461. return -ENOMEM;
  2462. /* Create the DAI widgets here. After adding DAIs, topology may
  2463. * also add routes that need these widgets as source or sink.
  2464. */
  2465. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  2466. if (ret != 0) {
  2467. dev_err(component->dev,
  2468. "Failed to create DAI widgets %d\n", ret);
  2469. }
  2470. return ret;
  2471. }
  2472. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2473. static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
  2474. enum snd_soc_dapm_type type, int subseq)
  2475. {
  2476. struct snd_soc_component *component = dapm->component;
  2477. component->driver->seq_notifier(component, type, subseq);
  2478. }
  2479. static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
  2480. int event)
  2481. {
  2482. struct snd_soc_component *component = dapm->component;
  2483. return component->driver->stream_event(component, event);
  2484. }
  2485. static int snd_soc_component_initialize(struct snd_soc_component *component,
  2486. const struct snd_soc_component_driver *driver, struct device *dev)
  2487. {
  2488. struct snd_soc_dapm_context *dapm;
  2489. component->name = fmt_single_name(dev, &component->id);
  2490. if (!component->name) {
  2491. dev_err(dev, "ASoC: Failed to allocate name\n");
  2492. return -ENOMEM;
  2493. }
  2494. component->dev = dev;
  2495. component->driver = driver;
  2496. component->probe = component->driver->probe;
  2497. component->remove = component->driver->remove;
  2498. component->suspend = component->driver->suspend;
  2499. component->resume = component->driver->resume;
  2500. dapm = &component->dapm;
  2501. dapm->dev = dev;
  2502. dapm->component = component;
  2503. dapm->bias_level = SND_SOC_BIAS_OFF;
  2504. dapm->idle_bias_off = true;
  2505. if (driver->seq_notifier)
  2506. dapm->seq_notifier = snd_soc_component_seq_notifier;
  2507. if (driver->stream_event)
  2508. dapm->stream_event = snd_soc_component_stream_event;
  2509. component->controls = driver->controls;
  2510. component->num_controls = driver->num_controls;
  2511. component->dapm_widgets = driver->dapm_widgets;
  2512. component->num_dapm_widgets = driver->num_dapm_widgets;
  2513. component->dapm_routes = driver->dapm_routes;
  2514. component->num_dapm_routes = driver->num_dapm_routes;
  2515. INIT_LIST_HEAD(&component->dai_list);
  2516. mutex_init(&component->io_mutex);
  2517. return 0;
  2518. }
  2519. static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
  2520. {
  2521. int val_bytes = regmap_get_val_bytes(component->regmap);
  2522. /* Errors are legitimate for non-integer byte multiples */
  2523. if (val_bytes > 0)
  2524. component->val_bytes = val_bytes;
  2525. }
  2526. #ifdef CONFIG_REGMAP
  2527. /**
  2528. * snd_soc_component_init_regmap() - Initialize regmap instance for the component
  2529. * @component: The component for which to initialize the regmap instance
  2530. * @regmap: The regmap instance that should be used by the component
  2531. *
  2532. * This function allows deferred assignment of the regmap instance that is
  2533. * associated with the component. Only use this if the regmap instance is not
  2534. * yet ready when the component is registered. The function must also be called
  2535. * before the first IO attempt of the component.
  2536. */
  2537. void snd_soc_component_init_regmap(struct snd_soc_component *component,
  2538. struct regmap *regmap)
  2539. {
  2540. component->regmap = regmap;
  2541. snd_soc_component_setup_regmap(component);
  2542. }
  2543. EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
  2544. /**
  2545. * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
  2546. * @component: The component for which to de-initialize the regmap instance
  2547. *
  2548. * Calls regmap_exit() on the regmap instance associated to the component and
  2549. * removes the regmap instance from the component.
  2550. *
  2551. * This function should only be used if snd_soc_component_init_regmap() was used
  2552. * to initialize the regmap instance.
  2553. */
  2554. void snd_soc_component_exit_regmap(struct snd_soc_component *component)
  2555. {
  2556. regmap_exit(component->regmap);
  2557. component->regmap = NULL;
  2558. }
  2559. EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
  2560. #endif
  2561. static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
  2562. {
  2563. if (!component->write && !component->read) {
  2564. if (!component->regmap)
  2565. component->regmap = dev_get_regmap(component->dev, NULL);
  2566. if (component->regmap)
  2567. snd_soc_component_setup_regmap(component);
  2568. }
  2569. list_add(&component->list, &component_list);
  2570. INIT_LIST_HEAD(&component->dobj_list);
  2571. }
  2572. static void snd_soc_component_add(struct snd_soc_component *component)
  2573. {
  2574. mutex_lock(&client_mutex);
  2575. snd_soc_component_add_unlocked(component);
  2576. mutex_unlock(&client_mutex);
  2577. }
  2578. static void snd_soc_component_cleanup(struct snd_soc_component *component)
  2579. {
  2580. snd_soc_unregister_dais(component);
  2581. kfree(component->name);
  2582. }
  2583. static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
  2584. {
  2585. list_del(&component->list);
  2586. }
  2587. int snd_soc_register_component(struct device *dev,
  2588. const struct snd_soc_component_driver *cmpnt_drv,
  2589. struct snd_soc_dai_driver *dai_drv,
  2590. int num_dai)
  2591. {
  2592. struct snd_soc_component *cmpnt;
  2593. int ret;
  2594. cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
  2595. if (!cmpnt) {
  2596. dev_err(dev, "ASoC: Failed to allocate memory\n");
  2597. return -ENOMEM;
  2598. }
  2599. ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
  2600. if (ret)
  2601. goto err_free;
  2602. cmpnt->ignore_pmdown_time = true;
  2603. cmpnt->registered_as_component = true;
  2604. ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
  2605. if (ret < 0) {
  2606. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2607. goto err_cleanup;
  2608. }
  2609. snd_soc_component_add(cmpnt);
  2610. return 0;
  2611. err_cleanup:
  2612. snd_soc_component_cleanup(cmpnt);
  2613. err_free:
  2614. kfree(cmpnt);
  2615. return ret;
  2616. }
  2617. EXPORT_SYMBOL_GPL(snd_soc_register_component);
  2618. /**
  2619. * snd_soc_unregister_component - Unregister a component from the ASoC core
  2620. *
  2621. * @dev: The device to unregister
  2622. */
  2623. void snd_soc_unregister_component(struct device *dev)
  2624. {
  2625. struct snd_soc_component *cmpnt;
  2626. mutex_lock(&client_mutex);
  2627. list_for_each_entry(cmpnt, &component_list, list) {
  2628. if (dev == cmpnt->dev && cmpnt->registered_as_component)
  2629. goto found;
  2630. }
  2631. mutex_unlock(&client_mutex);
  2632. return;
  2633. found:
  2634. snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
  2635. snd_soc_component_del_unlocked(cmpnt);
  2636. mutex_unlock(&client_mutex);
  2637. snd_soc_component_cleanup(cmpnt);
  2638. kfree(cmpnt);
  2639. }
  2640. EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
  2641. static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
  2642. {
  2643. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2644. return platform->driver->probe(platform);
  2645. }
  2646. static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
  2647. {
  2648. struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
  2649. platform->driver->remove(platform);
  2650. }
  2651. /**
  2652. * snd_soc_add_platform - Add a platform to the ASoC core
  2653. * @dev: The parent device for the platform
  2654. * @platform: The platform to add
  2655. * @platform_drv: The driver for the platform
  2656. */
  2657. int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
  2658. const struct snd_soc_platform_driver *platform_drv)
  2659. {
  2660. int ret;
  2661. ret = snd_soc_component_initialize(&platform->component,
  2662. &platform_drv->component_driver, dev);
  2663. if (ret)
  2664. return ret;
  2665. platform->dev = dev;
  2666. platform->driver = platform_drv;
  2667. if (platform_drv->probe)
  2668. platform->component.probe = snd_soc_platform_drv_probe;
  2669. if (platform_drv->remove)
  2670. platform->component.remove = snd_soc_platform_drv_remove;
  2671. #ifdef CONFIG_DEBUG_FS
  2672. platform->component.debugfs_prefix = "platform";
  2673. #endif
  2674. mutex_lock(&client_mutex);
  2675. snd_soc_component_add_unlocked(&platform->component);
  2676. list_add(&platform->list, &platform_list);
  2677. mutex_unlock(&client_mutex);
  2678. dev_dbg(dev, "ASoC: Registered platform '%s'\n",
  2679. platform->component.name);
  2680. return 0;
  2681. }
  2682. EXPORT_SYMBOL_GPL(snd_soc_add_platform);
  2683. /**
  2684. * snd_soc_register_platform - Register a platform with the ASoC core
  2685. *
  2686. * @dev: The device for the platform
  2687. * @platform_drv: The driver for the platform
  2688. */
  2689. int snd_soc_register_platform(struct device *dev,
  2690. const struct snd_soc_platform_driver *platform_drv)
  2691. {
  2692. struct snd_soc_platform *platform;
  2693. int ret;
  2694. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  2695. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  2696. if (platform == NULL)
  2697. return -ENOMEM;
  2698. ret = snd_soc_add_platform(dev, platform, platform_drv);
  2699. if (ret)
  2700. kfree(platform);
  2701. return ret;
  2702. }
  2703. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2704. /**
  2705. * snd_soc_remove_platform - Remove a platform from the ASoC core
  2706. * @platform: the platform to remove
  2707. */
  2708. void snd_soc_remove_platform(struct snd_soc_platform *platform)
  2709. {
  2710. mutex_lock(&client_mutex);
  2711. list_del(&platform->list);
  2712. snd_soc_component_del_unlocked(&platform->component);
  2713. mutex_unlock(&client_mutex);
  2714. dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
  2715. platform->component.name);
  2716. snd_soc_component_cleanup(&platform->component);
  2717. }
  2718. EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
  2719. struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
  2720. {
  2721. struct snd_soc_platform *platform;
  2722. mutex_lock(&client_mutex);
  2723. list_for_each_entry(platform, &platform_list, list) {
  2724. if (dev == platform->dev) {
  2725. mutex_unlock(&client_mutex);
  2726. return platform;
  2727. }
  2728. }
  2729. mutex_unlock(&client_mutex);
  2730. return NULL;
  2731. }
  2732. EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
  2733. /**
  2734. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2735. *
  2736. * @dev: platform to unregister
  2737. */
  2738. void snd_soc_unregister_platform(struct device *dev)
  2739. {
  2740. struct snd_soc_platform *platform;
  2741. platform = snd_soc_lookup_platform(dev);
  2742. if (!platform)
  2743. return;
  2744. snd_soc_remove_platform(platform);
  2745. kfree(platform);
  2746. }
  2747. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2748. static u64 codec_format_map[] = {
  2749. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2750. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2751. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2752. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2753. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2754. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2755. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2756. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2757. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2758. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2759. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2760. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2761. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2762. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2763. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2764. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2765. };
  2766. /* Fix up the DAI formats for endianness: codecs don't actually see
  2767. * the endianness of the data but we're using the CPU format
  2768. * definitions which do need to include endianness so we ensure that
  2769. * codec DAIs always have both big and little endian variants set.
  2770. */
  2771. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2772. {
  2773. int i;
  2774. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2775. if (stream->formats & codec_format_map[i])
  2776. stream->formats |= codec_format_map[i];
  2777. }
  2778. static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
  2779. {
  2780. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2781. return codec->driver->probe(codec);
  2782. }
  2783. static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
  2784. {
  2785. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2786. codec->driver->remove(codec);
  2787. }
  2788. static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
  2789. {
  2790. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2791. return codec->driver->suspend(codec);
  2792. }
  2793. static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
  2794. {
  2795. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2796. return codec->driver->resume(codec);
  2797. }
  2798. static int snd_soc_codec_drv_write(struct snd_soc_component *component,
  2799. unsigned int reg, unsigned int val)
  2800. {
  2801. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2802. return codec->driver->write(codec, reg, val);
  2803. }
  2804. static int snd_soc_codec_drv_read(struct snd_soc_component *component,
  2805. unsigned int reg, unsigned int *val)
  2806. {
  2807. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  2808. *val = codec->driver->read(codec, reg);
  2809. return 0;
  2810. }
  2811. static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
  2812. enum snd_soc_bias_level level)
  2813. {
  2814. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
  2815. return codec->driver->set_bias_level(codec, level);
  2816. }
  2817. /**
  2818. * snd_soc_register_codec - Register a codec with the ASoC core
  2819. *
  2820. * @dev: The parent device for this codec
  2821. * @codec_drv: Codec driver
  2822. * @dai_drv: The associated DAI driver
  2823. * @num_dai: Number of DAIs
  2824. */
  2825. int snd_soc_register_codec(struct device *dev,
  2826. const struct snd_soc_codec_driver *codec_drv,
  2827. struct snd_soc_dai_driver *dai_drv,
  2828. int num_dai)
  2829. {
  2830. struct snd_soc_dapm_context *dapm;
  2831. struct snd_soc_codec *codec;
  2832. struct snd_soc_dai *dai;
  2833. int ret, i;
  2834. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  2835. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  2836. if (codec == NULL)
  2837. return -ENOMEM;
  2838. codec->component.codec = codec;
  2839. ret = snd_soc_component_initialize(&codec->component,
  2840. &codec_drv->component_driver, dev);
  2841. if (ret)
  2842. goto err_free;
  2843. if (codec_drv->probe)
  2844. codec->component.probe = snd_soc_codec_drv_probe;
  2845. if (codec_drv->remove)
  2846. codec->component.remove = snd_soc_codec_drv_remove;
  2847. if (codec_drv->suspend)
  2848. codec->component.suspend = snd_soc_codec_drv_suspend;
  2849. if (codec_drv->resume)
  2850. codec->component.resume = snd_soc_codec_drv_resume;
  2851. if (codec_drv->write)
  2852. codec->component.write = snd_soc_codec_drv_write;
  2853. if (codec_drv->read)
  2854. codec->component.read = snd_soc_codec_drv_read;
  2855. codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  2856. dapm = snd_soc_codec_get_dapm(codec);
  2857. dapm->idle_bias_off = codec_drv->idle_bias_off;
  2858. dapm->suspend_bias_off = codec_drv->suspend_bias_off;
  2859. if (codec_drv->seq_notifier)
  2860. dapm->seq_notifier = codec_drv->seq_notifier;
  2861. if (codec_drv->set_bias_level)
  2862. dapm->set_bias_level = snd_soc_codec_set_bias_level;
  2863. codec->dev = dev;
  2864. codec->driver = codec_drv;
  2865. codec->component.val_bytes = codec_drv->reg_word_size;
  2866. #ifdef CONFIG_DEBUG_FS
  2867. codec->component.init_debugfs = soc_init_codec_debugfs;
  2868. codec->component.debugfs_prefix = "codec";
  2869. #endif
  2870. if (codec_drv->get_regmap)
  2871. codec->component.regmap = codec_drv->get_regmap(dev);
  2872. for (i = 0; i < num_dai; i++) {
  2873. fixup_codec_formats(&dai_drv[i].playback);
  2874. fixup_codec_formats(&dai_drv[i].capture);
  2875. }
  2876. ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
  2877. if (ret < 0) {
  2878. dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
  2879. goto err_cleanup;
  2880. }
  2881. list_for_each_entry(dai, &codec->component.dai_list, list)
  2882. dai->codec = codec;
  2883. mutex_lock(&client_mutex);
  2884. snd_soc_component_add_unlocked(&codec->component);
  2885. list_add(&codec->list, &codec_list);
  2886. mutex_unlock(&client_mutex);
  2887. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
  2888. codec->component.name);
  2889. return 0;
  2890. err_cleanup:
  2891. snd_soc_component_cleanup(&codec->component);
  2892. err_free:
  2893. kfree(codec);
  2894. return ret;
  2895. }
  2896. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2897. /**
  2898. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2899. *
  2900. * @dev: codec to unregister
  2901. */
  2902. void snd_soc_unregister_codec(struct device *dev)
  2903. {
  2904. struct snd_soc_codec *codec;
  2905. mutex_lock(&client_mutex);
  2906. list_for_each_entry(codec, &codec_list, list) {
  2907. if (dev == codec->dev)
  2908. goto found;
  2909. }
  2910. mutex_unlock(&client_mutex);
  2911. return;
  2912. found:
  2913. list_del(&codec->list);
  2914. snd_soc_component_del_unlocked(&codec->component);
  2915. mutex_unlock(&client_mutex);
  2916. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
  2917. codec->component.name);
  2918. snd_soc_component_cleanup(&codec->component);
  2919. snd_soc_cache_exit(codec);
  2920. kfree(codec);
  2921. }
  2922. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2923. /* Retrieve a card's name from device tree */
  2924. int snd_soc_of_parse_card_name_from_node(struct snd_soc_card *card,
  2925. struct device_node *np,
  2926. const char *propname)
  2927. {
  2928. int ret;
  2929. if (!card->dev) {
  2930. pr_err("card->dev is not set before calling %s\n", __func__);
  2931. return -EINVAL;
  2932. }
  2933. if (!np)
  2934. np = card->dev->of_node;
  2935. ret = of_property_read_string_index(np, propname, 0, &card->name);
  2936. /*
  2937. * EINVAL means the property does not exist. This is fine providing
  2938. * card->name was previously set, which is checked later in
  2939. * snd_soc_register_card.
  2940. */
  2941. if (ret < 0 && ret != -EINVAL) {
  2942. dev_err(card->dev,
  2943. "ASoC: Property '%s' could not be read: %d\n",
  2944. propname, ret);
  2945. return ret;
  2946. }
  2947. return 0;
  2948. }
  2949. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name_from_node);
  2950. static const struct snd_soc_dapm_widget simple_widgets[] = {
  2951. SND_SOC_DAPM_MIC("Microphone", NULL),
  2952. SND_SOC_DAPM_LINE("Line", NULL),
  2953. SND_SOC_DAPM_HP("Headphone", NULL),
  2954. SND_SOC_DAPM_SPK("Speaker", NULL),
  2955. };
  2956. int snd_soc_of_parse_audio_simple_widgets_from_node(struct snd_soc_card *card,
  2957. struct device_node *np,
  2958. const char *propname)
  2959. {
  2960. struct snd_soc_dapm_widget *widgets;
  2961. const char *template, *wname;
  2962. int i, j, num_widgets, ret;
  2963. if (!np)
  2964. np = card->dev->of_node;
  2965. num_widgets = of_property_count_strings(np, propname);
  2966. if (num_widgets < 0) {
  2967. dev_err(card->dev,
  2968. "ASoC: Property '%s' does not exist\n", propname);
  2969. return -EINVAL;
  2970. }
  2971. if (num_widgets & 1) {
  2972. dev_err(card->dev,
  2973. "ASoC: Property '%s' length is not even\n", propname);
  2974. return -EINVAL;
  2975. }
  2976. num_widgets /= 2;
  2977. if (!num_widgets) {
  2978. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  2979. propname);
  2980. return -EINVAL;
  2981. }
  2982. widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
  2983. GFP_KERNEL);
  2984. if (!widgets) {
  2985. dev_err(card->dev,
  2986. "ASoC: Could not allocate memory for widgets\n");
  2987. return -ENOMEM;
  2988. }
  2989. for (i = 0; i < num_widgets; i++) {
  2990. ret = of_property_read_string_index(np, propname,
  2991. 2 * i, &template);
  2992. if (ret) {
  2993. dev_err(card->dev,
  2994. "ASoC: Property '%s' index %d read error:%d\n",
  2995. propname, 2 * i, ret);
  2996. return -EINVAL;
  2997. }
  2998. for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
  2999. if (!strncmp(template, simple_widgets[j].name,
  3000. strlen(simple_widgets[j].name))) {
  3001. widgets[i] = simple_widgets[j];
  3002. break;
  3003. }
  3004. }
  3005. if (j >= ARRAY_SIZE(simple_widgets)) {
  3006. dev_err(card->dev,
  3007. "ASoC: DAPM widget '%s' is not supported\n",
  3008. template);
  3009. return -EINVAL;
  3010. }
  3011. ret = of_property_read_string_index(np, propname,
  3012. (2 * i) + 1,
  3013. &wname);
  3014. if (ret) {
  3015. dev_err(card->dev,
  3016. "ASoC: Property '%s' index %d read error:%d\n",
  3017. propname, (2 * i) + 1, ret);
  3018. return -EINVAL;
  3019. }
  3020. widgets[i].name = wname;
  3021. }
  3022. card->of_dapm_widgets = widgets;
  3023. card->num_of_dapm_widgets = num_widgets;
  3024. return 0;
  3025. }
  3026. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets_from_node);
  3027. static int snd_soc_of_get_slot_mask(struct device_node *np,
  3028. const char *prop_name,
  3029. unsigned int *mask)
  3030. {
  3031. u32 val;
  3032. const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
  3033. int i;
  3034. if (!of_slot_mask)
  3035. return 0;
  3036. val /= sizeof(u32);
  3037. for (i = 0; i < val; i++)
  3038. if (be32_to_cpup(&of_slot_mask[i]))
  3039. *mask |= (1 << i);
  3040. return val;
  3041. }
  3042. int snd_soc_of_parse_tdm_slot(struct device_node *np,
  3043. unsigned int *tx_mask,
  3044. unsigned int *rx_mask,
  3045. unsigned int *slots,
  3046. unsigned int *slot_width)
  3047. {
  3048. u32 val;
  3049. int ret;
  3050. if (tx_mask)
  3051. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
  3052. if (rx_mask)
  3053. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
  3054. if (of_property_read_bool(np, "dai-tdm-slot-num")) {
  3055. ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
  3056. if (ret)
  3057. return ret;
  3058. if (slots)
  3059. *slots = val;
  3060. }
  3061. if (of_property_read_bool(np, "dai-tdm-slot-width")) {
  3062. ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
  3063. if (ret)
  3064. return ret;
  3065. if (slot_width)
  3066. *slot_width = val;
  3067. }
  3068. return 0;
  3069. }
  3070. EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
  3071. void snd_soc_of_parse_audio_prefix_from_node(struct snd_soc_card *card,
  3072. struct device_node *np,
  3073. struct snd_soc_codec_conf *codec_conf,
  3074. struct device_node *of_node,
  3075. const char *propname)
  3076. {
  3077. const char *str;
  3078. int ret;
  3079. if (!np)
  3080. np = card->dev->of_node;
  3081. ret = of_property_read_string(np, propname, &str);
  3082. if (ret < 0) {
  3083. /* no prefix is not error */
  3084. return;
  3085. }
  3086. codec_conf->of_node = of_node;
  3087. codec_conf->name_prefix = str;
  3088. }
  3089. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix_from_node);
  3090. int snd_soc_of_parse_audio_routing_from_node(struct snd_soc_card *card,
  3091. struct device_node *np,
  3092. const char *propname)
  3093. {
  3094. int num_routes;
  3095. struct snd_soc_dapm_route *routes;
  3096. int i, ret;
  3097. if (!np)
  3098. np = card->dev->of_node;
  3099. num_routes = of_property_count_strings(np, propname);
  3100. if (num_routes < 0 || num_routes & 1) {
  3101. dev_err(card->dev,
  3102. "ASoC: Property '%s' does not exist or its length is not even\n",
  3103. propname);
  3104. return -EINVAL;
  3105. }
  3106. num_routes /= 2;
  3107. if (!num_routes) {
  3108. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3109. propname);
  3110. return -EINVAL;
  3111. }
  3112. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3113. GFP_KERNEL);
  3114. if (!routes) {
  3115. dev_err(card->dev,
  3116. "ASoC: Could not allocate DAPM route table\n");
  3117. return -EINVAL;
  3118. }
  3119. for (i = 0; i < num_routes; i++) {
  3120. ret = of_property_read_string_index(np, propname,
  3121. 2 * i, &routes[i].sink);
  3122. if (ret) {
  3123. dev_err(card->dev,
  3124. "ASoC: Property '%s' index %d could not be read: %d\n",
  3125. propname, 2 * i, ret);
  3126. return -EINVAL;
  3127. }
  3128. ret = of_property_read_string_index(np, propname,
  3129. (2 * i) + 1, &routes[i].source);
  3130. if (ret) {
  3131. dev_err(card->dev,
  3132. "ASoC: Property '%s' index %d could not be read: %d\n",
  3133. propname, (2 * i) + 1, ret);
  3134. return -EINVAL;
  3135. }
  3136. }
  3137. card->num_of_dapm_routes = num_routes;
  3138. card->of_dapm_routes = routes;
  3139. return 0;
  3140. }
  3141. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing_from_node);
  3142. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3143. const char *prefix,
  3144. struct device_node **bitclkmaster,
  3145. struct device_node **framemaster)
  3146. {
  3147. int ret, i;
  3148. char prop[128];
  3149. unsigned int format = 0;
  3150. int bit, frame;
  3151. const char *str;
  3152. struct {
  3153. char *name;
  3154. unsigned int val;
  3155. } of_fmt_table[] = {
  3156. { "i2s", SND_SOC_DAIFMT_I2S },
  3157. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3158. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3159. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3160. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3161. { "ac97", SND_SOC_DAIFMT_AC97 },
  3162. { "pdm", SND_SOC_DAIFMT_PDM},
  3163. { "msb", SND_SOC_DAIFMT_MSB },
  3164. { "lsb", SND_SOC_DAIFMT_LSB },
  3165. };
  3166. if (!prefix)
  3167. prefix = "";
  3168. /*
  3169. * check "[prefix]format = xxx"
  3170. * SND_SOC_DAIFMT_FORMAT_MASK area
  3171. */
  3172. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3173. ret = of_property_read_string(np, prop, &str);
  3174. if (ret == 0) {
  3175. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3176. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3177. format |= of_fmt_table[i].val;
  3178. break;
  3179. }
  3180. }
  3181. }
  3182. /*
  3183. * check "[prefix]continuous-clock"
  3184. * SND_SOC_DAIFMT_CLOCK_MASK area
  3185. */
  3186. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3187. if (of_property_read_bool(np, prop))
  3188. format |= SND_SOC_DAIFMT_CONT;
  3189. else
  3190. format |= SND_SOC_DAIFMT_GATED;
  3191. /*
  3192. * check "[prefix]bitclock-inversion"
  3193. * check "[prefix]frame-inversion"
  3194. * SND_SOC_DAIFMT_INV_MASK area
  3195. */
  3196. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3197. bit = !!of_get_property(np, prop, NULL);
  3198. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3199. frame = !!of_get_property(np, prop, NULL);
  3200. switch ((bit << 4) + frame) {
  3201. case 0x11:
  3202. format |= SND_SOC_DAIFMT_IB_IF;
  3203. break;
  3204. case 0x10:
  3205. format |= SND_SOC_DAIFMT_IB_NF;
  3206. break;
  3207. case 0x01:
  3208. format |= SND_SOC_DAIFMT_NB_IF;
  3209. break;
  3210. default:
  3211. /* SND_SOC_DAIFMT_NB_NF is default */
  3212. break;
  3213. }
  3214. /*
  3215. * check "[prefix]bitclock-master"
  3216. * check "[prefix]frame-master"
  3217. * SND_SOC_DAIFMT_MASTER_MASK area
  3218. */
  3219. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3220. bit = !!of_get_property(np, prop, NULL);
  3221. if (bit && bitclkmaster)
  3222. *bitclkmaster = of_parse_phandle(np, prop, 0);
  3223. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3224. frame = !!of_get_property(np, prop, NULL);
  3225. if (frame && framemaster)
  3226. *framemaster = of_parse_phandle(np, prop, 0);
  3227. switch ((bit << 4) + frame) {
  3228. case 0x11:
  3229. format |= SND_SOC_DAIFMT_CBM_CFM;
  3230. break;
  3231. case 0x10:
  3232. format |= SND_SOC_DAIFMT_CBM_CFS;
  3233. break;
  3234. case 0x01:
  3235. format |= SND_SOC_DAIFMT_CBS_CFM;
  3236. break;
  3237. default:
  3238. format |= SND_SOC_DAIFMT_CBS_CFS;
  3239. break;
  3240. }
  3241. return format;
  3242. }
  3243. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3244. int snd_soc_get_dai_name(struct of_phandle_args *args,
  3245. const char **dai_name)
  3246. {
  3247. struct snd_soc_component *pos;
  3248. struct device_node *component_of_node;
  3249. int ret = -EPROBE_DEFER;
  3250. mutex_lock(&client_mutex);
  3251. list_for_each_entry(pos, &component_list, list) {
  3252. component_of_node = pos->dev->of_node;
  3253. if (!component_of_node && pos->dev->parent)
  3254. component_of_node = pos->dev->parent->of_node;
  3255. if (component_of_node != args->np)
  3256. continue;
  3257. if (pos->driver->of_xlate_dai_name) {
  3258. ret = pos->driver->of_xlate_dai_name(pos,
  3259. args,
  3260. dai_name);
  3261. } else {
  3262. int id = -1;
  3263. switch (args->args_count) {
  3264. case 0:
  3265. id = 0; /* same as dai_drv[0] */
  3266. break;
  3267. case 1:
  3268. id = args->args[0];
  3269. break;
  3270. default:
  3271. /* not supported */
  3272. break;
  3273. }
  3274. if (id < 0 || id >= pos->num_dai) {
  3275. ret = -EINVAL;
  3276. continue;
  3277. }
  3278. ret = 0;
  3279. *dai_name = pos->dai_drv[id].name;
  3280. if (!*dai_name)
  3281. *dai_name = pos->name;
  3282. }
  3283. break;
  3284. }
  3285. mutex_unlock(&client_mutex);
  3286. return ret;
  3287. }
  3288. EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
  3289. int snd_soc_of_get_dai_name(struct device_node *of_node,
  3290. const char **dai_name)
  3291. {
  3292. struct of_phandle_args args;
  3293. int ret;
  3294. ret = of_parse_phandle_with_args(of_node, "sound-dai",
  3295. "#sound-dai-cells", 0, &args);
  3296. if (ret)
  3297. return ret;
  3298. ret = snd_soc_get_dai_name(&args, dai_name);
  3299. of_node_put(args.np);
  3300. return ret;
  3301. }
  3302. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
  3303. /*
  3304. * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
  3305. * @dev: Card device
  3306. * @of_node: Device node
  3307. * @dai_link: DAI link
  3308. *
  3309. * Builds an array of CODEC DAI components from the DAI link property
  3310. * 'sound-dai'.
  3311. * The array is set in the DAI link and the number of DAIs is set accordingly.
  3312. * The device nodes in the array (of_node) must be dereferenced by the caller.
  3313. *
  3314. * Returns 0 for success
  3315. */
  3316. int snd_soc_of_get_dai_link_codecs(struct device *dev,
  3317. struct device_node *of_node,
  3318. struct snd_soc_dai_link *dai_link)
  3319. {
  3320. struct of_phandle_args args;
  3321. struct snd_soc_dai_link_component *component;
  3322. char *name;
  3323. int index, num_codecs, ret;
  3324. /* Count the number of CODECs */
  3325. name = "sound-dai";
  3326. num_codecs = of_count_phandle_with_args(of_node, name,
  3327. "#sound-dai-cells");
  3328. if (num_codecs <= 0) {
  3329. if (num_codecs == -ENOENT)
  3330. dev_err(dev, "No 'sound-dai' property\n");
  3331. else
  3332. dev_err(dev, "Bad phandle in 'sound-dai'\n");
  3333. return num_codecs;
  3334. }
  3335. component = devm_kzalloc(dev,
  3336. sizeof *component * num_codecs,
  3337. GFP_KERNEL);
  3338. if (!component)
  3339. return -ENOMEM;
  3340. dai_link->codecs = component;
  3341. dai_link->num_codecs = num_codecs;
  3342. /* Parse the list */
  3343. for (index = 0, component = dai_link->codecs;
  3344. index < dai_link->num_codecs;
  3345. index++, component++) {
  3346. ret = of_parse_phandle_with_args(of_node, name,
  3347. "#sound-dai-cells",
  3348. index, &args);
  3349. if (ret)
  3350. goto err;
  3351. component->of_node = args.np;
  3352. ret = snd_soc_get_dai_name(&args, &component->dai_name);
  3353. if (ret < 0)
  3354. goto err;
  3355. }
  3356. return 0;
  3357. err:
  3358. for (index = 0, component = dai_link->codecs;
  3359. index < dai_link->num_codecs;
  3360. index++, component++) {
  3361. if (!component->of_node)
  3362. break;
  3363. of_node_put(component->of_node);
  3364. component->of_node = NULL;
  3365. }
  3366. dai_link->codecs = NULL;
  3367. dai_link->num_codecs = 0;
  3368. return ret;
  3369. }
  3370. EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
  3371. static int __init snd_soc_init(void)
  3372. {
  3373. snd_soc_debugfs_init();
  3374. snd_soc_util_init();
  3375. return platform_driver_register(&soc_driver);
  3376. }
  3377. module_init(snd_soc_init);
  3378. static void __exit snd_soc_exit(void)
  3379. {
  3380. snd_soc_util_exit();
  3381. snd_soc_debugfs_exit();
  3382. #ifdef CONFIG_DEBUG_FS
  3383. #endif
  3384. platform_driver_unregister(&soc_driver);
  3385. }
  3386. module_exit(snd_soc_exit);
  3387. /* Module information */
  3388. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3389. MODULE_DESCRIPTION("ALSA SoC Core");
  3390. MODULE_LICENSE("GPL");
  3391. MODULE_ALIAS("platform:soc-audio");