soc-core.c 85 KB

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