soc-core.c 88 KB

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