raumfeld.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * raumfeld_audio.c -- SoC audio for Raumfeld audio devices
  3. *
  4. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  5. *
  6. * based on code from:
  7. *
  8. * Wolfson Microelectronics PLC.
  9. * Openedhand Ltd.
  10. * Liam Girdwood <lrg@slimlogic.co.uk>
  11. * Richard Purdie <richard@openedhand.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/delay.h>
  21. #include <linux/gpio.h>
  22. #include <sound/pcm.h>
  23. #include <sound/soc.h>
  24. #include <asm/mach-types.h>
  25. #include "pxa-ssp.h"
  26. #define GPIO_SPDIF_RESET (38)
  27. #define GPIO_MCLK_RESET (111)
  28. #define GPIO_CODEC_RESET (120)
  29. static struct i2c_client *max9486_client;
  30. static struct i2c_board_info max9486_hwmon_info = {
  31. I2C_BOARD_INFO("max9485", 0x63),
  32. };
  33. #define MAX9485_MCLK_FREQ_112896 0x22
  34. #define MAX9485_MCLK_FREQ_122880 0x23
  35. #define MAX9485_MCLK_FREQ_225792 0x32
  36. #define MAX9485_MCLK_FREQ_245760 0x33
  37. static void set_max9485_clk(char clk)
  38. {
  39. i2c_master_send(max9486_client, &clk, 1);
  40. }
  41. static void raumfeld_enable_audio(bool en)
  42. {
  43. if (en) {
  44. gpio_set_value(GPIO_MCLK_RESET, 1);
  45. /* wait some time to let the clocks become stable */
  46. msleep(100);
  47. gpio_set_value(GPIO_SPDIF_RESET, 1);
  48. gpio_set_value(GPIO_CODEC_RESET, 1);
  49. } else {
  50. gpio_set_value(GPIO_MCLK_RESET, 0);
  51. gpio_set_value(GPIO_SPDIF_RESET, 0);
  52. gpio_set_value(GPIO_CODEC_RESET, 0);
  53. }
  54. }
  55. /* CS4270 */
  56. static int raumfeld_cs4270_startup(struct snd_pcm_substream *substream)
  57. {
  58. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  59. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  60. /* set freq to 0 to enable all possible codec sample rates */
  61. return snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
  62. }
  63. static void raumfeld_cs4270_shutdown(struct snd_pcm_substream *substream)
  64. {
  65. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  66. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  67. /* set freq to 0 to enable all possible codec sample rates */
  68. snd_soc_dai_set_sysclk(codec_dai, 0, 0, 0);
  69. }
  70. static int raumfeld_cs4270_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params)
  72. {
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  75. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  76. unsigned int clk = 0;
  77. int ret = 0;
  78. switch (params_rate(params)) {
  79. case 44100:
  80. set_max9485_clk(MAX9485_MCLK_FREQ_112896);
  81. clk = 11289600;
  82. break;
  83. case 48000:
  84. set_max9485_clk(MAX9485_MCLK_FREQ_122880);
  85. clk = 12288000;
  86. break;
  87. case 88200:
  88. set_max9485_clk(MAX9485_MCLK_FREQ_225792);
  89. clk = 22579200;
  90. break;
  91. case 96000:
  92. set_max9485_clk(MAX9485_MCLK_FREQ_245760);
  93. clk = 24576000;
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. ret = snd_soc_dai_set_sysclk(codec_dai, 0, clk, 0);
  99. if (ret < 0)
  100. return ret;
  101. /* setup the CPU DAI */
  102. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
  103. if (ret < 0)
  104. return ret;
  105. ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
  106. if (ret < 0)
  107. return ret;
  108. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
  109. if (ret < 0)
  110. return ret;
  111. return 0;
  112. }
  113. static struct snd_soc_ops raumfeld_cs4270_ops = {
  114. .startup = raumfeld_cs4270_startup,
  115. .shutdown = raumfeld_cs4270_shutdown,
  116. .hw_params = raumfeld_cs4270_hw_params,
  117. };
  118. static int raumfeld_analog_suspend(struct snd_soc_card *card)
  119. {
  120. raumfeld_enable_audio(false);
  121. return 0;
  122. }
  123. static int raumfeld_analog_resume(struct snd_soc_card *card)
  124. {
  125. raumfeld_enable_audio(true);
  126. return 0;
  127. }
  128. /* AK4104 */
  129. static int raumfeld_ak4104_hw_params(struct snd_pcm_substream *substream,
  130. struct snd_pcm_hw_params *params)
  131. {
  132. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  133. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  134. int ret = 0, clk = 0;
  135. switch (params_rate(params)) {
  136. case 44100:
  137. set_max9485_clk(MAX9485_MCLK_FREQ_112896);
  138. clk = 11289600;
  139. break;
  140. case 48000:
  141. set_max9485_clk(MAX9485_MCLK_FREQ_122880);
  142. clk = 12288000;
  143. break;
  144. case 88200:
  145. set_max9485_clk(MAX9485_MCLK_FREQ_225792);
  146. clk = 22579200;
  147. break;
  148. case 96000:
  149. set_max9485_clk(MAX9485_MCLK_FREQ_245760);
  150. clk = 24576000;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. /* setup the CPU DAI */
  156. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
  157. if (ret < 0)
  158. return ret;
  159. ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
  160. if (ret < 0)
  161. return ret;
  162. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
  163. if (ret < 0)
  164. return ret;
  165. return 0;
  166. }
  167. static struct snd_soc_ops raumfeld_ak4104_ops = {
  168. .hw_params = raumfeld_ak4104_hw_params,
  169. };
  170. #define DAI_LINK_CS4270 \
  171. { \
  172. .name = "CS4270", \
  173. .stream_name = "CS4270", \
  174. .cpu_dai_name = "pxa-ssp-dai.0", \
  175. .platform_name = "pxa-pcm-audio", \
  176. .codec_dai_name = "cs4270-hifi", \
  177. .codec_name = "cs4270.0-0048", \
  178. .dai_fmt = SND_SOC_DAIFMT_I2S | \
  179. SND_SOC_DAIFMT_NB_NF | \
  180. SND_SOC_DAIFMT_CBS_CFS, \
  181. .ops = &raumfeld_cs4270_ops, \
  182. }
  183. #define DAI_LINK_AK4104 \
  184. { \
  185. .name = "ak4104", \
  186. .stream_name = "Playback", \
  187. .cpu_dai_name = "pxa-ssp-dai.1", \
  188. .codec_dai_name = "ak4104-hifi", \
  189. .platform_name = "pxa-pcm-audio", \
  190. .dai_fmt = SND_SOC_DAIFMT_I2S | \
  191. SND_SOC_DAIFMT_NB_NF | \
  192. SND_SOC_DAIFMT_CBS_CFS, \
  193. .ops = &raumfeld_ak4104_ops, \
  194. .codec_name = "spi0.0", \
  195. }
  196. static struct snd_soc_dai_link snd_soc_raumfeld_connector_dai[] =
  197. {
  198. DAI_LINK_CS4270,
  199. DAI_LINK_AK4104,
  200. };
  201. static struct snd_soc_dai_link snd_soc_raumfeld_speaker_dai[] =
  202. {
  203. DAI_LINK_CS4270,
  204. };
  205. static struct snd_soc_card snd_soc_raumfeld_connector = {
  206. .name = "Raumfeld Connector",
  207. .owner = THIS_MODULE,
  208. .dai_link = snd_soc_raumfeld_connector_dai,
  209. .num_links = ARRAY_SIZE(snd_soc_raumfeld_connector_dai),
  210. .suspend_post = raumfeld_analog_suspend,
  211. .resume_pre = raumfeld_analog_resume,
  212. };
  213. static struct snd_soc_card snd_soc_raumfeld_speaker = {
  214. .name = "Raumfeld Speaker",
  215. .owner = THIS_MODULE,
  216. .dai_link = snd_soc_raumfeld_speaker_dai,
  217. .num_links = ARRAY_SIZE(snd_soc_raumfeld_speaker_dai),
  218. .suspend_post = raumfeld_analog_suspend,
  219. .resume_pre = raumfeld_analog_resume,
  220. };
  221. static struct platform_device *raumfeld_audio_device;
  222. static int __init raumfeld_audio_init(void)
  223. {
  224. int ret;
  225. if (!machine_is_raumfeld_speaker() &&
  226. !machine_is_raumfeld_connector())
  227. return 0;
  228. max9486_client = i2c_new_device(i2c_get_adapter(0),
  229. &max9486_hwmon_info);
  230. if (!max9486_client)
  231. return -ENOMEM;
  232. set_max9485_clk(MAX9485_MCLK_FREQ_122880);
  233. /* Register analog device */
  234. raumfeld_audio_device = platform_device_alloc("soc-audio", 0);
  235. if (!raumfeld_audio_device)
  236. return -ENOMEM;
  237. if (machine_is_raumfeld_speaker())
  238. platform_set_drvdata(raumfeld_audio_device,
  239. &snd_soc_raumfeld_speaker);
  240. if (machine_is_raumfeld_connector())
  241. platform_set_drvdata(raumfeld_audio_device,
  242. &snd_soc_raumfeld_connector);
  243. ret = platform_device_add(raumfeld_audio_device);
  244. if (ret < 0) {
  245. platform_device_put(raumfeld_audio_device);
  246. return ret;
  247. }
  248. raumfeld_enable_audio(true);
  249. return 0;
  250. }
  251. static void __exit raumfeld_audio_exit(void)
  252. {
  253. raumfeld_enable_audio(false);
  254. platform_device_unregister(raumfeld_audio_device);
  255. i2c_unregister_device(max9486_client);
  256. gpio_free(GPIO_MCLK_RESET);
  257. gpio_free(GPIO_CODEC_RESET);
  258. gpio_free(GPIO_SPDIF_RESET);
  259. }
  260. module_init(raumfeld_audio_init);
  261. module_exit(raumfeld_audio_exit);
  262. /* Module information */
  263. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  264. MODULE_DESCRIPTION("Raumfeld audio SoC");
  265. MODULE_LICENSE("GPL");