soc-core.c 86 KB

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