soc-core.c 88 KB

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