soc-core.c 85 KB

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