soc-core.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225
  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/ctype.h>
  33. #include <linux/slab.h>
  34. #include <linux/of.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dpcm.h>
  42. #include <sound/initval.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/asoc.h>
  45. #define NAME_SIZE 32
  46. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  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(dai_list);
  53. static LIST_HEAD(platform_list);
  54. static LIST_HEAD(codec_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. char tmpbuf[len + 1];
  88. char regbuf[regsize + 1];
  89. /* since tmpbuf is allocated on the stack, warn the callers if they
  90. * try to abuse this function */
  91. WARN_ON(len > 63);
  92. /* +2 for ': ' and + 1 for '\n' */
  93. if (wordsize + regsize + 2 + 1 != len)
  94. return -EINVAL;
  95. ret = snd_soc_read(codec, reg);
  96. if (ret < 0) {
  97. memset(regbuf, 'X', regsize);
  98. regbuf[regsize] = '\0';
  99. } else {
  100. snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
  101. }
  102. /* prepare the buffer */
  103. snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
  104. /* copy it back to the caller without the '\0' */
  105. memcpy(buf, tmpbuf, len);
  106. return 0;
  107. }
  108. /* codec register dump */
  109. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  110. size_t count, loff_t pos)
  111. {
  112. int i, step = 1;
  113. int wordsize, regsize;
  114. int len;
  115. size_t total = 0;
  116. loff_t p = 0;
  117. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  118. regsize = codec->driver->reg_word_size * 2;
  119. len = wordsize + regsize + 2 + 1;
  120. if (!codec->driver->reg_cache_size)
  121. return 0;
  122. if (codec->driver->reg_cache_step)
  123. step = codec->driver->reg_cache_step;
  124. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  125. if (!snd_soc_codec_readable_register(codec, i))
  126. continue;
  127. if (codec->driver->display_register) {
  128. count += codec->driver->display_register(codec, buf + count,
  129. PAGE_SIZE - count, i);
  130. } else {
  131. /* only support larger than PAGE_SIZE bytes debugfs
  132. * entries for the default case */
  133. if (p >= pos) {
  134. if (total + len >= count - 1)
  135. break;
  136. format_register_str(codec, i, buf + total, len);
  137. total += len;
  138. }
  139. p += len;
  140. }
  141. }
  142. total = min(total, count - 1);
  143. return total;
  144. }
  145. static ssize_t codec_reg_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  149. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  150. }
  151. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  152. static ssize_t pmdown_time_show(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  156. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  157. }
  158. static ssize_t pmdown_time_set(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  163. int ret;
  164. ret = strict_strtol(buf, 10, &rtd->pmdown_time);
  165. if (ret)
  166. return ret;
  167. return count;
  168. }
  169. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  170. #ifdef CONFIG_DEBUG_FS
  171. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. ssize_t ret;
  175. struct snd_soc_codec *codec = file->private_data;
  176. char *buf;
  177. if (*ppos < 0 || !count)
  178. return -EINVAL;
  179. buf = kmalloc(count, GFP_KERNEL);
  180. if (!buf)
  181. return -ENOMEM;
  182. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  183. if (ret >= 0) {
  184. if (copy_to_user(user_buf, buf, ret)) {
  185. kfree(buf);
  186. return -EFAULT;
  187. }
  188. *ppos += ret;
  189. }
  190. kfree(buf);
  191. return ret;
  192. }
  193. static ssize_t codec_reg_write_file(struct file *file,
  194. const char __user *user_buf, size_t count, loff_t *ppos)
  195. {
  196. char buf[32];
  197. size_t buf_size;
  198. char *start = buf;
  199. unsigned long reg, value;
  200. struct snd_soc_codec *codec = file->private_data;
  201. buf_size = min(count, (sizeof(buf)-1));
  202. if (copy_from_user(buf, user_buf, buf_size))
  203. return -EFAULT;
  204. buf[buf_size] = 0;
  205. while (*start == ' ')
  206. start++;
  207. reg = simple_strtoul(start, &start, 16);
  208. while (*start == ' ')
  209. start++;
  210. if (strict_strtoul(start, 16, &value))
  211. return -EINVAL;
  212. /* Userspace has been fiddling around behind the kernel's back */
  213. add_taint(TAINT_USER);
  214. snd_soc_write(codec, reg, value);
  215. return buf_size;
  216. }
  217. static const struct file_operations codec_reg_fops = {
  218. .open = simple_open,
  219. .read = codec_reg_read_file,
  220. .write = codec_reg_write_file,
  221. .llseek = default_llseek,
  222. };
  223. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  224. {
  225. struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
  226. codec->debugfs_codec_root = debugfs_create_dir(codec->name,
  227. debugfs_card_root);
  228. if (!codec->debugfs_codec_root) {
  229. dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
  230. " directory\n");
  231. return;
  232. }
  233. debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
  234. &codec->cache_sync);
  235. debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
  236. &codec->cache_only);
  237. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  238. codec->debugfs_codec_root,
  239. codec, &codec_reg_fops);
  240. if (!codec->debugfs_reg)
  241. dev_warn(codec->dev, "ASoC: Failed to create codec register"
  242. " debugfs file\n");
  243. snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
  244. }
  245. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  246. {
  247. debugfs_remove_recursive(codec->debugfs_codec_root);
  248. }
  249. static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  250. {
  251. struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
  252. platform->debugfs_platform_root = debugfs_create_dir(platform->name,
  253. debugfs_card_root);
  254. if (!platform->debugfs_platform_root) {
  255. dev_warn(platform->dev,
  256. "ASoC: Failed to create platform debugfs directory\n");
  257. return;
  258. }
  259. snd_soc_dapm_debugfs_init(&platform->dapm,
  260. platform->debugfs_platform_root);
  261. }
  262. static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  263. {
  264. debugfs_remove_recursive(platform->debugfs_platform_root);
  265. }
  266. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  267. size_t count, loff_t *ppos)
  268. {
  269. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  270. ssize_t len, ret = 0;
  271. struct snd_soc_codec *codec;
  272. if (!buf)
  273. return -ENOMEM;
  274. list_for_each_entry(codec, &codec_list, list) {
  275. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  276. codec->name);
  277. if (len >= 0)
  278. ret += len;
  279. if (ret > PAGE_SIZE) {
  280. ret = PAGE_SIZE;
  281. break;
  282. }
  283. }
  284. if (ret >= 0)
  285. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  286. kfree(buf);
  287. return ret;
  288. }
  289. static const struct file_operations codec_list_fops = {
  290. .read = codec_list_read_file,
  291. .llseek = default_llseek,/* read accesses f_pos */
  292. };
  293. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  294. size_t count, loff_t *ppos)
  295. {
  296. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  297. ssize_t len, ret = 0;
  298. struct snd_soc_dai *dai;
  299. if (!buf)
  300. return -ENOMEM;
  301. list_for_each_entry(dai, &dai_list, list) {
  302. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
  303. if (len >= 0)
  304. ret += len;
  305. if (ret > PAGE_SIZE) {
  306. ret = PAGE_SIZE;
  307. break;
  308. }
  309. }
  310. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  311. kfree(buf);
  312. return ret;
  313. }
  314. static const struct file_operations dai_list_fops = {
  315. .read = dai_list_read_file,
  316. .llseek = default_llseek,/* read accesses f_pos */
  317. };
  318. static ssize_t platform_list_read_file(struct file *file,
  319. char __user *user_buf,
  320. size_t count, loff_t *ppos)
  321. {
  322. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  323. ssize_t len, ret = 0;
  324. struct snd_soc_platform *platform;
  325. if (!buf)
  326. return -ENOMEM;
  327. list_for_each_entry(platform, &platform_list, list) {
  328. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  329. platform->name);
  330. if (len >= 0)
  331. ret += len;
  332. if (ret > PAGE_SIZE) {
  333. ret = PAGE_SIZE;
  334. break;
  335. }
  336. }
  337. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  338. kfree(buf);
  339. return ret;
  340. }
  341. static const struct file_operations platform_list_fops = {
  342. .read = platform_list_read_file,
  343. .llseek = default_llseek,/* read accesses f_pos */
  344. };
  345. static void soc_init_card_debugfs(struct snd_soc_card *card)
  346. {
  347. card->debugfs_card_root = debugfs_create_dir(card->name,
  348. snd_soc_debugfs_root);
  349. if (!card->debugfs_card_root) {
  350. dev_warn(card->dev,
  351. "ASoC: Failed to create card debugfs directory\n");
  352. return;
  353. }
  354. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  355. card->debugfs_card_root,
  356. &card->pop_time);
  357. if (!card->debugfs_pop_time)
  358. dev_warn(card->dev,
  359. "ASoC: Failed to create pop time debugfs file\n");
  360. }
  361. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  362. {
  363. debugfs_remove_recursive(card->debugfs_card_root);
  364. }
  365. #else
  366. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  367. {
  368. }
  369. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  370. {
  371. }
  372. static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  373. {
  374. }
  375. static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  376. {
  377. }
  378. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  379. {
  380. }
  381. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  382. {
  383. }
  384. #endif
  385. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  386. const char *dai_link, int stream)
  387. {
  388. int i;
  389. for (i = 0; i < card->num_links; i++) {
  390. if (card->rtd[i].dai_link->no_pcm &&
  391. !strcmp(card->rtd[i].dai_link->name, dai_link))
  392. return card->rtd[i].pcm->streams[stream].substream;
  393. }
  394. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  395. return NULL;
  396. }
  397. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  398. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  399. const char *dai_link)
  400. {
  401. int i;
  402. for (i = 0; i < card->num_links; i++) {
  403. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  404. return &card->rtd[i];
  405. }
  406. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  407. return NULL;
  408. }
  409. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  410. #ifdef CONFIG_SND_SOC_AC97_BUS
  411. /* unregister ac97 codec */
  412. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  413. {
  414. if (codec->ac97->dev.bus)
  415. device_unregister(&codec->ac97->dev);
  416. return 0;
  417. }
  418. /* stop no dev release warning */
  419. static void soc_ac97_device_release(struct device *dev){}
  420. /* register ac97 codec to bus */
  421. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  422. {
  423. int err;
  424. codec->ac97->dev.bus = &ac97_bus_type;
  425. codec->ac97->dev.parent = codec->card->dev;
  426. codec->ac97->dev.release = soc_ac97_device_release;
  427. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  428. codec->card->snd_card->number, 0, codec->name);
  429. err = device_register(&codec->ac97->dev);
  430. if (err < 0) {
  431. dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
  432. codec->ac97->dev.bus = NULL;
  433. return err;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. #ifdef CONFIG_PM_SLEEP
  439. /* powers down audio subsystem for suspend */
  440. int snd_soc_suspend(struct device *dev)
  441. {
  442. struct snd_soc_card *card = dev_get_drvdata(dev);
  443. struct snd_soc_codec *codec;
  444. int i;
  445. /* If the initialization of this soc device failed, there is no codec
  446. * associated with it. Just bail out in this case.
  447. */
  448. if (list_empty(&card->codec_dev_list))
  449. return 0;
  450. /* Due to the resume being scheduled into a workqueue we could
  451. * suspend before that's finished - wait for it to complete.
  452. */
  453. snd_power_lock(card->snd_card);
  454. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  455. snd_power_unlock(card->snd_card);
  456. /* we're going to block userspace touching us until resume completes */
  457. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  458. /* mute any active DACs */
  459. for (i = 0; i < card->num_rtd; i++) {
  460. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  461. struct snd_soc_dai_driver *drv = dai->driver;
  462. if (card->rtd[i].dai_link->ignore_suspend)
  463. continue;
  464. if (drv->ops->digital_mute && dai->playback_active)
  465. drv->ops->digital_mute(dai, 1);
  466. }
  467. /* suspend all pcms */
  468. for (i = 0; i < card->num_rtd; i++) {
  469. if (card->rtd[i].dai_link->ignore_suspend)
  470. continue;
  471. snd_pcm_suspend_all(card->rtd[i].pcm);
  472. }
  473. if (card->suspend_pre)
  474. card->suspend_pre(card);
  475. for (i = 0; i < card->num_rtd; i++) {
  476. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  477. struct snd_soc_platform *platform = card->rtd[i].platform;
  478. if (card->rtd[i].dai_link->ignore_suspend)
  479. continue;
  480. if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
  481. cpu_dai->driver->suspend(cpu_dai);
  482. if (platform->driver->suspend && !platform->suspended) {
  483. platform->driver->suspend(cpu_dai);
  484. platform->suspended = 1;
  485. }
  486. }
  487. /* close any waiting streams and save state */
  488. for (i = 0; i < card->num_rtd; i++) {
  489. flush_delayed_work(&card->rtd[i].delayed_work);
  490. card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
  491. }
  492. for (i = 0; i < card->num_rtd; i++) {
  493. if (card->rtd[i].dai_link->ignore_suspend)
  494. continue;
  495. snd_soc_dapm_stream_event(&card->rtd[i],
  496. SNDRV_PCM_STREAM_PLAYBACK,
  497. SND_SOC_DAPM_STREAM_SUSPEND);
  498. snd_soc_dapm_stream_event(&card->rtd[i],
  499. SNDRV_PCM_STREAM_CAPTURE,
  500. SND_SOC_DAPM_STREAM_SUSPEND);
  501. }
  502. /* Recheck all analogue paths too */
  503. dapm_mark_io_dirty(&card->dapm);
  504. snd_soc_dapm_sync(&card->dapm);
  505. /* suspend all CODECs */
  506. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  507. /* If there are paths active then the CODEC will be held with
  508. * bias _ON and should not be suspended. */
  509. if (!codec->suspended && codec->driver->suspend) {
  510. switch (codec->dapm.bias_level) {
  511. case SND_SOC_BIAS_STANDBY:
  512. /*
  513. * If the CODEC is capable of idle
  514. * bias off then being in STANDBY
  515. * means it's doing something,
  516. * otherwise fall through.
  517. */
  518. if (codec->dapm.idle_bias_off) {
  519. dev_dbg(codec->dev,
  520. "ASoC: idle_bias_off CODEC on"
  521. " over suspend\n");
  522. break;
  523. }
  524. case SND_SOC_BIAS_OFF:
  525. codec->driver->suspend(codec);
  526. codec->suspended = 1;
  527. codec->cache_sync = 1;
  528. if (codec->using_regmap)
  529. regcache_mark_dirty(codec->control_data);
  530. break;
  531. default:
  532. dev_dbg(codec->dev, "ASoC: CODEC is on"
  533. " over suspend\n");
  534. break;
  535. }
  536. }
  537. }
  538. for (i = 0; i < card->num_rtd; i++) {
  539. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  540. if (card->rtd[i].dai_link->ignore_suspend)
  541. continue;
  542. if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
  543. cpu_dai->driver->suspend(cpu_dai);
  544. }
  545. if (card->suspend_post)
  546. card->suspend_post(card);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  550. /* deferred resume work, so resume can complete before we finished
  551. * setting our codec back up, which can be very slow on I2C
  552. */
  553. static void soc_resume_deferred(struct work_struct *work)
  554. {
  555. struct snd_soc_card *card =
  556. container_of(work, struct snd_soc_card, deferred_resume_work);
  557. struct snd_soc_codec *codec;
  558. int i;
  559. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  560. * so userspace apps are blocked from touching us
  561. */
  562. dev_dbg(card->dev, "ASoC: starting resume work\n");
  563. /* Bring us up into D2 so that DAPM starts enabling things */
  564. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  565. if (card->resume_pre)
  566. card->resume_pre(card);
  567. /* resume AC97 DAIs */
  568. for (i = 0; i < card->num_rtd; i++) {
  569. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  570. if (card->rtd[i].dai_link->ignore_suspend)
  571. continue;
  572. if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
  573. cpu_dai->driver->resume(cpu_dai);
  574. }
  575. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  576. /* If the CODEC was idle over suspend then it will have been
  577. * left with bias OFF or STANDBY and suspended so we must now
  578. * resume. Otherwise the suspend was suppressed.
  579. */
  580. if (codec->driver->resume && codec->suspended) {
  581. switch (codec->dapm.bias_level) {
  582. case SND_SOC_BIAS_STANDBY:
  583. case SND_SOC_BIAS_OFF:
  584. codec->driver->resume(codec);
  585. codec->suspended = 0;
  586. break;
  587. default:
  588. dev_dbg(codec->dev, "ASoC: CODEC was on over"
  589. " suspend\n");
  590. break;
  591. }
  592. }
  593. }
  594. for (i = 0; i < card->num_rtd; i++) {
  595. if (card->rtd[i].dai_link->ignore_suspend)
  596. continue;
  597. snd_soc_dapm_stream_event(&card->rtd[i],
  598. SNDRV_PCM_STREAM_PLAYBACK,
  599. SND_SOC_DAPM_STREAM_RESUME);
  600. snd_soc_dapm_stream_event(&card->rtd[i],
  601. SNDRV_PCM_STREAM_CAPTURE,
  602. SND_SOC_DAPM_STREAM_RESUME);
  603. }
  604. /* unmute any active DACs */
  605. for (i = 0; i < card->num_rtd; i++) {
  606. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  607. struct snd_soc_dai_driver *drv = dai->driver;
  608. if (card->rtd[i].dai_link->ignore_suspend)
  609. continue;
  610. if (drv->ops->digital_mute && dai->playback_active)
  611. drv->ops->digital_mute(dai, 0);
  612. }
  613. for (i = 0; i < card->num_rtd; i++) {
  614. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  615. struct snd_soc_platform *platform = card->rtd[i].platform;
  616. if (card->rtd[i].dai_link->ignore_suspend)
  617. continue;
  618. if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
  619. cpu_dai->driver->resume(cpu_dai);
  620. if (platform->driver->resume && platform->suspended) {
  621. platform->driver->resume(cpu_dai);
  622. platform->suspended = 0;
  623. }
  624. }
  625. if (card->resume_post)
  626. card->resume_post(card);
  627. dev_dbg(card->dev, "ASoC: resume work completed\n");
  628. /* userspace can access us now we are back as we were before */
  629. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  630. /* Recheck all analogue paths too */
  631. dapm_mark_io_dirty(&card->dapm);
  632. snd_soc_dapm_sync(&card->dapm);
  633. }
  634. /* powers up audio subsystem after a suspend */
  635. int snd_soc_resume(struct device *dev)
  636. {
  637. struct snd_soc_card *card = dev_get_drvdata(dev);
  638. int i, ac97_control = 0;
  639. /* If the initialization of this soc device failed, there is no codec
  640. * associated with it. Just bail out in this case.
  641. */
  642. if (list_empty(&card->codec_dev_list))
  643. return 0;
  644. /* AC97 devices might have other drivers hanging off them so
  645. * need to resume immediately. Other drivers don't have that
  646. * problem and may take a substantial amount of time to resume
  647. * due to I/O costs and anti-pop so handle them out of line.
  648. */
  649. for (i = 0; i < card->num_rtd; i++) {
  650. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  651. ac97_control |= cpu_dai->driver->ac97_control;
  652. }
  653. if (ac97_control) {
  654. dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
  655. soc_resume_deferred(&card->deferred_resume_work);
  656. } else {
  657. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  658. if (!schedule_work(&card->deferred_resume_work))
  659. dev_err(dev, "ASoC: resume work item may be lost\n");
  660. }
  661. return 0;
  662. }
  663. EXPORT_SYMBOL_GPL(snd_soc_resume);
  664. #else
  665. #define snd_soc_suspend NULL
  666. #define snd_soc_resume NULL
  667. #endif
  668. static const struct snd_soc_dai_ops null_dai_ops = {
  669. };
  670. static int soc_bind_dai_link(struct snd_soc_card *card, int num)
  671. {
  672. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  673. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  674. struct snd_soc_codec *codec;
  675. struct snd_soc_platform *platform;
  676. struct snd_soc_dai *codec_dai, *cpu_dai;
  677. const char *platform_name;
  678. dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
  679. /* Find CPU DAI from registered DAIs*/
  680. list_for_each_entry(cpu_dai, &dai_list, list) {
  681. if (dai_link->cpu_of_node &&
  682. (cpu_dai->dev->of_node != dai_link->cpu_of_node))
  683. continue;
  684. if (dai_link->cpu_name &&
  685. strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
  686. continue;
  687. if (dai_link->cpu_dai_name &&
  688. strcmp(cpu_dai->name, dai_link->cpu_dai_name))
  689. continue;
  690. rtd->cpu_dai = cpu_dai;
  691. }
  692. if (!rtd->cpu_dai) {
  693. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  694. dai_link->cpu_dai_name);
  695. return -EPROBE_DEFER;
  696. }
  697. /* Find CODEC from registered CODECs */
  698. list_for_each_entry(codec, &codec_list, list) {
  699. if (dai_link->codec_of_node) {
  700. if (codec->dev->of_node != dai_link->codec_of_node)
  701. continue;
  702. } else {
  703. if (strcmp(codec->name, dai_link->codec_name))
  704. continue;
  705. }
  706. rtd->codec = codec;
  707. /*
  708. * CODEC found, so find CODEC DAI from registered DAIs from
  709. * this CODEC
  710. */
  711. list_for_each_entry(codec_dai, &dai_list, list) {
  712. if (codec->dev == codec_dai->dev &&
  713. !strcmp(codec_dai->name,
  714. dai_link->codec_dai_name)) {
  715. rtd->codec_dai = codec_dai;
  716. }
  717. }
  718. if (!rtd->codec_dai) {
  719. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  720. dai_link->codec_dai_name);
  721. return -EPROBE_DEFER;
  722. }
  723. }
  724. if (!rtd->codec) {
  725. dev_err(card->dev, "ASoC: CODEC %s not registered\n",
  726. dai_link->codec_name);
  727. return -EPROBE_DEFER;
  728. }
  729. /* if there's no platform we match on the empty platform */
  730. platform_name = dai_link->platform_name;
  731. if (!platform_name && !dai_link->platform_of_node)
  732. platform_name = "snd-soc-dummy";
  733. /* find one from the set of registered platforms */
  734. list_for_each_entry(platform, &platform_list, list) {
  735. if (dai_link->platform_of_node) {
  736. if (platform->dev->of_node !=
  737. dai_link->platform_of_node)
  738. continue;
  739. } else {
  740. if (strcmp(platform->name, platform_name))
  741. continue;
  742. }
  743. rtd->platform = platform;
  744. }
  745. if (!rtd->platform) {
  746. dev_err(card->dev, "ASoC: platform %s not registered\n",
  747. dai_link->platform_name);
  748. return -EPROBE_DEFER;
  749. }
  750. card->num_rtd++;
  751. return 0;
  752. }
  753. static int soc_remove_platform(struct snd_soc_platform *platform)
  754. {
  755. int ret;
  756. if (platform->driver->remove) {
  757. ret = platform->driver->remove(platform);
  758. if (ret < 0)
  759. dev_err(platform->dev, "ASoC: failed to remove %d\n",
  760. ret);
  761. }
  762. /* Make sure all DAPM widgets are freed */
  763. snd_soc_dapm_free(&platform->dapm);
  764. soc_cleanup_platform_debugfs(platform);
  765. platform->probed = 0;
  766. list_del(&platform->card_list);
  767. module_put(platform->dev->driver->owner);
  768. return 0;
  769. }
  770. static void soc_remove_codec(struct snd_soc_codec *codec)
  771. {
  772. int err;
  773. if (codec->driver->remove) {
  774. err = codec->driver->remove(codec);
  775. if (err < 0)
  776. dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
  777. }
  778. /* Make sure all DAPM widgets are freed */
  779. snd_soc_dapm_free(&codec->dapm);
  780. soc_cleanup_codec_debugfs(codec);
  781. codec->probed = 0;
  782. list_del(&codec->card_list);
  783. module_put(codec->dev->driver->owner);
  784. }
  785. static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
  786. {
  787. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  788. struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
  789. int err;
  790. /* unregister the rtd device */
  791. if (rtd->dev_registered) {
  792. device_remove_file(rtd->dev, &dev_attr_pmdown_time);
  793. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  794. device_unregister(rtd->dev);
  795. rtd->dev_registered = 0;
  796. }
  797. /* remove the CODEC DAI */
  798. if (codec_dai && codec_dai->probed &&
  799. codec_dai->driver->remove_order == order) {
  800. if (codec_dai->driver->remove) {
  801. err = codec_dai->driver->remove(codec_dai);
  802. if (err < 0)
  803. dev_err(codec_dai->dev,
  804. "ASoC: failed to remove %s: %d\n",
  805. codec_dai->name, err);
  806. }
  807. codec_dai->probed = 0;
  808. list_del(&codec_dai->card_list);
  809. }
  810. /* remove the cpu_dai */
  811. if (cpu_dai && cpu_dai->probed &&
  812. cpu_dai->driver->remove_order == order) {
  813. if (cpu_dai->driver->remove) {
  814. err = cpu_dai->driver->remove(cpu_dai);
  815. if (err < 0)
  816. dev_err(cpu_dai->dev,
  817. "ASoC: failed to remove %s: %d\n",
  818. cpu_dai->name, err);
  819. }
  820. cpu_dai->probed = 0;
  821. list_del(&cpu_dai->card_list);
  822. if (!cpu_dai->codec) {
  823. snd_soc_dapm_free(&cpu_dai->dapm);
  824. module_put(cpu_dai->dev->driver->owner);
  825. }
  826. }
  827. }
  828. static void soc_remove_link_components(struct snd_soc_card *card, int num,
  829. int order)
  830. {
  831. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  832. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  833. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  834. struct snd_soc_platform *platform = rtd->platform;
  835. struct snd_soc_codec *codec;
  836. /* remove the platform */
  837. if (platform && platform->probed &&
  838. platform->driver->remove_order == order) {
  839. soc_remove_platform(platform);
  840. }
  841. /* remove the CODEC-side CODEC */
  842. if (codec_dai) {
  843. codec = codec_dai->codec;
  844. if (codec && codec->probed &&
  845. codec->driver->remove_order == order)
  846. soc_remove_codec(codec);
  847. }
  848. /* remove any CPU-side CODEC */
  849. if (cpu_dai) {
  850. codec = cpu_dai->codec;
  851. if (codec && codec->probed &&
  852. codec->driver->remove_order == order)
  853. soc_remove_codec(codec);
  854. }
  855. }
  856. static void soc_remove_dai_links(struct snd_soc_card *card)
  857. {
  858. int dai, order;
  859. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  860. order++) {
  861. for (dai = 0; dai < card->num_rtd; dai++)
  862. soc_remove_link_dais(card, dai, order);
  863. }
  864. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  865. order++) {
  866. for (dai = 0; dai < card->num_rtd; dai++)
  867. soc_remove_link_components(card, dai, order);
  868. }
  869. card->num_rtd = 0;
  870. }
  871. static void soc_set_name_prefix(struct snd_soc_card *card,
  872. struct snd_soc_codec *codec)
  873. {
  874. int i;
  875. if (card->codec_conf == NULL)
  876. return;
  877. for (i = 0; i < card->num_configs; i++) {
  878. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  879. if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
  880. codec->name_prefix = map->name_prefix;
  881. break;
  882. }
  883. }
  884. }
  885. static int soc_probe_codec(struct snd_soc_card *card,
  886. struct snd_soc_codec *codec)
  887. {
  888. int ret = 0;
  889. const struct snd_soc_codec_driver *driver = codec->driver;
  890. struct snd_soc_dai *dai;
  891. codec->card = card;
  892. codec->dapm.card = card;
  893. soc_set_name_prefix(card, codec);
  894. if (!try_module_get(codec->dev->driver->owner))
  895. return -ENODEV;
  896. soc_init_codec_debugfs(codec);
  897. if (driver->dapm_widgets)
  898. snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
  899. driver->num_dapm_widgets);
  900. /* Create DAPM widgets for each DAI stream */
  901. list_for_each_entry(dai, &dai_list, list) {
  902. if (dai->dev != codec->dev)
  903. continue;
  904. snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
  905. }
  906. codec->dapm.idle_bias_off = driver->idle_bias_off;
  907. if (driver->probe) {
  908. ret = driver->probe(codec);
  909. if (ret < 0) {
  910. dev_err(codec->dev,
  911. "ASoC: failed to probe CODEC %d\n", ret);
  912. goto err_probe;
  913. }
  914. }
  915. /* If the driver didn't set I/O up try regmap */
  916. if (!codec->write && dev_get_regmap(codec->dev, NULL))
  917. snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  918. if (driver->controls)
  919. snd_soc_add_codec_controls(codec, driver->controls,
  920. driver->num_controls);
  921. if (driver->dapm_routes)
  922. snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
  923. driver->num_dapm_routes);
  924. /* mark codec as probed and add to card codec list */
  925. codec->probed = 1;
  926. list_add(&codec->card_list, &card->codec_dev_list);
  927. list_add(&codec->dapm.list, &card->dapm_list);
  928. return 0;
  929. err_probe:
  930. soc_cleanup_codec_debugfs(codec);
  931. module_put(codec->dev->driver->owner);
  932. return ret;
  933. }
  934. static int soc_probe_platform(struct snd_soc_card *card,
  935. struct snd_soc_platform *platform)
  936. {
  937. int ret = 0;
  938. const struct snd_soc_platform_driver *driver = platform->driver;
  939. struct snd_soc_dai *dai;
  940. platform->card = card;
  941. platform->dapm.card = card;
  942. if (!try_module_get(platform->dev->driver->owner))
  943. return -ENODEV;
  944. soc_init_platform_debugfs(platform);
  945. if (driver->dapm_widgets)
  946. snd_soc_dapm_new_controls(&platform->dapm,
  947. driver->dapm_widgets, driver->num_dapm_widgets);
  948. /* Create DAPM widgets for each DAI stream */
  949. list_for_each_entry(dai, &dai_list, list) {
  950. if (dai->dev != platform->dev)
  951. continue;
  952. snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
  953. }
  954. platform->dapm.idle_bias_off = 1;
  955. if (driver->probe) {
  956. ret = driver->probe(platform);
  957. if (ret < 0) {
  958. dev_err(platform->dev,
  959. "ASoC: failed to probe platform %d\n", ret);
  960. goto err_probe;
  961. }
  962. }
  963. if (driver->controls)
  964. snd_soc_add_platform_controls(platform, driver->controls,
  965. driver->num_controls);
  966. if (driver->dapm_routes)
  967. snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
  968. driver->num_dapm_routes);
  969. /* mark platform as probed and add to card platform list */
  970. platform->probed = 1;
  971. list_add(&platform->card_list, &card->platform_dev_list);
  972. list_add(&platform->dapm.list, &card->dapm_list);
  973. return 0;
  974. err_probe:
  975. soc_cleanup_platform_debugfs(platform);
  976. module_put(platform->dev->driver->owner);
  977. return ret;
  978. }
  979. static void rtd_release(struct device *dev)
  980. {
  981. kfree(dev);
  982. }
  983. static int soc_post_component_init(struct snd_soc_card *card,
  984. struct snd_soc_codec *codec,
  985. int num, int dailess)
  986. {
  987. struct snd_soc_dai_link *dai_link = NULL;
  988. struct snd_soc_aux_dev *aux_dev = NULL;
  989. struct snd_soc_pcm_runtime *rtd;
  990. const char *temp, *name;
  991. int ret = 0;
  992. if (!dailess) {
  993. dai_link = &card->dai_link[num];
  994. rtd = &card->rtd[num];
  995. name = dai_link->name;
  996. } else {
  997. aux_dev = &card->aux_dev[num];
  998. rtd = &card->rtd_aux[num];
  999. name = aux_dev->name;
  1000. }
  1001. rtd->card = card;
  1002. /* Make sure all DAPM widgets are instantiated */
  1003. snd_soc_dapm_new_widgets(&codec->dapm);
  1004. /* machine controls, routes and widgets are not prefixed */
  1005. temp = codec->name_prefix;
  1006. codec->name_prefix = NULL;
  1007. /* do machine specific initialization */
  1008. if (!dailess && dai_link->init)
  1009. ret = dai_link->init(rtd);
  1010. else if (dailess && aux_dev->init)
  1011. ret = aux_dev->init(&codec->dapm);
  1012. if (ret < 0) {
  1013. dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
  1014. return ret;
  1015. }
  1016. codec->name_prefix = temp;
  1017. /* register the rtd device */
  1018. rtd->codec = codec;
  1019. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1020. if (!rtd->dev)
  1021. return -ENOMEM;
  1022. device_initialize(rtd->dev);
  1023. rtd->dev->parent = card->dev;
  1024. rtd->dev->release = rtd_release;
  1025. rtd->dev->init_name = name;
  1026. dev_set_drvdata(rtd->dev, rtd);
  1027. mutex_init(&rtd->pcm_mutex);
  1028. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1029. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1030. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1031. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1032. ret = device_add(rtd->dev);
  1033. if (ret < 0) {
  1034. dev_err(card->dev,
  1035. "ASoC: failed to register runtime device: %d\n", ret);
  1036. return ret;
  1037. }
  1038. rtd->dev_registered = 1;
  1039. /* add DAPM sysfs entries for this codec */
  1040. ret = snd_soc_dapm_sys_add(rtd->dev);
  1041. if (ret < 0)
  1042. dev_err(codec->dev,
  1043. "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
  1044. /* add codec sysfs entries */
  1045. ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
  1046. if (ret < 0)
  1047. dev_err(codec->dev,
  1048. "ASoC: failed to add codec sysfs files: %d\n", ret);
  1049. #ifdef CONFIG_DEBUG_FS
  1050. /* add DPCM sysfs entries */
  1051. if (!dailess && !dai_link->dynamic)
  1052. goto out;
  1053. ret = soc_dpcm_debugfs_add(rtd);
  1054. if (ret < 0)
  1055. dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
  1056. out:
  1057. #endif
  1058. return 0;
  1059. }
  1060. static int soc_probe_link_components(struct snd_soc_card *card, int num,
  1061. int order)
  1062. {
  1063. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1064. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1065. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1066. struct snd_soc_platform *platform = rtd->platform;
  1067. int ret;
  1068. /* probe the CPU-side component, if it is a CODEC */
  1069. if (cpu_dai->codec &&
  1070. !cpu_dai->codec->probed &&
  1071. cpu_dai->codec->driver->probe_order == order) {
  1072. ret = soc_probe_codec(card, cpu_dai->codec);
  1073. if (ret < 0)
  1074. return ret;
  1075. }
  1076. /* probe the CODEC-side component */
  1077. if (!codec_dai->codec->probed &&
  1078. codec_dai->codec->driver->probe_order == order) {
  1079. ret = soc_probe_codec(card, codec_dai->codec);
  1080. if (ret < 0)
  1081. return ret;
  1082. }
  1083. /* probe the platform */
  1084. if (!platform->probed &&
  1085. platform->driver->probe_order == order) {
  1086. ret = soc_probe_platform(card, platform);
  1087. if (ret < 0)
  1088. return ret;
  1089. }
  1090. return 0;
  1091. }
  1092. static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
  1093. {
  1094. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  1095. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1096. struct snd_soc_codec *codec = rtd->codec;
  1097. struct snd_soc_platform *platform = rtd->platform;
  1098. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1099. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1100. struct snd_soc_dapm_widget *play_w, *capture_w;
  1101. int ret;
  1102. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1103. card->name, num, order);
  1104. /* config components */
  1105. cpu_dai->platform = platform;
  1106. codec_dai->card = card;
  1107. cpu_dai->card = card;
  1108. /* set default power off timeout */
  1109. rtd->pmdown_time = pmdown_time;
  1110. /* probe the cpu_dai */
  1111. if (!cpu_dai->probed &&
  1112. cpu_dai->driver->probe_order == order) {
  1113. if (!cpu_dai->codec) {
  1114. cpu_dai->dapm.card = card;
  1115. if (!try_module_get(cpu_dai->dev->driver->owner))
  1116. return -ENODEV;
  1117. list_add(&cpu_dai->dapm.list, &card->dapm_list);
  1118. snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
  1119. }
  1120. if (cpu_dai->driver->probe) {
  1121. ret = cpu_dai->driver->probe(cpu_dai);
  1122. if (ret < 0) {
  1123. dev_err(cpu_dai->dev,
  1124. "ASoC: failed to probe CPU DAI %s: %d\n",
  1125. cpu_dai->name, ret);
  1126. module_put(cpu_dai->dev->driver->owner);
  1127. return ret;
  1128. }
  1129. }
  1130. cpu_dai->probed = 1;
  1131. /* mark cpu_dai as probed and add to card dai list */
  1132. list_add(&cpu_dai->card_list, &card->dai_dev_list);
  1133. }
  1134. /* probe the CODEC DAI */
  1135. if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
  1136. if (codec_dai->driver->probe) {
  1137. ret = codec_dai->driver->probe(codec_dai);
  1138. if (ret < 0) {
  1139. dev_err(codec_dai->dev,
  1140. "ASoC: failed to probe CODEC DAI %s: %d\n",
  1141. codec_dai->name, ret);
  1142. return ret;
  1143. }
  1144. }
  1145. /* mark codec_dai as probed and add to card dai list */
  1146. codec_dai->probed = 1;
  1147. list_add(&codec_dai->card_list, &card->dai_dev_list);
  1148. }
  1149. /* complete DAI probe during last probe */
  1150. if (order != SND_SOC_COMP_ORDER_LAST)
  1151. return 0;
  1152. ret = soc_post_component_init(card, codec, num, 0);
  1153. if (ret)
  1154. return ret;
  1155. ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
  1156. if (ret < 0)
  1157. dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
  1158. ret);
  1159. if (cpu_dai->driver->compress_dai) {
  1160. /*create compress_device"*/
  1161. ret = soc_new_compress(rtd, num);
  1162. if (ret < 0) {
  1163. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1164. dai_link->stream_name);
  1165. return ret;
  1166. }
  1167. } else {
  1168. if (!dai_link->params) {
  1169. /* create the pcm */
  1170. ret = soc_new_pcm(rtd, num);
  1171. if (ret < 0) {
  1172. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1173. dai_link->stream_name, ret);
  1174. return ret;
  1175. }
  1176. } else {
  1177. /* link the DAI widgets */
  1178. play_w = codec_dai->playback_widget;
  1179. capture_w = cpu_dai->capture_widget;
  1180. if (play_w && capture_w) {
  1181. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1182. capture_w, play_w);
  1183. if (ret != 0) {
  1184. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1185. play_w->name, capture_w->name, ret);
  1186. return ret;
  1187. }
  1188. }
  1189. play_w = cpu_dai->playback_widget;
  1190. capture_w = codec_dai->capture_widget;
  1191. if (play_w && capture_w) {
  1192. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1193. capture_w, play_w);
  1194. if (ret != 0) {
  1195. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1196. play_w->name, capture_w->name, ret);
  1197. return ret;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. /* add platform data for AC97 devices */
  1203. if (rtd->codec_dai->driver->ac97_control)
  1204. snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
  1205. return 0;
  1206. }
  1207. #ifdef CONFIG_SND_SOC_AC97_BUS
  1208. static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
  1209. {
  1210. int ret;
  1211. /* Only instantiate AC97 if not already done by the adaptor
  1212. * for the generic AC97 subsystem.
  1213. */
  1214. if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
  1215. /*
  1216. * It is possible that the AC97 device is already registered to
  1217. * the device subsystem. This happens when the device is created
  1218. * via snd_ac97_mixer(). Currently only SoC codec that does so
  1219. * is the generic AC97 glue but others migh emerge.
  1220. *
  1221. * In those cases we don't try to register the device again.
  1222. */
  1223. if (!rtd->codec->ac97_created)
  1224. return 0;
  1225. ret = soc_ac97_dev_register(rtd->codec);
  1226. if (ret < 0) {
  1227. dev_err(rtd->codec->dev,
  1228. "ASoC: AC97 device register failed: %d\n", ret);
  1229. return ret;
  1230. }
  1231. rtd->codec->ac97_registered = 1;
  1232. }
  1233. return 0;
  1234. }
  1235. static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
  1236. {
  1237. if (codec->ac97_registered) {
  1238. soc_ac97_dev_unregister(codec);
  1239. codec->ac97_registered = 0;
  1240. }
  1241. }
  1242. #endif
  1243. static int soc_check_aux_dev(struct snd_soc_card *card, int num)
  1244. {
  1245. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1246. struct snd_soc_codec *codec;
  1247. /* find CODEC from registered CODECs*/
  1248. list_for_each_entry(codec, &codec_list, list) {
  1249. if (!strcmp(codec->name, aux_dev->codec_name))
  1250. return 0;
  1251. }
  1252. dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
  1253. return -EPROBE_DEFER;
  1254. }
  1255. static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
  1256. {
  1257. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1258. struct snd_soc_codec *codec;
  1259. int ret = -ENODEV;
  1260. /* find CODEC from registered CODECs*/
  1261. list_for_each_entry(codec, &codec_list, list) {
  1262. if (!strcmp(codec->name, aux_dev->codec_name)) {
  1263. if (codec->probed) {
  1264. dev_err(codec->dev,
  1265. "ASoC: codec already probed");
  1266. ret = -EBUSY;
  1267. goto out;
  1268. }
  1269. goto found;
  1270. }
  1271. }
  1272. /* codec not found */
  1273. dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
  1274. return -EPROBE_DEFER;
  1275. found:
  1276. ret = soc_probe_codec(card, codec);
  1277. if (ret < 0)
  1278. return ret;
  1279. ret = soc_post_component_init(card, codec, num, 1);
  1280. out:
  1281. return ret;
  1282. }
  1283. static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
  1284. {
  1285. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1286. struct snd_soc_codec *codec = rtd->codec;
  1287. /* unregister the rtd device */
  1288. if (rtd->dev_registered) {
  1289. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  1290. device_del(rtd->dev);
  1291. rtd->dev_registered = 0;
  1292. }
  1293. if (codec && codec->probed)
  1294. soc_remove_codec(codec);
  1295. }
  1296. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
  1297. enum snd_soc_compress_type compress_type)
  1298. {
  1299. int ret;
  1300. if (codec->cache_init)
  1301. return 0;
  1302. /* override the compress_type if necessary */
  1303. if (compress_type && codec->compress_type != compress_type)
  1304. codec->compress_type = compress_type;
  1305. ret = snd_soc_cache_init(codec);
  1306. if (ret < 0) {
  1307. dev_err(codec->dev, "ASoC: Failed to set cache compression"
  1308. " type: %d\n", ret);
  1309. return ret;
  1310. }
  1311. codec->cache_init = 1;
  1312. return 0;
  1313. }
  1314. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1315. {
  1316. struct snd_soc_codec *codec;
  1317. struct snd_soc_codec_conf *codec_conf;
  1318. enum snd_soc_compress_type compress_type;
  1319. struct snd_soc_dai_link *dai_link;
  1320. int ret, i, order, dai_fmt;
  1321. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1322. /* bind DAIs */
  1323. for (i = 0; i < card->num_links; i++) {
  1324. ret = soc_bind_dai_link(card, i);
  1325. if (ret != 0)
  1326. goto base_error;
  1327. }
  1328. /* check aux_devs too */
  1329. for (i = 0; i < card->num_aux_devs; i++) {
  1330. ret = soc_check_aux_dev(card, i);
  1331. if (ret != 0)
  1332. goto base_error;
  1333. }
  1334. /* initialize the register cache for each available codec */
  1335. list_for_each_entry(codec, &codec_list, list) {
  1336. if (codec->cache_init)
  1337. continue;
  1338. /* by default we don't override the compress_type */
  1339. compress_type = 0;
  1340. /* check to see if we need to override the compress_type */
  1341. for (i = 0; i < card->num_configs; ++i) {
  1342. codec_conf = &card->codec_conf[i];
  1343. if (!strcmp(codec->name, codec_conf->dev_name)) {
  1344. compress_type = codec_conf->compress_type;
  1345. if (compress_type && compress_type
  1346. != codec->compress_type)
  1347. break;
  1348. }
  1349. }
  1350. ret = snd_soc_init_codec_cache(codec, compress_type);
  1351. if (ret < 0)
  1352. goto base_error;
  1353. }
  1354. /* card bind complete so register a sound card */
  1355. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1356. card->owner, 0, &card->snd_card);
  1357. if (ret < 0) {
  1358. dev_err(card->dev, "ASoC: can't create sound card for"
  1359. " card %s: %d\n", card->name, ret);
  1360. goto base_error;
  1361. }
  1362. card->snd_card->dev = card->dev;
  1363. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1364. card->dapm.dev = card->dev;
  1365. card->dapm.card = card;
  1366. list_add(&card->dapm.list, &card->dapm_list);
  1367. #ifdef CONFIG_DEBUG_FS
  1368. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1369. #endif
  1370. #ifdef CONFIG_PM_SLEEP
  1371. /* deferred resume work */
  1372. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1373. #endif
  1374. if (card->dapm_widgets)
  1375. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1376. card->num_dapm_widgets);
  1377. /* initialise the sound card only once */
  1378. if (card->probe) {
  1379. ret = card->probe(card);
  1380. if (ret < 0)
  1381. goto card_probe_error;
  1382. }
  1383. /* probe all components used by DAI links on this card */
  1384. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1385. order++) {
  1386. for (i = 0; i < card->num_links; i++) {
  1387. ret = soc_probe_link_components(card, i, order);
  1388. if (ret < 0) {
  1389. dev_err(card->dev,
  1390. "ASoC: failed to instantiate card %d\n",
  1391. ret);
  1392. goto probe_dai_err;
  1393. }
  1394. }
  1395. }
  1396. /* probe all DAI links on this card */
  1397. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1398. order++) {
  1399. for (i = 0; i < card->num_links; i++) {
  1400. ret = soc_probe_link_dais(card, i, order);
  1401. if (ret < 0) {
  1402. dev_err(card->dev,
  1403. "ASoC: failed to instantiate card %d\n",
  1404. ret);
  1405. goto probe_dai_err;
  1406. }
  1407. }
  1408. }
  1409. for (i = 0; i < card->num_aux_devs; i++) {
  1410. ret = soc_probe_aux_dev(card, i);
  1411. if (ret < 0) {
  1412. dev_err(card->dev,
  1413. "ASoC: failed to add auxiliary devices %d\n",
  1414. ret);
  1415. goto probe_aux_dev_err;
  1416. }
  1417. }
  1418. snd_soc_dapm_link_dai_widgets(card);
  1419. if (card->controls)
  1420. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1421. if (card->dapm_routes)
  1422. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1423. card->num_dapm_routes);
  1424. snd_soc_dapm_new_widgets(&card->dapm);
  1425. for (i = 0; i < card->num_links; i++) {
  1426. dai_link = &card->dai_link[i];
  1427. dai_fmt = dai_link->dai_fmt;
  1428. if (dai_fmt) {
  1429. ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
  1430. dai_fmt);
  1431. if (ret != 0 && ret != -ENOTSUPP)
  1432. dev_warn(card->rtd[i].codec_dai->dev,
  1433. "ASoC: Failed to set DAI format: %d\n",
  1434. ret);
  1435. }
  1436. /* If this is a regular CPU link there will be a platform */
  1437. if (dai_fmt &&
  1438. (dai_link->platform_name || dai_link->platform_of_node)) {
  1439. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1440. dai_fmt);
  1441. if (ret != 0 && ret != -ENOTSUPP)
  1442. dev_warn(card->rtd[i].cpu_dai->dev,
  1443. "ASoC: Failed to set DAI format: %d\n",
  1444. ret);
  1445. } else if (dai_fmt) {
  1446. /* Flip the polarity for the "CPU" end */
  1447. dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  1448. switch (dai_link->dai_fmt &
  1449. SND_SOC_DAIFMT_MASTER_MASK) {
  1450. case SND_SOC_DAIFMT_CBM_CFM:
  1451. dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1452. break;
  1453. case SND_SOC_DAIFMT_CBM_CFS:
  1454. dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1455. break;
  1456. case SND_SOC_DAIFMT_CBS_CFM:
  1457. dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1458. break;
  1459. case SND_SOC_DAIFMT_CBS_CFS:
  1460. dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1461. break;
  1462. }
  1463. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1464. dai_fmt);
  1465. if (ret != 0 && ret != -ENOTSUPP)
  1466. dev_warn(card->rtd[i].cpu_dai->dev,
  1467. "ASoC: Failed to set DAI format: %d\n",
  1468. ret);
  1469. }
  1470. }
  1471. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1472. "%s", card->name);
  1473. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1474. "%s", card->long_name ? card->long_name : card->name);
  1475. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1476. "%s", card->driver_name ? card->driver_name : card->name);
  1477. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1478. switch (card->snd_card->driver[i]) {
  1479. case '_':
  1480. case '-':
  1481. case '\0':
  1482. break;
  1483. default:
  1484. if (!isalnum(card->snd_card->driver[i]))
  1485. card->snd_card->driver[i] = '_';
  1486. break;
  1487. }
  1488. }
  1489. if (card->late_probe) {
  1490. ret = card->late_probe(card);
  1491. if (ret < 0) {
  1492. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1493. card->name, ret);
  1494. goto probe_aux_dev_err;
  1495. }
  1496. }
  1497. snd_soc_dapm_new_widgets(&card->dapm);
  1498. if (card->fully_routed)
  1499. list_for_each_entry(codec, &card->codec_dev_list, card_list)
  1500. snd_soc_dapm_auto_nc_codec_pins(codec);
  1501. ret = snd_card_register(card->snd_card);
  1502. if (ret < 0) {
  1503. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1504. ret);
  1505. goto probe_aux_dev_err;
  1506. }
  1507. #ifdef CONFIG_SND_SOC_AC97_BUS
  1508. /* register any AC97 codecs */
  1509. for (i = 0; i < card->num_rtd; i++) {
  1510. ret = soc_register_ac97_dai_link(&card->rtd[i]);
  1511. if (ret < 0) {
  1512. dev_err(card->dev, "ASoC: failed to register AC97:"
  1513. " %d\n", ret);
  1514. while (--i >= 0)
  1515. soc_unregister_ac97_dai_link(card->rtd[i].codec);
  1516. goto probe_aux_dev_err;
  1517. }
  1518. }
  1519. #endif
  1520. card->instantiated = 1;
  1521. snd_soc_dapm_sync(&card->dapm);
  1522. mutex_unlock(&card->mutex);
  1523. return 0;
  1524. probe_aux_dev_err:
  1525. for (i = 0; i < card->num_aux_devs; i++)
  1526. soc_remove_aux_dev(card, i);
  1527. probe_dai_err:
  1528. soc_remove_dai_links(card);
  1529. card_probe_error:
  1530. if (card->remove)
  1531. card->remove(card);
  1532. snd_card_free(card->snd_card);
  1533. base_error:
  1534. mutex_unlock(&card->mutex);
  1535. return ret;
  1536. }
  1537. /* probes a new socdev */
  1538. static int soc_probe(struct platform_device *pdev)
  1539. {
  1540. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1541. /*
  1542. * no card, so machine driver should be registering card
  1543. * we should not be here in that case so ret error
  1544. */
  1545. if (!card)
  1546. return -EINVAL;
  1547. dev_warn(&pdev->dev,
  1548. "ASoC: machine %s should use snd_soc_register_card()\n",
  1549. card->name);
  1550. /* Bodge while we unpick instantiation */
  1551. card->dev = &pdev->dev;
  1552. return snd_soc_register_card(card);
  1553. }
  1554. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1555. {
  1556. int i;
  1557. /* make sure any delayed work runs */
  1558. for (i = 0; i < card->num_rtd; i++) {
  1559. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1560. flush_delayed_work(&rtd->delayed_work);
  1561. }
  1562. /* remove auxiliary devices */
  1563. for (i = 0; i < card->num_aux_devs; i++)
  1564. soc_remove_aux_dev(card, i);
  1565. /* remove and free each DAI */
  1566. soc_remove_dai_links(card);
  1567. soc_cleanup_card_debugfs(card);
  1568. /* remove the card */
  1569. if (card->remove)
  1570. card->remove(card);
  1571. snd_soc_dapm_free(&card->dapm);
  1572. snd_card_free(card->snd_card);
  1573. return 0;
  1574. }
  1575. /* removes a socdev */
  1576. static int soc_remove(struct platform_device *pdev)
  1577. {
  1578. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1579. snd_soc_unregister_card(card);
  1580. return 0;
  1581. }
  1582. int snd_soc_poweroff(struct device *dev)
  1583. {
  1584. struct snd_soc_card *card = dev_get_drvdata(dev);
  1585. int i;
  1586. if (!card->instantiated)
  1587. return 0;
  1588. /* Flush out pmdown_time work - we actually do want to run it
  1589. * now, we're shutting down so no imminent restart. */
  1590. for (i = 0; i < card->num_rtd; i++) {
  1591. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1592. flush_delayed_work(&rtd->delayed_work);
  1593. }
  1594. snd_soc_dapm_shutdown(card);
  1595. return 0;
  1596. }
  1597. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1598. const struct dev_pm_ops snd_soc_pm_ops = {
  1599. .suspend = snd_soc_suspend,
  1600. .resume = snd_soc_resume,
  1601. .freeze = snd_soc_suspend,
  1602. .thaw = snd_soc_resume,
  1603. .poweroff = snd_soc_poweroff,
  1604. .restore = snd_soc_resume,
  1605. };
  1606. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1607. /* ASoC platform driver */
  1608. static struct platform_driver soc_driver = {
  1609. .driver = {
  1610. .name = "soc-audio",
  1611. .owner = THIS_MODULE,
  1612. .pm = &snd_soc_pm_ops,
  1613. },
  1614. .probe = soc_probe,
  1615. .remove = soc_remove,
  1616. };
  1617. /**
  1618. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1619. *
  1620. * @codec: CODEC to query.
  1621. * @reg: Register to query.
  1622. *
  1623. * Boolean function indiciating if a CODEC register is volatile.
  1624. */
  1625. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1626. unsigned int reg)
  1627. {
  1628. if (codec->volatile_register)
  1629. return codec->volatile_register(codec, reg);
  1630. else
  1631. return 0;
  1632. }
  1633. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1634. /**
  1635. * snd_soc_codec_readable_register: Report if a register is readable.
  1636. *
  1637. * @codec: CODEC to query.
  1638. * @reg: Register to query.
  1639. *
  1640. * Boolean function indicating if a CODEC register is readable.
  1641. */
  1642. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1643. unsigned int reg)
  1644. {
  1645. if (codec->readable_register)
  1646. return codec->readable_register(codec, reg);
  1647. else
  1648. return 1;
  1649. }
  1650. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1651. /**
  1652. * snd_soc_codec_writable_register: Report if a register is writable.
  1653. *
  1654. * @codec: CODEC to query.
  1655. * @reg: Register to query.
  1656. *
  1657. * Boolean function indicating if a CODEC register is writable.
  1658. */
  1659. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1660. unsigned int reg)
  1661. {
  1662. if (codec->writable_register)
  1663. return codec->writable_register(codec, reg);
  1664. else
  1665. return 1;
  1666. }
  1667. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1668. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1669. unsigned int reg)
  1670. {
  1671. unsigned int ret;
  1672. if (!platform->driver->read) {
  1673. dev_err(platform->dev, "ASoC: platform has no read back\n");
  1674. return -1;
  1675. }
  1676. ret = platform->driver->read(platform, reg);
  1677. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1678. trace_snd_soc_preg_read(platform, reg, ret);
  1679. return ret;
  1680. }
  1681. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1682. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1683. unsigned int reg, unsigned int val)
  1684. {
  1685. if (!platform->driver->write) {
  1686. dev_err(platform->dev, "ASoC: platform has no write back\n");
  1687. return -1;
  1688. }
  1689. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1690. trace_snd_soc_preg_write(platform, reg, val);
  1691. return platform->driver->write(platform, reg, val);
  1692. }
  1693. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1694. /**
  1695. * snd_soc_new_ac97_codec - initailise AC97 device
  1696. * @codec: audio codec
  1697. * @ops: AC97 bus operations
  1698. * @num: AC97 codec number
  1699. *
  1700. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1701. */
  1702. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1703. struct snd_ac97_bus_ops *ops, int num)
  1704. {
  1705. mutex_lock(&codec->mutex);
  1706. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1707. if (codec->ac97 == NULL) {
  1708. mutex_unlock(&codec->mutex);
  1709. return -ENOMEM;
  1710. }
  1711. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1712. if (codec->ac97->bus == NULL) {
  1713. kfree(codec->ac97);
  1714. codec->ac97 = NULL;
  1715. mutex_unlock(&codec->mutex);
  1716. return -ENOMEM;
  1717. }
  1718. codec->ac97->bus->ops = ops;
  1719. codec->ac97->num = num;
  1720. /*
  1721. * Mark the AC97 device to be created by us. This way we ensure that the
  1722. * device will be registered with the device subsystem later on.
  1723. */
  1724. codec->ac97_created = 1;
  1725. mutex_unlock(&codec->mutex);
  1726. return 0;
  1727. }
  1728. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1729. /**
  1730. * snd_soc_free_ac97_codec - free AC97 codec device
  1731. * @codec: audio codec
  1732. *
  1733. * Frees AC97 codec device resources.
  1734. */
  1735. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1736. {
  1737. mutex_lock(&codec->mutex);
  1738. #ifdef CONFIG_SND_SOC_AC97_BUS
  1739. soc_unregister_ac97_dai_link(codec);
  1740. #endif
  1741. kfree(codec->ac97->bus);
  1742. kfree(codec->ac97);
  1743. codec->ac97 = NULL;
  1744. codec->ac97_created = 0;
  1745. mutex_unlock(&codec->mutex);
  1746. }
  1747. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1748. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1749. {
  1750. unsigned int ret;
  1751. ret = codec->read(codec, reg);
  1752. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1753. trace_snd_soc_reg_read(codec, reg, ret);
  1754. return ret;
  1755. }
  1756. EXPORT_SYMBOL_GPL(snd_soc_read);
  1757. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1758. unsigned int reg, unsigned int val)
  1759. {
  1760. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1761. trace_snd_soc_reg_write(codec, reg, val);
  1762. return codec->write(codec, reg, val);
  1763. }
  1764. EXPORT_SYMBOL_GPL(snd_soc_write);
  1765. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1766. unsigned int reg, const void *data, size_t len)
  1767. {
  1768. return codec->bulk_write_raw(codec, reg, data, len);
  1769. }
  1770. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1771. /**
  1772. * snd_soc_update_bits - update codec register bits
  1773. * @codec: audio codec
  1774. * @reg: codec register
  1775. * @mask: register mask
  1776. * @value: new value
  1777. *
  1778. * Writes new register value.
  1779. *
  1780. * Returns 1 for change, 0 for no change, or negative error code.
  1781. */
  1782. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1783. unsigned int mask, unsigned int value)
  1784. {
  1785. bool change;
  1786. unsigned int old, new;
  1787. int ret;
  1788. if (codec->using_regmap) {
  1789. ret = regmap_update_bits_check(codec->control_data, reg,
  1790. mask, value, &change);
  1791. } else {
  1792. ret = snd_soc_read(codec, reg);
  1793. if (ret < 0)
  1794. return ret;
  1795. old = ret;
  1796. new = (old & ~mask) | (value & mask);
  1797. change = old != new;
  1798. if (change)
  1799. ret = snd_soc_write(codec, reg, new);
  1800. }
  1801. if (ret < 0)
  1802. return ret;
  1803. return change;
  1804. }
  1805. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1806. /**
  1807. * snd_soc_update_bits_locked - update codec register bits
  1808. * @codec: audio codec
  1809. * @reg: codec register
  1810. * @mask: register mask
  1811. * @value: new value
  1812. *
  1813. * Writes new register value, and takes the codec mutex.
  1814. *
  1815. * Returns 1 for change else 0.
  1816. */
  1817. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1818. unsigned short reg, unsigned int mask,
  1819. unsigned int value)
  1820. {
  1821. int change;
  1822. mutex_lock(&codec->mutex);
  1823. change = snd_soc_update_bits(codec, reg, mask, value);
  1824. mutex_unlock(&codec->mutex);
  1825. return change;
  1826. }
  1827. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1828. /**
  1829. * snd_soc_test_bits - test register for change
  1830. * @codec: audio codec
  1831. * @reg: codec register
  1832. * @mask: register mask
  1833. * @value: new value
  1834. *
  1835. * Tests a register with a new value and checks if the new value is
  1836. * different from the old value.
  1837. *
  1838. * Returns 1 for change else 0.
  1839. */
  1840. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1841. unsigned int mask, unsigned int value)
  1842. {
  1843. int change;
  1844. unsigned int old, new;
  1845. old = snd_soc_read(codec, reg);
  1846. new = (old & ~mask) | value;
  1847. change = old != new;
  1848. return change;
  1849. }
  1850. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1851. /**
  1852. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1853. * @substream: the pcm substream
  1854. * @hw: the hardware parameters
  1855. *
  1856. * Sets the substream runtime hardware parameters.
  1857. */
  1858. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1859. const struct snd_pcm_hardware *hw)
  1860. {
  1861. struct snd_pcm_runtime *runtime = substream->runtime;
  1862. runtime->hw.info = hw->info;
  1863. runtime->hw.formats = hw->formats;
  1864. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1865. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1866. runtime->hw.periods_min = hw->periods_min;
  1867. runtime->hw.periods_max = hw->periods_max;
  1868. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1869. runtime->hw.fifo_size = hw->fifo_size;
  1870. return 0;
  1871. }
  1872. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1873. /**
  1874. * snd_soc_cnew - create new control
  1875. * @_template: control template
  1876. * @data: control private data
  1877. * @long_name: control long name
  1878. * @prefix: control name prefix
  1879. *
  1880. * Create a new mixer control from a template control.
  1881. *
  1882. * Returns 0 for success, else error.
  1883. */
  1884. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1885. void *data, const char *long_name,
  1886. const char *prefix)
  1887. {
  1888. struct snd_kcontrol_new template;
  1889. struct snd_kcontrol *kcontrol;
  1890. char *name = NULL;
  1891. int name_len;
  1892. memcpy(&template, _template, sizeof(template));
  1893. template.index = 0;
  1894. if (!long_name)
  1895. long_name = template.name;
  1896. if (prefix) {
  1897. name_len = strlen(long_name) + strlen(prefix) + 2;
  1898. name = kmalloc(name_len, GFP_KERNEL);
  1899. if (!name)
  1900. return NULL;
  1901. snprintf(name, name_len, "%s %s", prefix, long_name);
  1902. template.name = name;
  1903. } else {
  1904. template.name = long_name;
  1905. }
  1906. kcontrol = snd_ctl_new1(&template, data);
  1907. kfree(name);
  1908. return kcontrol;
  1909. }
  1910. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1911. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1912. const struct snd_kcontrol_new *controls, int num_controls,
  1913. const char *prefix, void *data)
  1914. {
  1915. int err, i;
  1916. for (i = 0; i < num_controls; i++) {
  1917. const struct snd_kcontrol_new *control = &controls[i];
  1918. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1919. control->name, prefix));
  1920. if (err < 0) {
  1921. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1922. control->name, err);
  1923. return err;
  1924. }
  1925. }
  1926. return 0;
  1927. }
  1928. /**
  1929. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1930. * Convenience function to add a list of controls. Many codecs were
  1931. * duplicating this code.
  1932. *
  1933. * @codec: codec to add controls to
  1934. * @controls: array of controls to add
  1935. * @num_controls: number of elements in the array
  1936. *
  1937. * Return 0 for success, else error.
  1938. */
  1939. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1940. const struct snd_kcontrol_new *controls, int num_controls)
  1941. {
  1942. struct snd_card *card = codec->card->snd_card;
  1943. return snd_soc_add_controls(card, codec->dev, controls, num_controls,
  1944. codec->name_prefix, codec);
  1945. }
  1946. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1947. /**
  1948. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1949. * Convenience function to add a list of controls.
  1950. *
  1951. * @platform: platform to add controls to
  1952. * @controls: array of controls to add
  1953. * @num_controls: number of elements in the array
  1954. *
  1955. * Return 0 for success, else error.
  1956. */
  1957. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1958. const struct snd_kcontrol_new *controls, int num_controls)
  1959. {
  1960. struct snd_card *card = platform->card->snd_card;
  1961. return snd_soc_add_controls(card, platform->dev, controls, num_controls,
  1962. NULL, platform);
  1963. }
  1964. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1965. /**
  1966. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1967. * Convenience function to add a list of controls.
  1968. *
  1969. * @soc_card: SoC card to add controls to
  1970. * @controls: array of controls to add
  1971. * @num_controls: number of elements in the array
  1972. *
  1973. * Return 0 for success, else error.
  1974. */
  1975. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1976. const struct snd_kcontrol_new *controls, int num_controls)
  1977. {
  1978. struct snd_card *card = soc_card->snd_card;
  1979. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1980. NULL, soc_card);
  1981. }
  1982. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1983. /**
  1984. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1985. * Convienience function to add a list of controls.
  1986. *
  1987. * @dai: DAI to add controls to
  1988. * @controls: array of controls to add
  1989. * @num_controls: number of elements in the array
  1990. *
  1991. * Return 0 for success, else error.
  1992. */
  1993. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1994. const struct snd_kcontrol_new *controls, int num_controls)
  1995. {
  1996. struct snd_card *card = dai->card->snd_card;
  1997. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  1998. NULL, dai);
  1999. }
  2000. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  2001. /**
  2002. * snd_soc_info_enum_double - enumerated double mixer info callback
  2003. * @kcontrol: mixer control
  2004. * @uinfo: control element information
  2005. *
  2006. * Callback to provide information about a double enumerated
  2007. * mixer control.
  2008. *
  2009. * Returns 0 for success.
  2010. */
  2011. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  2012. struct snd_ctl_elem_info *uinfo)
  2013. {
  2014. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2015. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2016. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  2017. uinfo->value.enumerated.items = e->max;
  2018. if (uinfo->value.enumerated.item > e->max - 1)
  2019. uinfo->value.enumerated.item = e->max - 1;
  2020. strcpy(uinfo->value.enumerated.name,
  2021. e->texts[uinfo->value.enumerated.item]);
  2022. return 0;
  2023. }
  2024. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  2025. /**
  2026. * snd_soc_get_enum_double - enumerated double mixer get callback
  2027. * @kcontrol: mixer control
  2028. * @ucontrol: control element information
  2029. *
  2030. * Callback to get the value of a double enumerated mixer.
  2031. *
  2032. * Returns 0 for success.
  2033. */
  2034. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  2035. struct snd_ctl_elem_value *ucontrol)
  2036. {
  2037. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2038. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2039. unsigned int val;
  2040. val = snd_soc_read(codec, e->reg);
  2041. ucontrol->value.enumerated.item[0]
  2042. = (val >> e->shift_l) & e->mask;
  2043. if (e->shift_l != e->shift_r)
  2044. ucontrol->value.enumerated.item[1] =
  2045. (val >> e->shift_r) & e->mask;
  2046. return 0;
  2047. }
  2048. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  2049. /**
  2050. * snd_soc_put_enum_double - enumerated double mixer put callback
  2051. * @kcontrol: mixer control
  2052. * @ucontrol: control element information
  2053. *
  2054. * Callback to set the value of a double enumerated mixer.
  2055. *
  2056. * Returns 0 for success.
  2057. */
  2058. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  2059. struct snd_ctl_elem_value *ucontrol)
  2060. {
  2061. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2062. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2063. unsigned int val;
  2064. unsigned int mask;
  2065. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2066. return -EINVAL;
  2067. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  2068. mask = e->mask << e->shift_l;
  2069. if (e->shift_l != e->shift_r) {
  2070. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2071. return -EINVAL;
  2072. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2073. mask |= e->mask << e->shift_r;
  2074. }
  2075. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2076. }
  2077. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  2078. /**
  2079. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  2080. * @kcontrol: mixer control
  2081. * @ucontrol: control element information
  2082. *
  2083. * Callback to get the value of a double semi enumerated mixer.
  2084. *
  2085. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2086. * used for handling bitfield coded enumeration for example.
  2087. *
  2088. * Returns 0 for success.
  2089. */
  2090. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2091. struct snd_ctl_elem_value *ucontrol)
  2092. {
  2093. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2094. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2095. unsigned int reg_val, val, mux;
  2096. reg_val = snd_soc_read(codec, e->reg);
  2097. val = (reg_val >> e->shift_l) & e->mask;
  2098. for (mux = 0; mux < e->max; mux++) {
  2099. if (val == e->values[mux])
  2100. break;
  2101. }
  2102. ucontrol->value.enumerated.item[0] = mux;
  2103. if (e->shift_l != e->shift_r) {
  2104. val = (reg_val >> e->shift_r) & e->mask;
  2105. for (mux = 0; mux < e->max; mux++) {
  2106. if (val == e->values[mux])
  2107. break;
  2108. }
  2109. ucontrol->value.enumerated.item[1] = mux;
  2110. }
  2111. return 0;
  2112. }
  2113. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  2114. /**
  2115. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  2116. * @kcontrol: mixer control
  2117. * @ucontrol: control element information
  2118. *
  2119. * Callback to set the value of a double semi enumerated mixer.
  2120. *
  2121. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2122. * used for handling bitfield coded enumeration for example.
  2123. *
  2124. * Returns 0 for success.
  2125. */
  2126. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2127. struct snd_ctl_elem_value *ucontrol)
  2128. {
  2129. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2130. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2131. unsigned int val;
  2132. unsigned int mask;
  2133. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2134. return -EINVAL;
  2135. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2136. mask = e->mask << e->shift_l;
  2137. if (e->shift_l != e->shift_r) {
  2138. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2139. return -EINVAL;
  2140. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2141. mask |= e->mask << e->shift_r;
  2142. }
  2143. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2144. }
  2145. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  2146. /**
  2147. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  2148. * @kcontrol: mixer control
  2149. * @uinfo: control element information
  2150. *
  2151. * Callback to provide information about an external enumerated
  2152. * single mixer.
  2153. *
  2154. * Returns 0 for success.
  2155. */
  2156. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  2157. struct snd_ctl_elem_info *uinfo)
  2158. {
  2159. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2160. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2161. uinfo->count = 1;
  2162. uinfo->value.enumerated.items = e->max;
  2163. if (uinfo->value.enumerated.item > e->max - 1)
  2164. uinfo->value.enumerated.item = e->max - 1;
  2165. strcpy(uinfo->value.enumerated.name,
  2166. e->texts[uinfo->value.enumerated.item]);
  2167. return 0;
  2168. }
  2169. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  2170. /**
  2171. * snd_soc_info_volsw_ext - external single mixer info callback
  2172. * @kcontrol: mixer control
  2173. * @uinfo: control element information
  2174. *
  2175. * Callback to provide information about a single external mixer control.
  2176. *
  2177. * Returns 0 for success.
  2178. */
  2179. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  2180. struct snd_ctl_elem_info *uinfo)
  2181. {
  2182. int max = kcontrol->private_value;
  2183. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2184. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2185. else
  2186. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2187. uinfo->count = 1;
  2188. uinfo->value.integer.min = 0;
  2189. uinfo->value.integer.max = max;
  2190. return 0;
  2191. }
  2192. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  2193. /**
  2194. * snd_soc_info_volsw - single mixer info callback
  2195. * @kcontrol: mixer control
  2196. * @uinfo: control element information
  2197. *
  2198. * Callback to provide information about a single mixer control, or a double
  2199. * mixer control that spans 2 registers.
  2200. *
  2201. * Returns 0 for success.
  2202. */
  2203. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  2204. struct snd_ctl_elem_info *uinfo)
  2205. {
  2206. struct soc_mixer_control *mc =
  2207. (struct soc_mixer_control *)kcontrol->private_value;
  2208. int platform_max;
  2209. if (!mc->platform_max)
  2210. mc->platform_max = mc->max;
  2211. platform_max = mc->platform_max;
  2212. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2213. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2214. else
  2215. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2216. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2217. uinfo->value.integer.min = 0;
  2218. uinfo->value.integer.max = platform_max;
  2219. return 0;
  2220. }
  2221. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  2222. /**
  2223. * snd_soc_get_volsw - single mixer get callback
  2224. * @kcontrol: mixer control
  2225. * @ucontrol: control element information
  2226. *
  2227. * Callback to get the value of a single mixer control, or a double mixer
  2228. * control that spans 2 registers.
  2229. *
  2230. * Returns 0 for success.
  2231. */
  2232. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  2233. struct snd_ctl_elem_value *ucontrol)
  2234. {
  2235. struct soc_mixer_control *mc =
  2236. (struct soc_mixer_control *)kcontrol->private_value;
  2237. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2238. unsigned int reg = mc->reg;
  2239. unsigned int reg2 = mc->rreg;
  2240. unsigned int shift = mc->shift;
  2241. unsigned int rshift = mc->rshift;
  2242. int max = mc->max;
  2243. unsigned int mask = (1 << fls(max)) - 1;
  2244. unsigned int invert = mc->invert;
  2245. ucontrol->value.integer.value[0] =
  2246. (snd_soc_read(codec, reg) >> shift) & mask;
  2247. if (invert)
  2248. ucontrol->value.integer.value[0] =
  2249. max - ucontrol->value.integer.value[0];
  2250. if (snd_soc_volsw_is_stereo(mc)) {
  2251. if (reg == reg2)
  2252. ucontrol->value.integer.value[1] =
  2253. (snd_soc_read(codec, reg) >> rshift) & mask;
  2254. else
  2255. ucontrol->value.integer.value[1] =
  2256. (snd_soc_read(codec, reg2) >> shift) & mask;
  2257. if (invert)
  2258. ucontrol->value.integer.value[1] =
  2259. max - ucontrol->value.integer.value[1];
  2260. }
  2261. return 0;
  2262. }
  2263. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2264. /**
  2265. * snd_soc_put_volsw - single mixer put callback
  2266. * @kcontrol: mixer control
  2267. * @ucontrol: control element information
  2268. *
  2269. * Callback to set the value of a single mixer control, or a double mixer
  2270. * control that spans 2 registers.
  2271. *
  2272. * Returns 0 for success.
  2273. */
  2274. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2275. struct snd_ctl_elem_value *ucontrol)
  2276. {
  2277. struct soc_mixer_control *mc =
  2278. (struct soc_mixer_control *)kcontrol->private_value;
  2279. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2280. unsigned int reg = mc->reg;
  2281. unsigned int reg2 = mc->rreg;
  2282. unsigned int shift = mc->shift;
  2283. unsigned int rshift = mc->rshift;
  2284. int max = mc->max;
  2285. unsigned int mask = (1 << fls(max)) - 1;
  2286. unsigned int invert = mc->invert;
  2287. int err;
  2288. bool type_2r = 0;
  2289. unsigned int val2 = 0;
  2290. unsigned int val, val_mask;
  2291. val = (ucontrol->value.integer.value[0] & mask);
  2292. if (invert)
  2293. val = max - val;
  2294. val_mask = mask << shift;
  2295. val = val << shift;
  2296. if (snd_soc_volsw_is_stereo(mc)) {
  2297. val2 = (ucontrol->value.integer.value[1] & mask);
  2298. if (invert)
  2299. val2 = max - val2;
  2300. if (reg == reg2) {
  2301. val_mask |= mask << rshift;
  2302. val |= val2 << rshift;
  2303. } else {
  2304. val2 = val2 << shift;
  2305. type_2r = 1;
  2306. }
  2307. }
  2308. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2309. if (err < 0)
  2310. return err;
  2311. if (type_2r)
  2312. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2313. return err;
  2314. }
  2315. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2316. /**
  2317. * snd_soc_get_volsw_sx - single mixer get callback
  2318. * @kcontrol: mixer control
  2319. * @ucontrol: control element information
  2320. *
  2321. * Callback to get the value of a single mixer control, or a double mixer
  2322. * control that spans 2 registers.
  2323. *
  2324. * Returns 0 for success.
  2325. */
  2326. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  2327. struct snd_ctl_elem_value *ucontrol)
  2328. {
  2329. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2330. struct soc_mixer_control *mc =
  2331. (struct soc_mixer_control *)kcontrol->private_value;
  2332. unsigned int reg = mc->reg;
  2333. unsigned int reg2 = mc->rreg;
  2334. unsigned int shift = mc->shift;
  2335. unsigned int rshift = mc->rshift;
  2336. int max = mc->max;
  2337. int min = mc->min;
  2338. int mask = (1 << (fls(min + max) - 1)) - 1;
  2339. ucontrol->value.integer.value[0] =
  2340. ((snd_soc_read(codec, reg) >> shift) - min) & mask;
  2341. if (snd_soc_volsw_is_stereo(mc))
  2342. ucontrol->value.integer.value[1] =
  2343. ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
  2344. return 0;
  2345. }
  2346. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  2347. /**
  2348. * snd_soc_put_volsw_sx - double mixer set callback
  2349. * @kcontrol: mixer control
  2350. * @uinfo: control element information
  2351. *
  2352. * Callback to set the value of a double mixer control that spans 2 registers.
  2353. *
  2354. * Returns 0 for success.
  2355. */
  2356. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  2357. struct snd_ctl_elem_value *ucontrol)
  2358. {
  2359. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2360. struct soc_mixer_control *mc =
  2361. (struct soc_mixer_control *)kcontrol->private_value;
  2362. unsigned int reg = mc->reg;
  2363. unsigned int reg2 = mc->rreg;
  2364. unsigned int shift = mc->shift;
  2365. unsigned int rshift = mc->rshift;
  2366. int max = mc->max;
  2367. int min = mc->min;
  2368. int mask = (1 << (fls(min + max) - 1)) - 1;
  2369. int err = 0;
  2370. unsigned short val, val_mask, val2 = 0;
  2371. val_mask = mask << shift;
  2372. val = (ucontrol->value.integer.value[0] + min) & mask;
  2373. val = val << shift;
  2374. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2375. if (err < 0)
  2376. return err;
  2377. if (snd_soc_volsw_is_stereo(mc)) {
  2378. val_mask = mask << rshift;
  2379. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  2380. val2 = val2 << rshift;
  2381. if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
  2382. return err;
  2383. }
  2384. return 0;
  2385. }
  2386. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  2387. /**
  2388. * snd_soc_info_volsw_s8 - signed mixer info callback
  2389. * @kcontrol: mixer control
  2390. * @uinfo: control element information
  2391. *
  2392. * Callback to provide information about a signed mixer control.
  2393. *
  2394. * Returns 0 for success.
  2395. */
  2396. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2397. struct snd_ctl_elem_info *uinfo)
  2398. {
  2399. struct soc_mixer_control *mc =
  2400. (struct soc_mixer_control *)kcontrol->private_value;
  2401. int platform_max;
  2402. int min = mc->min;
  2403. if (!mc->platform_max)
  2404. mc->platform_max = mc->max;
  2405. platform_max = mc->platform_max;
  2406. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2407. uinfo->count = 2;
  2408. uinfo->value.integer.min = 0;
  2409. uinfo->value.integer.max = platform_max - min;
  2410. return 0;
  2411. }
  2412. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2413. /**
  2414. * snd_soc_get_volsw_s8 - signed mixer get callback
  2415. * @kcontrol: mixer control
  2416. * @ucontrol: control element information
  2417. *
  2418. * Callback to get the value of a signed mixer control.
  2419. *
  2420. * Returns 0 for success.
  2421. */
  2422. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2423. struct snd_ctl_elem_value *ucontrol)
  2424. {
  2425. struct soc_mixer_control *mc =
  2426. (struct soc_mixer_control *)kcontrol->private_value;
  2427. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2428. unsigned int reg = mc->reg;
  2429. int min = mc->min;
  2430. int val = snd_soc_read(codec, reg);
  2431. ucontrol->value.integer.value[0] =
  2432. ((signed char)(val & 0xff))-min;
  2433. ucontrol->value.integer.value[1] =
  2434. ((signed char)((val >> 8) & 0xff))-min;
  2435. return 0;
  2436. }
  2437. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2438. /**
  2439. * snd_soc_put_volsw_sgn - signed mixer put callback
  2440. * @kcontrol: mixer control
  2441. * @ucontrol: control element information
  2442. *
  2443. * Callback to set the value of a signed mixer control.
  2444. *
  2445. * Returns 0 for success.
  2446. */
  2447. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2448. struct snd_ctl_elem_value *ucontrol)
  2449. {
  2450. struct soc_mixer_control *mc =
  2451. (struct soc_mixer_control *)kcontrol->private_value;
  2452. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2453. unsigned int reg = mc->reg;
  2454. int min = mc->min;
  2455. unsigned int val;
  2456. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2457. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2458. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2459. }
  2460. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2461. /**
  2462. * snd_soc_info_volsw_range - single mixer info callback with range.
  2463. * @kcontrol: mixer control
  2464. * @uinfo: control element information
  2465. *
  2466. * Callback to provide information, within a range, about a single
  2467. * mixer control.
  2468. *
  2469. * returns 0 for success.
  2470. */
  2471. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  2472. struct snd_ctl_elem_info *uinfo)
  2473. {
  2474. struct soc_mixer_control *mc =
  2475. (struct soc_mixer_control *)kcontrol->private_value;
  2476. int platform_max;
  2477. int min = mc->min;
  2478. if (!mc->platform_max)
  2479. mc->platform_max = mc->max;
  2480. platform_max = mc->platform_max;
  2481. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2482. uinfo->count = 1;
  2483. uinfo->value.integer.min = 0;
  2484. uinfo->value.integer.max = platform_max - min;
  2485. return 0;
  2486. }
  2487. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  2488. /**
  2489. * snd_soc_put_volsw_range - single mixer put value callback with range.
  2490. * @kcontrol: mixer control
  2491. * @ucontrol: control element information
  2492. *
  2493. * Callback to set the value, within a range, for a single mixer control.
  2494. *
  2495. * Returns 0 for success.
  2496. */
  2497. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  2498. struct snd_ctl_elem_value *ucontrol)
  2499. {
  2500. struct soc_mixer_control *mc =
  2501. (struct soc_mixer_control *)kcontrol->private_value;
  2502. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2503. unsigned int reg = mc->reg;
  2504. unsigned int shift = mc->shift;
  2505. int min = mc->min;
  2506. int max = mc->max;
  2507. unsigned int mask = (1 << fls(max)) - 1;
  2508. unsigned int invert = mc->invert;
  2509. unsigned int val, val_mask;
  2510. val = ((ucontrol->value.integer.value[0] + min) & mask);
  2511. if (invert)
  2512. val = max - val;
  2513. val_mask = mask << shift;
  2514. val = val << shift;
  2515. return snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2516. }
  2517. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  2518. /**
  2519. * snd_soc_get_volsw_range - single mixer get callback with range
  2520. * @kcontrol: mixer control
  2521. * @ucontrol: control element information
  2522. *
  2523. * Callback to get the value, within a range, of a single mixer control.
  2524. *
  2525. * Returns 0 for success.
  2526. */
  2527. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  2528. struct snd_ctl_elem_value *ucontrol)
  2529. {
  2530. struct soc_mixer_control *mc =
  2531. (struct soc_mixer_control *)kcontrol->private_value;
  2532. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2533. unsigned int reg = mc->reg;
  2534. unsigned int shift = mc->shift;
  2535. int min = mc->min;
  2536. int max = mc->max;
  2537. unsigned int mask = (1 << fls(max)) - 1;
  2538. unsigned int invert = mc->invert;
  2539. ucontrol->value.integer.value[0] =
  2540. (snd_soc_read(codec, reg) >> shift) & mask;
  2541. if (invert)
  2542. ucontrol->value.integer.value[0] =
  2543. max - ucontrol->value.integer.value[0];
  2544. ucontrol->value.integer.value[0] =
  2545. ucontrol->value.integer.value[0] - min;
  2546. return 0;
  2547. }
  2548. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  2549. /**
  2550. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2551. *
  2552. * @codec: where to look for the control
  2553. * @name: Name of the control
  2554. * @max: new maximum limit
  2555. *
  2556. * Return 0 for success, else error.
  2557. */
  2558. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2559. const char *name, int max)
  2560. {
  2561. struct snd_card *card = codec->card->snd_card;
  2562. struct snd_kcontrol *kctl;
  2563. struct soc_mixer_control *mc;
  2564. int found = 0;
  2565. int ret = -EINVAL;
  2566. /* Sanity check for name and max */
  2567. if (unlikely(!name || max <= 0))
  2568. return -EINVAL;
  2569. list_for_each_entry(kctl, &card->controls, list) {
  2570. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2571. found = 1;
  2572. break;
  2573. }
  2574. }
  2575. if (found) {
  2576. mc = (struct soc_mixer_control *)kctl->private_value;
  2577. if (max <= mc->max) {
  2578. mc->platform_max = max;
  2579. ret = 0;
  2580. }
  2581. }
  2582. return ret;
  2583. }
  2584. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2585. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  2586. struct snd_ctl_elem_info *uinfo)
  2587. {
  2588. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2589. struct soc_bytes *params = (void *)kcontrol->private_value;
  2590. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  2591. uinfo->count = params->num_regs * codec->val_bytes;
  2592. return 0;
  2593. }
  2594. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  2595. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  2596. struct snd_ctl_elem_value *ucontrol)
  2597. {
  2598. struct soc_bytes *params = (void *)kcontrol->private_value;
  2599. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2600. int ret;
  2601. if (codec->using_regmap)
  2602. ret = regmap_raw_read(codec->control_data, params->base,
  2603. ucontrol->value.bytes.data,
  2604. params->num_regs * codec->val_bytes);
  2605. else
  2606. ret = -EINVAL;
  2607. /* Hide any masked bytes to ensure consistent data reporting */
  2608. if (ret == 0 && params->mask) {
  2609. switch (codec->val_bytes) {
  2610. case 1:
  2611. ucontrol->value.bytes.data[0] &= ~params->mask;
  2612. break;
  2613. case 2:
  2614. ((u16 *)(&ucontrol->value.bytes.data))[0]
  2615. &= ~params->mask;
  2616. break;
  2617. case 4:
  2618. ((u32 *)(&ucontrol->value.bytes.data))[0]
  2619. &= ~params->mask;
  2620. break;
  2621. default:
  2622. return -EINVAL;
  2623. }
  2624. }
  2625. return ret;
  2626. }
  2627. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  2628. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  2629. struct snd_ctl_elem_value *ucontrol)
  2630. {
  2631. struct soc_bytes *params = (void *)kcontrol->private_value;
  2632. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2633. int ret, len;
  2634. unsigned int val;
  2635. void *data;
  2636. if (!codec->using_regmap)
  2637. return -EINVAL;
  2638. data = ucontrol->value.bytes.data;
  2639. len = params->num_regs * codec->val_bytes;
  2640. /*
  2641. * If we've got a mask then we need to preserve the register
  2642. * bits. We shouldn't modify the incoming data so take a
  2643. * copy.
  2644. */
  2645. if (params->mask) {
  2646. ret = regmap_read(codec->control_data, params->base, &val);
  2647. if (ret != 0)
  2648. return ret;
  2649. val &= params->mask;
  2650. data = kmemdup(data, len, GFP_KERNEL);
  2651. if (!data)
  2652. return -ENOMEM;
  2653. switch (codec->val_bytes) {
  2654. case 1:
  2655. ((u8 *)data)[0] &= ~params->mask;
  2656. ((u8 *)data)[0] |= val;
  2657. break;
  2658. case 2:
  2659. ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
  2660. ((u16 *)data)[0] |= cpu_to_be16(val);
  2661. break;
  2662. case 4:
  2663. ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
  2664. ((u32 *)data)[0] |= cpu_to_be32(val);
  2665. break;
  2666. default:
  2667. return -EINVAL;
  2668. }
  2669. }
  2670. ret = regmap_raw_write(codec->control_data, params->base,
  2671. data, len);
  2672. if (params->mask)
  2673. kfree(data);
  2674. return ret;
  2675. }
  2676. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  2677. /**
  2678. * snd_soc_info_xr_sx - signed multi register info callback
  2679. * @kcontrol: mreg control
  2680. * @uinfo: control element information
  2681. *
  2682. * Callback to provide information of a control that can
  2683. * span multiple codec registers which together
  2684. * forms a single signed value in a MSB/LSB manner.
  2685. *
  2686. * Returns 0 for success.
  2687. */
  2688. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  2689. struct snd_ctl_elem_info *uinfo)
  2690. {
  2691. struct soc_mreg_control *mc =
  2692. (struct soc_mreg_control *)kcontrol->private_value;
  2693. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2694. uinfo->count = 1;
  2695. uinfo->value.integer.min = mc->min;
  2696. uinfo->value.integer.max = mc->max;
  2697. return 0;
  2698. }
  2699. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  2700. /**
  2701. * snd_soc_get_xr_sx - signed multi register get callback
  2702. * @kcontrol: mreg control
  2703. * @ucontrol: control element information
  2704. *
  2705. * Callback to get the value of a control that can span
  2706. * multiple codec registers which together forms a single
  2707. * signed value in a MSB/LSB manner. The control supports
  2708. * specifying total no of bits used to allow for bitfields
  2709. * across the multiple codec registers.
  2710. *
  2711. * Returns 0 for success.
  2712. */
  2713. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  2714. struct snd_ctl_elem_value *ucontrol)
  2715. {
  2716. struct soc_mreg_control *mc =
  2717. (struct soc_mreg_control *)kcontrol->private_value;
  2718. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2719. unsigned int regbase = mc->regbase;
  2720. unsigned int regcount = mc->regcount;
  2721. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2722. unsigned int regwmask = (1<<regwshift)-1;
  2723. unsigned int invert = mc->invert;
  2724. unsigned long mask = (1UL<<mc->nbits)-1;
  2725. long min = mc->min;
  2726. long max = mc->max;
  2727. long val = 0;
  2728. unsigned long regval;
  2729. unsigned int i;
  2730. for (i = 0; i < regcount; i++) {
  2731. regval = snd_soc_read(codec, regbase+i) & regwmask;
  2732. val |= regval << (regwshift*(regcount-i-1));
  2733. }
  2734. val &= mask;
  2735. if (min < 0 && val > max)
  2736. val |= ~mask;
  2737. if (invert)
  2738. val = max - val;
  2739. ucontrol->value.integer.value[0] = val;
  2740. return 0;
  2741. }
  2742. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  2743. /**
  2744. * snd_soc_put_xr_sx - signed multi register get callback
  2745. * @kcontrol: mreg control
  2746. * @ucontrol: control element information
  2747. *
  2748. * Callback to set the value of a control that can span
  2749. * multiple codec registers which together forms a single
  2750. * signed value in a MSB/LSB manner. The control supports
  2751. * specifying total no of bits used to allow for bitfields
  2752. * across the multiple codec registers.
  2753. *
  2754. * Returns 0 for success.
  2755. */
  2756. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  2757. struct snd_ctl_elem_value *ucontrol)
  2758. {
  2759. struct soc_mreg_control *mc =
  2760. (struct soc_mreg_control *)kcontrol->private_value;
  2761. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2762. unsigned int regbase = mc->regbase;
  2763. unsigned int regcount = mc->regcount;
  2764. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2765. unsigned int regwmask = (1<<regwshift)-1;
  2766. unsigned int invert = mc->invert;
  2767. unsigned long mask = (1UL<<mc->nbits)-1;
  2768. long max = mc->max;
  2769. long val = ucontrol->value.integer.value[0];
  2770. unsigned int i, regval, regmask;
  2771. int err;
  2772. if (invert)
  2773. val = max - val;
  2774. val &= mask;
  2775. for (i = 0; i < regcount; i++) {
  2776. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  2777. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  2778. err = snd_soc_update_bits_locked(codec, regbase+i,
  2779. regmask, regval);
  2780. if (err < 0)
  2781. return err;
  2782. }
  2783. return 0;
  2784. }
  2785. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  2786. /**
  2787. * snd_soc_get_strobe - strobe get callback
  2788. * @kcontrol: mixer control
  2789. * @ucontrol: control element information
  2790. *
  2791. * Callback get the value of a strobe mixer control.
  2792. *
  2793. * Returns 0 for success.
  2794. */
  2795. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  2796. struct snd_ctl_elem_value *ucontrol)
  2797. {
  2798. struct soc_mixer_control *mc =
  2799. (struct soc_mixer_control *)kcontrol->private_value;
  2800. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2801. unsigned int reg = mc->reg;
  2802. unsigned int shift = mc->shift;
  2803. unsigned int mask = 1 << shift;
  2804. unsigned int invert = mc->invert != 0;
  2805. unsigned int val = snd_soc_read(codec, reg) & mask;
  2806. if (shift != 0 && val != 0)
  2807. val = val >> shift;
  2808. ucontrol->value.enumerated.item[0] = val ^ invert;
  2809. return 0;
  2810. }
  2811. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  2812. /**
  2813. * snd_soc_put_strobe - strobe put callback
  2814. * @kcontrol: mixer control
  2815. * @ucontrol: control element information
  2816. *
  2817. * Callback strobe a register bit to high then low (or the inverse)
  2818. * in one pass of a single mixer enum control.
  2819. *
  2820. * Returns 1 for success.
  2821. */
  2822. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  2823. struct snd_ctl_elem_value *ucontrol)
  2824. {
  2825. struct soc_mixer_control *mc =
  2826. (struct soc_mixer_control *)kcontrol->private_value;
  2827. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2828. unsigned int reg = mc->reg;
  2829. unsigned int shift = mc->shift;
  2830. unsigned int mask = 1 << shift;
  2831. unsigned int invert = mc->invert != 0;
  2832. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  2833. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  2834. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  2835. int err;
  2836. err = snd_soc_update_bits_locked(codec, reg, mask, val1);
  2837. if (err < 0)
  2838. return err;
  2839. err = snd_soc_update_bits_locked(codec, reg, mask, val2);
  2840. return err;
  2841. }
  2842. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
  2843. /**
  2844. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2845. * @dai: DAI
  2846. * @clk_id: DAI specific clock ID
  2847. * @freq: new clock frequency in Hz
  2848. * @dir: new clock direction - input/output.
  2849. *
  2850. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2851. */
  2852. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2853. unsigned int freq, int dir)
  2854. {
  2855. if (dai->driver && dai->driver->ops->set_sysclk)
  2856. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2857. else if (dai->codec && dai->codec->driver->set_sysclk)
  2858. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2859. freq, dir);
  2860. else
  2861. return -EINVAL;
  2862. }
  2863. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2864. /**
  2865. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2866. * @codec: CODEC
  2867. * @clk_id: DAI specific clock ID
  2868. * @source: Source for the clock
  2869. * @freq: new clock frequency in Hz
  2870. * @dir: new clock direction - input/output.
  2871. *
  2872. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2873. */
  2874. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2875. int source, unsigned int freq, int dir)
  2876. {
  2877. if (codec->driver->set_sysclk)
  2878. return codec->driver->set_sysclk(codec, clk_id, source,
  2879. freq, dir);
  2880. else
  2881. return -EINVAL;
  2882. }
  2883. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2884. /**
  2885. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2886. * @dai: DAI
  2887. * @div_id: DAI specific clock divider ID
  2888. * @div: new clock divisor.
  2889. *
  2890. * Configures the clock dividers. This is used to derive the best DAI bit and
  2891. * frame clocks from the system or master clock. It's best to set the DAI bit
  2892. * and frame clocks as low as possible to save system power.
  2893. */
  2894. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2895. int div_id, int div)
  2896. {
  2897. if (dai->driver && dai->driver->ops->set_clkdiv)
  2898. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2899. else
  2900. return -EINVAL;
  2901. }
  2902. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2903. /**
  2904. * snd_soc_dai_set_pll - configure DAI PLL.
  2905. * @dai: DAI
  2906. * @pll_id: DAI specific PLL ID
  2907. * @source: DAI specific source for the PLL
  2908. * @freq_in: PLL input clock frequency in Hz
  2909. * @freq_out: requested PLL output clock frequency in Hz
  2910. *
  2911. * Configures and enables PLL to generate output clock based on input clock.
  2912. */
  2913. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2914. unsigned int freq_in, unsigned int freq_out)
  2915. {
  2916. if (dai->driver && dai->driver->ops->set_pll)
  2917. return dai->driver->ops->set_pll(dai, pll_id, source,
  2918. freq_in, freq_out);
  2919. else if (dai->codec && dai->codec->driver->set_pll)
  2920. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2921. freq_in, freq_out);
  2922. else
  2923. return -EINVAL;
  2924. }
  2925. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2926. /*
  2927. * snd_soc_codec_set_pll - configure codec PLL.
  2928. * @codec: CODEC
  2929. * @pll_id: DAI specific PLL ID
  2930. * @source: DAI specific source for the PLL
  2931. * @freq_in: PLL input clock frequency in Hz
  2932. * @freq_out: requested PLL output clock frequency in Hz
  2933. *
  2934. * Configures and enables PLL to generate output clock based on input clock.
  2935. */
  2936. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2937. unsigned int freq_in, unsigned int freq_out)
  2938. {
  2939. if (codec->driver->set_pll)
  2940. return codec->driver->set_pll(codec, pll_id, source,
  2941. freq_in, freq_out);
  2942. else
  2943. return -EINVAL;
  2944. }
  2945. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2946. /**
  2947. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2948. * @dai: DAI
  2949. * @fmt: SND_SOC_DAIFMT_ format value.
  2950. *
  2951. * Configures the DAI hardware format and clocking.
  2952. */
  2953. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2954. {
  2955. if (dai->driver == NULL)
  2956. return -EINVAL;
  2957. if (dai->driver->ops->set_fmt == NULL)
  2958. return -ENOTSUPP;
  2959. return dai->driver->ops->set_fmt(dai, fmt);
  2960. }
  2961. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2962. /**
  2963. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2964. * @dai: DAI
  2965. * @tx_mask: bitmask representing active TX slots.
  2966. * @rx_mask: bitmask representing active RX slots.
  2967. * @slots: Number of slots in use.
  2968. * @slot_width: Width in bits for each slot.
  2969. *
  2970. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2971. * specific.
  2972. */
  2973. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2974. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2975. {
  2976. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2977. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2978. slots, slot_width);
  2979. else
  2980. return -EINVAL;
  2981. }
  2982. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  2983. /**
  2984. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  2985. * @dai: DAI
  2986. * @tx_num: how many TX channels
  2987. * @tx_slot: pointer to an array which imply the TX slot number channel
  2988. * 0~num-1 uses
  2989. * @rx_num: how many RX channels
  2990. * @rx_slot: pointer to an array which imply the RX slot number channel
  2991. * 0~num-1 uses
  2992. *
  2993. * configure the relationship between channel number and TDM slot number.
  2994. */
  2995. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  2996. unsigned int tx_num, unsigned int *tx_slot,
  2997. unsigned int rx_num, unsigned int *rx_slot)
  2998. {
  2999. if (dai->driver && dai->driver->ops->set_channel_map)
  3000. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  3001. rx_num, rx_slot);
  3002. else
  3003. return -EINVAL;
  3004. }
  3005. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  3006. /**
  3007. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  3008. * @dai: DAI
  3009. * @tristate: tristate enable
  3010. *
  3011. * Tristates the DAI so that others can use it.
  3012. */
  3013. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  3014. {
  3015. if (dai->driver && dai->driver->ops->set_tristate)
  3016. return dai->driver->ops->set_tristate(dai, tristate);
  3017. else
  3018. return -EINVAL;
  3019. }
  3020. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  3021. /**
  3022. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  3023. * @dai: DAI
  3024. * @mute: mute enable
  3025. *
  3026. * Mutes the DAI DAC.
  3027. */
  3028. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  3029. {
  3030. if (dai->driver && dai->driver->ops->digital_mute)
  3031. return dai->driver->ops->digital_mute(dai, mute);
  3032. else
  3033. return -ENOTSUPP;
  3034. }
  3035. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  3036. /**
  3037. * snd_soc_register_card - Register a card with the ASoC core
  3038. *
  3039. * @card: Card to register
  3040. *
  3041. */
  3042. int snd_soc_register_card(struct snd_soc_card *card)
  3043. {
  3044. int i, ret;
  3045. if (!card->name || !card->dev)
  3046. return -EINVAL;
  3047. for (i = 0; i < card->num_links; i++) {
  3048. struct snd_soc_dai_link *link = &card->dai_link[i];
  3049. /*
  3050. * Codec must be specified by 1 of name or OF node,
  3051. * not both or neither.
  3052. */
  3053. if (!!link->codec_name == !!link->codec_of_node) {
  3054. dev_err(card->dev, "ASoC: Neither/both codec"
  3055. " name/of_node are set for %s\n", link->name);
  3056. return -EINVAL;
  3057. }
  3058. /* Codec DAI name must be specified */
  3059. if (!link->codec_dai_name) {
  3060. dev_err(card->dev, "ASoC: codec_dai_name not"
  3061. " set for %s\n", link->name);
  3062. return -EINVAL;
  3063. }
  3064. /*
  3065. * Platform may be specified by either name or OF node, but
  3066. * can be left unspecified, and a dummy platform will be used.
  3067. */
  3068. if (link->platform_name && link->platform_of_node) {
  3069. dev_err(card->dev, "ASoC: Both platform name/of_node"
  3070. " are set for %s\n", link->name);
  3071. return -EINVAL;
  3072. }
  3073. /*
  3074. * CPU device may be specified by either name or OF node, but
  3075. * can be left unspecified, and will be matched based on DAI
  3076. * name alone..
  3077. */
  3078. if (link->cpu_name && link->cpu_of_node) {
  3079. dev_err(card->dev, "ASoC: Neither/both "
  3080. "cpu name/of_node are set for %s\n",link->name);
  3081. return -EINVAL;
  3082. }
  3083. /*
  3084. * At least one of CPU DAI name or CPU device name/node must be
  3085. * specified
  3086. */
  3087. if (!link->cpu_dai_name &&
  3088. !(link->cpu_name || link->cpu_of_node)) {
  3089. dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
  3090. "cpu_name/of_node are set for %s\n", link->name);
  3091. return -EINVAL;
  3092. }
  3093. }
  3094. dev_set_drvdata(card->dev, card);
  3095. snd_soc_initialize_card_lists(card);
  3096. soc_init_card_debugfs(card);
  3097. card->rtd = devm_kzalloc(card->dev,
  3098. sizeof(struct snd_soc_pcm_runtime) *
  3099. (card->num_links + card->num_aux_devs),
  3100. GFP_KERNEL);
  3101. if (card->rtd == NULL)
  3102. return -ENOMEM;
  3103. card->num_rtd = 0;
  3104. card->rtd_aux = &card->rtd[card->num_links];
  3105. for (i = 0; i < card->num_links; i++)
  3106. card->rtd[i].dai_link = &card->dai_link[i];
  3107. INIT_LIST_HEAD(&card->list);
  3108. INIT_LIST_HEAD(&card->dapm_dirty);
  3109. card->instantiated = 0;
  3110. mutex_init(&card->mutex);
  3111. mutex_init(&card->dapm_mutex);
  3112. ret = snd_soc_instantiate_card(card);
  3113. if (ret != 0)
  3114. soc_cleanup_card_debugfs(card);
  3115. return ret;
  3116. }
  3117. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  3118. /**
  3119. * snd_soc_unregister_card - Unregister a card with the ASoC core
  3120. *
  3121. * @card: Card to unregister
  3122. *
  3123. */
  3124. int snd_soc_unregister_card(struct snd_soc_card *card)
  3125. {
  3126. if (card->instantiated)
  3127. soc_cleanup_card_resources(card);
  3128. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  3129. return 0;
  3130. }
  3131. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  3132. /*
  3133. * Simplify DAI link configuration by removing ".-1" from device names
  3134. * and sanitizing names.
  3135. */
  3136. static char *fmt_single_name(struct device *dev, int *id)
  3137. {
  3138. char *found, name[NAME_SIZE];
  3139. int id1, id2;
  3140. if (dev_name(dev) == NULL)
  3141. return NULL;
  3142. strlcpy(name, dev_name(dev), NAME_SIZE);
  3143. /* are we a "%s.%d" name (platform and SPI components) */
  3144. found = strstr(name, dev->driver->name);
  3145. if (found) {
  3146. /* get ID */
  3147. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  3148. /* discard ID from name if ID == -1 */
  3149. if (*id == -1)
  3150. found[strlen(dev->driver->name)] = '\0';
  3151. }
  3152. } else {
  3153. /* I2C component devices are named "bus-addr" */
  3154. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  3155. char tmp[NAME_SIZE];
  3156. /* create unique ID number from I2C addr and bus */
  3157. *id = ((id1 & 0xffff) << 16) + id2;
  3158. /* sanitize component name for DAI link creation */
  3159. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  3160. strlcpy(name, tmp, NAME_SIZE);
  3161. } else
  3162. *id = 0;
  3163. }
  3164. return kstrdup(name, GFP_KERNEL);
  3165. }
  3166. /*
  3167. * Simplify DAI link naming for single devices with multiple DAIs by removing
  3168. * any ".-1" and using the DAI name (instead of device name).
  3169. */
  3170. static inline char *fmt_multiple_name(struct device *dev,
  3171. struct snd_soc_dai_driver *dai_drv)
  3172. {
  3173. if (dai_drv->name == NULL) {
  3174. dev_err(dev, "ASoC: error - multiple DAI %s registered with"
  3175. " no name\n", dev_name(dev));
  3176. return NULL;
  3177. }
  3178. return kstrdup(dai_drv->name, GFP_KERNEL);
  3179. }
  3180. /**
  3181. * snd_soc_register_dai - Register a DAI with the ASoC core
  3182. *
  3183. * @dai: DAI to register
  3184. */
  3185. int snd_soc_register_dai(struct device *dev,
  3186. struct snd_soc_dai_driver *dai_drv)
  3187. {
  3188. struct snd_soc_codec *codec;
  3189. struct snd_soc_dai *dai;
  3190. dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
  3191. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3192. if (dai == NULL)
  3193. return -ENOMEM;
  3194. /* create DAI component name */
  3195. dai->name = fmt_single_name(dev, &dai->id);
  3196. if (dai->name == NULL) {
  3197. kfree(dai);
  3198. return -ENOMEM;
  3199. }
  3200. dai->dev = dev;
  3201. dai->driver = dai_drv;
  3202. dai->dapm.dev = dev;
  3203. if (!dai->driver->ops)
  3204. dai->driver->ops = &null_dai_ops;
  3205. mutex_lock(&client_mutex);
  3206. list_for_each_entry(codec, &codec_list, list) {
  3207. if (codec->dev == dev) {
  3208. dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
  3209. dai->name, codec->name);
  3210. dai->codec = codec;
  3211. break;
  3212. }
  3213. }
  3214. if (!dai->codec)
  3215. dai->dapm.idle_bias_off = 1;
  3216. list_add(&dai->list, &dai_list);
  3217. mutex_unlock(&client_mutex);
  3218. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3219. return 0;
  3220. }
  3221. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  3222. /**
  3223. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  3224. *
  3225. * @dai: DAI to unregister
  3226. */
  3227. void snd_soc_unregister_dai(struct device *dev)
  3228. {
  3229. struct snd_soc_dai *dai;
  3230. list_for_each_entry(dai, &dai_list, list) {
  3231. if (dev == dai->dev)
  3232. goto found;
  3233. }
  3234. return;
  3235. found:
  3236. mutex_lock(&client_mutex);
  3237. list_del(&dai->list);
  3238. mutex_unlock(&client_mutex);
  3239. dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
  3240. kfree(dai->name);
  3241. kfree(dai);
  3242. }
  3243. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  3244. /**
  3245. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  3246. *
  3247. * @dai: Array of DAIs to register
  3248. * @count: Number of DAIs
  3249. */
  3250. int snd_soc_register_dais(struct device *dev,
  3251. struct snd_soc_dai_driver *dai_drv, size_t count)
  3252. {
  3253. struct snd_soc_codec *codec;
  3254. struct snd_soc_dai *dai;
  3255. int i, ret = 0;
  3256. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  3257. for (i = 0; i < count; i++) {
  3258. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3259. if (dai == NULL) {
  3260. ret = -ENOMEM;
  3261. goto err;
  3262. }
  3263. /* create DAI component name */
  3264. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  3265. if (dai->name == NULL) {
  3266. kfree(dai);
  3267. ret = -EINVAL;
  3268. goto err;
  3269. }
  3270. dai->dev = dev;
  3271. dai->driver = &dai_drv[i];
  3272. if (dai->driver->id)
  3273. dai->id = dai->driver->id;
  3274. else
  3275. dai->id = i;
  3276. dai->dapm.dev = dev;
  3277. if (!dai->driver->ops)
  3278. dai->driver->ops = &null_dai_ops;
  3279. mutex_lock(&client_mutex);
  3280. list_for_each_entry(codec, &codec_list, list) {
  3281. if (codec->dev == dev) {
  3282. dev_dbg(dev, "ASoC: Mapped DAI %s to "
  3283. "CODEC %s\n", dai->name, codec->name);
  3284. dai->codec = codec;
  3285. break;
  3286. }
  3287. }
  3288. if (!dai->codec)
  3289. dai->dapm.idle_bias_off = 1;
  3290. list_add(&dai->list, &dai_list);
  3291. mutex_unlock(&client_mutex);
  3292. dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3293. }
  3294. return 0;
  3295. err:
  3296. for (i--; i >= 0; i--)
  3297. snd_soc_unregister_dai(dev);
  3298. return ret;
  3299. }
  3300. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  3301. /**
  3302. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  3303. *
  3304. * @dai: Array of DAIs to unregister
  3305. * @count: Number of DAIs
  3306. */
  3307. void snd_soc_unregister_dais(struct device *dev, size_t count)
  3308. {
  3309. int i;
  3310. for (i = 0; i < count; i++)
  3311. snd_soc_unregister_dai(dev);
  3312. }
  3313. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  3314. /**
  3315. * snd_soc_register_platform - Register a platform with the ASoC core
  3316. *
  3317. * @platform: platform to register
  3318. */
  3319. int snd_soc_register_platform(struct device *dev,
  3320. struct snd_soc_platform_driver *platform_drv)
  3321. {
  3322. struct snd_soc_platform *platform;
  3323. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  3324. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  3325. if (platform == NULL)
  3326. return -ENOMEM;
  3327. /* create platform component name */
  3328. platform->name = fmt_single_name(dev, &platform->id);
  3329. if (platform->name == NULL) {
  3330. kfree(platform);
  3331. return -ENOMEM;
  3332. }
  3333. platform->dev = dev;
  3334. platform->driver = platform_drv;
  3335. platform->dapm.dev = dev;
  3336. platform->dapm.platform = platform;
  3337. platform->dapm.stream_event = platform_drv->stream_event;
  3338. mutex_init(&platform->mutex);
  3339. mutex_lock(&client_mutex);
  3340. list_add(&platform->list, &platform_list);
  3341. mutex_unlock(&client_mutex);
  3342. dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
  3343. return 0;
  3344. }
  3345. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  3346. /**
  3347. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  3348. *
  3349. * @platform: platform to unregister
  3350. */
  3351. void snd_soc_unregister_platform(struct device *dev)
  3352. {
  3353. struct snd_soc_platform *platform;
  3354. list_for_each_entry(platform, &platform_list, list) {
  3355. if (dev == platform->dev)
  3356. goto found;
  3357. }
  3358. return;
  3359. found:
  3360. mutex_lock(&client_mutex);
  3361. list_del(&platform->list);
  3362. mutex_unlock(&client_mutex);
  3363. dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
  3364. kfree(platform->name);
  3365. kfree(platform);
  3366. }
  3367. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  3368. static u64 codec_format_map[] = {
  3369. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  3370. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  3371. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  3372. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  3373. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  3374. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  3375. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3376. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3377. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  3378. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  3379. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  3380. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  3381. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  3382. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  3383. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  3384. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  3385. };
  3386. /* Fix up the DAI formats for endianness: codecs don't actually see
  3387. * the endianness of the data but we're using the CPU format
  3388. * definitions which do need to include endianness so we ensure that
  3389. * codec DAIs always have both big and little endian variants set.
  3390. */
  3391. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  3392. {
  3393. int i;
  3394. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  3395. if (stream->formats & codec_format_map[i])
  3396. stream->formats |= codec_format_map[i];
  3397. }
  3398. /**
  3399. * snd_soc_register_codec - Register a codec with the ASoC core
  3400. *
  3401. * @codec: codec to register
  3402. */
  3403. int snd_soc_register_codec(struct device *dev,
  3404. const struct snd_soc_codec_driver *codec_drv,
  3405. struct snd_soc_dai_driver *dai_drv,
  3406. int num_dai)
  3407. {
  3408. size_t reg_size;
  3409. struct snd_soc_codec *codec;
  3410. int ret, i;
  3411. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  3412. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  3413. if (codec == NULL)
  3414. return -ENOMEM;
  3415. /* create CODEC component name */
  3416. codec->name = fmt_single_name(dev, &codec->id);
  3417. if (codec->name == NULL) {
  3418. kfree(codec);
  3419. return -ENOMEM;
  3420. }
  3421. if (codec_drv->compress_type)
  3422. codec->compress_type = codec_drv->compress_type;
  3423. else
  3424. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  3425. codec->write = codec_drv->write;
  3426. codec->read = codec_drv->read;
  3427. codec->volatile_register = codec_drv->volatile_register;
  3428. codec->readable_register = codec_drv->readable_register;
  3429. codec->writable_register = codec_drv->writable_register;
  3430. codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  3431. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  3432. codec->dapm.dev = dev;
  3433. codec->dapm.codec = codec;
  3434. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  3435. codec->dapm.stream_event = codec_drv->stream_event;
  3436. codec->dev = dev;
  3437. codec->driver = codec_drv;
  3438. codec->num_dai = num_dai;
  3439. mutex_init(&codec->mutex);
  3440. /* allocate CODEC register cache */
  3441. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  3442. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  3443. codec->reg_size = reg_size;
  3444. /* it is necessary to make a copy of the default register cache
  3445. * because in the case of using a compression type that requires
  3446. * the default register cache to be marked as the
  3447. * kernel might have freed the array by the time we initialize
  3448. * the cache.
  3449. */
  3450. if (codec_drv->reg_cache_default) {
  3451. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  3452. reg_size, GFP_KERNEL);
  3453. if (!codec->reg_def_copy) {
  3454. ret = -ENOMEM;
  3455. goto fail;
  3456. }
  3457. }
  3458. }
  3459. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  3460. if (!codec->volatile_register)
  3461. codec->volatile_register = snd_soc_default_volatile_register;
  3462. if (!codec->readable_register)
  3463. codec->readable_register = snd_soc_default_readable_register;
  3464. if (!codec->writable_register)
  3465. codec->writable_register = snd_soc_default_writable_register;
  3466. }
  3467. for (i = 0; i < num_dai; i++) {
  3468. fixup_codec_formats(&dai_drv[i].playback);
  3469. fixup_codec_formats(&dai_drv[i].capture);
  3470. }
  3471. mutex_lock(&client_mutex);
  3472. list_add(&codec->list, &codec_list);
  3473. mutex_unlock(&client_mutex);
  3474. /* register any DAIs */
  3475. if (num_dai) {
  3476. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3477. if (ret < 0)
  3478. dev_err(codec->dev, "ASoC: Failed to regster"
  3479. " DAIs: %d\n", ret);
  3480. }
  3481. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
  3482. return 0;
  3483. fail:
  3484. kfree(codec->name);
  3485. kfree(codec);
  3486. return ret;
  3487. }
  3488. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  3489. /**
  3490. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  3491. *
  3492. * @codec: codec to unregister
  3493. */
  3494. void snd_soc_unregister_codec(struct device *dev)
  3495. {
  3496. struct snd_soc_codec *codec;
  3497. int i;
  3498. list_for_each_entry(codec, &codec_list, list) {
  3499. if (dev == codec->dev)
  3500. goto found;
  3501. }
  3502. return;
  3503. found:
  3504. if (codec->num_dai)
  3505. for (i = 0; i < codec->num_dai; i++)
  3506. snd_soc_unregister_dai(dev);
  3507. mutex_lock(&client_mutex);
  3508. list_del(&codec->list);
  3509. mutex_unlock(&client_mutex);
  3510. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
  3511. snd_soc_cache_exit(codec);
  3512. kfree(codec->reg_def_copy);
  3513. kfree(codec->name);
  3514. kfree(codec);
  3515. }
  3516. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  3517. /* Retrieve a card's name from device tree */
  3518. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  3519. const char *propname)
  3520. {
  3521. struct device_node *np = card->dev->of_node;
  3522. int ret;
  3523. ret = of_property_read_string_index(np, propname, 0, &card->name);
  3524. /*
  3525. * EINVAL means the property does not exist. This is fine providing
  3526. * card->name was previously set, which is checked later in
  3527. * snd_soc_register_card.
  3528. */
  3529. if (ret < 0 && ret != -EINVAL) {
  3530. dev_err(card->dev,
  3531. "ASoC: Property '%s' could not be read: %d\n",
  3532. propname, ret);
  3533. return ret;
  3534. }
  3535. return 0;
  3536. }
  3537. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  3538. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3539. const char *propname)
  3540. {
  3541. struct device_node *np = card->dev->of_node;
  3542. int num_routes;
  3543. struct snd_soc_dapm_route *routes;
  3544. int i, ret;
  3545. num_routes = of_property_count_strings(np, propname);
  3546. if (num_routes < 0 || num_routes & 1) {
  3547. dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
  3548. " length is not even\n", propname);
  3549. return -EINVAL;
  3550. }
  3551. num_routes /= 2;
  3552. if (!num_routes) {
  3553. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3554. propname);
  3555. return -EINVAL;
  3556. }
  3557. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3558. GFP_KERNEL);
  3559. if (!routes) {
  3560. dev_err(card->dev,
  3561. "ASoC: Could not allocate DAPM route table\n");
  3562. return -EINVAL;
  3563. }
  3564. for (i = 0; i < num_routes; i++) {
  3565. ret = of_property_read_string_index(np, propname,
  3566. 2 * i, &routes[i].sink);
  3567. if (ret) {
  3568. dev_err(card->dev,
  3569. "ASoC: Property '%s' index %d could not be read: %d\n",
  3570. propname, 2 * i, ret);
  3571. kfree(routes);
  3572. return -EINVAL;
  3573. }
  3574. ret = of_property_read_string_index(np, propname,
  3575. (2 * i) + 1, &routes[i].source);
  3576. if (ret) {
  3577. dev_err(card->dev,
  3578. "ASoC: Property '%s' index %d could not be read: %d\n",
  3579. propname, (2 * i) + 1, ret);
  3580. kfree(routes);
  3581. return -EINVAL;
  3582. }
  3583. }
  3584. card->num_dapm_routes = num_routes;
  3585. card->dapm_routes = routes;
  3586. return 0;
  3587. }
  3588. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3589. static int __init snd_soc_init(void)
  3590. {
  3591. #ifdef CONFIG_DEBUG_FS
  3592. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  3593. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  3594. pr_warn("ASoC: Failed to create debugfs directory\n");
  3595. snd_soc_debugfs_root = NULL;
  3596. }
  3597. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  3598. &codec_list_fops))
  3599. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  3600. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  3601. &dai_list_fops))
  3602. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  3603. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  3604. &platform_list_fops))
  3605. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  3606. #endif
  3607. snd_soc_util_init();
  3608. return platform_driver_register(&soc_driver);
  3609. }
  3610. module_init(snd_soc_init);
  3611. static void __exit snd_soc_exit(void)
  3612. {
  3613. snd_soc_util_exit();
  3614. #ifdef CONFIG_DEBUG_FS
  3615. debugfs_remove_recursive(snd_soc_debugfs_root);
  3616. #endif
  3617. platform_driver_unregister(&soc_driver);
  3618. }
  3619. module_exit(snd_soc_exit);
  3620. /* Module information */
  3621. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3622. MODULE_DESCRIPTION("ALSA SoC Core");
  3623. MODULE_LICENSE("GPL");
  3624. MODULE_ALIAS("platform:soc-audio");