pcm3008.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * ALSA Soc PCM3008 codec support
  3. *
  4. * Author: Hugo Villeneuve
  5. * Copyright (C) 2008 Lyrtech inc
  6. *
  7. * Based on AC97 Soc codec, original copyright follow:
  8. * Copyright 2005 Wolfson Microelectronics PLC.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * Generic PCM3008 support.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/device.h>
  20. #include <linux/gpio.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/initval.h>
  24. #include <sound/soc.h>
  25. #include "pcm3008.h"
  26. #define PCM3008_VERSION "0.2"
  27. #define PCM3008_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  28. SNDRV_PCM_RATE_48000)
  29. struct snd_soc_dai pcm3008_dai = {
  30. .name = "PCM3008 HiFi",
  31. .playback = {
  32. .stream_name = "PCM3008 Playback",
  33. .channels_min = 1,
  34. .channels_max = 2,
  35. .rates = PCM3008_RATES,
  36. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  37. },
  38. .capture = {
  39. .stream_name = "PCM3008 Capture",
  40. .channels_min = 1,
  41. .channels_max = 2,
  42. .rates = PCM3008_RATES,
  43. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  44. },
  45. };
  46. EXPORT_SYMBOL_GPL(pcm3008_dai);
  47. static void pcm3008_gpio_free(struct pcm3008_setup_data *setup)
  48. {
  49. gpio_free(setup->dem0_pin);
  50. gpio_free(setup->dem1_pin);
  51. gpio_free(setup->pdad_pin);
  52. gpio_free(setup->pdda_pin);
  53. }
  54. static int pcm3008_soc_probe(struct platform_device *pdev)
  55. {
  56. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  57. struct snd_soc_codec *codec;
  58. struct pcm3008_setup_data *setup = socdev->codec_data;
  59. int ret = 0;
  60. printk(KERN_INFO "PCM3008 SoC Audio Codec %s\n", PCM3008_VERSION);
  61. socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  62. if (!socdev->card->codec)
  63. return -ENOMEM;
  64. codec = socdev->card->codec;
  65. mutex_init(&codec->mutex);
  66. codec->name = "PCM3008";
  67. codec->owner = THIS_MODULE;
  68. codec->dai = &pcm3008_dai;
  69. codec->num_dai = 1;
  70. codec->write = NULL;
  71. codec->read = NULL;
  72. INIT_LIST_HEAD(&codec->dapm_widgets);
  73. INIT_LIST_HEAD(&codec->dapm_paths);
  74. /* Register PCMs. */
  75. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  76. if (ret < 0) {
  77. printk(KERN_ERR "pcm3008: failed to create pcms\n");
  78. goto pcm_err;
  79. }
  80. /* Register Card. */
  81. ret = snd_soc_init_card(socdev);
  82. if (ret < 0) {
  83. printk(KERN_ERR "pcm3008: failed to register card\n");
  84. goto card_err;
  85. }
  86. /* DEM1 DEM0 DE-EMPHASIS_MODE
  87. * Low Low De-emphasis 44.1 kHz ON
  88. * Low High De-emphasis OFF
  89. * High Low De-emphasis 48 kHz ON
  90. * High High De-emphasis 32 kHz ON
  91. */
  92. /* Configure DEM0 GPIO (turning OFF DAC De-emphasis). */
  93. ret = gpio_request(setup->dem0_pin, "codec_dem0");
  94. if (ret == 0)
  95. ret = gpio_direction_output(setup->dem0_pin, 1);
  96. if (ret != 0)
  97. goto gpio_err;
  98. /* Configure DEM1 GPIO (turning OFF DAC De-emphasis). */
  99. ret = gpio_request(setup->dem1_pin, "codec_dem1");
  100. if (ret == 0)
  101. ret = gpio_direction_output(setup->dem1_pin, 0);
  102. if (ret != 0)
  103. goto gpio_err;
  104. /* Configure PDAD GPIO. */
  105. ret = gpio_request(setup->pdad_pin, "codec_pdad");
  106. if (ret == 0)
  107. ret = gpio_direction_output(setup->pdad_pin, 1);
  108. if (ret != 0)
  109. goto gpio_err;
  110. /* Configure PDDA GPIO. */
  111. ret = gpio_request(setup->pdda_pin, "codec_pdda");
  112. if (ret == 0)
  113. ret = gpio_direction_output(setup->pdda_pin, 1);
  114. if (ret != 0)
  115. goto gpio_err;
  116. return ret;
  117. gpio_err:
  118. pcm3008_gpio_free(setup);
  119. card_err:
  120. snd_soc_free_pcms(socdev);
  121. pcm_err:
  122. kfree(socdev->card->codec);
  123. return ret;
  124. }
  125. static int pcm3008_soc_remove(struct platform_device *pdev)
  126. {
  127. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  128. struct snd_soc_codec *codec = socdev->card->codec;
  129. struct pcm3008_setup_data *setup = socdev->codec_data;
  130. if (!codec)
  131. return 0;
  132. pcm3008_gpio_free(setup);
  133. snd_soc_free_pcms(socdev);
  134. kfree(socdev->card->codec);
  135. return 0;
  136. }
  137. #ifdef CONFIG_PM
  138. static int pcm3008_soc_suspend(struct platform_device *pdev, pm_message_t msg)
  139. {
  140. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  141. struct pcm3008_setup_data *setup = socdev->codec_data;
  142. gpio_set_value(setup->pdad_pin, 0);
  143. gpio_set_value(setup->pdda_pin, 0);
  144. return 0;
  145. }
  146. static int pcm3008_soc_resume(struct platform_device *pdev)
  147. {
  148. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  149. struct pcm3008_setup_data *setup = socdev->codec_data;
  150. gpio_set_value(setup->pdad_pin, 1);
  151. gpio_set_value(setup->pdda_pin, 1);
  152. return 0;
  153. }
  154. #else
  155. #define pcm3008_soc_suspend NULL
  156. #define pcm3008_soc_resume NULL
  157. #endif
  158. struct snd_soc_codec_device soc_codec_dev_pcm3008 = {
  159. .probe = pcm3008_soc_probe,
  160. .remove = pcm3008_soc_remove,
  161. .suspend = pcm3008_soc_suspend,
  162. .resume = pcm3008_soc_resume,
  163. };
  164. EXPORT_SYMBOL_GPL(soc_codec_dev_pcm3008);
  165. static int __init pcm3008_init(void)
  166. {
  167. return snd_soc_register_dai(&pcm3008_dai);
  168. }
  169. module_init(pcm3008_init);
  170. static void __exit pcm3008_exit(void)
  171. {
  172. snd_soc_unregister_dai(&pcm3008_dai);
  173. }
  174. module_exit(pcm3008_exit);
  175. MODULE_DESCRIPTION("Soc PCM3008 driver");
  176. MODULE_AUTHOR("Hugo Villeneuve");
  177. MODULE_LICENSE("GPL");