ak5386.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. .component_driver = {
  68. .dapm_widgets = ak5386_dapm_widgets,
  69. .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
  70. .dapm_routes = ak5386_dapm_routes,
  71. .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
  72. },
  73. };
  74. static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
  75. unsigned int format)
  76. {
  77. struct snd_soc_codec *codec = codec_dai->codec;
  78. format &= SND_SOC_DAIFMT_FORMAT_MASK;
  79. if (format != SND_SOC_DAIFMT_LEFT_J &&
  80. format != SND_SOC_DAIFMT_I2S) {
  81. dev_err(codec->dev, "Invalid DAI format\n");
  82. return -EINVAL;
  83. }
  84. return 0;
  85. }
  86. static int ak5386_hw_params(struct snd_pcm_substream *substream,
  87. struct snd_pcm_hw_params *params,
  88. struct snd_soc_dai *dai)
  89. {
  90. struct snd_soc_codec *codec = dai->codec;
  91. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  92. /*
  93. * From the datasheet:
  94. *
  95. * All external clocks (MCLK, SCLK and LRCK) must be present unless
  96. * PDN pin = “L”. If these clocks are not provided, the AK5386 may
  97. * draw excess current due to its use of internal dynamically
  98. * refreshed logic. If the external clocks are not present, place
  99. * the AK5386 in power-down mode (PDN pin = “L”).
  100. */
  101. if (gpio_is_valid(priv->reset_gpio))
  102. gpio_set_value(priv->reset_gpio, 1);
  103. return 0;
  104. }
  105. static int ak5386_hw_free(struct snd_pcm_substream *substream,
  106. struct snd_soc_dai *dai)
  107. {
  108. struct snd_soc_codec *codec = dai->codec;
  109. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  110. if (gpio_is_valid(priv->reset_gpio))
  111. gpio_set_value(priv->reset_gpio, 0);
  112. return 0;
  113. }
  114. static const struct snd_soc_dai_ops ak5386_dai_ops = {
  115. .set_fmt = ak5386_set_dai_fmt,
  116. .hw_params = ak5386_hw_params,
  117. .hw_free = ak5386_hw_free,
  118. };
  119. static struct snd_soc_dai_driver ak5386_dai = {
  120. .name = "ak5386-hifi",
  121. .capture = {
  122. .stream_name = "Capture",
  123. .channels_min = 1,
  124. .channels_max = 2,
  125. .rates = SNDRV_PCM_RATE_8000_192000,
  126. .formats = SNDRV_PCM_FMTBIT_S8 |
  127. SNDRV_PCM_FMTBIT_S16_LE |
  128. SNDRV_PCM_FMTBIT_S24_LE |
  129. SNDRV_PCM_FMTBIT_S24_3LE,
  130. },
  131. .ops = &ak5386_dai_ops,
  132. };
  133. #ifdef CONFIG_OF
  134. static const struct of_device_id ak5386_dt_ids[] = {
  135. { .compatible = "asahi-kasei,ak5386", },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
  139. #endif
  140. static int ak5386_probe(struct platform_device *pdev)
  141. {
  142. struct device *dev = &pdev->dev;
  143. struct ak5386_priv *priv;
  144. int ret, i;
  145. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  146. if (!priv)
  147. return -ENOMEM;
  148. priv->reset_gpio = -EINVAL;
  149. dev_set_drvdata(dev, priv);
  150. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  151. priv->supplies[i].supply = supply_names[i];
  152. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
  153. priv->supplies);
  154. if (ret < 0)
  155. return ret;
  156. if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
  157. priv->reset_gpio = of_get_named_gpio(dev->of_node,
  158. "reset-gpio", 0);
  159. if (gpio_is_valid(priv->reset_gpio))
  160. if (devm_gpio_request_one(dev, priv->reset_gpio,
  161. GPIOF_OUT_INIT_LOW,
  162. "AK5386 Reset"))
  163. priv->reset_gpio = -EINVAL;
  164. return snd_soc_register_codec(dev, &soc_codec_ak5386,
  165. &ak5386_dai, 1);
  166. }
  167. static int ak5386_remove(struct platform_device *pdev)
  168. {
  169. snd_soc_unregister_codec(&pdev->dev);
  170. return 0;
  171. }
  172. static struct platform_driver ak5386_driver = {
  173. .probe = ak5386_probe,
  174. .remove = ak5386_remove,
  175. .driver = {
  176. .name = "ak5386",
  177. .of_match_table = of_match_ptr(ak5386_dt_ids),
  178. },
  179. };
  180. module_platform_driver(ak5386_driver);
  181. MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
  182. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  183. MODULE_LICENSE("GPL");