soc-core.c 88 KB

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