soc-core.c 100 KB

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