soc-core.c 86 KB

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