soc-core.c 101 KB

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