ak5386.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA SoC driver for
  4. * Asahi Kasei AK5386 Single-ended 24-Bit 192kHz delta-sigma ADC
  5. *
  6. * (c) 2013 Daniel Mack <zonque@gmail.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/of_gpio.h>
  12. #include <linux/of_device.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <sound/soc.h>
  15. #include <sound/pcm.h>
  16. #include <sound/initval.h>
  17. static const char * const supply_names[] = {
  18. "va", "vd"
  19. };
  20. struct ak5386_priv {
  21. int reset_gpio;
  22. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  23. };
  24. static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
  25. SND_SOC_DAPM_INPUT("AINL"),
  26. SND_SOC_DAPM_INPUT("AINR"),
  27. };
  28. static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
  29. { "Capture", NULL, "AINL" },
  30. { "Capture", NULL, "AINR" },
  31. };
  32. static int ak5386_soc_probe(struct snd_soc_component *component)
  33. {
  34. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  35. return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
  36. }
  37. static void ak5386_soc_remove(struct snd_soc_component *component)
  38. {
  39. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  40. regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
  41. }
  42. #ifdef CONFIG_PM
  43. static int ak5386_soc_suspend(struct snd_soc_component *component)
  44. {
  45. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  46. regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
  47. return 0;
  48. }
  49. static int ak5386_soc_resume(struct snd_soc_component *component)
  50. {
  51. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  52. return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
  53. }
  54. #else
  55. #define ak5386_soc_suspend NULL
  56. #define ak5386_soc_resume NULL
  57. #endif /* CONFIG_PM */
  58. static const struct snd_soc_component_driver soc_component_ak5386 = {
  59. .probe = ak5386_soc_probe,
  60. .remove = ak5386_soc_remove,
  61. .suspend = ak5386_soc_suspend,
  62. .resume = ak5386_soc_resume,
  63. .dapm_widgets = ak5386_dapm_widgets,
  64. .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
  65. .dapm_routes = ak5386_dapm_routes,
  66. .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
  67. .idle_bias_on = 1,
  68. .use_pmdown_time = 1,
  69. .endianness = 1,
  70. .non_legacy_dai_naming = 1,
  71. };
  72. static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
  73. unsigned int format)
  74. {
  75. struct snd_soc_component *component = codec_dai->component;
  76. format &= SND_SOC_DAIFMT_FORMAT_MASK;
  77. if (format != SND_SOC_DAIFMT_LEFT_J &&
  78. format != SND_SOC_DAIFMT_I2S) {
  79. dev_err(component->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_component *component = dai->component;
  89. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  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_component *component = dai->component;
  107. struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
  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 devm_snd_soc_register_component(dev, &soc_component_ak5386,
  163. &ak5386_dai, 1);
  164. }
  165. static struct platform_driver ak5386_driver = {
  166. .probe = ak5386_probe,
  167. .driver = {
  168. .name = "ak5386",
  169. .of_match_table = of_match_ptr(ak5386_dt_ids),
  170. },
  171. };
  172. module_platform_driver(ak5386_driver);
  173. MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
  174. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  175. MODULE_LICENSE("GPL");