soc-core.c 102 KB

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