soc-core.c 93 KB

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