soc-core.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /*
  2. * soc-core.c -- ALSA SoC Audio Layer
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Author: Liam Girdwood
  8. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  9. * with code, comments and ideas from :-
  10. * Richard Purdie <richard@openedhand.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * TODO:
  18. * o Add hw rules to enforce rates, etc.
  19. * o More testing with other codecs/machines.
  20. * o Add more codecs and platforms to ensure good API coverage.
  21. * o Support TDM on PCM and I2S
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/bitops.h>
  29. #include <linux/platform_device.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/soc.h>
  34. #include <sound/soc-dapm.h>
  35. #include <sound/initval.h>
  36. /* debug */
  37. #define SOC_DEBUG 0
  38. #if SOC_DEBUG
  39. #define dbg(format, arg...) printk(format, ## arg)
  40. #else
  41. #define dbg(format, arg...)
  42. #endif
  43. static DEFINE_MUTEX(pcm_mutex);
  44. static DEFINE_MUTEX(io_mutex);
  45. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  46. /*
  47. * This is a timeout to do a DAPM powerdown after a stream is closed().
  48. * It can be used to eliminate pops between different playback streams, e.g.
  49. * between two audio tracks.
  50. */
  51. static int pmdown_time = 5000;
  52. module_param(pmdown_time, int, 0);
  53. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  54. /*
  55. * This function forces any delayed work to be queued and run.
  56. */
  57. static int run_delayed_work(struct delayed_work *dwork)
  58. {
  59. int ret;
  60. /* cancel any work waiting to be queued. */
  61. ret = cancel_delayed_work(dwork);
  62. /* if there was any work waiting then we run it now and
  63. * wait for it's completion */
  64. if (ret) {
  65. schedule_delayed_work(dwork, 0);
  66. flush_scheduled_work();
  67. }
  68. return ret;
  69. }
  70. #ifdef CONFIG_SND_SOC_AC97_BUS
  71. /* unregister ac97 codec */
  72. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  73. {
  74. if (codec->ac97->dev.bus)
  75. device_unregister(&codec->ac97->dev);
  76. return 0;
  77. }
  78. /* stop no dev release warning */
  79. static void soc_ac97_device_release(struct device *dev){}
  80. /* register ac97 codec to bus */
  81. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  82. {
  83. int err;
  84. codec->ac97->dev.bus = &ac97_bus_type;
  85. codec->ac97->dev.parent = NULL;
  86. codec->ac97->dev.release = soc_ac97_device_release;
  87. snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
  88. codec->card->number, 0, codec->name);
  89. err = device_register(&codec->ac97->dev);
  90. if (err < 0) {
  91. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  92. codec->ac97->dev.bus = NULL;
  93. return err;
  94. }
  95. return 0;
  96. }
  97. #endif
  98. static inline const char* get_dai_name(int type)
  99. {
  100. switch(type) {
  101. case SND_SOC_DAI_AC97_BUS:
  102. case SND_SOC_DAI_AC97:
  103. return "AC97";
  104. case SND_SOC_DAI_I2S:
  105. return "I2S";
  106. case SND_SOC_DAI_PCM:
  107. return "PCM";
  108. }
  109. return NULL;
  110. }
  111. /*
  112. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  113. * then initialized and any private data can be allocated. This also calls
  114. * startup for the cpu DAI, platform, machine and codec DAI.
  115. */
  116. static int soc_pcm_open(struct snd_pcm_substream *substream)
  117. {
  118. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  119. struct snd_soc_device *socdev = rtd->socdev;
  120. struct snd_pcm_runtime *runtime = substream->runtime;
  121. struct snd_soc_dai_link *machine = rtd->dai;
  122. struct snd_soc_platform *platform = socdev->platform;
  123. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  124. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  125. int ret = 0;
  126. mutex_lock(&pcm_mutex);
  127. /* startup the audio subsystem */
  128. if (cpu_dai->ops.startup) {
  129. ret = cpu_dai->ops.startup(substream);
  130. if (ret < 0) {
  131. printk(KERN_ERR "asoc: can't open interface %s\n",
  132. cpu_dai->name);
  133. goto out;
  134. }
  135. }
  136. if (platform->pcm_ops->open) {
  137. ret = platform->pcm_ops->open(substream);
  138. if (ret < 0) {
  139. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  140. goto platform_err;
  141. }
  142. }
  143. if (codec_dai->ops.startup) {
  144. ret = codec_dai->ops.startup(substream);
  145. if (ret < 0) {
  146. printk(KERN_ERR "asoc: can't open codec %s\n",
  147. codec_dai->name);
  148. goto codec_dai_err;
  149. }
  150. }
  151. if (machine->ops && machine->ops->startup) {
  152. ret = machine->ops->startup(substream);
  153. if (ret < 0) {
  154. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  155. goto machine_err;
  156. }
  157. }
  158. /* Check that the codec and cpu DAI's are compatible */
  159. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  160. runtime->hw.rate_min =
  161. max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
  162. runtime->hw.rate_max =
  163. min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
  164. runtime->hw.channels_min =
  165. max(codec_dai->playback.channels_min,
  166. cpu_dai->playback.channels_min);
  167. runtime->hw.channels_max =
  168. min(codec_dai->playback.channels_max,
  169. cpu_dai->playback.channels_max);
  170. runtime->hw.formats =
  171. codec_dai->playback.formats & cpu_dai->playback.formats;
  172. runtime->hw.rates =
  173. codec_dai->playback.rates & cpu_dai->playback.rates;
  174. } else {
  175. runtime->hw.rate_min =
  176. max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
  177. runtime->hw.rate_max =
  178. min(codec_dai->capture.rate_max, cpu_dai->capture.rate_max);
  179. runtime->hw.channels_min =
  180. max(codec_dai->capture.channels_min,
  181. cpu_dai->capture.channels_min);
  182. runtime->hw.channels_max =
  183. min(codec_dai->capture.channels_max,
  184. cpu_dai->capture.channels_max);
  185. runtime->hw.formats =
  186. codec_dai->capture.formats & cpu_dai->capture.formats;
  187. runtime->hw.rates =
  188. codec_dai->capture.rates & cpu_dai->capture.rates;
  189. }
  190. snd_pcm_limit_hw_rates(runtime);
  191. if (!runtime->hw.rates) {
  192. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  193. codec_dai->name, cpu_dai->name);
  194. goto machine_err;
  195. }
  196. if (!runtime->hw.formats) {
  197. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  198. codec_dai->name, cpu_dai->name);
  199. goto machine_err;
  200. }
  201. if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
  202. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  203. codec_dai->name, cpu_dai->name);
  204. goto machine_err;
  205. }
  206. dbg("asoc: %s <-> %s info:\n",codec_dai->name, cpu_dai->name);
  207. dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
  208. dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  209. runtime->hw.channels_max);
  210. dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  211. runtime->hw.rate_max);
  212. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  213. cpu_dai->playback.active = codec_dai->playback.active = 1;
  214. else
  215. cpu_dai->capture.active = codec_dai->capture.active = 1;
  216. cpu_dai->active = codec_dai->active = 1;
  217. cpu_dai->runtime = runtime;
  218. socdev->codec->active++;
  219. mutex_unlock(&pcm_mutex);
  220. return 0;
  221. machine_err:
  222. if (machine->ops && machine->ops->shutdown)
  223. machine->ops->shutdown(substream);
  224. codec_dai_err:
  225. if (platform->pcm_ops->close)
  226. platform->pcm_ops->close(substream);
  227. platform_err:
  228. if (cpu_dai->ops.shutdown)
  229. cpu_dai->ops.shutdown(substream);
  230. out:
  231. mutex_unlock(&pcm_mutex);
  232. return ret;
  233. }
  234. /*
  235. * Power down the audio subsystem pmdown_time msecs after close is called.
  236. * This is to ensure there are no pops or clicks in between any music tracks
  237. * due to DAPM power cycling.
  238. */
  239. static void close_delayed_work(struct work_struct *work)
  240. {
  241. struct snd_soc_device *socdev =
  242. container_of(work, struct snd_soc_device, delayed_work.work);
  243. struct snd_soc_codec *codec = socdev->codec;
  244. struct snd_soc_codec_dai *codec_dai;
  245. int i;
  246. mutex_lock(&pcm_mutex);
  247. for(i = 0; i < codec->num_dai; i++) {
  248. codec_dai = &codec->dai[i];
  249. dbg("pop wq checking: %s status: %s waiting: %s\n",
  250. codec_dai->playback.stream_name,
  251. codec_dai->playback.active ? "active" : "inactive",
  252. codec_dai->pop_wait ? "yes" : "no");
  253. /* are we waiting on this codec DAI stream */
  254. if (codec_dai->pop_wait == 1) {
  255. /* power down the codec to D1 if no longer active */
  256. if (codec->active == 0) {
  257. dbg("pop wq D1 %s %s\n", codec->name,
  258. codec_dai->playback.stream_name);
  259. snd_soc_dapm_device_event(socdev,
  260. SNDRV_CTL_POWER_D1);
  261. }
  262. codec_dai->pop_wait = 0;
  263. snd_soc_dapm_stream_event(codec,
  264. codec_dai->playback.stream_name,
  265. SND_SOC_DAPM_STREAM_STOP);
  266. /* power down the codec power domain if no longer active */
  267. if (codec->active == 0) {
  268. dbg("pop wq D3 %s %s\n", codec->name,
  269. codec_dai->playback.stream_name);
  270. snd_soc_dapm_device_event(socdev,
  271. SNDRV_CTL_POWER_D3hot);
  272. }
  273. }
  274. }
  275. mutex_unlock(&pcm_mutex);
  276. }
  277. /*
  278. * Called by ALSA when a PCM substream is closed. Private data can be
  279. * freed here. The cpu DAI, codec DAI, machine and platform are also
  280. * shutdown.
  281. */
  282. static int soc_codec_close(struct snd_pcm_substream *substream)
  283. {
  284. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  285. struct snd_soc_device *socdev = rtd->socdev;
  286. struct snd_soc_dai_link *machine = rtd->dai;
  287. struct snd_soc_platform *platform = socdev->platform;
  288. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  289. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  290. struct snd_soc_codec *codec = socdev->codec;
  291. mutex_lock(&pcm_mutex);
  292. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  293. cpu_dai->playback.active = codec_dai->playback.active = 0;
  294. else
  295. cpu_dai->capture.active = codec_dai->capture.active = 0;
  296. if (codec_dai->playback.active == 0 &&
  297. codec_dai->capture.active == 0) {
  298. cpu_dai->active = codec_dai->active = 0;
  299. }
  300. codec->active--;
  301. if (cpu_dai->ops.shutdown)
  302. cpu_dai->ops.shutdown(substream);
  303. if (codec_dai->ops.shutdown)
  304. codec_dai->ops.shutdown(substream);
  305. if (machine->ops && machine->ops->shutdown)
  306. machine->ops->shutdown(substream);
  307. if (platform->pcm_ops->close)
  308. platform->pcm_ops->close(substream);
  309. cpu_dai->runtime = NULL;
  310. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  311. /* start delayed pop wq here for playback streams */
  312. codec_dai->pop_wait = 1;
  313. schedule_delayed_work(&socdev->delayed_work,
  314. msecs_to_jiffies(pmdown_time));
  315. } else {
  316. /* capture streams can be powered down now */
  317. snd_soc_dapm_stream_event(codec,
  318. codec_dai->capture.stream_name,
  319. SND_SOC_DAPM_STREAM_STOP);
  320. if (codec->active == 0 && codec_dai->pop_wait == 0)
  321. snd_soc_dapm_device_event(socdev,
  322. SNDRV_CTL_POWER_D3hot);
  323. }
  324. mutex_unlock(&pcm_mutex);
  325. return 0;
  326. }
  327. /*
  328. * Called by ALSA when the PCM substream is prepared, can set format, sample
  329. * rate, etc. This function is non atomic and can be called multiple times,
  330. * it can refer to the runtime info.
  331. */
  332. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  333. {
  334. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  335. struct snd_soc_device *socdev = rtd->socdev;
  336. struct snd_soc_dai_link *machine = rtd->dai;
  337. struct snd_soc_platform *platform = socdev->platform;
  338. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  339. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  340. struct snd_soc_codec *codec = socdev->codec;
  341. int ret = 0;
  342. mutex_lock(&pcm_mutex);
  343. if (machine->ops && machine->ops->prepare) {
  344. ret = machine->ops->prepare(substream);
  345. if (ret < 0) {
  346. printk(KERN_ERR "asoc: machine prepare error\n");
  347. goto out;
  348. }
  349. }
  350. if (platform->pcm_ops->prepare) {
  351. ret = platform->pcm_ops->prepare(substream);
  352. if (ret < 0) {
  353. printk(KERN_ERR "asoc: platform prepare error\n");
  354. goto out;
  355. }
  356. }
  357. if (codec_dai->ops.prepare) {
  358. ret = codec_dai->ops.prepare(substream);
  359. if (ret < 0) {
  360. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  361. goto out;
  362. }
  363. }
  364. if (cpu_dai->ops.prepare) {
  365. ret = cpu_dai->ops.prepare(substream);
  366. if (ret < 0) {
  367. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  368. goto out;
  369. }
  370. }
  371. /* we only want to start a DAPM playback stream if we are not waiting
  372. * on an existing one stopping */
  373. if (codec_dai->pop_wait) {
  374. /* we are waiting for the delayed work to start */
  375. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  376. snd_soc_dapm_stream_event(socdev->codec,
  377. codec_dai->capture.stream_name,
  378. SND_SOC_DAPM_STREAM_START);
  379. else {
  380. codec_dai->pop_wait = 0;
  381. cancel_delayed_work(&socdev->delayed_work);
  382. if (codec_dai->dai_ops.digital_mute)
  383. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  384. }
  385. } else {
  386. /* no delayed work - do we need to power up codec */
  387. if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
  388. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D1);
  389. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  390. snd_soc_dapm_stream_event(codec,
  391. codec_dai->playback.stream_name,
  392. SND_SOC_DAPM_STREAM_START);
  393. else
  394. snd_soc_dapm_stream_event(codec,
  395. codec_dai->capture.stream_name,
  396. SND_SOC_DAPM_STREAM_START);
  397. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D0);
  398. if (codec_dai->dai_ops.digital_mute)
  399. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  400. } else {
  401. /* codec already powered - power on widgets */
  402. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  403. snd_soc_dapm_stream_event(codec,
  404. codec_dai->playback.stream_name,
  405. SND_SOC_DAPM_STREAM_START);
  406. else
  407. snd_soc_dapm_stream_event(codec,
  408. codec_dai->capture.stream_name,
  409. SND_SOC_DAPM_STREAM_START);
  410. if (codec_dai->dai_ops.digital_mute)
  411. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  412. }
  413. }
  414. out:
  415. mutex_unlock(&pcm_mutex);
  416. return ret;
  417. }
  418. /*
  419. * Called by ALSA when the hardware params are set by application. This
  420. * function can also be called multiple times and can allocate buffers
  421. * (using snd_pcm_lib_* ). It's non-atomic.
  422. */
  423. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  424. struct snd_pcm_hw_params *params)
  425. {
  426. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  427. struct snd_soc_device *socdev = rtd->socdev;
  428. struct snd_soc_dai_link *machine = rtd->dai;
  429. struct snd_soc_platform *platform = socdev->platform;
  430. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  431. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  432. int ret = 0;
  433. mutex_lock(&pcm_mutex);
  434. if (machine->ops && machine->ops->hw_params) {
  435. ret = machine->ops->hw_params(substream, params);
  436. if (ret < 0) {
  437. printk(KERN_ERR "asoc: machine hw_params failed\n");
  438. goto out;
  439. }
  440. }
  441. if (codec_dai->ops.hw_params) {
  442. ret = codec_dai->ops.hw_params(substream, params);
  443. if (ret < 0) {
  444. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  445. codec_dai->name);
  446. goto codec_err;
  447. }
  448. }
  449. if (cpu_dai->ops.hw_params) {
  450. ret = cpu_dai->ops.hw_params(substream, params);
  451. if (ret < 0) {
  452. printk(KERN_ERR "asoc: can't set interface %s hw params\n",
  453. cpu_dai->name);
  454. goto interface_err;
  455. }
  456. }
  457. if (platform->pcm_ops->hw_params) {
  458. ret = platform->pcm_ops->hw_params(substream, params);
  459. if (ret < 0) {
  460. printk(KERN_ERR "asoc: can't set platform %s hw params\n",
  461. platform->name);
  462. goto platform_err;
  463. }
  464. }
  465. out:
  466. mutex_unlock(&pcm_mutex);
  467. return ret;
  468. platform_err:
  469. if (cpu_dai->ops.hw_free)
  470. cpu_dai->ops.hw_free(substream);
  471. interface_err:
  472. if (codec_dai->ops.hw_free)
  473. codec_dai->ops.hw_free(substream);
  474. codec_err:
  475. if(machine->ops && machine->ops->hw_free)
  476. machine->ops->hw_free(substream);
  477. mutex_unlock(&pcm_mutex);
  478. return ret;
  479. }
  480. /*
  481. * Free's resources allocated by hw_params, can be called multiple times
  482. */
  483. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  484. {
  485. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  486. struct snd_soc_device *socdev = rtd->socdev;
  487. struct snd_soc_dai_link *machine = rtd->dai;
  488. struct snd_soc_platform *platform = socdev->platform;
  489. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  490. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  491. struct snd_soc_codec *codec = socdev->codec;
  492. mutex_lock(&pcm_mutex);
  493. /* apply codec digital mute */
  494. if (!codec->active && codec_dai->dai_ops.digital_mute)
  495. codec_dai->dai_ops.digital_mute(codec_dai, 1);
  496. /* free any machine hw params */
  497. if (machine->ops && machine->ops->hw_free)
  498. machine->ops->hw_free(substream);
  499. /* free any DMA resources */
  500. if (platform->pcm_ops->hw_free)
  501. platform->pcm_ops->hw_free(substream);
  502. /* now free hw params for the DAI's */
  503. if (codec_dai->ops.hw_free)
  504. codec_dai->ops.hw_free(substream);
  505. if (cpu_dai->ops.hw_free)
  506. cpu_dai->ops.hw_free(substream);
  507. mutex_unlock(&pcm_mutex);
  508. return 0;
  509. }
  510. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  511. {
  512. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  513. struct snd_soc_device *socdev = rtd->socdev;
  514. struct snd_soc_dai_link *machine = rtd->dai;
  515. struct snd_soc_platform *platform = socdev->platform;
  516. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  517. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  518. int ret;
  519. if (codec_dai->ops.trigger) {
  520. ret = codec_dai->ops.trigger(substream, cmd);
  521. if (ret < 0)
  522. return ret;
  523. }
  524. if (platform->pcm_ops->trigger) {
  525. ret = platform->pcm_ops->trigger(substream, cmd);
  526. if (ret < 0)
  527. return ret;
  528. }
  529. if (cpu_dai->ops.trigger) {
  530. ret = cpu_dai->ops.trigger(substream, cmd);
  531. if (ret < 0)
  532. return ret;
  533. }
  534. return 0;
  535. }
  536. /* ASoC PCM operations */
  537. static struct snd_pcm_ops soc_pcm_ops = {
  538. .open = soc_pcm_open,
  539. .close = soc_codec_close,
  540. .hw_params = soc_pcm_hw_params,
  541. .hw_free = soc_pcm_hw_free,
  542. .prepare = soc_pcm_prepare,
  543. .trigger = soc_pcm_trigger,
  544. };
  545. #ifdef CONFIG_PM
  546. /* powers down audio subsystem for suspend */
  547. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  548. {
  549. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  550. struct snd_soc_machine *machine = socdev->machine;
  551. struct snd_soc_platform *platform = socdev->platform;
  552. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  553. struct snd_soc_codec *codec = socdev->codec;
  554. int i;
  555. /* mute any active DAC's */
  556. for(i = 0; i < machine->num_links; i++) {
  557. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  558. if (dai->dai_ops.digital_mute && dai->playback.active)
  559. dai->dai_ops.digital_mute(dai, 1);
  560. }
  561. /* suspend all pcms */
  562. for (i = 0; i < machine->num_links; i++)
  563. snd_pcm_suspend_all(machine->dai_link[i].pcm);
  564. if (machine->suspend_pre)
  565. machine->suspend_pre(pdev, state);
  566. for(i = 0; i < machine->num_links; i++) {
  567. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  568. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  569. cpu_dai->suspend(pdev, cpu_dai);
  570. if (platform->suspend)
  571. platform->suspend(pdev, cpu_dai);
  572. }
  573. /* close any waiting streams and save state */
  574. run_delayed_work(&socdev->delayed_work);
  575. codec->suspend_dapm_state = codec->dapm_state;
  576. for(i = 0; i < codec->num_dai; i++) {
  577. char *stream = codec->dai[i].playback.stream_name;
  578. if (stream != NULL)
  579. snd_soc_dapm_stream_event(codec, stream,
  580. SND_SOC_DAPM_STREAM_SUSPEND);
  581. stream = codec->dai[i].capture.stream_name;
  582. if (stream != NULL)
  583. snd_soc_dapm_stream_event(codec, stream,
  584. SND_SOC_DAPM_STREAM_SUSPEND);
  585. }
  586. if (codec_dev->suspend)
  587. codec_dev->suspend(pdev, state);
  588. for(i = 0; i < machine->num_links; i++) {
  589. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  590. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  591. cpu_dai->suspend(pdev, cpu_dai);
  592. }
  593. if (machine->suspend_post)
  594. machine->suspend_post(pdev, state);
  595. return 0;
  596. }
  597. /* powers up audio subsystem after a suspend */
  598. static int soc_resume(struct platform_device *pdev)
  599. {
  600. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  601. struct snd_soc_machine *machine = socdev->machine;
  602. struct snd_soc_platform *platform = socdev->platform;
  603. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  604. struct snd_soc_codec *codec = socdev->codec;
  605. int i;
  606. if (machine->resume_pre)
  607. machine->resume_pre(pdev);
  608. for(i = 0; i < machine->num_links; i++) {
  609. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  610. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  611. cpu_dai->resume(pdev, cpu_dai);
  612. }
  613. if (codec_dev->resume)
  614. codec_dev->resume(pdev);
  615. for(i = 0; i < codec->num_dai; i++) {
  616. char* stream = codec->dai[i].playback.stream_name;
  617. if (stream != NULL)
  618. snd_soc_dapm_stream_event(codec, stream,
  619. SND_SOC_DAPM_STREAM_RESUME);
  620. stream = codec->dai[i].capture.stream_name;
  621. if (stream != NULL)
  622. snd_soc_dapm_stream_event(codec, stream,
  623. SND_SOC_DAPM_STREAM_RESUME);
  624. }
  625. /* unmute any active DAC's */
  626. for(i = 0; i < machine->num_links; i++) {
  627. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  628. if (dai->dai_ops.digital_mute && dai->playback.active)
  629. dai->dai_ops.digital_mute(dai, 0);
  630. }
  631. for(i = 0; i < machine->num_links; i++) {
  632. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  633. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  634. cpu_dai->resume(pdev, cpu_dai);
  635. if (platform->resume)
  636. platform->resume(pdev, cpu_dai);
  637. }
  638. if (machine->resume_post)
  639. machine->resume_post(pdev);
  640. return 0;
  641. }
  642. #else
  643. #define soc_suspend NULL
  644. #define soc_resume NULL
  645. #endif
  646. /* probes a new socdev */
  647. static int soc_probe(struct platform_device *pdev)
  648. {
  649. int ret = 0, i;
  650. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  651. struct snd_soc_machine *machine = socdev->machine;
  652. struct snd_soc_platform *platform = socdev->platform;
  653. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  654. if (machine->probe) {
  655. ret = machine->probe(pdev);
  656. if(ret < 0)
  657. return ret;
  658. }
  659. for (i = 0; i < machine->num_links; i++) {
  660. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  661. if (cpu_dai->probe) {
  662. ret = cpu_dai->probe(pdev);
  663. if(ret < 0)
  664. goto cpu_dai_err;
  665. }
  666. }
  667. if (codec_dev->probe) {
  668. ret = codec_dev->probe(pdev);
  669. if(ret < 0)
  670. goto cpu_dai_err;
  671. }
  672. if (platform->probe) {
  673. ret = platform->probe(pdev);
  674. if(ret < 0)
  675. goto platform_err;
  676. }
  677. /* DAPM stream work */
  678. INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
  679. return 0;
  680. platform_err:
  681. if (codec_dev->remove)
  682. codec_dev->remove(pdev);
  683. cpu_dai_err:
  684. for (i--; i >= 0; i--) {
  685. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  686. if (cpu_dai->remove)
  687. cpu_dai->remove(pdev);
  688. }
  689. if (machine->remove)
  690. machine->remove(pdev);
  691. return ret;
  692. }
  693. /* removes a socdev */
  694. static int soc_remove(struct platform_device *pdev)
  695. {
  696. int i;
  697. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  698. struct snd_soc_machine *machine = socdev->machine;
  699. struct snd_soc_platform *platform = socdev->platform;
  700. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  701. run_delayed_work(&socdev->delayed_work);
  702. if (platform->remove)
  703. platform->remove(pdev);
  704. if (codec_dev->remove)
  705. codec_dev->remove(pdev);
  706. for (i = 0; i < machine->num_links; i++) {
  707. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  708. if (cpu_dai->remove)
  709. cpu_dai->remove(pdev);
  710. }
  711. if (machine->remove)
  712. machine->remove(pdev);
  713. return 0;
  714. }
  715. /* ASoC platform driver */
  716. static struct platform_driver soc_driver = {
  717. .driver = {
  718. .name = "soc-audio",
  719. .owner = THIS_MODULE,
  720. },
  721. .probe = soc_probe,
  722. .remove = soc_remove,
  723. .suspend = soc_suspend,
  724. .resume = soc_resume,
  725. };
  726. /* create a new pcm */
  727. static int soc_new_pcm(struct snd_soc_device *socdev,
  728. struct snd_soc_dai_link *dai_link, int num)
  729. {
  730. struct snd_soc_codec *codec = socdev->codec;
  731. struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
  732. struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
  733. struct snd_soc_pcm_runtime *rtd;
  734. struct snd_pcm *pcm;
  735. char new_name[64];
  736. int ret = 0, playback = 0, capture = 0;
  737. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  738. if (rtd == NULL)
  739. return -ENOMEM;
  740. rtd->dai = dai_link;
  741. rtd->socdev = socdev;
  742. codec_dai->codec = socdev->codec;
  743. /* check client and interface hw capabilities */
  744. sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
  745. get_dai_name(cpu_dai->type), num);
  746. if (codec_dai->playback.channels_min)
  747. playback = 1;
  748. if (codec_dai->capture.channels_min)
  749. capture = 1;
  750. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  751. capture, &pcm);
  752. if (ret < 0) {
  753. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  754. kfree(rtd);
  755. return ret;
  756. }
  757. dai_link->pcm = pcm;
  758. pcm->private_data = rtd;
  759. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  760. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  761. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  762. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  763. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  764. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  765. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  766. if (playback)
  767. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  768. if (capture)
  769. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  770. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  771. if (ret < 0) {
  772. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  773. kfree(rtd);
  774. return ret;
  775. }
  776. pcm->private_free = socdev->platform->pcm_free;
  777. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  778. cpu_dai->name);
  779. return ret;
  780. }
  781. /* codec register dump */
  782. static ssize_t codec_reg_show(struct device *dev,
  783. struct device_attribute *attr, char *buf)
  784. {
  785. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  786. struct snd_soc_codec *codec = devdata->codec;
  787. int i, step = 1, count = 0;
  788. if (!codec->reg_cache_size)
  789. return 0;
  790. if (codec->reg_cache_step)
  791. step = codec->reg_cache_step;
  792. count += sprintf(buf, "%s registers\n", codec->name);
  793. for(i = 0; i < codec->reg_cache_size; i += step)
  794. count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
  795. return count;
  796. }
  797. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  798. /**
  799. * snd_soc_new_ac97_codec - initailise AC97 device
  800. * @codec: audio codec
  801. * @ops: AC97 bus operations
  802. * @num: AC97 codec number
  803. *
  804. * Initialises AC97 codec resources for use by ad-hoc devices only.
  805. */
  806. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  807. struct snd_ac97_bus_ops *ops, int num)
  808. {
  809. mutex_lock(&codec->mutex);
  810. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  811. if (codec->ac97 == NULL) {
  812. mutex_unlock(&codec->mutex);
  813. return -ENOMEM;
  814. }
  815. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  816. if (codec->ac97->bus == NULL) {
  817. kfree(codec->ac97);
  818. codec->ac97 = NULL;
  819. mutex_unlock(&codec->mutex);
  820. return -ENOMEM;
  821. }
  822. codec->ac97->bus->ops = ops;
  823. codec->ac97->num = num;
  824. mutex_unlock(&codec->mutex);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  828. /**
  829. * snd_soc_free_ac97_codec - free AC97 codec device
  830. * @codec: audio codec
  831. *
  832. * Frees AC97 codec device resources.
  833. */
  834. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  835. {
  836. mutex_lock(&codec->mutex);
  837. kfree(codec->ac97->bus);
  838. kfree(codec->ac97);
  839. codec->ac97 = NULL;
  840. mutex_unlock(&codec->mutex);
  841. }
  842. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  843. /**
  844. * snd_soc_update_bits - update codec register bits
  845. * @codec: audio codec
  846. * @reg: codec register
  847. * @mask: register mask
  848. * @value: new value
  849. *
  850. * Writes new register value.
  851. *
  852. * Returns 1 for change else 0.
  853. */
  854. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  855. unsigned short mask, unsigned short value)
  856. {
  857. int change;
  858. unsigned short old, new;
  859. mutex_lock(&io_mutex);
  860. old = snd_soc_read(codec, reg);
  861. new = (old & ~mask) | value;
  862. change = old != new;
  863. if (change)
  864. snd_soc_write(codec, reg, new);
  865. mutex_unlock(&io_mutex);
  866. return change;
  867. }
  868. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  869. /**
  870. * snd_soc_test_bits - test register for change
  871. * @codec: audio codec
  872. * @reg: codec register
  873. * @mask: register mask
  874. * @value: new value
  875. *
  876. * Tests a register with a new value and checks if the new value is
  877. * different from the old value.
  878. *
  879. * Returns 1 for change else 0.
  880. */
  881. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  882. unsigned short mask, unsigned short value)
  883. {
  884. int change;
  885. unsigned short old, new;
  886. mutex_lock(&io_mutex);
  887. old = snd_soc_read(codec, reg);
  888. new = (old & ~mask) | value;
  889. change = old != new;
  890. mutex_unlock(&io_mutex);
  891. return change;
  892. }
  893. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  894. /**
  895. * snd_soc_new_pcms - create new sound card and pcms
  896. * @socdev: the SoC audio device
  897. *
  898. * Create a new sound card based upon the codec and interface pcms.
  899. *
  900. * Returns 0 for success, else error.
  901. */
  902. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  903. {
  904. struct snd_soc_codec *codec = socdev->codec;
  905. struct snd_soc_machine *machine = socdev->machine;
  906. int ret = 0, i;
  907. mutex_lock(&codec->mutex);
  908. /* register a sound card */
  909. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  910. if (!codec->card) {
  911. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  912. codec->name);
  913. mutex_unlock(&codec->mutex);
  914. return -ENODEV;
  915. }
  916. codec->card->dev = socdev->dev;
  917. codec->card->private_data = codec;
  918. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  919. /* create the pcms */
  920. for(i = 0; i < machine->num_links; i++) {
  921. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  922. if (ret < 0) {
  923. printk(KERN_ERR "asoc: can't create pcm %s\n",
  924. machine->dai_link[i].stream_name);
  925. mutex_unlock(&codec->mutex);
  926. return ret;
  927. }
  928. }
  929. mutex_unlock(&codec->mutex);
  930. return ret;
  931. }
  932. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  933. /**
  934. * snd_soc_register_card - register sound card
  935. * @socdev: the SoC audio device
  936. *
  937. * Register a SoC sound card. Also registers an AC97 device if the
  938. * codec is AC97 for ad hoc devices.
  939. *
  940. * Returns 0 for success, else error.
  941. */
  942. int snd_soc_register_card(struct snd_soc_device *socdev)
  943. {
  944. struct snd_soc_codec *codec = socdev->codec;
  945. struct snd_soc_machine *machine = socdev->machine;
  946. int ret = 0, i, ac97 = 0, err = 0;
  947. for(i = 0; i < machine->num_links; i++) {
  948. if (socdev->machine->dai_link[i].init) {
  949. err = socdev->machine->dai_link[i].init(codec);
  950. if (err < 0) {
  951. printk(KERN_ERR "asoc: failed to init %s\n",
  952. socdev->machine->dai_link[i].stream_name);
  953. continue;
  954. }
  955. }
  956. if (socdev->machine->dai_link[i].codec_dai->type ==
  957. SND_SOC_DAI_AC97_BUS)
  958. ac97 = 1;
  959. }
  960. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  961. "%s", machine->name);
  962. snprintf(codec->card->longname, sizeof(codec->card->longname),
  963. "%s (%s)", machine->name, codec->name);
  964. ret = snd_card_register(codec->card);
  965. if (ret < 0) {
  966. printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
  967. codec->name);
  968. goto out;
  969. }
  970. mutex_lock(&codec->mutex);
  971. #ifdef CONFIG_SND_SOC_AC97_BUS
  972. if (ac97) {
  973. ret = soc_ac97_dev_register(codec);
  974. if (ret < 0) {
  975. printk(KERN_ERR "asoc: AC97 device register failed\n");
  976. snd_card_free(codec->card);
  977. mutex_unlock(&codec->mutex);
  978. goto out;
  979. }
  980. }
  981. #endif
  982. err = snd_soc_dapm_sys_add(socdev->dev);
  983. if (err < 0)
  984. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  985. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  986. if (err < 0)
  987. printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
  988. mutex_unlock(&codec->mutex);
  989. out:
  990. return ret;
  991. }
  992. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  993. /**
  994. * snd_soc_free_pcms - free sound card and pcms
  995. * @socdev: the SoC audio device
  996. *
  997. * Frees sound card and pcms associated with the socdev.
  998. * Also unregister the codec if it is an AC97 device.
  999. */
  1000. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1001. {
  1002. struct snd_soc_codec *codec = socdev->codec;
  1003. #ifdef CONFIG_SND_SOC_AC97_BUS
  1004. struct snd_soc_codec_dai *codec_dai;
  1005. int i;
  1006. #endif
  1007. mutex_lock(&codec->mutex);
  1008. #ifdef CONFIG_SND_SOC_AC97_BUS
  1009. for(i = 0; i < codec->num_dai; i++) {
  1010. codec_dai = &codec->dai[i];
  1011. if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
  1012. soc_ac97_dev_unregister(codec);
  1013. goto free_card;
  1014. }
  1015. }
  1016. free_card:
  1017. #endif
  1018. if (codec->card)
  1019. snd_card_free(codec->card);
  1020. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1021. mutex_unlock(&codec->mutex);
  1022. }
  1023. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1024. /**
  1025. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1026. * @substream: the pcm substream
  1027. * @hw: the hardware parameters
  1028. *
  1029. * Sets the substream runtime hardware parameters.
  1030. */
  1031. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1032. const struct snd_pcm_hardware *hw)
  1033. {
  1034. struct snd_pcm_runtime *runtime = substream->runtime;
  1035. runtime->hw.info = hw->info;
  1036. runtime->hw.formats = hw->formats;
  1037. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1038. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1039. runtime->hw.periods_min = hw->periods_min;
  1040. runtime->hw.periods_max = hw->periods_max;
  1041. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1042. runtime->hw.fifo_size = hw->fifo_size;
  1043. return 0;
  1044. }
  1045. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1046. /**
  1047. * snd_soc_cnew - create new control
  1048. * @_template: control template
  1049. * @data: control private data
  1050. * @lnng_name: control long name
  1051. *
  1052. * Create a new mixer control from a template control.
  1053. *
  1054. * Returns 0 for success, else error.
  1055. */
  1056. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1057. void *data, char *long_name)
  1058. {
  1059. struct snd_kcontrol_new template;
  1060. memcpy(&template, _template, sizeof(template));
  1061. if (long_name)
  1062. template.name = long_name;
  1063. template.index = 0;
  1064. return snd_ctl_new1(&template, data);
  1065. }
  1066. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1067. /**
  1068. * snd_soc_info_enum_double - enumerated double mixer info callback
  1069. * @kcontrol: mixer control
  1070. * @uinfo: control element information
  1071. *
  1072. * Callback to provide information about a double enumerated
  1073. * mixer control.
  1074. *
  1075. * Returns 0 for success.
  1076. */
  1077. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1078. struct snd_ctl_elem_info *uinfo)
  1079. {
  1080. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1081. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1082. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1083. uinfo->value.enumerated.items = e->mask;
  1084. if (uinfo->value.enumerated.item > e->mask - 1)
  1085. uinfo->value.enumerated.item = e->mask - 1;
  1086. strcpy(uinfo->value.enumerated.name,
  1087. e->texts[uinfo->value.enumerated.item]);
  1088. return 0;
  1089. }
  1090. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1091. /**
  1092. * snd_soc_get_enum_double - enumerated double mixer get callback
  1093. * @kcontrol: mixer control
  1094. * @uinfo: control element information
  1095. *
  1096. * Callback to get the value of a double enumerated mixer.
  1097. *
  1098. * Returns 0 for success.
  1099. */
  1100. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1101. struct snd_ctl_elem_value *ucontrol)
  1102. {
  1103. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1104. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1105. unsigned short val, bitmask;
  1106. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1107. ;
  1108. val = snd_soc_read(codec, e->reg);
  1109. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1110. if (e->shift_l != e->shift_r)
  1111. ucontrol->value.enumerated.item[1] =
  1112. (val >> e->shift_r) & (bitmask - 1);
  1113. return 0;
  1114. }
  1115. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1116. /**
  1117. * snd_soc_put_enum_double - enumerated double mixer put callback
  1118. * @kcontrol: mixer control
  1119. * @uinfo: control element information
  1120. *
  1121. * Callback to set the value of a double enumerated mixer.
  1122. *
  1123. * Returns 0 for success.
  1124. */
  1125. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1126. struct snd_ctl_elem_value *ucontrol)
  1127. {
  1128. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1129. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1130. unsigned short val;
  1131. unsigned short mask, bitmask;
  1132. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1133. ;
  1134. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1135. return -EINVAL;
  1136. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1137. mask = (bitmask - 1) << e->shift_l;
  1138. if (e->shift_l != e->shift_r) {
  1139. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1140. return -EINVAL;
  1141. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1142. mask |= (bitmask - 1) << e->shift_r;
  1143. }
  1144. return snd_soc_update_bits(codec, e->reg, mask, val);
  1145. }
  1146. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1147. /**
  1148. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1149. * @kcontrol: mixer control
  1150. * @uinfo: control element information
  1151. *
  1152. * Callback to provide information about an external enumerated
  1153. * single mixer.
  1154. *
  1155. * Returns 0 for success.
  1156. */
  1157. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1158. struct snd_ctl_elem_info *uinfo)
  1159. {
  1160. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1161. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1162. uinfo->count = 1;
  1163. uinfo->value.enumerated.items = e->mask;
  1164. if (uinfo->value.enumerated.item > e->mask - 1)
  1165. uinfo->value.enumerated.item = e->mask - 1;
  1166. strcpy(uinfo->value.enumerated.name,
  1167. e->texts[uinfo->value.enumerated.item]);
  1168. return 0;
  1169. }
  1170. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1171. /**
  1172. * snd_soc_info_volsw_ext - external single mixer info callback
  1173. * @kcontrol: mixer control
  1174. * @uinfo: control element information
  1175. *
  1176. * Callback to provide information about a single external mixer control.
  1177. *
  1178. * Returns 0 for success.
  1179. */
  1180. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1181. struct snd_ctl_elem_info *uinfo)
  1182. {
  1183. int max = kcontrol->private_value;
  1184. if (max == 1)
  1185. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1186. else
  1187. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1188. uinfo->count = 1;
  1189. uinfo->value.integer.min = 0;
  1190. uinfo->value.integer.max = max;
  1191. return 0;
  1192. }
  1193. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1194. /**
  1195. * snd_soc_info_volsw - single mixer info callback
  1196. * @kcontrol: mixer control
  1197. * @uinfo: control element information
  1198. *
  1199. * Callback to provide information about a single mixer control.
  1200. *
  1201. * Returns 0 for success.
  1202. */
  1203. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1204. struct snd_ctl_elem_info *uinfo)
  1205. {
  1206. int max = (kcontrol->private_value >> 16) & 0xff;
  1207. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1208. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1209. if (max == 1)
  1210. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1211. else
  1212. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1213. uinfo->count = shift == rshift ? 1 : 2;
  1214. uinfo->value.integer.min = 0;
  1215. uinfo->value.integer.max = max;
  1216. return 0;
  1217. }
  1218. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1219. /**
  1220. * snd_soc_get_volsw - single mixer get callback
  1221. * @kcontrol: mixer control
  1222. * @uinfo: control element information
  1223. *
  1224. * Callback to get the value of a single mixer control.
  1225. *
  1226. * Returns 0 for success.
  1227. */
  1228. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1229. struct snd_ctl_elem_value *ucontrol)
  1230. {
  1231. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1232. int reg = kcontrol->private_value & 0xff;
  1233. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1234. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1235. int max = (kcontrol->private_value >> 16) & 0xff;
  1236. int mask = (1 << fls(max)) - 1;
  1237. int invert = (kcontrol->private_value >> 24) & 0x01;
  1238. ucontrol->value.integer.value[0] =
  1239. (snd_soc_read(codec, reg) >> shift) & mask;
  1240. if (shift != rshift)
  1241. ucontrol->value.integer.value[1] =
  1242. (snd_soc_read(codec, reg) >> rshift) & mask;
  1243. if (invert) {
  1244. ucontrol->value.integer.value[0] =
  1245. max - ucontrol->value.integer.value[0];
  1246. if (shift != rshift)
  1247. ucontrol->value.integer.value[1] =
  1248. max - ucontrol->value.integer.value[1];
  1249. }
  1250. return 0;
  1251. }
  1252. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1253. /**
  1254. * snd_soc_put_volsw - single mixer put callback
  1255. * @kcontrol: mixer control
  1256. * @uinfo: control element information
  1257. *
  1258. * Callback to set the value of a single mixer control.
  1259. *
  1260. * Returns 0 for success.
  1261. */
  1262. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1263. struct snd_ctl_elem_value *ucontrol)
  1264. {
  1265. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1266. int reg = kcontrol->private_value & 0xff;
  1267. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1268. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1269. int max = (kcontrol->private_value >> 16) & 0xff;
  1270. int mask = (1 << fls(max)) - 1;
  1271. int invert = (kcontrol->private_value >> 24) & 0x01;
  1272. unsigned short val, val2, val_mask;
  1273. val = (ucontrol->value.integer.value[0] & mask);
  1274. if (invert)
  1275. val = max - val;
  1276. val_mask = mask << shift;
  1277. val = val << shift;
  1278. if (shift != rshift) {
  1279. val2 = (ucontrol->value.integer.value[1] & mask);
  1280. if (invert)
  1281. val2 = max - val2;
  1282. val_mask |= mask << rshift;
  1283. val |= val2 << rshift;
  1284. }
  1285. return snd_soc_update_bits(codec, reg, val_mask, val);
  1286. }
  1287. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1288. /**
  1289. * snd_soc_info_volsw_2r - double mixer info callback
  1290. * @kcontrol: mixer control
  1291. * @uinfo: control element information
  1292. *
  1293. * Callback to provide information about a double mixer control that
  1294. * spans 2 codec registers.
  1295. *
  1296. * Returns 0 for success.
  1297. */
  1298. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1299. struct snd_ctl_elem_info *uinfo)
  1300. {
  1301. int max = (kcontrol->private_value >> 12) & 0xff;
  1302. if (max == 1)
  1303. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1304. else
  1305. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1306. uinfo->count = 2;
  1307. uinfo->value.integer.min = 0;
  1308. uinfo->value.integer.max = max;
  1309. return 0;
  1310. }
  1311. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1312. /**
  1313. * snd_soc_get_volsw_2r - double mixer get callback
  1314. * @kcontrol: mixer control
  1315. * @uinfo: control element information
  1316. *
  1317. * Callback to get the value of a double mixer control that spans 2 registers.
  1318. *
  1319. * Returns 0 for success.
  1320. */
  1321. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1322. struct snd_ctl_elem_value *ucontrol)
  1323. {
  1324. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1325. int reg = kcontrol->private_value & 0xff;
  1326. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1327. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1328. int max = (kcontrol->private_value >> 12) & 0xff;
  1329. int mask = (1<<fls(max))-1;
  1330. int invert = (kcontrol->private_value >> 20) & 0x01;
  1331. ucontrol->value.integer.value[0] =
  1332. (snd_soc_read(codec, reg) >> shift) & mask;
  1333. ucontrol->value.integer.value[1] =
  1334. (snd_soc_read(codec, reg2) >> shift) & mask;
  1335. if (invert) {
  1336. ucontrol->value.integer.value[0] =
  1337. max - ucontrol->value.integer.value[0];
  1338. ucontrol->value.integer.value[1] =
  1339. max - ucontrol->value.integer.value[1];
  1340. }
  1341. return 0;
  1342. }
  1343. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1344. /**
  1345. * snd_soc_put_volsw_2r - double mixer set callback
  1346. * @kcontrol: mixer control
  1347. * @uinfo: control element information
  1348. *
  1349. * Callback to set the value of a double mixer control that spans 2 registers.
  1350. *
  1351. * Returns 0 for success.
  1352. */
  1353. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1354. struct snd_ctl_elem_value *ucontrol)
  1355. {
  1356. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1357. int reg = kcontrol->private_value & 0xff;
  1358. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1359. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1360. int max = (kcontrol->private_value >> 12) & 0xff;
  1361. int mask = (1 << fls(max)) - 1;
  1362. int invert = (kcontrol->private_value >> 20) & 0x01;
  1363. int err;
  1364. unsigned short val, val2, val_mask;
  1365. val_mask = mask << shift;
  1366. val = (ucontrol->value.integer.value[0] & mask);
  1367. val2 = (ucontrol->value.integer.value[1] & mask);
  1368. if (invert) {
  1369. val = max - val;
  1370. val2 = max - val2;
  1371. }
  1372. val = val << shift;
  1373. val2 = val2 << shift;
  1374. if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
  1375. return err;
  1376. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1377. return err;
  1378. }
  1379. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1380. static int __devinit snd_soc_init(void)
  1381. {
  1382. printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
  1383. return platform_driver_register(&soc_driver);
  1384. }
  1385. static void snd_soc_exit(void)
  1386. {
  1387. platform_driver_unregister(&soc_driver);
  1388. }
  1389. module_init(snd_soc_init);
  1390. module_exit(snd_soc_exit);
  1391. /* Module information */
  1392. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1393. MODULE_DESCRIPTION("ALSA SoC Core");
  1394. MODULE_LICENSE("GPL");
  1395. MODULE_ALIAS("platform:soc-audio");