soc-core.c 85 KB

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