ak5386.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * ALSA SoC driver for
  3. * Asahi Kasei AK5386 Single-ended 24-Bit 192kHz delta-sigma ADC
  4. *
  5. * (c) 2013 Daniel Mack <zonque@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/of.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_device.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <sound/soc.h>
  18. #include <sound/pcm.h>
  19. #include <sound/initval.h>
  20. static const char * const supply_names[] = {
  21. "va", "vd"
  22. };
  23. struct ak5386_priv {
  24. int reset_gpio;
  25. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  26. };
  27. static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
  28. SND_SOC_DAPM_INPUT("AINL"),
  29. SND_SOC_DAPM_INPUT("AINR"),
  30. };
  31. static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
  32. { "Capture", NULL, "AINL" },
  33. { "Capture", NULL, "AINR" },
  34. };
  35. static int ak5386_soc_probe(struct snd_soc_codec *codec)
  36. {
  37. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  38. return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
  39. }
  40. static int ak5386_soc_remove(struct snd_soc_codec *codec)
  41. {
  42. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  43. regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
  44. return 0;
  45. }
  46. #ifdef CONFIG_PM
  47. static int ak5386_soc_suspend(struct snd_soc_codec *codec)
  48. {
  49. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  50. regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
  51. return 0;
  52. }
  53. static int ak5386_soc_resume(struct snd_soc_codec *codec)
  54. {
  55. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  56. return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
  57. }
  58. #else
  59. #define ak5386_soc_suspend NULL
  60. #define ak5386_soc_resume NULL
  61. #endif /* CONFIG_PM */
  62. static struct snd_soc_codec_driver soc_codec_ak5386 = {
  63. .probe = ak5386_soc_probe,
  64. .remove = ak5386_soc_remove,
  65. .suspend = ak5386_soc_suspend,
  66. .resume = ak5386_soc_resume,
  67. .dapm_widgets = ak5386_dapm_widgets,
  68. .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
  69. .dapm_routes = ak5386_dapm_routes,
  70. .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
  71. };
  72. static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
  73. unsigned int format)
  74. {
  75. struct snd_soc_codec *codec = codec_dai->codec;
  76. format &= SND_SOC_DAIFMT_FORMAT_MASK;
  77. if (format != SND_SOC_DAIFMT_LEFT_J &&
  78. format != SND_SOC_DAIFMT_I2S) {
  79. dev_err(codec->dev, "Invalid DAI format\n");
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. static int ak5386_hw_params(struct snd_pcm_substream *substream,
  85. struct snd_pcm_hw_params *params,
  86. struct snd_soc_dai *dai)
  87. {
  88. struct snd_soc_codec *codec = dai->codec;
  89. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  90. /*
  91. * From the datasheet:
  92. *
  93. * All external clocks (MCLK, SCLK and LRCK) must be present unless
  94. * PDN pin = “L”. If these clocks are not provided, the AK5386 may
  95. * draw excess current due to its use of internal dynamically
  96. * refreshed logic. If the external clocks are not present, place
  97. * the AK5386 in power-down mode (PDN pin = “L”).
  98. */
  99. if (gpio_is_valid(priv->reset_gpio))
  100. gpio_set_value(priv->reset_gpio, 1);
  101. return 0;
  102. }
  103. static int ak5386_hw_free(struct snd_pcm_substream *substream,
  104. struct snd_soc_dai *dai)
  105. {
  106. struct snd_soc_codec *codec = dai->codec;
  107. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  108. if (gpio_is_valid(priv->reset_gpio))
  109. gpio_set_value(priv->reset_gpio, 0);
  110. return 0;
  111. }
  112. static const struct snd_soc_dai_ops ak5386_dai_ops = {
  113. .set_fmt = ak5386_set_dai_fmt,
  114. .hw_params = ak5386_hw_params,
  115. .hw_free = ak5386_hw_free,
  116. };
  117. static struct snd_soc_dai_driver ak5386_dai = {
  118. .name = "ak5386-hifi",
  119. .capture = {
  120. .stream_name = "Capture",
  121. .channels_min = 1,
  122. .channels_max = 2,
  123. .rates = SNDRV_PCM_RATE_8000_192000,
  124. .formats = SNDRV_PCM_FMTBIT_S8 |
  125. SNDRV_PCM_FMTBIT_S16_LE |
  126. SNDRV_PCM_FMTBIT_S24_LE |
  127. SNDRV_PCM_FMTBIT_S24_3LE,
  128. },
  129. .ops = &ak5386_dai_ops,
  130. };
  131. #ifdef CONFIG_OF
  132. static const struct of_device_id ak5386_dt_ids[] = {
  133. { .compatible = "asahi-kasei,ak5386", },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
  137. #endif
  138. static int ak5386_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct ak5386_priv *priv;
  142. int ret, i;
  143. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. priv->reset_gpio = -EINVAL;
  147. dev_set_drvdata(dev, priv);
  148. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  149. priv->supplies[i].supply = supply_names[i];
  150. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
  151. priv->supplies);
  152. if (ret < 0)
  153. return ret;
  154. if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
  155. priv->reset_gpio = of_get_named_gpio(dev->of_node,
  156. "reset-gpio", 0);
  157. if (gpio_is_valid(priv->reset_gpio))
  158. if (devm_gpio_request_one(dev, priv->reset_gpio,
  159. GPIOF_OUT_INIT_LOW,
  160. "AK5386 Reset"))
  161. priv->reset_gpio = -EINVAL;
  162. return snd_soc_register_codec(dev, &soc_codec_ak5386,
  163. &ak5386_dai, 1);
  164. }
  165. static int ak5386_remove(struct platform_device *pdev)
  166. {
  167. snd_soc_unregister_codec(&pdev->dev);
  168. return 0;
  169. }
  170. static struct platform_driver ak5386_driver = {
  171. .probe = ak5386_probe,
  172. .remove = ak5386_remove,
  173. .driver = {
  174. .name = "ak5386",
  175. .owner = THIS_MODULE,
  176. .of_match_table = of_match_ptr(ak5386_dt_ids),
  177. },
  178. };
  179. module_platform_driver(ak5386_driver);
  180. MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
  181. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  182. MODULE_LICENSE("GPL");