soc-core.c 43 KB

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