soc-core.c 117 KB

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