soc-core.c 89 KB

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