soc-core.c 86 KB

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