raumfeld.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 fmt, 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. fmt = SND_SOC_DAIFMT_I2S |
  99. SND_SOC_DAIFMT_NB_NF |
  100. SND_SOC_DAIFMT_CBS_CFS;
  101. /* setup the CODEC DAI */
  102. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  103. if (ret < 0)
  104. return ret;
  105. ret = snd_soc_dai_set_sysclk(codec_dai, 0, clk, 0);
  106. if (ret < 0)
  107. return ret;
  108. /* setup the CPU DAI */
  109. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
  110. if (ret < 0)
  111. return ret;
  112. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  113. if (ret < 0)
  114. return ret;
  115. ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
  116. if (ret < 0)
  117. return ret;
  118. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
  119. if (ret < 0)
  120. return ret;
  121. return 0;
  122. }
  123. static struct snd_soc_ops raumfeld_cs4270_ops = {
  124. .startup = raumfeld_cs4270_startup,
  125. .shutdown = raumfeld_cs4270_shutdown,
  126. .hw_params = raumfeld_cs4270_hw_params,
  127. };
  128. static int raumfeld_analog_suspend(struct snd_soc_card *card)
  129. {
  130. raumfeld_enable_audio(false);
  131. return 0;
  132. }
  133. static int raumfeld_analog_resume(struct snd_soc_card *card)
  134. {
  135. raumfeld_enable_audio(true);
  136. return 0;
  137. }
  138. /* AK4104 */
  139. static int raumfeld_ak4104_hw_params(struct snd_pcm_substream *substream,
  140. struct snd_pcm_hw_params *params)
  141. {
  142. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  143. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  144. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  145. int fmt, ret = 0, clk = 0;
  146. switch (params_rate(params)) {
  147. case 44100:
  148. set_max9485_clk(MAX9485_MCLK_FREQ_112896);
  149. clk = 11289600;
  150. break;
  151. case 48000:
  152. set_max9485_clk(MAX9485_MCLK_FREQ_122880);
  153. clk = 12288000;
  154. break;
  155. case 88200:
  156. set_max9485_clk(MAX9485_MCLK_FREQ_225792);
  157. clk = 22579200;
  158. break;
  159. case 96000:
  160. set_max9485_clk(MAX9485_MCLK_FREQ_245760);
  161. clk = 24576000;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF;
  167. /* setup the CODEC DAI */
  168. ret = snd_soc_dai_set_fmt(codec_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
  169. if (ret < 0)
  170. return ret;
  171. /* setup the CPU DAI */
  172. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, clk);
  173. if (ret < 0)
  174. return ret;
  175. ret = snd_soc_dai_set_fmt(cpu_dai, fmt | SND_SOC_DAIFMT_CBS_CFS);
  176. if (ret < 0)
  177. return ret;
  178. ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_DIV_SCR, 4);
  179. if (ret < 0)
  180. return ret;
  181. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_EXT, clk, 1);
  182. if (ret < 0)
  183. return ret;
  184. return 0;
  185. }
  186. static struct snd_soc_ops raumfeld_ak4104_ops = {
  187. .hw_params = raumfeld_ak4104_hw_params,
  188. };
  189. #define DAI_LINK_CS4270 \
  190. { \
  191. .name = "CS4270", \
  192. .stream_name = "CS4270", \
  193. .cpu_dai_name = "pxa-ssp-dai.0", \
  194. .platform_name = "pxa-pcm-audio", \
  195. .codec_dai_name = "cs4270-hifi", \
  196. .codec_name = "cs4270.0-0048", \
  197. .ops = &raumfeld_cs4270_ops, \
  198. }
  199. #define DAI_LINK_AK4104 \
  200. { \
  201. .name = "ak4104", \
  202. .stream_name = "Playback", \
  203. .cpu_dai_name = "pxa-ssp-dai.1", \
  204. .codec_dai_name = "ak4104-hifi", \
  205. .platform_name = "pxa-pcm-audio", \
  206. .ops = &raumfeld_ak4104_ops, \
  207. .codec_name = "spi0.0", \
  208. }
  209. static struct snd_soc_dai_link snd_soc_raumfeld_connector_dai[] =
  210. {
  211. DAI_LINK_CS4270,
  212. DAI_LINK_AK4104,
  213. };
  214. static struct snd_soc_dai_link snd_soc_raumfeld_speaker_dai[] =
  215. {
  216. DAI_LINK_CS4270,
  217. };
  218. static struct snd_soc_card snd_soc_raumfeld_connector = {
  219. .name = "Raumfeld Connector",
  220. .owner = THIS_MODULE,
  221. .dai_link = snd_soc_raumfeld_connector_dai,
  222. .num_links = ARRAY_SIZE(snd_soc_raumfeld_connector_dai),
  223. .suspend_post = raumfeld_analog_suspend,
  224. .resume_pre = raumfeld_analog_resume,
  225. };
  226. static struct snd_soc_card snd_soc_raumfeld_speaker = {
  227. .name = "Raumfeld Speaker",
  228. .owner = THIS_MODULE,
  229. .dai_link = snd_soc_raumfeld_speaker_dai,
  230. .num_links = ARRAY_SIZE(snd_soc_raumfeld_speaker_dai),
  231. .suspend_post = raumfeld_analog_suspend,
  232. .resume_pre = raumfeld_analog_resume,
  233. };
  234. static struct platform_device *raumfeld_audio_device;
  235. static int __init raumfeld_audio_init(void)
  236. {
  237. int ret;
  238. if (!machine_is_raumfeld_speaker() &&
  239. !machine_is_raumfeld_connector())
  240. return 0;
  241. max9486_client = i2c_new_device(i2c_get_adapter(0),
  242. &max9486_hwmon_info);
  243. if (!max9486_client)
  244. return -ENOMEM;
  245. set_max9485_clk(MAX9485_MCLK_FREQ_122880);
  246. /* Register analog device */
  247. raumfeld_audio_device = platform_device_alloc("soc-audio", 0);
  248. if (!raumfeld_audio_device)
  249. return -ENOMEM;
  250. if (machine_is_raumfeld_speaker())
  251. platform_set_drvdata(raumfeld_audio_device,
  252. &snd_soc_raumfeld_speaker);
  253. if (machine_is_raumfeld_connector())
  254. platform_set_drvdata(raumfeld_audio_device,
  255. &snd_soc_raumfeld_connector);
  256. ret = platform_device_add(raumfeld_audio_device);
  257. if (ret < 0) {
  258. platform_device_put(raumfeld_audio_device);
  259. return ret;
  260. }
  261. raumfeld_enable_audio(true);
  262. return 0;
  263. }
  264. static void __exit raumfeld_audio_exit(void)
  265. {
  266. raumfeld_enable_audio(false);
  267. platform_device_unregister(raumfeld_audio_device);
  268. i2c_unregister_device(max9486_client);
  269. gpio_free(GPIO_MCLK_RESET);
  270. gpio_free(GPIO_CODEC_RESET);
  271. gpio_free(GPIO_SPDIF_RESET);
  272. }
  273. module_init(raumfeld_audio_init);
  274. module_exit(raumfeld_audio_exit);
  275. /* Module information */
  276. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  277. MODULE_DESCRIPTION("Raumfeld audio SoC");
  278. MODULE_LICENSE("GPL");